Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the actions class which performs semantic analysis and |
| 11 | // builds an AST out of a parse stream. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
| 16 | #include "clang/Sema/DelayedDiagnostic.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 17 | #include "TargetAttributesSema.h" |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/APFloat.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 21 | #include "clang/Sema/CXXFieldCollector.h" |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 22 | #include "clang/Sema/ExternalSemaSource.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 23 | #include "clang/Sema/PrettyDeclStackTrace.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 25 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 26 | #include "clang/Sema/SemaConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 28 | #include "clang/AST/ASTDiagnostic.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 29 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 30 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 31 | #include "clang/AST/Expr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | #include "clang/Lex/Preprocessor.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 33 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 34 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 36 | using namespace sema; |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 37 | |
| 38 | FunctionScopeInfo::~FunctionScopeInfo() { } |
| 39 | |
| 40 | void FunctionScopeInfo::Clear(unsigned NumErrors) { |
John McCall | b60a77e | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 41 | HasBranchProtectedScope = false; |
| 42 | HasBranchIntoScope = false; |
| 43 | HasIndirectGoto = false; |
| 44 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 45 | LabelMap.clear(); |
| 46 | SwitchStack.clear(); |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 47 | Returns.clear(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 48 | NumErrorsAtStartOfFunction = NumErrors; |
| 49 | } |
| 50 | |
| 51 | BlockScopeInfo::~BlockScopeInfo() { } |
| 52 | |
Douglas Gregor | c1a3e5e | 2010-08-25 18:07:12 +0000 | [diff] [blame] | 53 | void Sema::ActOnTranslationUnitScope(Scope *S) { |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 54 | TUScope = S; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 55 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 57 | VAListTagName = PP.getIdentifierInfo("__va_list_tag"); |
| 58 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 59 | if (!Context.isInt128Installed() && // May be set by ASTReader. |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 60 | PP.getTargetInfo().getPointerWidth(0) >= 64) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 61 | TypeSourceInfo *TInfo; |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 63 | // Install [u]int128_t for 64-bit targets. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 64 | TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 65 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 66 | SourceLocation(), |
| 67 | &Context.Idents.get("__int128_t"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 68 | TInfo), TUScope); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 69 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 70 | TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 71 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 72 | SourceLocation(), |
| 73 | &Context.Idents.get("__uint128_t"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 74 | TInfo), TUScope); |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 75 | Context.setInt128Installed(); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 76 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
| 78 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 79 | if (!PP.getLangOptions().ObjC1) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 81 | // Built-in ObjC types may already be set by ASTReader (hence isNull checks). |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 82 | if (Context.getObjCSelType().isNull()) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 83 | // Create the built-in typedef for 'SEL'. |
Fariborz Jahanian | 04765ac | 2009-11-23 18:04:25 +0000 | [diff] [blame] | 84 | QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 85 | TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 86 | TypedefDecl *SelTypedef |
| 87 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 88 | &Context.Idents.get("SEL"), SelInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 89 | PushOnScopeChains(SelTypedef, TUScope); |
| 90 | Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 91 | Context.ObjCSelRedefinitionType = Context.getObjCSelType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 92 | } |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 94 | // Synthesize "@class Protocol; |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 95 | if (Context.getObjCProtoType().isNull()) { |
| 96 | ObjCInterfaceDecl *ProtocolDecl = |
| 97 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | &Context.Idents.get("Protocol"), |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 99 | SourceLocation(), true); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 100 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
Fariborz Jahanian | 10324db | 2009-11-18 23:15:37 +0000 | [diff] [blame] | 101 | PushOnScopeChains(ProtocolDecl, TUScope, false); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 102 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 103 | // Create the built-in typedef for 'id'. |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 104 | if (Context.getObjCIdType().isNull()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 105 | QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0); |
| 106 | T = Context.getObjCObjectPointerType(T); |
| 107 | TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 108 | TypedefDecl *IdTypedef |
| 109 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 110 | &Context.Idents.get("id"), IdInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 111 | PushOnScopeChains(IdTypedef, TUScope); |
| 112 | Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 113 | Context.ObjCIdRedefinitionType = Context.getObjCIdType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 114 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 115 | // Create the built-in typedef for 'Class'. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 116 | if (Context.getObjCClassType().isNull()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 117 | QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0); |
| 118 | T = Context.getObjCObjectPointerType(T); |
| 119 | TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 120 | TypedefDecl *ClassTypedef |
| 121 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 122 | &Context.Idents.get("Class"), ClassInfo); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 123 | PushOnScopeChains(ClassTypedef, TUScope); |
| 124 | Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 125 | Context.ObjCClassRedefinitionType = Context.getObjCClassType(); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 126 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 129 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 130 | bool CompleteTranslationUnit, |
| 131 | CodeCompleteConsumer *CodeCompleter) |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 132 | : TheTargetAttributesSema(0), |
| 133 | LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 135 | ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0), |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 136 | PackContext(0), VisContext(0), ParsingDeclDepth(0), |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 137 | IdResolver(pp.getLangOptions()), GlobalNewDeleteDeclared(false), |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 138 | CompleteTranslationUnit(CompleteTranslationUnit), |
Chandler Carruth | 926c4b4 | 2010-06-28 08:39:25 +0000 | [diff] [blame] | 139 | NumSFINAEErrors(0), SuppressAccessChecking(false), |
| 140 | NonInstantiationEntries(0), CurrentInstantiationScope(0), TyposCorrected(0), |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 141 | AnalysisWarnings(*this) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 142 | { |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 143 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 144 | if (getLangOptions().CPlusPlus) |
| 145 | FieldCollector.reset(new CXXFieldCollector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 147 | // Tell diagnostics how to render things from the AST library. |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 148 | PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 149 | &Context); |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 150 | |
| 151 | ExprEvalContexts.push_back( |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 152 | ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0)); |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 153 | |
| 154 | FunctionScopes.push_back(new FunctionScopeInfo(Diags.getNumErrors())); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void Sema::Initialize() { |
| 158 | // Tell the AST consumer about this Sema object. |
| 159 | Consumer.Initialize(Context); |
| 160 | |
| 161 | // FIXME: Isn't this redundant with the initialization above? |
| 162 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) |
| 163 | SC->InitializeSema(*this); |
| 164 | |
| 165 | // Tell the external Sema source about this Sema object. |
| 166 | if (ExternalSemaSource *ExternalSema |
| 167 | = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) |
| 168 | ExternalSema->InitializeSema(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 171 | Sema::~Sema() { |
| 172 | if (PackContext) FreePackedContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 173 | if (VisContext) FreeVisContext(); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 174 | delete TheTargetAttributesSema; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 175 | |
| 176 | // Kill all the active scopes. |
| 177 | for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I) |
| 178 | delete FunctionScopes[I]; |
| 179 | if (FunctionScopes.size() == 1) |
| 180 | delete FunctionScopes[0]; |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 181 | |
| 182 | // Tell the SemaConsumer to forget about us; we're going out of scope. |
| 183 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) |
| 184 | SC->ForgetSema(); |
| 185 | |
| 186 | // Detach from the external Sema source. |
| 187 | if (ExternalSemaSource *ExternalSema |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 188 | = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 189 | ExternalSema->ForgetSema(); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 193 | /// If there is already an implicit cast, merge into the existing one. |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 194 | /// The result is of the given category. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 196 | CastKind Kind, ExprValueKind VK, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 197 | const CXXCastPath *BasePath) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 198 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 199 | QualType TypeTy = Context.getCanonicalType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 201 | if (ExprTy == TypeTy) |
| 202 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 204 | if (Expr->getType()->isPointerType() && Ty->isPointerType()) { |
| 205 | QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType(); |
| 206 | QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 207 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 208 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 209 | << Expr->getSourceRange(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 213 | // If this is a derived-to-base cast to a through a virtual base, we |
| 214 | // need a vtable. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 215 | if (Kind == CK_DerivedToBase && |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 216 | BasePathInvolvesVirtualBase(*BasePath)) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 217 | QualType T = Expr->getType(); |
| 218 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 219 | T = Pointer->getPointeeType(); |
| 220 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
| 221 | MarkVTableUsed(Expr->getLocStart(), |
| 222 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 223 | } |
| 224 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 225 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 226 | if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) { |
Anders Carlsson | 4c5fad3 | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 227 | ImpCast->setType(Ty); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 228 | ImpCast->setValueKind(VK); |
Anders Carlsson | 4c5fad3 | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | } |
| 232 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 233 | Expr = ImplicitCastExpr::Create(Context, Ty, Kind, Expr, BasePath, VK); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 234 | } |
| 235 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 236 | ExprValueKind Sema::CastCategory(Expr *E) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 237 | Expr::Classification Classification = E->Classify(Context); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 238 | return Classification.isRValue() ? VK_RValue : |
| 239 | (Classification.isLValue() ? VK_LValue : VK_XValue); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 242 | /// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector. |
| 243 | static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) { |
| 244 | if (D->isUsed()) |
| 245 | return true; |
| 246 | |
| 247 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 248 | // UnusedFileScopedDecls stores the first declaration. |
| 249 | // The declaration may have become definition so check again. |
| 250 | const FunctionDecl *DeclToCheck; |
| 251 | if (FD->hasBody(DeclToCheck)) |
| 252 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 253 | |
| 254 | // Later redecls may add new information resulting in not having to warn, |
| 255 | // so check again. |
| 256 | DeclToCheck = FD->getMostRecentDeclaration(); |
| 257 | if (DeclToCheck != FD) |
| 258 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 259 | } |
| 260 | |
| 261 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 262 | // UnusedFileScopedDecls stores the first declaration. |
| 263 | // The declaration may have become definition so check again. |
| 264 | const VarDecl *DeclToCheck = VD->getDefinition(); |
| 265 | if (DeclToCheck) |
| 266 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 267 | |
| 268 | // Later redecls may add new information resulting in not having to warn, |
| 269 | // so check again. |
| 270 | DeclToCheck = VD->getMostRecentDeclaration(); |
| 271 | if (DeclToCheck != VD) |
| 272 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 273 | } |
| 274 | |
| 275 | return false; |
| 276 | } |
| 277 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 278 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 279 | /// translation unit when EOF is reached and all but the top-level scope is |
| 280 | /// popped. |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 281 | void Sema::ActOnEndOfTranslationUnit() { |
| 282 | // At PCH writing, implicit instantiations and VTable handling info are |
| 283 | // stored and performed when the PCH is included. |
| 284 | if (CompleteTranslationUnit) |
| 285 | while (1) { |
| 286 | // C++: Perform implicit template instantiations. |
| 287 | // |
| 288 | // FIXME: When we perform these implicit instantiations, we do not |
| 289 | // carefully keep track of the point of instantiation (C++ [temp.point]). |
| 290 | // This means that name lookup that occurs within the template |
| 291 | // instantiation will always happen at the end of the translation unit, |
| 292 | // so it will find some names that should not be found. Although this is |
| 293 | // common behavior for C++ compilers, it is technically wrong. In the |
| 294 | // future, we either need to be able to filter the results of name lookup |
| 295 | // or we need to perform template instantiations earlier. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 296 | PerformPendingInstantiations(); |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 297 | |
| 298 | /// If DefinedUsedVTables ends up marking any virtual member |
| 299 | /// functions it might lead to more pending template |
| 300 | /// instantiations, which is why we need to loop here. |
| 301 | if (!DefineUsedVTables()) |
| 302 | break; |
| 303 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 304 | |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 305 | // Remove file scoped decls that turned out to be used. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 306 | UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(), |
| 307 | UnusedFileScopedDecls.end(), |
| 308 | std::bind1st(std::ptr_fun(ShouldRemoveFromUnused), |
| 309 | this)), |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 310 | UnusedFileScopedDecls.end()); |
Douglas Gregor | 47268a3 | 2010-04-09 17:41:13 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 312 | if (!CompleteTranslationUnit) { |
| 313 | TUScope = 0; |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 314 | return; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 315 | } |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 317 | // Check for #pragma weak identifiers that were never declared |
| 318 | // FIXME: This will cause diagnostics to be emitted in a non-determinstic |
| 319 | // order! Iterating over a densemap like this is bad. |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 320 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 321 | I = WeakUndeclaredIdentifiers.begin(), |
| 322 | E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 323 | if (I->second.getUsed()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 325 | Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 326 | << I->first; |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 329 | // C99 6.9.2p2: |
| 330 | // A declaration of an identifier for an object that has file |
| 331 | // scope without an initializer, and without a storage-class |
| 332 | // specifier or with the storage-class specifier static, |
| 333 | // constitutes a tentative definition. If a translation unit |
| 334 | // contains one or more tentative definitions for an identifier, |
| 335 | // and the translation unit contains no external definition for |
| 336 | // that identifier, then the behavior is exactly as if the |
| 337 | // translation unit contains a file scope declaration of that |
| 338 | // identifier, with the composite type as of the end of the |
| 339 | // translation unit, with an initializer equal to 0. |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 340 | llvm::SmallSet<VarDecl *, 32> Seen; |
| 341 | for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) { |
| 342 | VarDecl *VD = TentativeDefinitions[i]->getActingDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 344 | // If the tentative definition was completed, getActingDefinition() returns |
| 345 | // null. If we've already seen this variable before, insert()'s second |
| 346 | // return value is false. |
| 347 | if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD)) |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 348 | continue; |
| 349 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | if (const IncompleteArrayType *ArrayT |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 351 | = Context.getAsIncompleteArrayType(VD->getType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | if (RequireCompleteType(VD->getLocation(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 353 | ArrayT->getElementType(), |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 354 | diag::err_tentative_def_incomplete_type_arr)) { |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 355 | VD->setInvalidDecl(); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 356 | continue; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 357 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 359 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 360 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 361 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 362 | QualType T = Context.getConstantArrayType(ArrayT->getElementType(), |
| 363 | One, ArrayType::Normal, 0); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 364 | VD->setType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 366 | diag::err_tentative_def_incomplete_type)) |
| 367 | VD->setInvalidDecl(); |
| 368 | |
| 369 | // Notify the consumer that we've completed a tentative definition. |
| 370 | if (!VD->isInvalidDecl()) |
| 371 | Consumer.CompleteTentativeDefinition(VD); |
| 372 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 373 | } |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 374 | |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 375 | // Output warning for unused file scoped decls. |
John McCall | aab0132 | 2010-08-24 17:40:45 +0000 | [diff] [blame] | 376 | for (llvm::SmallVectorImpl<const DeclaratorDecl*>::iterator |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 377 | I = UnusedFileScopedDecls.begin(), |
| 378 | E = UnusedFileScopedDecls.end(); I != E; ++I) { |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 379 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 380 | const FunctionDecl *DiagD; |
| 381 | if (!FD->hasBody(DiagD)) |
| 382 | DiagD = FD; |
Argyrios Kyrtzidis | 3d27b10 | 2010-08-17 21:43:11 +0000 | [diff] [blame] | 383 | Diag(DiagD->getLocation(), |
Argyrios Kyrtzidis | 42cbd78 | 2010-08-17 22:06:44 +0000 | [diff] [blame] | 384 | isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function |
Argyrios Kyrtzidis | 3d27b10 | 2010-08-17 21:43:11 +0000 | [diff] [blame] | 385 | : diag::warn_unused_function) |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 386 | << DiagD->getDeclName(); |
| 387 | } else { |
| 388 | const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition(); |
| 389 | if (!DiagD) |
| 390 | DiagD = cast<VarDecl>(*I); |
| 391 | Diag(DiagD->getLocation(), diag::warn_unused_variable) |
| 392 | << DiagD->getDeclName(); |
| 393 | } |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 394 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 395 | |
| 396 | TUScope = 0; |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | //===----------------------------------------------------------------------===// |
| 401 | // Helper functions. |
| 402 | //===----------------------------------------------------------------------===// |
| 403 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 404 | DeclContext *Sema::getFunctionLevelDeclContext() { |
John McCall | db0ee1d | 2009-12-19 10:53:49 +0000 | [diff] [blame] | 405 | DeclContext *DC = CurContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | d900831 | 2010-05-22 16:25:05 +0000 | [diff] [blame] | 407 | while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 408 | DC = DC->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 410 | return DC; |
| 411 | } |
| 412 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 413 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 414 | /// to the function decl for the function being parsed. If we're currently |
| 415 | /// in a 'block', this returns the containing context. |
| 416 | FunctionDecl *Sema::getCurFunctionDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 417 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 418 | return dyn_cast<FunctionDecl>(DC); |
| 419 | } |
| 420 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 421 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 422 | DeclContext *DC = getFunctionLevelDeclContext(); |
Steve Naroff | d7612e1 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 423 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 424 | } |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 425 | |
| 426 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 427 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 428 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 429 | return cast<NamedDecl>(DC); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 433 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 434 | if (!this->Emit()) |
| 435 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 437 | // If this is not a note, and we're in a template instantiation |
| 438 | // that is different from the last template instantiation where |
| 439 | // we emitted an error, print a template instantiation |
| 440 | // backtrace. |
| 441 | if (!SemaRef.Diags.isBuiltinNote(DiagID) && |
| 442 | !SemaRef.ActiveTemplateInstantiations.empty() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | SemaRef.ActiveTemplateInstantiations.back() |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 444 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 445 | SemaRef.PrintInstantiationStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 447 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 448 | } |
| 449 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 450 | |
Douglas Gregor | eab5d1e | 2010-03-25 22:17:48 +0000 | [diff] [blame] | 451 | Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
| 452 | if (isSFINAEContext()) { |
| 453 | switch (Diagnostic::getDiagnosticSFINAEResponse(DiagID)) { |
| 454 | case Diagnostic::SFINAE_Report: |
| 455 | // Fall through; we'll report the diagnostic below. |
| 456 | break; |
| 457 | |
| 458 | case Diagnostic::SFINAE_SubstitutionFailure: |
| 459 | // Count this failure so that we know that template argument deduction |
| 460 | // has failed. |
| 461 | ++NumSFINAEErrors; |
| 462 | // Fall through |
| 463 | |
| 464 | case Diagnostic::SFINAE_Suppress: |
| 465 | // Suppress this diagnostic. |
| 466 | Diags.setLastDiagnosticIgnored(); |
| 467 | return SemaDiagnosticBuilder(*this); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | DiagnosticBuilder DB = Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID); |
| 472 | return SemaDiagnosticBuilder(DB, *this, DiagID); |
| 473 | } |
| 474 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 475 | Sema::SemaDiagnosticBuilder |
| 476 | Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { |
| 477 | SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); |
| 478 | PD.Emit(Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 480 | return Builder; |
| 481 | } |
| 482 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 483 | /// \brief Determines the active Scope associated with the given declaration |
| 484 | /// context. |
| 485 | /// |
| 486 | /// This routine maps a declaration context to the active Scope object that |
| 487 | /// represents that declaration context in the parser. It is typically used |
| 488 | /// from "scope-less" code (e.g., template instantiation, lazy creation of |
| 489 | /// declarations) that injects a name for name-lookup purposes and, therefore, |
| 490 | /// must update the Scope. |
| 491 | /// |
| 492 | /// \returns The scope corresponding to the given declaraion context, or NULL |
| 493 | /// if no such scope is open. |
| 494 | Scope *Sema::getScopeForContext(DeclContext *Ctx) { |
| 495 | |
| 496 | if (!Ctx) |
| 497 | return 0; |
| 498 | |
| 499 | Ctx = Ctx->getPrimaryContext(); |
| 500 | for (Scope *S = getCurScope(); S; S = S->getParent()) { |
Sebastian Redl | cddc69f | 2010-07-08 23:07:34 +0000 | [diff] [blame] | 501 | // Ignore scopes that cannot have declarations. This is important for |
| 502 | // out-of-line definitions of static class members. |
| 503 | if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) |
| 504 | if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity())) |
| 505 | if (Ctx == Entity->getPrimaryContext()) |
| 506 | return S; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | return 0; |
| 510 | } |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 511 | |
| 512 | /// \brief Enter a new function scope |
| 513 | void Sema::PushFunctionScope() { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 514 | if (FunctionScopes.size() == 1) { |
| 515 | // Use the "top" function scope rather than having to allocate |
| 516 | // memory for a new scope. |
| 517 | FunctionScopes.back()->Clear(getDiagnostics().getNumErrors()); |
| 518 | FunctionScopes.push_back(FunctionScopes.back()); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 519 | return; |
| 520 | } |
| 521 | |
| 522 | FunctionScopes.push_back( |
| 523 | new FunctionScopeInfo(getDiagnostics().getNumErrors())); |
| 524 | } |
| 525 | |
| 526 | void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { |
| 527 | FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics().getNumErrors(), |
| 528 | BlockScope, Block)); |
| 529 | } |
| 530 | |
| 531 | void Sema::PopFunctionOrBlockScope() { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 532 | FunctionScopeInfo *Scope = FunctionScopes.pop_back_val(); |
| 533 | assert(!FunctionScopes.empty() && "mismatched push/pop!"); |
| 534 | if (FunctionScopes.back() != Scope) |
| 535 | delete Scope; |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /// \brief Determine whether any errors occurred within this function/method/ |
| 539 | /// block. |
| 540 | bool Sema::hasAnyErrorsInThisFunction() const { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 541 | return getCurFunction()->NumErrorsAtStartOfFunction |
| 542 | != getDiagnostics().getNumErrors(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | BlockScopeInfo *Sema::getCurBlock() { |
| 546 | if (FunctionScopes.empty()) |
| 547 | return 0; |
| 548 | |
| 549 | return dyn_cast<BlockScopeInfo>(FunctionScopes.back()); |
| 550 | } |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 551 | |
| 552 | // Pin this vtable to this file. |
| 553 | ExternalSemaSource::~ExternalSemaSource() {} |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 554 | |
| 555 | void PrettyDeclStackTraceEntry::print(llvm::raw_ostream &OS) const { |
| 556 | SourceLocation Loc = this->Loc; |
| 557 | if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); |
| 558 | if (Loc.isValid()) { |
| 559 | Loc.print(OS, S.getSourceManager()); |
| 560 | OS << ": "; |
| 561 | } |
| 562 | OS << Message; |
| 563 | |
| 564 | if (TheDecl && isa<NamedDecl>(TheDecl)) { |
| 565 | std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString(); |
| 566 | if (!Name.empty()) |
| 567 | OS << " '" << Name << '\''; |
| 568 | } |
| 569 | |
| 570 | OS << '\n'; |
| 571 | } |