Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 54c580a35187c0de074592ab39d7b5b2803a71c6 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*******************************************************************************
 * Copyright (c) 2004, 2010 IBM Corporation and others.
 * 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:
 *     Andrew Niefer (IBM Corporation) - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDirective;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPScopeMapper.InlineNamespaceDirective;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.index.IIndexScope;
import org.eclipse.core.runtime.CoreException;

/**
 * Implementation of namespace scopes, including global scope.
 */
public class CPPNamespaceScope extends CPPScope implements ICPPInternalNamespaceScope {
	private static final ICPPInternalNamespaceScope[] NO_NAMESPACE_SCOPES = {};

	private List<ICPPUsingDirective> fUsingDirectives;

	private boolean fIsInline;
	private boolean fIsInlineInitialized;
	private ICPPNamespaceScope[] fEnclosingNamespaceSet;
	private List<ICPPASTNamespaceDefinition> fInlineNamespaceDefinitions;
	private ICPPInternalNamespaceScope[] fInlineNamespaces;
	
	public CPPNamespaceScope(IASTNode physicalNode) {
		super(physicalNode);
	}

	@Override
	public EScopeKind getKind() {
		if (getPhysicalNode() instanceof IASTTranslationUnit)
			return EScopeKind.eGlobal;
		
		return EScopeKind.eNamespace;
	}

	@Override
	public ICPPUsingDirective[] getUsingDirectives() {
		initUsingDirectives();
		populateCache();
		return fUsingDirectives.toArray(new ICPPUsingDirective[fUsingDirectives.size()]);
	}
	
	private void initUsingDirectives() {
		if (fUsingDirectives == null) {
			fUsingDirectives= new ArrayList<ICPPUsingDirective>(1);
			// Insert a using directive for every inline namespace found in the index.
			for (ICPPInternalNamespaceScope inline : getIndexInlineNamespaces()) {
				if (!(inline instanceof CPPNamespaceScope)) {
					fUsingDirectives.add(new InlineNamespaceDirective(this, inline));
				}
			}
		}
	}

	@Override
	public void addUsingDirective(ICPPUsingDirective directive) {
		initUsingDirectives();
		fUsingDirectives.add(directive);
	}

    @Override
	public IName getScopeName() {
        IASTNode node = getPhysicalNode();
        if (node instanceof ICPPASTNamespaceDefinition) {
            return ((ICPPASTNamespaceDefinition) node).getName();
        }
        return null;
    }

    public IScope findNamespaceScope(IIndexScope scope) {
    	final String[] qname= CPPVisitor.getQualifiedName(scope.getScopeBinding());
    	final IScope[] result= { null };
    	final ASTVisitor visitor= new ASTVisitor() {
    		private int depth= 0;
    		{
    			shouldVisitNamespaces= shouldVisitDeclarations= true;
    		}

    		@Override
    		public int visit( IASTDeclaration declaration ){
    			if (declaration instanceof ICPPASTLinkageSpecification)
    				return PROCESS_CONTINUE;
    			return PROCESS_SKIP;
    		}

    		@Override
    		public int visit(ICPPASTNamespaceDefinition namespace) {
    			final String name = namespace.getName().toString();
    			if (name.length() == 0) {
    				return PROCESS_CONTINUE;
    			}
    			if (qname[depth].equals(name)) {
    				if (++depth == qname.length) {
    					result[0]= namespace.getScope();
    					return PROCESS_ABORT;
    				}
    				return PROCESS_CONTINUE;
    			}
    			return PROCESS_SKIP;
    		}

    		@Override
    		public int leave(ICPPASTNamespaceDefinition namespace) {
    			if (namespace.getName().getLookupKey().length > 0) {
    				--depth;
    			}
    			return PROCESS_CONTINUE;
    		}
    	};

    	getPhysicalNode().accept(visitor);
    	return result[0];
    }

    @Override
	public void addName(IASTName name) {
    	if (name instanceof ICPPASTQualifiedName && !canDenoteNamespaceMember((ICPPASTQualifiedName) name))
    		return;
    	super.addName(name);
    }
    
	public boolean canDenoteNamespaceMember(ICPPASTQualifiedName name) {
		IScope scope= this;
		IASTName[] segments= name.getNames();
		try {
			for (int i= segments.length - 1; --i >= 0;) {
				if (scope == null)
					return false;
				IName scopeName = scope.getScopeName();
				if (scopeName == null)
					return false;

				IASTName segmentName = segments[i];
				if (segmentName instanceof ICPPASTTemplateId ||
						!CharArrayUtils.equals(scopeName.getSimpleID(), segmentName.getSimpleID())) {
					return false;
				}
				scope= scope.getParent();
			}
			if (!name.isFullyQualified() || scope == null) {
				return true;
			}
			return ASTInternal.getPhysicalNodeOfScope(scope) instanceof IASTTranslationUnit;
		} catch (DOMException e) {
			return false;
		}
	}

	@Override
	public boolean isInlineNamepace() {
		if (!fIsInlineInitialized) {
			fIsInline= computeIsInline();
			fIsInlineInitialized= true;
		}
		return fIsInline;
	}

