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