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