	public boolean computeIsInline() {
		final IASTNode node= getPhysicalNode();
		if (!(node instanceof ICPPASTNamespaceDefinition)) {
			return false;
		}

		if (((ICPPASTNamespaceDefinition) node).isInline())
			return true;
		
		IASTTranslationUnit tu= node.getTranslationUnit();
		if (tu != null) {
			final IIndex index= tu.getIndex();
			IIndexFileSet fileSet= tu.getASTFileSet();
			if (index != null && fileSet != null) {
				fileSet= fileSet.invert();
				ICPPNamespace nsBinding = getNamespaceIndexBinding(index);
				if (nsBinding != null && nsBinding.isInline()) {
					try {
						IIndexName[] names = index.findDefinitions(nsBinding);
						for (IIndexName name : names) {
							if (name.isInlineNamespaceDefinition() && fileSet.contains(name.getFile())) {
								return true;
							}
						}
					} catch (CoreException e) {
						CCorePlugin.log(e);
					}
				}
			}
		}
		return false;
	}

	@Override
	public ICPPNamespaceScope[] getEnclosingNamespaceSet() {
		if (fEnclosingNamespaceSet == null) {
			return fEnclosingNamespaceSet= computeEnclosingNamespaceSet(this);
		}
		return fEnclosingNamespaceSet;
	}
	
	@Override
	public ICPPInternalNamespaceScope[] getInlineNamespaces() {
		if (getKind() == EScopeKind.eLocal)
			return NO_NAMESPACE_SCOPES;
		
		if (fInlineNamespaces == null) {
			fInlineNamespaces= computeInlineNamespaces();
		}
		return fInlineNamespaces;
	}
	
	ICPPInternalNamespaceScope[] computeInlineNamespaces() {
		populateCache();
		Set<ICPPInternalNamespaceScope> result= null;
		if (fInlineNamespaceDefinitions != null) {
			result= new HashSet<ICPPInternalNamespaceScope>(fInlineNamespaceDefinitions.size());
			for (ICPPASTNamespaceDefinition nsdef : fInlineNamespaceDefinitions) {
				final IScope scope = nsdef.getScope();
				if (scope instanceof ICPPInternalNamespaceScope) {
					result.add((ICPPInternalNamespaceScope) scope);
				}
			}
		}
		
		for (ICPPInternalNamespaceScope inline : getIndexInlineNamespaces()) {
			if (result == null)
				result = new HashSet<ICPPInternalNamespaceScope>();
			result.add(inline);
		}
		
		if (result == null) {
			return NO_NAMESPACE_SCOPES;
		}
		return result.toArray(new ICPPInternalNamespaceScope[result.size()]);
	}

	private ICPPInternalNamespaceScope[] getIndexInlineNamespaces() {
		IASTTranslationUnit tu= getPhysicalNode().getTranslationUnit();
		if (tu instanceof CPPASTTranslationUnit) { 
			CPPASTTranslationUnit cpptu= (CPPASTTranslationUnit) tu;
			IIndex index= tu.getIndex();
			if (index != null) {
				IScope[] inlineScopes= null;
				ICPPNamespace namespace= getNamespaceIndexBinding(index);
				try {
					if (namespace != null) {
						ICPPNamespaceScope scope = namespace.getNamespaceScope();
						inlineScopes= scope.getInlineNamespaces();
					} else if (getKind() == EScopeKind.eGlobal) {
						inlineScopes= index.getInlineNamespaces();
					}
				} catch (CoreException e) {
				}
				if (inlineScopes != null) {
					List<ICPPInternalNamespaceScope> result= null;
					for (IScope scope : inlineScopes) {
						if (scope instanceof IIndexScope) {
							scope= cpptu.mapToASTScope((IIndexScope) scope);
						}
						if (scope instanceof ICPPInternalNamespaceScope) {
							if (result == null) {
								result= new ArrayList<ICPPInternalNamespaceScope>();
							}
							result.add((ICPPInternalNamespaceScope) scope);
						}
					}
					if (result != null) {
						return result.toArray(new ICPPInternalNamespaceScope[result.size()]);
					}
				}
			}
		}
		return NO_NAMESPACE_SCOPES;
	}

	/**
	 * Called while populating scope.
	 */
	public void addInlineNamespace(ICPPASTNamespaceDefinition nsDef) {
		if (fInlineNamespaceDefinitions == null) {
			fInlineNamespaceDefinitions= new ArrayList<ICPPASTNamespaceDefinition>();
		}
		fInlineNamespaceDefinitions.add(nsDef);
	}

	public static ICPPNamespaceScope[] computeEnclosingNamespaceSet(ICPPInternalNamespaceScope nsScope) {
		if (nsScope.isInlineNamepace()) {
			try {
				IScope parent= nsScope.getParent();
				if (parent instanceof ICPPInternalNamespaceScope) {
					return ((ICPPInternalNamespaceScope) parent).getEnclosingNamespaceSet();
				}
			} catch (DOMException e) {
				CCorePlugin.log(e);
			}
		}
		
		Set<ICPPInternalNamespaceScope> result= new HashSet<ICPPInternalNamespaceScope>();
		result.add(nsScope);
		addInlineNamespaces(nsScope, result);
		return result.toArray(new ICPPNamespaceScope[result.size()]);
	}

	private static void addInlineNamespaces(ICPPInternalNamespaceScope nsScope, Set<ICPPInternalNamespaceScope> result) {
		ICPPInternalNamespaceScope[] inlineNss = nsScope.getInlineNamespaces();
		for (ICPPInternalNamespaceScope inlineNs : inlineNss) {
			if (result.add(inlineNs)) {
				addInlineNamespaces(inlineNs, result);
			}
		}
	}
}

Back to the top