Scope.h 18 KB

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