~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateParameter.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2005, 2011 IBM Corporation and others.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *    Andrew Niefer (IBM) - Initial API and implementation
 
10
 *    Markus Schorn (Wind River Systems)
 
11
 *******************************************************************************/
 
12
package org.eclipse.cdt.internal.core.dom.parser.cpp;
 
13
 
 
14
import org.eclipse.cdt.core.dom.ILinkage;
 
15
import org.eclipse.cdt.core.dom.ast.IASTName;
 
16
import org.eclipse.cdt.core.dom.ast.IASTNode;
 
17
import org.eclipse.cdt.core.dom.ast.IBinding;
 
18
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
 
19
import org.eclipse.cdt.core.dom.ast.IScope;
 
20
import org.eclipse.cdt.core.dom.ast.IType;
 
21
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
 
22
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter;
 
23
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
 
24
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
 
25
import org.eclipse.cdt.core.parser.util.ArrayUtil;
 
26
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
 
27
import org.eclipse.cdt.internal.core.dom.Linkage;
 
28
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
 
29
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
 
30
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates;
 
31
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
 
32
import org.eclipse.core.runtime.PlatformObject;
 
33
 
 
34
/**
 
35
 * Base implementation for template parameter bindings in the AST.
 
36
 */
 
37
public abstract class CPPTemplateParameter extends PlatformObject 
 
