John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1 | //===- Scope.cpp - Lexical scope information --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Scope class, which is used for recording |
| 11 | // information about a lexical scope. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Sema/Scope.h" |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
Akira Hatanaka | 10aced8 | 2016-04-29 02:24:14 +0000 | [diff] [blame] | 21 | void Scope::setFlags(Scope *parent, unsigned flags) { |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 22 | AnyParent = parent; |
| 23 | Flags = flags; |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 24 | |
| 25 | if (parent && !(flags & FnScope)) { |
| 26 | BreakParent = parent->BreakParent; |
| 27 | ContinueParent = parent->ContinueParent; |
| 28 | } else { |
| 29 | // Control scopes do not contain the contents of nested function scopes for |
| 30 | // control flow purposes. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 31 | BreakParent = ContinueParent = nullptr; |
Richard Smith | 1002d10 | 2012-02-17 01:35:32 +0000 | [diff] [blame] | 32 | } |
| 33 | |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 34 | if (parent) { |
| 35 | Depth = parent->Depth + 1; |
| 36 | PrototypeDepth = parent->PrototypeDepth; |
| 37 | PrototypeIndex = 0; |
| 38 | FnParent = parent->FnParent; |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 39 | BlockParent = parent->BlockParent; |
| 40 | TemplateParamParent = parent->TemplateParamParent; |
David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 41 | MSLastManglingParent = parent->MSLastManglingParent; |
| 42 | MSCurManglingNumber = getMSLastManglingNumber(); |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 43 | if ((Flags & (FnScope | ClassScope | BlockScope | TemplateParamScope | |
| 44 | FunctionPrototypeScope | AtCatchScope | ObjCMethodScope)) == |
| 45 | 0) |
| 46 | Flags |= parent->getFlags() & OpenMPSimdDirectiveScope; |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 47 | } else { |
| 48 | Depth = 0; |
| 49 | PrototypeDepth = 0; |
| 50 | PrototypeIndex = 0; |
David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 51 | MSLastManglingParent = FnParent = BlockParent = nullptr; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 52 | TemplateParamParent = nullptr; |
David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 53 | MSLastManglingNumber = 1; |
| 54 | MSCurManglingNumber = 1; |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // If this scope is a function or contains breaks/continues, remember it. |
| 58 | if (flags & FnScope) FnParent = this; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 59 | // The MS mangler uses the number of scopes that can hold declarations as |
| 60 | // part of an external name. |
| 61 | if (Flags & (ClassScope | FnScope)) { |
David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 62 | MSLastManglingNumber = getMSLastManglingNumber(); |
| 63 | MSLastManglingParent = this; |
| 64 | MSCurManglingNumber = 1; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 65 | } |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 66 | if (flags & BreakScope) BreakParent = this; |
| 67 | if (flags & ContinueScope) ContinueParent = this; |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 68 | if (flags & BlockScope) BlockParent = this; |
| 69 | if (flags & TemplateParamScope) TemplateParamParent = this; |
| 70 | |
| 71 | // If this is a prototype scope, record that. |
| 72 | if (flags & FunctionPrototypeScope) PrototypeDepth++; |
Hans Wennborg | fe78145 | 2014-06-17 00:00:18 +0000 | [diff] [blame] | 73 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 74 | if (flags & DeclScope) { |
| 75 | if (flags & FunctionPrototypeScope) |
| 76 | ; // Prototype scopes are uninteresting. |
| 77 | else if ((flags & ClassScope) && getParent()->isClassScope()) |
| 78 | ; // Nested class scopes aren't ambiguous. |
| 79 | else if ((flags & ClassScope) && getParent()->getFlags() == DeclScope) |
| 80 | ; // Classes inside of namespaces aren't ambiguous. |
Hans Wennborg | fe78145 | 2014-06-17 00:00:18 +0000 | [diff] [blame] | 81 | else if ((flags & EnumScope)) |
| 82 | ; // Don't increment for enum scopes. |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 83 | else |
David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 84 | incrementMSManglingNumber(); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 85 | } |
Akira Hatanaka | 10aced8 | 2016-04-29 02:24:14 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void Scope::Init(Scope *parent, unsigned flags) { |
| 89 | setFlags(parent, flags); |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 90 | |
| 91 | DeclsInScope.clear(); |
| 92 | UsingDirectives.clear(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 93 | Entity = nullptr; |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 94 | ErrorTrap.reset(); |
| 95 | } |
James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 96 | |
| 97 | bool Scope::containedInPrototypeScope() const { |
| 98 | const Scope *S = this; |
| 99 | while (S) { |
| 100 | if (S->isFunctionPrototypeScope()) |
| 101 | return true; |
| 102 | S = S->getParent(); |
| 103 | } |
| 104 | return false; |
| 105 | } |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 106 | |
| 107 | void Scope::AddFlags(unsigned FlagsToSet) { |
| 108 | assert((FlagsToSet & ~(BreakScope | ContinueScope)) == 0 && |
| 109 | "Unsupported scope flags"); |
| 110 | if (FlagsToSet & BreakScope) { |
| 111 | assert((Flags & BreakScope) == 0 && "Already set"); |
| 112 | BreakParent = this; |
| 113 | } |
| 114 | if (FlagsToSet & ContinueScope) { |
| 115 | assert((Flags & ContinueScope) == 0 && "Already set"); |
| 116 | ContinueParent = this; |
| 117 | } |
| 118 | Flags |= FlagsToSet; |
| 119 | } |
| 120 | |
Taiju Tsuiki | b000a88 | 2018-06-19 04:39:07 +0000 | [diff] [blame] | 121 | void Scope::setNRVOCandidate(VarDecl *Candidate) { |
| 122 | for (Decl *D : DeclsInScope) { |
| 123 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 124 | if (VD && VD != Candidate && VD->isNRVOCandidate()) |
| 125 | VD->setNRVOVariable(false); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Taiju Tsuiki | b000a88 | 2018-06-19 04:39:07 +0000 | [diff] [blame] | 128 | if (Scope *parent = getParent()) |
| 129 | parent->setNRVOCandidate(Candidate); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 132 | LLVM_DUMP_METHOD void Scope::dump() const { dumpImpl(llvm::errs()); } |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 133 | |
| 134 | void Scope::dumpImpl(raw_ostream &OS) const { |
| 135 | unsigned Flags = getFlags(); |
| 136 | bool HasFlags = Flags != 0; |
| 137 | |
| 138 | if (HasFlags) |
| 139 | OS << "Flags: "; |
| 140 | |
Richard Trieu | df5ec07 | 2018-01-18 04:28:56 +0000 | [diff] [blame] | 141 | std::pair<unsigned, const char *> FlagInfo[] = { |
| 142 | {FnScope, "FnScope"}, |
| 143 | {BreakScope, "BreakScope"}, |
| 144 | {ContinueScope, "ContinueScope"}, |
| 145 | {DeclScope, "DeclScope"}, |
| 146 | {ControlScope, "ControlScope"}, |
| 147 | {ClassScope, "ClassScope"}, |
| 148 | {BlockScope, "BlockScope"}, |
| 149 | {TemplateParamScope, "TemplateParamScope"}, |
| 150 | {FunctionPrototypeScope, "FunctionPrototypeScope"}, |
| 151 | {FunctionDeclarationScope, "FunctionDeclarationScope"}, |
| 152 | {AtCatchScope, "AtCatchScope"}, |
| 153 | {ObjCMethodScope, "ObjCMethodScope"}, |
| 154 | {SwitchScope, "SwitchScope"}, |
| 155 | {TryScope, "TryScope"}, |
| 156 | {FnTryCatchScope, "FnTryCatchScope"}, |
| 157 | {OpenMPDirectiveScope, "OpenMPDirectiveScope"}, |
| 158 | {OpenMPLoopDirectiveScope, "OpenMPLoopDirectiveScope"}, |
| 159 | {OpenMPSimdDirectiveScope, "OpenMPSimdDirectiveScope"}, |
| 160 | {EnumScope, "EnumScope"}, |
| 161 | {SEHTryScope, "SEHTryScope"}, |
| 162 | {SEHExceptScope, "SEHExceptScope"}, |
| 163 | {SEHFilterScope, "SEHFilterScope"}, |
| 164 | {CompoundStmtScope, "CompoundStmtScope"}, |
| 165 | {ClassInheritanceScope, "ClassInheritanceScope"}}; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 166 | |
Richard Trieu | df5ec07 | 2018-01-18 04:28:56 +0000 | [diff] [blame] | 167 | for (auto Info : FlagInfo) { |
| 168 | if (Flags & Info.first) { |
| 169 | OS << Info.second; |
| 170 | Flags &= ~Info.first; |
| 171 | if (Flags) |
| 172 | OS << " | "; |
| 173 | } |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 174 | } |
Richard Trieu | df5ec07 | 2018-01-18 04:28:56 +0000 | [diff] [blame] | 175 | |
| 176 | assert(Flags == 0 && "Unknown scope flags"); |
| 177 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 178 | if (HasFlags) |
| 179 | OS << '\n'; |
| 180 | |
| 181 | if (const Scope *Parent = getParent()) |
| 182 | OS << "Parent: (clang::Scope*)" << Parent << '\n'; |
| 183 | |
| 184 | OS << "Depth: " << Depth << '\n'; |
David Majnemer | a7f8c46 | 2015-03-19 21:54:30 +0000 | [diff] [blame] | 185 | OS << "MSLastManglingNumber: " << getMSLastManglingNumber() << '\n'; |
| 186 | OS << "MSCurManglingNumber: " << getMSCurManglingNumber() << '\n'; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 187 | if (const DeclContext *DC = getEntity()) |
| 188 | OS << "Entity : (clang::DeclContext*)" << DC << '\n'; |
| 189 | } |