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 | |
| 15 | #include "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 | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 20 | #include "clang/Sema/ExternalSemaSource.h" |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 23 | #include "clang/AST/ASTDiagnostic.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 25 | #include "clang/AST/Expr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 27 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | using namespace clang; |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 30 | |
| 31 | FunctionScopeInfo::~FunctionScopeInfo() { } |
| 32 | |
| 33 | void FunctionScopeInfo::Clear(unsigned NumErrors) { |
| 34 | NeedsScopeChecking = false; |
| 35 | LabelMap.clear(); |
| 36 | SwitchStack.clear(); |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 37 | Returns.clear(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 38 | NumErrorsAtStartOfFunction = NumErrors; |
| 39 | } |
| 40 | |
| 41 | BlockScopeInfo::~BlockScopeInfo() { } |
| 42 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 43 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 44 | TUScope = S; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 45 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 47 | VAListTagName = PP.getIdentifierInfo("__va_list_tag"); |
| 48 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 49 | if (PP.getTargetInfo().getPointerWidth(0) >= 64) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 50 | TypeSourceInfo *TInfo; |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 52 | // Install [u]int128_t for 64-bit targets. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 53 | TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 54 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 55 | SourceLocation(), |
| 56 | &Context.Idents.get("__int128_t"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 57 | TInfo), TUScope); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 58 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 59 | TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 60 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 61 | SourceLocation(), |
| 62 | &Context.Idents.get("__uint128_t"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 63 | TInfo), TUScope); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 64 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
| 66 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 67 | if (!PP.getLangOptions().ObjC1) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Steve Naroff | cb83c53 | 2009-06-16 00:20:10 +0000 | [diff] [blame] | 69 | // Built-in ObjC types may already be set by PCHReader (hence isNull checks). |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 70 | if (Context.getObjCSelType().isNull()) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 71 | // Create the built-in typedef for 'SEL'. |
Fariborz Jahanian | 04765ac | 2009-11-23 18:04:25 +0000 | [diff] [blame] | 72 | QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 73 | TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 74 | TypedefDecl *SelTypedef |
| 75 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 76 | &Context.Idents.get("SEL"), SelInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 77 | PushOnScopeChains(SelTypedef, TUScope); |
| 78 | Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 79 | Context.ObjCSelRedefinitionType = Context.getObjCSelType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 80 | } |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 82 | // Synthesize "@class Protocol; |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 83 | if (Context.getObjCProtoType().isNull()) { |
| 84 | ObjCInterfaceDecl *ProtocolDecl = |
| 85 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | &Context.Idents.get("Protocol"), |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 87 | SourceLocation(), true); |
| 88 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
Fariborz Jahanian | 10324db | 2009-11-18 23:15:37 +0000 | [diff] [blame] | 89 | PushOnScopeChains(ProtocolDecl, TUScope, false); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 90 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 91 | // Create the built-in typedef for 'id'. |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 92 | if (Context.getObjCIdType().isNull()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 93 | QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0); |
| 94 | T = Context.getObjCObjectPointerType(T); |
| 95 | TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 96 | TypedefDecl *IdTypedef |
| 97 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 98 | &Context.Idents.get("id"), IdInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 99 | PushOnScopeChains(IdTypedef, TUScope); |
| 100 | Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 101 | Context.ObjCIdRedefinitionType = Context.getObjCIdType(); |
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 'Class'. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 104 | if (Context.getObjCClassType().isNull()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 105 | QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0); |
| 106 | T = Context.getObjCObjectPointerType(T); |
| 107 | TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 108 | TypedefDecl *ClassTypedef |
| 109 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 110 | &Context.Idents.get("Class"), ClassInfo); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 111 | PushOnScopeChains(ClassTypedef, TUScope); |
| 112 | Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 113 | Context.ObjCClassRedefinitionType = Context.getObjCClassType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 114 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 117 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 118 | bool CompleteTranslationUnit, |
| 119 | CodeCompleteConsumer *CodeCompleter) |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 120 | : TheTargetAttributesSema(0), |
| 121 | LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 123 | ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0), |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 124 | PackContext(0), TopFunctionScope(0), ParsingDeclDepth(0), |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 125 | IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0), |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 126 | GlobalNewDeleteDeclared(false), |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 127 | CompleteTranslationUnit(CompleteTranslationUnit), |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 128 | NumSFINAEErrors(0), NonInstantiationEntries(0), |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 129 | CurrentInstantiationScope(0), TyposCorrected(0), |
| 130 | AnalysisWarnings(*this) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 131 | { |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 132 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 133 | if (getLangOptions().CPlusPlus) |
| 134 | FieldCollector.reset(new CXXFieldCollector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 136 | // Tell diagnostics how to render things from the AST library. |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 137 | PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 138 | &Context); |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 139 | |
| 140 | ExprEvalContexts.push_back( |
| 141 | ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 144 | Sema::~Sema() { |
| 145 | if (PackContext) FreePackedContext(); |
| 146 | delete TheTargetAttributesSema; |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 147 | while (!FunctionScopes.empty()) |
| 148 | PopFunctionOrBlockScope(); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 152 | /// If there is already an implicit cast, merge into the existing one. |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 153 | /// If isLvalue, the result of the cast is an lvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, |
Anders Carlsson | 88465d3 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 155 | CastExpr::CastKind Kind, |
Anders Carlsson | f1b48b7 | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 156 | bool isLvalue, CXXBaseSpecifierArray BasePath) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 157 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 158 | QualType TypeTy = Context.getCanonicalType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 160 | if (ExprTy == TypeTy) |
| 161 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 163 | if (Expr->getType()->isPointerType() && Ty->isPointerType()) { |
| 164 | QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType(); |
| 165 | QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 166 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 167 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 168 | << Expr->getSourceRange(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 172 | // If this is a derived-to-base cast to a through a virtual base, we |
| 173 | // need a vtable. |
| 174 | if (Kind == CastExpr::CK_DerivedToBase && |
| 175 | BasePathInvolvesVirtualBase(BasePath)) { |
| 176 | QualType T = Expr->getType(); |
| 177 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 178 | T = Pointer->getPointeeType(); |
| 179 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
| 180 | MarkVTableUsed(Expr->getLocStart(), |
| 181 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 182 | } |
| 183 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 184 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 185 | if (ImpCast->getCastKind() == Kind && BasePath.empty()) { |
Anders Carlsson | 4c5fad3 | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 186 | ImpCast->setType(Ty); |
| 187 | ImpCast->setLvalueCast(isLvalue); |
| 188 | return; |
| 189 | } |
| 190 | } |
| 191 | |
Anders Carlsson | 7ab9d57 | 2010-04-24 16:34:21 +0000 | [diff] [blame] | 192 | Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, BasePath, isLvalue); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 195 | void Sema::DeleteExpr(ExprTy *E) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 196 | if (E) static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 197 | } |
| 198 | void Sema::DeleteStmt(StmtTy *S) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 199 | if (S) static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 202 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 203 | /// translation unit when EOF is reached and all but the top-level scope is |
| 204 | /// popped. |
Douglas Gregor | 47268a3 | 2010-04-09 17:41:13 +0000 | [diff] [blame] | 205 | void Sema::ActOnEndOfTranslationUnit() { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 206 | while (1) { |
| 207 | // C++: Perform implicit template instantiations. |
| 208 | // |
| 209 | // FIXME: When we perform these implicit instantiations, we do not carefully |
| 210 | // keep track of the point of instantiation (C++ [temp.point]). This means |
| 211 | // that name lookup that occurs within the template instantiation will |
| 212 | // always happen at the end of the translation unit, so it will find |
| 213 | // some names that should not be found. Although this is common behavior |
| 214 | // for C++ compilers, it is technically wrong. In the future, we either need |
| 215 | // to be able to filter the results of name lookup or we need to perform |
| 216 | // template instantiations earlier. |
| 217 | PerformPendingImplicitInstantiations(); |
| 218 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 219 | /// If DefinedUsedVTables ends up marking any virtual member |
| 220 | /// functions it might lead to more pending template |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 221 | /// instantiations, which is why we need to loop here. |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 222 | if (!DefineUsedVTables()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | |
Douglas Gregor | 47268a3 | 2010-04-09 17:41:13 +0000 | [diff] [blame] | 226 | // Remove functions that turned out to be used. |
| 227 | UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(), |
| 228 | UnusedStaticFuncs.end(), |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame^] | 229 | std::bind2nd(std::mem_fun(&FunctionDecl::isUsed), |
| 230 | true)), |
Douglas Gregor | 47268a3 | 2010-04-09 17:41:13 +0000 | [diff] [blame] | 231 | UnusedStaticFuncs.end()); |
| 232 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 233 | // Check for #pragma weak identifiers that were never declared |
| 234 | // FIXME: This will cause diagnostics to be emitted in a non-determinstic |
| 235 | // order! Iterating over a densemap like this is bad. |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 236 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 237 | I = WeakUndeclaredIdentifiers.begin(), |
| 238 | E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 239 | if (I->second.getUsed()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 241 | Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 242 | << I->first; |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 245 | if (!CompleteTranslationUnit) |
| 246 | return; |
| 247 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 248 | // C99 6.9.2p2: |
| 249 | // A declaration of an identifier for an object that has file |
| 250 | // scope without an initializer, and without a storage-class |
| 251 | // specifier or with the storage-class specifier static, |
| 252 | // constitutes a tentative definition. If a translation unit |
| 253 | // contains one or more tentative definitions for an identifier, |
| 254 | // and the translation unit contains no external definition for |
| 255 | // that identifier, then the behavior is exactly as if the |
| 256 | // translation unit contains a file scope declaration of that |
| 257 | // identifier, with the composite type as of the end of the |
| 258 | // translation unit, with an initializer equal to 0. |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 259 | llvm::SmallSet<VarDecl *, 32> Seen; |
| 260 | for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) { |
| 261 | VarDecl *VD = TentativeDefinitions[i]->getActingDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 263 | // If the tentative definition was completed, getActingDefinition() returns |
| 264 | // null. If we've already seen this variable before, insert()'s second |
| 265 | // return value is false. |
| 266 | if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD)) |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 267 | continue; |
| 268 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | if (const IncompleteArrayType *ArrayT |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 270 | = Context.getAsIncompleteArrayType(VD->getType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | if (RequireCompleteType(VD->getLocation(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 272 | ArrayT->getElementType(), |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 273 | diag::err_tentative_def_incomplete_type_arr)) { |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 274 | VD->setInvalidDecl(); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 275 | continue; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 278 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 279 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 280 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 281 | QualType T = Context.getConstantArrayType(ArrayT->getElementType(), |
| 282 | One, ArrayType::Normal, 0); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 283 | VD->setType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 285 | diag::err_tentative_def_incomplete_type)) |
| 286 | VD->setInvalidDecl(); |
| 287 | |
| 288 | // Notify the consumer that we've completed a tentative definition. |
| 289 | if (!VD->isInvalidDecl()) |
| 290 | Consumer.CompleteTentativeDefinition(VD); |
| 291 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 292 | } |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 293 | |
| 294 | // Output warning for unused functions. |
| 295 | for (std::vector<FunctionDecl*>::iterator |
| 296 | F = UnusedStaticFuncs.begin(), |
| 297 | FEnd = UnusedStaticFuncs.end(); |
| 298 | F != FEnd; |
| 299 | ++F) |
| 300 | Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName(); |
| 301 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 305 | //===----------------------------------------------------------------------===// |
| 306 | // Helper functions. |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 309 | DeclContext *Sema::getFunctionLevelDeclContext() { |
John McCall | db0ee1d | 2009-12-19 10:53:49 +0000 | [diff] [blame] | 310 | DeclContext *DC = CurContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | d900831 | 2010-05-22 16:25:05 +0000 | [diff] [blame] | 312 | while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 313 | DC = DC->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 315 | return DC; |
| 316 | } |
| 317 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 318 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 319 | /// to the function decl for the function being parsed. If we're currently |
| 320 | /// in a 'block', this returns the containing context. |
| 321 | FunctionDecl *Sema::getCurFunctionDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 322 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 323 | return dyn_cast<FunctionDecl>(DC); |
| 324 | } |
| 325 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 326 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 327 | DeclContext *DC = getFunctionLevelDeclContext(); |
Steve Naroff | d7612e1 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 328 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 330 | |
| 331 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 332 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 333 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 334 | return cast<NamedDecl>(DC); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 338 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 339 | if (!this->Emit()) |
| 340 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 342 | // If this is not a note, and we're in a template instantiation |
| 343 | // that is different from the last template instantiation where |
| 344 | // we emitted an error, print a template instantiation |
| 345 | // backtrace. |
| 346 | if (!SemaRef.Diags.isBuiltinNote(DiagID) && |
| 347 | !SemaRef.ActiveTemplateInstantiations.empty() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | SemaRef.ActiveTemplateInstantiations.back() |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 349 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 350 | SemaRef.PrintInstantiationStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 352 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 353 | } |
| 354 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 355 | |
Douglas Gregor | eab5d1e | 2010-03-25 22:17:48 +0000 | [diff] [blame] | 356 | Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
| 357 | if (isSFINAEContext()) { |
| 358 | switch (Diagnostic::getDiagnosticSFINAEResponse(DiagID)) { |
| 359 | case Diagnostic::SFINAE_Report: |
| 360 | // Fall through; we'll report the diagnostic below. |
| 361 | break; |
| 362 | |
| 363 | case Diagnostic::SFINAE_SubstitutionFailure: |
| 364 | // Count this failure so that we know that template argument deduction |
| 365 | // has failed. |
| 366 | ++NumSFINAEErrors; |
| 367 | // Fall through |
| 368 | |
| 369 | case Diagnostic::SFINAE_Suppress: |
| 370 | // Suppress this diagnostic. |
| 371 | Diags.setLastDiagnosticIgnored(); |
| 372 | return SemaDiagnosticBuilder(*this); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | DiagnosticBuilder DB = Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID); |
| 377 | return SemaDiagnosticBuilder(DB, *this, DiagID); |
| 378 | } |
| 379 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 380 | Sema::SemaDiagnosticBuilder |
| 381 | Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { |
| 382 | SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); |
| 383 | PD.Emit(Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 385 | return Builder; |
| 386 | } |
| 387 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 388 | |
| 389 | /// \brief Enter a new function scope |
| 390 | void Sema::PushFunctionScope() { |
| 391 | if (FunctionScopes.empty()) { |
| 392 | // Use the "top" function scope rather than having to allocate memory for |
| 393 | // a new scope. |
| 394 | TopFunctionScope.Clear(getDiagnostics().getNumErrors()); |
| 395 | FunctionScopes.push_back(&TopFunctionScope); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | FunctionScopes.push_back( |
| 400 | new FunctionScopeInfo(getDiagnostics().getNumErrors())); |
| 401 | } |
| 402 | |
| 403 | void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { |
| 404 | FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics().getNumErrors(), |
| 405 | BlockScope, Block)); |
| 406 | } |
| 407 | |
| 408 | void Sema::PopFunctionOrBlockScope() { |
| 409 | if (FunctionScopes.back() != &TopFunctionScope) |
| 410 | delete FunctionScopes.back(); |
| 411 | else |
| 412 | TopFunctionScope.Clear(getDiagnostics().getNumErrors()); |
| 413 | |
| 414 | FunctionScopes.pop_back(); |
| 415 | } |
| 416 | |
| 417 | /// \brief Determine whether any errors occurred within this function/method/ |
| 418 | /// block. |
| 419 | bool Sema::hasAnyErrorsInThisFunction() const { |
| 420 | unsigned NumErrors = TopFunctionScope.NumErrorsAtStartOfFunction; |
| 421 | if (!FunctionScopes.empty()) |
| 422 | NumErrors = FunctionScopes.back()->NumErrorsAtStartOfFunction; |
| 423 | return NumErrors != getDiagnostics().getNumErrors(); |
| 424 | } |
| 425 | |
| 426 | BlockScopeInfo *Sema::getCurBlock() { |
| 427 | if (FunctionScopes.empty()) |
| 428 | return 0; |
| 429 | |
| 430 | return dyn_cast<BlockScopeInfo>(FunctionScopes.back()); |
| 431 | } |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 432 | |
| 433 | // Pin this vtable to this file. |
| 434 | ExternalSemaSource::~ExternalSemaSource() {} |