38
                implements ICPPTemplateParameter, ICPPInternalBinding, ICPPTwoPhaseBinding {
 
39
        private IASTName[] declarations;
 
40
        private final int fParameterID;
 
41
        
 
42
        public CPPTemplateParameter(IASTName name) {
 
43
                declarations = new IASTName[] {name};
 
44
                fParameterID= computeParameterID(name);
 
45
        }
 
46
 
 
47
        private int computeParameterID(IASTName name) {
 
48
                int nesting= 0;
 
49
                ICPPASTTemplateParameter tp= null;
 
50
                ICPPASTTemplateParameter[] tps= null;
 
51
                for (IASTNode node= name.getParent(); node != null; node= node.getParent()) {
 
52
                        if (tp == null && node instanceof ICPPASTTemplateParameter) {
 
53
                                tp= (ICPPASTTemplateParameter) node;
 
54
                        } else if (node instanceof ICPPASTInternalTemplateDeclaration) {
 
55
                                final ICPPASTInternalTemplateDeclaration tdecl= (ICPPASTInternalTemplateDeclaration) node;
 
56
                                nesting+= tdecl.getNestingLevel();
 
57
                                if (tps == null) {
 
58
                                        tps= tdecl.getTemplateParameters();
 
59
                                }
 
60
                                break;
 
61
                        } else if (node instanceof ICPPASTTemplatedTypeTemplateParameter) {
 
62
                                nesting++;
 
63
                                if (tps == null) {
 
64
                                        tps= ((ICPPASTTemplatedTypeTemplateParameter) node).getTemplateParameters();
 
65
                                }
 
66
                        }
 
67
                }
 
68
                int pos= 0;
 
69
                if (tps != null && tp != null) {
 
70
                        for (int i = 0; i < tps.length; i++) {
 
71
                                if (tps[i] == tp) {
 
72
                                        pos= i;
 
73
                                        break;
 
74
                                }
 
75
                        }
 
76
                }
 
77
 
 
78
                return (nesting << 16) + (pos & 0xffff);
 
79
        }
 
80
 
 
81
        @Override
 
82
        public Object clone() {
 
83
        IType t = null;
 
84
                try {
 
85
            t = (IType) super.clone();
 
86
        } catch (CloneNotSupportedException e) {
 
87
            //not going to happen
 
88
        }
 
89
        return t;
 
90
    }
 
91
        
 
92
        /* (non-Javadoc)
 
93
         * @see org.eclipse.cdt.core.dom.ast.IBinding#getName()
 
94
         */
 
95
        public final String getName() {
 
96
                return new String(getNameCharArray());
 
97
        }
 
98
 
 
99
        /* (non-Javadoc)
 
100
         * @see org.eclipse.cdt.core.dom.ast.IBinding#getNameCharArray()
 
101
         */
 
102
        public final char[] getNameCharArray() {
 
103
                // Search for the first declaration that has a name.
 
104
                for (IASTName decl : declarations) {
 
105
                        if (decl == null)
 
106
                                break;
 
107
                        
 
108
                        final char[] result= decl.getSimpleID();
 
109
                        if (result.length > 0)
 
110
                                return result;
 
111
                }
 
112
                return CharArrayUtils.EMPTY;
 
113
        }
 
114
 
 
115
        public int getParameterID() {
 
116
                return fParameterID;
 
117
        }
 
118
 
 
119
        public short getParameterPosition() {
 
120
                return (short) fParameterID;
 
121
        }
 
122
 
 
123
        public short getTemplateNestingLevel() {
 
124
                return (short) (fParameterID >> 16);
 
125
        }
 
126
 
 
127
        public IASTName getPrimaryDeclaration () {
 
128
                return declarations[0];
 
129
        }
 
130
        
 
131
        private ICPPASTTemplateParameter getASTTemplateParameter() {
 
132
                IASTNode node= declarations[0];
 
133
                while (node != null && !(node instanceof ICPPASTTemplateParameter))
 
134
                        node= node.getParent();
 
135
                assert node != null;
 
136
                return (ICPPASTTemplateParameter) node;
 
137
        }
 
138
 
 
139
        /* (non-Javadoc)
 
140
         * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope()
 
141
         */
 
142
        public IScope getScope() {
 
143
                return CPPVisitor.getContainingScope(getPrimaryDeclaration());
 
144
        }
 
145
 
 
146
        /* (non-Javadoc)
 
147
         * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedName()
 
148
         */
 
149
        public String[] getQualifiedName() {
 
150
                return new String[] { getName() };
 
151
        }
 
152
 
 
153
        /* (non-Javadoc)
 
154
         * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedNameCharArray()
 
155
         */
 
156
        public char[][] getQualifiedNameCharArray() {
 
157
                return new char[][] {getNameCharArray() };
 
158
        }
 
159
 
 
160
        /* (non-Javadoc)
 
161
         * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#isGloballyQualified()
 
162
         */
 
163
        public boolean isGloballyQualified() {
 
164
                return false;
 
165
        }
 
166
 
 
167
        /* (non-Javadoc)
 
168
         * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDeclarations()
 
169
         */
 
170
        public IASTName[] getDeclarations() {
 
171
                return declarations;
 
172
        }
 
173
 
 
174
        /* (non-Javadoc)
 
175
         * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDefinition()
 
176
         */
 
177
        public IASTNode getDefinition() {
 
178
                if (declarations != null && declarations.length > 0)
 
179
                        return declarations[0];
 
180
                return null;
 
181
        }
 
182
 
 
183
        /* (non-Javadoc)
 
184
         * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#addDefinition(org.eclipse.cdt.core.dom.ast.IASTNode)
 
185
         */
 
186
        public void addDefinition(IASTNode node) {
 
187
                addDeclaration(node);
 
188
        }
 
189
 
 
190
        /* (non-Javadoc)
 
191
         * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#addDeclaration(org.eclipse.cdt.core.dom.ast.IASTNode)
 
192
         */
 
193
        public void addDeclaration(IASTNode node) {
 
194
                if (!(node instanceof IASTName))
 
195
                        return;
 
196
                IASTName name = (IASTName) node;
 
197
                if (declarations == null) {
 
198
                declarations = new IASTName[] { name };
 
199
                } else {
 
200
                if (declarations.length > 0 && declarations[0] == node)
 
201
                    return;
 
202
                        // keep the lowest offset declaration in [0]
 
203
                        if (declarations.length > 0 && ((ASTNode)node).getOffset() < ((ASTNode)declarations[0]).getOffset()) {
 
204
                                declarations = (IASTName[]) ArrayUtil.prepend(IASTName.class, declarations, name);
 
205
                        } else {
 
206
                                declarations = (IASTName[]) ArrayUtil.append(IASTName.class, declarations, name);
 
207
                        }
 
208
            }
 
209
        }
 
210
        
 
211
        public ILinkage getLinkage() {
 
212
                return Linkage.CPP_LINKAGE;
 
213
        }
 
214
        
 
215
        @Override
 
216
        public String toString() {
 
217
                return getName();
 
218
        }       
 
219
        
 
220
        public IBinding getOwner() {
 
221
                if (declarations == null || declarations.length == 0)
 
222
                        return null;
 
223
                
 
224
                IASTNode node= declarations[0];
 
225
                while (!(node instanceof ICPPASTTemplateParameter)) {
 
226
                        if (node == null)
 
227
                                return null;
 
228
                        
 
229
                        node= node.getParent();
 
230
                }
 
231
                
 
232
                return CPPTemplates.getContainingTemplate((ICPPASTTemplateParameter) node);
 
233
        }
 
234
        
 
235
        public IBinding resolveFinalBinding(CPPASTNameBase name) {
 
236
                // check if the binding has been updated.
 
237
                IBinding current= name.getPreBinding();
 
238
                if (current != this)
 
239
                        return current;
 
240
                
 
241
                ICPPTemplateDefinition template= CPPTemplates.getContainingTemplate(getASTTemplateParameter());
 
242
                if (template instanceof ICPPInternalTemplate) {
 
243
                        return ((ICPPInternalTemplate) template).resolveTemplateParameter(this);
 
244
                }
 
245
 
 
246
                // problem finding the containing template
 
247
                if (template == null) {
 
248
                        return this;
 
249
                }
 
250
                
 
251
                ICPPTemplateParameter[] params = template.getTemplateParameters();
 
252
                final int pos= getParameterPosition();
 
253
                if (pos < params.length) 
 
254
                        return params[pos];
 
255
                return new ProblemBinding(getPrimaryDeclaration(), IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND);
 
256
        }
 
257
}