/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Sema/Scope.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Scope.h - Scope interface --------------------------------*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines the Scope interface. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #ifndef LLVM_CLANG_SEMA_SCOPE_H |
14 | | #define LLVM_CLANG_SEMA_SCOPE_H |
15 | | |
16 | | #include "clang/AST/Decl.h" |
17 | | #include "clang/Basic/Diagnostic.h" |
18 | | #include "llvm/ADT/PointerIntPair.h" |
19 | | #include "llvm/ADT/SmallPtrSet.h" |
20 | | #include "llvm/ADT/SmallVector.h" |
21 | | #include "llvm/ADT/iterator_range.h" |
22 | | #include <cassert> |
23 | | |
24 | | namespace llvm { |
25 | | |
26 | | class raw_ostream; |
27 | | |
28 | | } // namespace llvm |
29 | | |
30 | | namespace clang { |
31 | | |
32 | | class Decl; |
33 | | class DeclContext; |
34 | | class UsingDirectiveDecl; |
35 | | class VarDecl; |
36 | | |
37 | | /// Scope - A scope is a transient data structure that is used while parsing the |
38 | | /// program. It assists with resolving identifiers to the appropriate |
39 | | /// declaration. |
40 | | class Scope { |
41 | | public: |
42 | | /// ScopeFlags - These are bitfields that are or'd together when creating a |
43 | | /// scope, which defines the sorts of things the scope contains. |
44 | | enum ScopeFlags { |
45 | | /// This indicates that the scope corresponds to a function, which |
46 | | /// means that labels are set here. |
47 | | FnScope = 0x01, |
48 | | |
49 | | /// This is a while, do, switch, for, etc that can have break |
50 | | /// statements embedded into it. |
51 | | BreakScope = 0x02, |
52 | | |
53 | | /// This is a while, do, for, which can have continue statements |
54 | | /// embedded into it. |
55 | | ContinueScope = 0x04, |
56 | | |
57 | | /// This is a scope that can contain a declaration. Some scopes |
58 | | /// just contain loop constructs but don't contain decls. |
59 | | DeclScope = 0x08, |
60 | | |
61 | | /// The controlling scope in a if/switch/while/for statement. |
62 | | ControlScope = 0x10, |
63 | | |
64 | | /// The scope of a struct/union/class definition. |
65 | | ClassScope = 0x20, |
66 | | |
67 | | /// This is a scope that corresponds to a block/closure object. |
68 | | /// Blocks serve as top-level scopes for some objects like labels, they |
69 | | /// also prevent things like break and continue. BlockScopes always have |
70 | | /// the FnScope and DeclScope flags set as well. |
71 | | BlockScope = 0x40, |
72 | | |
73 | | /// This is a scope that corresponds to the |
74 | | /// template parameters of a C++ template. Template parameter |
75 | | /// scope starts at the 'template' keyword and ends when the |
76 | | /// template declaration ends. |
77 | | TemplateParamScope = 0x80, |
78 | | |
79 | | /// This is a scope that corresponds to the |
80 | | /// parameters within a function prototype. |
81 | | FunctionPrototypeScope = 0x100, |
82 | | |
83 | | /// This is a scope that corresponds to the parameters within |
84 | | /// a function prototype for a function declaration (as opposed to any |
85 | | /// other kind of function declarator). Always has FunctionPrototypeScope |
86 | | /// set as well. |
87 | | FunctionDeclarationScope = 0x200, |
88 | | |
89 | | /// This is a scope that corresponds to the Objective-C |
90 | | /// \@catch statement. |
91 | | AtCatchScope = 0x400, |
92 | | |
93 | | /// This scope corresponds to an Objective-C method body. |
94 | | /// It always has FnScope and DeclScope set as well. |
95 | | ObjCMethodScope = 0x800, |
96 | | |
97 | | /// This is a scope that corresponds to a switch statement. |
98 | | SwitchScope = 0x1000, |
99 | | |
100 | | /// This is the scope of a C++ try statement. |
101 | | TryScope = 0x2000, |
102 | | |
103 | | /// This is the scope for a function-level C++ try or catch scope. |
104 | | FnTryCatchScope = 0x4000, |
105 | | |
106 | | /// This is the scope of OpenMP executable directive. |
107 | | OpenMPDirectiveScope = 0x8000, |
108 | | |
109 | | /// This is the scope of some OpenMP loop directive. |
110 | | OpenMPLoopDirectiveScope = 0x10000, |
111 | | |
112 | | /// This is the scope of some OpenMP simd directive. |
113 | | /// For example, it is used for 'omp simd', 'omp for simd'. |
114 | | /// This flag is propagated to children scopes. |
115 | | OpenMPSimdDirectiveScope = 0x20000, |
116 | | |
117 | | /// This scope corresponds to an enum. |
118 | | EnumScope = 0x40000, |
119 | | |
120 | | /// This scope corresponds to an SEH try. |
121 | | SEHTryScope = 0x80000, |
122 | | |
123 | | /// This scope corresponds to an SEH except. |
124 | | SEHExceptScope = 0x100000, |
125 | | |
126 | | /// We are currently in the filter expression of an SEH except block. |
127 | | SEHFilterScope = 0x200000, |
128 | | |
129 | | /// This is a compound statement scope. |
130 | | CompoundStmtScope = 0x400000, |
131 | | |
132 | | /// We are between inheritance colon and the real class/struct definition scope. |
133 | | ClassInheritanceScope = 0x800000, |
134 | | |
135 | | /// This is the scope of a C++ catch statement. |
136 | | CatchScope = 0x1000000, |
137 | | }; |
138 | | |
139 | | private: |
140 | | /// The parent scope for this scope. This is null for the translation-unit |
141 | | /// scope. |
142 | | Scope *AnyParent; |
143 | | |
144 | | /// Flags - This contains a set of ScopeFlags, which indicates how the scope |
145 | | /// interrelates with other control flow statements. |
146 | | unsigned Flags; |
147 | | |
148 | | /// Depth - This is the depth of this scope. The translation-unit scope has |
149 | | /// depth 0. |
150 | | unsigned short Depth; |
151 | | |
152 | | /// Declarations with static linkage are mangled with the number of |
153 | | /// scopes seen as a component. |
154 | | unsigned short MSLastManglingNumber; |
155 | | |
156 | | unsigned short MSCurManglingNumber; |
157 | | |
158 | | /// PrototypeDepth - This is the number of function prototype scopes |
159 | | /// enclosing this scope, including this scope. |
160 | | unsigned short PrototypeDepth; |
161 | | |
162 | | /// PrototypeIndex - This is the number of parameters currently |
163 | | /// declared in this scope. |
164 | | unsigned short PrototypeIndex; |
165 | | |
166 | | /// FnParent - If this scope has a parent scope that is a function body, this |
167 | | /// pointer is non-null and points to it. This is used for label processing. |
168 | | Scope *FnParent; |
169 | | Scope *MSLastManglingParent; |
170 | | |
171 | | /// BreakParent/ContinueParent - This is a direct link to the innermost |
172 | | /// BreakScope/ContinueScope which contains the contents of this scope |
173 | | /// for control flow purposes (and might be this scope itself), or null |
174 | | /// if there is no such scope. |
175 | | Scope *BreakParent, *ContinueParent; |
176 | | |
177 | | /// BlockParent - This is a direct link to the immediately containing |
178 | | /// BlockScope if this scope is not one, or null if there is none. |
179 | | Scope *BlockParent; |
180 | | |
181 | | /// TemplateParamParent - This is a direct link to the |
182 | | /// immediately containing template parameter scope. In the |
183 | | /// case of nested templates, template parameter scopes can have |
184 | | /// other template parameter scopes as parents. |
185 | | Scope *TemplateParamParent; |
186 | | |
187 | | /// DeclsInScope - This keeps track of all declarations in this scope. When |
188 | | /// the declaration is added to the scope, it is set as the current |
189 | | /// declaration for the identifier in the IdentifierTable. When the scope is |
190 | | /// popped, these declarations are removed from the IdentifierTable's notion |
191 | | /// of current declaration. It is up to the current Action implementation to |
192 | | /// implement these semantics. |
193 | | using DeclSetTy = llvm::SmallPtrSet<Decl *, 32>; |
194 | | DeclSetTy DeclsInScope; |
195 | | |
196 | | /// The DeclContext with which this scope is associated. For |
197 | | /// example, the entity of a class scope is the class itself, the |
198 | | /// entity of a function scope is a function, etc. |
199 | | DeclContext *Entity; |
200 | | |
201 | | using UsingDirectivesTy = SmallVector<UsingDirectiveDecl *, 2>; |
202 | | UsingDirectivesTy UsingDirectives; |
203 | | |
204 | | /// Used to determine if errors occurred in this scope. |
205 | | DiagnosticErrorTrap ErrorTrap; |
206 | | |
207 | | /// A lattice consisting of undefined, a single NRVO candidate variable in |
208 | | /// this scope, or over-defined. The bit is true when over-defined. |
209 | | llvm::PointerIntPair<VarDecl *, 1, bool> NRVO; |
210 | | |
211 | | void setFlags(Scope *Parent, unsigned F); |
212 | | |
213 | | public: |
214 | | Scope(Scope *Parent, unsigned ScopeFlags, DiagnosticsEngine &Diag) |
215 | 306k | : ErrorTrap(Diag) { |
216 | 306k | Init(Parent, ScopeFlags); |
217 | 306k | } |
218 | | |
219 | | /// getFlags - Return the flags for this scope. |
220 | 680M | unsigned getFlags() const { return Flags; } |
221 | | |
222 | 256 | void setFlags(unsigned F) { setFlags(getParent(), F); } |
223 | | |
224 | | /// isBlockScope - Return true if this scope correspond to a closure. |
225 | 0 | bool isBlockScope() const { return Flags & BlockScope; } |
226 | | |
227 | | /// getParent - Return the scope that this is nested in. |
228 | 332k | const Scope *getParent() const { return AnyParent; } |
229 | 311M | Scope *getParent() { return AnyParent; } |
230 | | |
231 | | /// getFnParent - Return the closest scope that is a function body. |
232 | 234k | const Scope *getFnParent() const { return FnParent; } |
233 | 5.80M | Scope *getFnParent() { return FnParent; } |
234 | | |
235 | 29.2M | const Scope *getMSLastManglingParent() const { |
236 | 29.2M | return MSLastManglingParent; |
237 | 29.2M | } |
238 | 6.20M | Scope *getMSLastManglingParent() { return MSLastManglingParent; } |
239 | | |
240 | | /// getContinueParent - Return the closest scope that a continue statement |
241 | | /// would be affected by. |
242 | 8.20k | Scope *getContinueParent() { |
243 | 8.20k | return ContinueParent; |
244 | 8.20k | } |
245 | | |
246 | 0 | const Scope *getContinueParent() const { |
247 | 0 | return const_cast<Scope*>(this)->getContinueParent(); |
248 | 0 | } |
249 | | |
250 | | /// getBreakParent - Return the closest scope that a break statement |
251 | | /// would be affected by. |
252 | 52.5k | Scope *getBreakParent() { |
253 | 52.5k | return BreakParent; |
254 | 52.5k | } |
255 | 0 | const Scope *getBreakParent() const { |
256 | 0 | return const_cast<Scope*>(this)->getBreakParent(); |
257 | 0 | } |
258 | | |
259 | 76.6k | Scope *getBlockParent() { return BlockParent; } |
260 | 0 | const Scope *getBlockParent() const { return BlockParent; } |
261 | | |
262 | 1.43M | Scope *getTemplateParamParent() { return TemplateParamParent; } |
263 | 0 | const Scope *getTemplateParamParent() const { return TemplateParamParent; } |
264 | | |
265 | | /// Returns the depth of this scope. The translation-unit has scope depth 0. |
266 | 8.21k | unsigned getDepth() const { return Depth; } |
267 | | |
268 | | /// Returns the number of function prototype scopes in this scope |
269 | | /// chain. |
270 | 84.6M | unsigned getFunctionPrototypeDepth() const { |
271 | 84.6M | return PrototypeDepth; |
272 | 84.6M | } |
273 | | |
274 | | /// Return the number of parameters declared in this function |
275 | | /// prototype, increasing it by one for the next call. |
276 | 35.3M | unsigned getNextFunctionPrototypeIndex() { |
277 | 35.3M | assert(isFunctionPrototypeScope()); |
278 | 35.3M | return PrototypeIndex++; |
279 | 35.3M | } |
280 | | |
281 | | using decl_range = llvm::iterator_range<DeclSetTy::iterator>; |
282 | | |
283 | 31.7M | decl_range decls() const { |
284 | 31.7M | return decl_range(DeclsInScope.begin(), DeclsInScope.end()); |
285 | 31.7M | } |
286 | | |
287 | 24.2M | bool decl_empty() const { return DeclsInScope.empty(); } |
288 | | |
289 | 75.8M | void AddDecl(Decl *D) { |
290 | 75.8M | DeclsInScope.insert(D); |
291 | 75.8M | } |
292 | | |
293 | 442k | void RemoveDecl(Decl *D) { |
294 | 442k | DeclsInScope.erase(D); |
295 | 442k | } |
296 | | |
297 | 6.01M | void incrementMSManglingNumber() { |
298 | 6.01M | if (Scope *MSLMP = getMSLastManglingParent()) { |
299 | 5.70M | MSLMP->MSLastManglingNumber += 1; |
300 | 5.70M | MSCurManglingNumber += 1; |
301 | 5.70M | } |
302 | 6.01M | } |
303 | | |
304 | 190k | void decrementMSManglingNumber() { |
305 | 190k | if (Scope *MSLMP = getMSLastManglingParent()) { |
306 | 190k | MSLMP->MSLastManglingNumber -= 1; |
307 | 190k | MSCurManglingNumber -= 1; |
308 | 190k | } |
309 | 190k | } |
310 | | |
311 | 29.2M | unsigned getMSLastManglingNumber() const { |
312 | 29.2M | if (const Scope *MSLMP = getMSLastManglingParent()) |
313 | 5.66M | return MSLMP->MSLastManglingNumber; |
314 | 23.5M | return 1; |
315 | 23.5M | } |
316 | | |
317 | 148 | unsigned getMSCurManglingNumber() const { |
318 | 148 | return MSCurManglingNumber; |
319 | 148 | } |
320 | | |
321 | | /// isDeclScope - Return true if this is the scope that the specified decl is |
322 | | /// declared in. |
323 | 226M | bool isDeclScope(const Decl *D) const { return DeclsInScope.count(D) != 0; } |
324 | | |
325 | | /// Get the entity corresponding to this scope. |
326 | 401M | DeclContext *getEntity() const { |
327 | 370M | return isTemplateParamScope() ? nullptr30.4M : Entity; |
328 | 401M | } |
329 | | |
330 | | /// Get the DeclContext in which to continue unqualified lookup after a |
331 | | /// lookup in this scope. |
332 | 129M | DeclContext *getLookupEntity() const { return Entity; } |
333 | | |
334 | 5.98M | void setEntity(DeclContext *E) { |
335 | 5.98M | assert(!isTemplateParamScope() && |
336 | 5.98M | "entity associated with template param scope"); |
337 | 5.98M | Entity = E; |
338 | 5.98M | } |
339 | 387k | void setLookupEntity(DeclContext *E) { Entity = E; } |
340 | | |
341 | | /// Determine whether any unrecoverable errors have occurred within this |
342 | | /// scope. Note that this may return false even if the scope contains invalid |
343 | | /// declarations or statements, if the errors for those invalid constructs |
344 | | /// were suppressed because some prior invalid construct was referenced. |
345 | 52.8M | bool hasUnrecoverableErrorOccurred() const { |
346 | 52.8M | return ErrorTrap.hasUnrecoverableErrorOccurred(); |
347 | 52.8M | } |
348 | | |
349 | | /// isFunctionScope() - Return true if this scope is a function scope. |
350 | 230 | bool isFunctionScope() const { return (getFlags() & Scope::FnScope); } |
351 | | |
352 | | /// isClassScope - Return true if this scope is a class/struct/union scope. |
353 | 22.1M | bool isClassScope() const { |
354 | 22.1M | return (getFlags() & Scope::ClassScope); |
355 | 22.1M | } |
356 | | |
357 | | /// isInCXXInlineMethodScope - Return true if this scope is a C++ inline |
358 | | /// method scope or is inside one. |
359 | 0 | bool isInCXXInlineMethodScope() const { |
360 | 0 | if (const Scope *FnS = getFnParent()) { |
361 | 0 | assert(FnS->getParent() && "TUScope not created?"); |
362 | 0 | return FnS->getParent()->isClassScope(); |
363 | 0 | } |
364 | 0 | return false; |
365 | 0 | } |
366 | | |
367 | | /// isInObjcMethodScope - Return true if this scope is, or is contained in, an |
368 | | /// Objective-C method body. Note that this method is not constant time. |
369 | 90.9k | bool isInObjcMethodScope() const { |
370 | 242k | for (const Scope *S = this; S; S = S->getParent()151k ) { |
371 | | // If this scope is an objc method scope, then we succeed. |
372 | 152k | if (S->getFlags() & ObjCMethodScope) |
373 | 1.36k | return true; |
374 | 152k | } |
375 | 89.6k | return false; |
376 | 90.9k | } |
377 | | |
378 | | /// isInObjcMethodOuterScope - Return true if this scope is an |
379 | | /// Objective-C method outer most body. |
380 | 0 | bool isInObjcMethodOuterScope() const { |
381 | 0 | if (const Scope *S = this) { |
382 | 0 | // If this scope is an objc method scope, then we succeed. |
383 | 0 | if (S->getFlags() & ObjCMethodScope) |
384 | 0 | return true; |
385 | 0 | } |
386 | 0 | return false; |
387 | 0 | } |
388 | | |
389 | | /// isTemplateParamScope - Return true if this scope is a C++ |
390 | | /// template parameter scope. |
391 | 426M | bool isTemplateParamScope() const { |
392 | 426M | return getFlags() & Scope::TemplateParamScope; |
393 | 426M | } |
394 | | |
395 | | /// isFunctionPrototypeScope - Return true if this scope is a |
396 | | /// function prototype scope. |
397 | 135M | bool isFunctionPrototypeScope() const { |
398 | 135M | return getFlags() & Scope::FunctionPrototypeScope; |
399 | 135M | } |
400 | | |
401 | | /// isFunctionDeclarationScope - Return true if this scope is a |
402 | | /// function prototype scope. |
403 | 98 | bool isFunctionDeclarationScope() const { |
404 | 98 | return getFlags() & Scope::FunctionDeclarationScope; |
405 | 98 | } |
406 | | |
407 | | /// isAtCatchScope - Return true if this scope is \@catch. |
408 | 26 | bool isAtCatchScope() const { |
409 | 26 | return getFlags() & Scope::AtCatchScope; |
410 | 26 | } |
411 | | |
412 | | /// isSwitchScope - Return true if this scope is a switch scope. |
413 | 19 | bool isSwitchScope() const { |
414 | 53 | for (const Scope *S = this; S; S = S->getParent()34 ) { |
415 | 53 | if (S->getFlags() & Scope::SwitchScope) |
416 | 18 | return true; |
417 | 35 | else if (S->getFlags() & (Scope::FnScope | Scope::ClassScope | |
418 | 35 | Scope::BlockScope | Scope::TemplateParamScope | |
419 | 35 | Scope::FunctionPrototypeScope | |
420 | 35 | Scope::AtCatchScope | Scope::ObjCMethodScope)) |
421 | 1 | return false; |
422 | 53 | } |
423 | 0 | return false; |
424 | 19 | } |
425 | | |
426 | | /// Determines whether this scope is the OpenMP directive scope |
427 | 326 | bool isOpenMPDirectiveScope() const { |
428 | 326 | return (getFlags() & Scope::OpenMPDirectiveScope); |
429 | 326 | } |
430 | | |
431 | | /// Determine whether this scope is some OpenMP loop directive scope |
432 | | /// (for example, 'omp for', 'omp simd'). |
433 | 32.6k | bool isOpenMPLoopDirectiveScope() const { |
434 | 32.6k | if (getFlags() & Scope::OpenMPLoopDirectiveScope) { |
435 | 326 | assert(isOpenMPDirectiveScope() && |
436 | 326 | "OpenMP loop directive scope is not a directive scope"); |
437 | 326 | return true; |
438 | 326 | } |
439 | 32.3k | return false; |
440 | 32.3k | } |
441 | | |
442 | | /// Determine whether this scope is (or is nested into) some OpenMP |
443 | | /// loop simd directive scope (for example, 'omp simd', 'omp for simd'). |
444 | 24.2k | bool isOpenMPSimdDirectiveScope() const { |
445 | 24.2k | return getFlags() & Scope::OpenMPSimdDirectiveScope; |
446 | 24.2k | } |
447 | | |
448 | | /// Determine whether this scope is a loop having OpenMP loop |
449 | | /// directive attached. |
450 | 32.6k | bool isOpenMPLoopScope() const { |
451 | 32.6k | const Scope *P = getParent(); |
452 | 32.6k | return P && P->isOpenMPLoopDirectiveScope(); |
453 | 32.6k | } |
454 | | |
455 | | /// Determine whether this scope is a C++ 'try' block. |
456 | 0 | bool isTryScope() const { return getFlags() & Scope::TryScope; } |
457 | | |
458 | | /// Determine whether this scope is a SEH '__try' block. |
459 | 72 | bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; } |
460 | | |
461 | | /// Determine whether this scope is a SEH '__except' block. |
462 | 62 | bool isSEHExceptScope() const { return getFlags() & Scope::SEHExceptScope; } |
463 | | |
464 | | /// Determine whether this scope is a compound statement scope. |
465 | 5.02k | bool isCompoundStmtScope() const { |
466 | 5.02k | return getFlags() & Scope::CompoundStmtScope; |
467 | 5.02k | } |
468 | | |
469 | | /// Returns if rhs has a higher scope depth than this. |
470 | | /// |
471 | | /// The caller is responsible for calling this only if one of the two scopes |
472 | | /// is an ancestor of the other. |
473 | 32 | bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; } |
474 | | |
475 | | /// containedInPrototypeScope - Return true if this or a parent scope |
476 | | /// is a FunctionPrototypeScope. |
477 | | bool containedInPrototypeScope() const; |
478 | | |
479 | 1.82k | void PushUsingDirective(UsingDirectiveDecl *UDir) { |
480 | 1.82k | UsingDirectives.push_back(UDir); |
481 | 1.82k | } |
482 | | |
483 | | using using_directives_range = |
484 | | llvm::iterator_range<UsingDirectivesTy::iterator>; |
485 | | |
486 | 30.6M | using_directives_range using_directives() { |
487 | 30.6M | return using_directives_range(UsingDirectives.begin(), |
488 | 30.6M | UsingDirectives.end()); |
489 | 30.6M | } |
490 | | |
491 | 62.5k | void addNRVOCandidate(VarDecl *VD) { |
492 | 62.5k | if (NRVO.getInt()) |
493 | 2.25k | return; |
494 | 60.2k | if (NRVO.getPointer() == nullptr) { |
495 | 59.1k | NRVO.setPointer(VD); |
496 | 59.1k | return; |
497 | 59.1k | } |
498 | 1.17k | if (NRVO.getPointer() != VD) |
499 | 17 | setNoNRVO(); |
500 | 1.17k | } |
501 | | |
502 | 3.10M | void setNoNRVO() { |
503 | 3.10M | NRVO.setInt(true); |
504 | 3.10M | NRVO.setPointer(nullptr); |
505 | 3.10M | } |
506 | | |
507 | | void mergeNRVOIntoParent(); |
508 | | |
509 | | /// Init - This is used by the parser to implement scope caching. |
510 | | void Init(Scope *parent, unsigned flags); |
511 | | |
512 | | /// Sets up the specified scope flags and adjusts the scope state |
513 | | /// variables accordingly. |
514 | | void AddFlags(unsigned Flags); |
515 | | |
516 | | void dumpImpl(raw_ostream &OS) const; |
517 | | void dump() const; |
518 | | }; |
519 | | |
520 | | } // namespace clang |
521 | | |
522 | | #endif // LLVM_CLANG_SEMA_SCOPE_H |