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