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