Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d861558e941122e7be944295f5e552ba9f95f834 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat Incorporated - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.cdt.libhover;

import java.io.Serializable;
import java.util.ArrayList;

public class TypedefInfo implements Serializable {
	private static final long serialVersionUID = 1L;
	private String[] templates = new String[0];
	private String typedefName;
	private String transformedType;
	private ArrayList<TypedefInfo> children = null;
	public TypedefInfo(String typedefName, String transformedType) {
		this.typedefName = typedefName;
		this.transformedType = transformedType;
	}
	public String getTypedefName() {
		return typedefName;
	}
	public void setTypedefName(String name) {
		typedefName = name;
	}
	
	private String[] getTemplateArgs(String str) {
		ArrayList<String> list = new ArrayList<String>();
		int index = 0;
		int lastIndex = 0;
		int templateCounter = 0;
		while (index < str.length()) {
			char ch = str.charAt(index);
			if (ch == '<') {
				if (templateCounter == 0)
					lastIndex = index + 1;
				templateCounter++;
			} else if (ch == '>') {
				templateCounter--;
			} else if (ch == ',' && templateCounter == 1) {
				// FIXME: do we have to strip out all blanks here?
				list.add(str.substring(lastIndex, index).trim());
				lastIndex = index + 1;
			}
			++index;
		}
		String[] args = new String[list.size()];
		return list.toArray(args);
	}
	
	public String getTransformedType(String className) {
		int index = className.indexOf('<');
		if (index > 0) {
			TypedefInfo e = this;
			// Search the children list in case the given class name
			// matches a specific template case.
			ArrayList<TypedefInfo> children = getChildren();
			for (int x = 0; x < children.size(); ++x) {
				TypedefInfo child = children.get(x);
				if (className.matches(child.getTypedefName())) {
					e = child;
					break;
				}
			}
			String[] templates = e.getTemplates();
			String transformedName = e.transformedType;
			// Check if there are any template arguments to replace.  If not,
			// we can just return the transformed type we have.
			if (templates.length <= 0)
				return transformedName;
			String[] args = getTemplateArgs(className);
			String[] templateArgs = getTemplateArgs(e.getTypedefName());
			int j = 0;
			// For every argument that doesn't match up, it must be a template
			// parameter so we'll replace the template parameter name with the
			// supplied parameter.  We have to query the template parameter list
			// for the names to replace because for partial specific templates
			// those names will have been replaced with regex sequences designed to
			// help us identify when the specific template has matched.  For example,
			// <char, _Tp> will be stored as <char,[a-zA-Z0-9_]*> and if we have
			// <char,char> we will replace _Tp with char in the transformed type.
			for (int i = 0; i < args.length; ++i) {
				if (!args[i].equals(templateArgs[i])) {
					transformedName.replaceAll(templates[j], args[i]);
					++j;
				}
			}
			return transformedName;
		} else {
			// There is no template specified.
			return transformedType;
		}
	}
	
	public void addTypedef(TypedefInfo typedef) {
		if (children == null)
			children = new ArrayList<TypedefInfo>();
		children.add(typedef);
	}
	public ArrayList<TypedefInfo> getChildren() {
		return children;
	}
	public void copyTemplates(String[] newTemplates) {
		templates = new String[newTemplates.length];
		for (int i = 0; i < templates.length; ++i)
			templates[i] = newTemplates[i];
	}
	public String[] getTemplates() {
		return templates;
	}
}

Back to the top