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 | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 30 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 31 | if (C.getLangOptions().CPlusPlus) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 33 | C.getTranslationUnitDecl(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 34 | SourceLocation(), &C.Idents.get(Name)); |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 35 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | return RecordDecl::Create(C, TagDecl::TK_struct, |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 37 | C.getTranslationUnitDecl(), |
| 38 | SourceLocation(), &C.Idents.get(Name)); |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 41 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 42 | TUScope = S; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 43 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 45 | if (PP.getTargetInfo().getPointerWidth(0) >= 64) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 46 | TypeSourceInfo *TInfo; |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 48 | // Install [u]int128_t for 64-bit targets. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 49 | TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 50 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 51 | SourceLocation(), |
| 52 | &Context.Idents.get("__int128_t"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 53 | TInfo), TUScope); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 54 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 55 | TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 56 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 57 | SourceLocation(), |
| 58 | &Context.Idents.get("__uint128_t"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 59 | TInfo), TUScope); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 60 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
| 62 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 63 | if (!PP.getLangOptions().ObjC1) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Steve Naroff | cb83c53 | 2009-06-16 00:20:10 +0000 | [diff] [blame] | 65 | // 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] | 66 | if (Context.getObjCSelType().isNull()) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 67 | // Create the built-in typedef for 'SEL'. |
Fariborz Jahanian | 04765ac | 2009-11-23 18:04:25 +0000 | [diff] [blame] | 68 | QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 69 | TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 70 | TypedefDecl *SelTypedef |
| 71 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 72 | &Context.Idents.get("SEL"), SelInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 73 | PushOnScopeChains(SelTypedef, TUScope); |
| 74 | Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 75 | Context.ObjCSelRedefinitionType = Context.getObjCSelType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 76 | } |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 78 | // Synthesize "@class Protocol; |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 79 | if (Context.getObjCProtoType().isNull()) { |
| 80 | ObjCInterfaceDecl *ProtocolDecl = |
| 81 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | &Context.Idents.get("Protocol"), |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 83 | SourceLocation(), true); |
| 84 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
Fariborz Jahanian | 10324db | 2009-11-18 23:15:37 +0000 | [diff] [blame] | 85 | PushOnScopeChains(ProtocolDecl, TUScope, false); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 86 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 87 | // Create the built-in typedef for 'id'. |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 88 | if (Context.getObjCIdType().isNull()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 89 | QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 90 | TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(IdT); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 91 | TypedefDecl *IdTypedef |
| 92 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 93 | &Context.Idents.get("id"), IdInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 94 | PushOnScopeChains(IdTypedef, TUScope); |
| 95 | Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 96 | Context.ObjCIdRedefinitionType = Context.getObjCIdType(); |
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 'Class'. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 99 | if (Context.getObjCClassType().isNull()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 100 | QualType ClassType |
| 101 | = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 102 | TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(ClassType); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 103 | TypedefDecl *ClassTypedef |
| 104 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 105 | &Context.Idents.get("Class"), ClassInfo); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 106 | PushOnScopeChains(ClassTypedef, TUScope); |
| 107 | Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 108 | Context.ObjCClassRedefinitionType = Context.getObjCClassType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 109 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 112 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 113 | bool CompleteTranslationUnit, |
| 114 | CodeCompleteConsumer *CodeCompleter) |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 115 | : TheTargetAttributesSema(0), |
| 116 | LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 118 | ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0), |
John McCall | db0ee1d | 2009-12-19 10:53:49 +0000 | [diff] [blame] | 119 | CurBlock(0), PackContext(0), ParsingDeclDepth(0), |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 120 | IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0), |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 121 | GlobalNewDeleteDeclared(false), |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 122 | CompleteTranslationUnit(CompleteTranslationUnit), |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 123 | NumSFINAEErrors(0), NonInstantiationEntries(0), |
Ted Kremenek | d0ed448 | 2010-02-02 02:07:01 +0000 | [diff] [blame] | 124 | CurrentInstantiationScope(0), TyposCorrected(0) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 125 | { |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 126 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 127 | if (getLangOptions().CPlusPlus) |
| 128 | FieldCollector.reset(new CXXFieldCollector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 6d97e5e | 2010-03-01 20:59:53 +0000 | [diff] [blame^] | 130 | NumErrorsAtStartOfFunction = 0; |
| 131 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 132 | // Tell diagnostics how to render things from the AST library. |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 133 | PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 134 | &Context); |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 135 | |
| 136 | ExprEvalContexts.push_back( |
| 137 | ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 140 | Sema::~Sema() { |
| 141 | if (PackContext) FreePackedContext(); |
| 142 | delete TheTargetAttributesSema; |
| 143 | } |
| 144 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 146 | /// If there is already an implicit cast, merge into the existing one. |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 147 | /// If isLvalue, the result of the cast is an lvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, |
Anders Carlsson | c0a2fd8 | 2009-09-15 05:13:45 +0000 | [diff] [blame] | 149 | CastExpr::CastKind Kind, bool isLvalue) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 150 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 151 | QualType TypeTy = Context.getCanonicalType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 153 | if (ExprTy == TypeTy) |
| 154 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 156 | if (Expr->getType()->isPointerType() && Ty->isPointerType()) { |
| 157 | QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType(); |
| 158 | QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 159 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 160 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 161 | << Expr->getSourceRange(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 165 | CheckImplicitConversion(Expr, Ty); |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 166 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 167 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Anders Carlsson | 4c5fad3 | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 168 | if (ImpCast->getCastKind() == Kind) { |
| 169 | ImpCast->setType(Ty); |
| 170 | ImpCast->setLvalueCast(isLvalue); |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 178 | void Sema::DeleteExpr(ExprTy *E) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 179 | if (E) static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 180 | } |
| 181 | void Sema::DeleteStmt(StmtTy *S) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 182 | if (S) static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 185 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 186 | /// translation unit when EOF is reached and all but the top-level scope is |
| 187 | /// popped. |
| 188 | void Sema::ActOnEndOfTranslationUnit() { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 189 | |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 190 | // Remove functions that turned out to be used. |
| 191 | UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(), |
| 192 | UnusedStaticFuncs.end(), |
| 193 | std::mem_fun(&FunctionDecl::isUsed)), |
| 194 | UnusedStaticFuncs.end()); |
| 195 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 196 | while (1) { |
| 197 | // C++: Perform implicit template instantiations. |
| 198 | // |
| 199 | // FIXME: When we perform these implicit instantiations, we do not carefully |
| 200 | // keep track of the point of instantiation (C++ [temp.point]). This means |
| 201 | // that name lookup that occurs within the template instantiation will |
| 202 | // always happen at the end of the translation unit, so it will find |
| 203 | // some names that should not be found. Although this is common behavior |
| 204 | // for C++ compilers, it is technically wrong. In the future, we either need |
| 205 | // to be able to filter the results of name lookup or we need to perform |
| 206 | // template instantiations earlier. |
| 207 | PerformPendingImplicitInstantiations(); |
| 208 | |
| 209 | /// If ProcessPendingClassesWithUnmarkedVirtualMembers ends up marking |
| 210 | /// any virtual member functions it might lead to more pending template |
| 211 | /// instantiations, which is why we need to loop here. |
| 212 | if (!ProcessPendingClassesWithUnmarkedVirtualMembers()) |
| 213 | break; |
| 214 | } |
| 215 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 216 | // Check for #pragma weak identifiers that were never declared |
| 217 | // FIXME: This will cause diagnostics to be emitted in a non-determinstic |
| 218 | // order! Iterating over a densemap like this is bad. |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 219 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 220 | I = WeakUndeclaredIdentifiers.begin(), |
| 221 | E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 222 | if (I->second.getUsed()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 224 | Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 225 | << I->first; |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 228 | if (!CompleteTranslationUnit) |
| 229 | return; |
| 230 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 231 | // C99 6.9.2p2: |
| 232 | // A declaration of an identifier for an object that has file |
| 233 | // scope without an initializer, and without a storage-class |
| 234 | // specifier or with the storage-class specifier static, |
| 235 | // constitutes a tentative definition. If a translation unit |
| 236 | // contains one or more tentative definitions for an identifier, |
| 237 | // and the translation unit contains no external definition for |
| 238 | // that identifier, then the behavior is exactly as if the |
| 239 | // translation unit contains a file scope declaration of that |
| 240 | // identifier, with the composite type as of the end of the |
| 241 | // translation unit, with an initializer equal to 0. |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 242 | llvm::SmallSet<VarDecl *, 32> Seen; |
| 243 | for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) { |
| 244 | VarDecl *VD = TentativeDefinitions[i]->getActingDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 246 | // If the tentative definition was completed, getActingDefinition() returns |
| 247 | // null. If we've already seen this variable before, insert()'s second |
| 248 | // return value is false. |
| 249 | if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD)) |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 250 | continue; |
| 251 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | if (const IncompleteArrayType *ArrayT |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 253 | = Context.getAsIncompleteArrayType(VD->getType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | if (RequireCompleteType(VD->getLocation(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 255 | ArrayT->getElementType(), |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 256 | diag::err_tentative_def_incomplete_type_arr)) { |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 257 | VD->setInvalidDecl(); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 258 | continue; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 259 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 261 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 262 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 263 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 264 | QualType T = Context.getConstantArrayType(ArrayT->getElementType(), |
| 265 | One, ArrayType::Normal, 0); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 266 | VD->setType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 268 | diag::err_tentative_def_incomplete_type)) |
| 269 | VD->setInvalidDecl(); |
| 270 | |
| 271 | // Notify the consumer that we've completed a tentative definition. |
| 272 | if (!VD->isInvalidDecl()) |
| 273 | Consumer.CompleteTentativeDefinition(VD); |
| 274 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 275 | } |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 276 | |
| 277 | // Output warning for unused functions. |
| 278 | for (std::vector<FunctionDecl*>::iterator |
| 279 | F = UnusedStaticFuncs.begin(), |
| 280 | FEnd = UnusedStaticFuncs.end(); |
| 281 | F != FEnd; |
| 282 | ++F) |
| 283 | Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName(); |
| 284 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | //===----------------------------------------------------------------------===// |
| 289 | // Helper functions. |
| 290 | //===----------------------------------------------------------------------===// |
| 291 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 292 | DeclContext *Sema::getFunctionLevelDeclContext() { |
John McCall | db0ee1d | 2009-12-19 10:53:49 +0000 | [diff] [blame] | 293 | DeclContext *DC = CurContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 295 | while (isa<BlockDecl>(DC)) |
| 296 | DC = DC->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 298 | return DC; |
| 299 | } |
| 300 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 301 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 302 | /// to the function decl for the function being parsed. If we're currently |
| 303 | /// in a 'block', this returns the containing context. |
| 304 | FunctionDecl *Sema::getCurFunctionDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 305 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 306 | return dyn_cast<FunctionDecl>(DC); |
| 307 | } |
| 308 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 309 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 310 | DeclContext *DC = getFunctionLevelDeclContext(); |
Steve Naroff | d7612e1 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 311 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 313 | |
| 314 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 315 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 316 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 317 | return cast<NamedDecl>(DC); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 321 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 322 | if (!this->Emit()) |
| 323 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 325 | // If this is not a note, and we're in a template instantiation |
| 326 | // that is different from the last template instantiation where |
| 327 | // we emitted an error, print a template instantiation |
| 328 | // backtrace. |
| 329 | if (!SemaRef.Diags.isBuiltinNote(DiagID) && |
| 330 | !SemaRef.ActiveTemplateInstantiations.empty() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | SemaRef.ActiveTemplateInstantiations.back() |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 332 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 333 | SemaRef.PrintInstantiationStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 335 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 336 | } |
| 337 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 338 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 339 | Sema::SemaDiagnosticBuilder |
| 340 | Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { |
| 341 | SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); |
| 342 | PD.Emit(Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 344 | return Builder; |
| 345 | } |
| 346 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 347 | void Sema::ActOnComment(SourceRange Comment) { |
| 348 | Context.Comments.push_back(Comment); |
| 349 | } |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 350 | |