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" |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 25 | /// ConvertQualTypeToStringFn - This function is used to pretty print the |
| 26 | /// specified QualType as a string in diagnostics. |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 27 | static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val, |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 28 | const char *Modifier, unsigned ModLen, |
| 29 | const char *Argument, unsigned ArgLen, |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 30 | llvm::SmallVectorImpl<char> &Output, |
| 31 | void *Cookie) { |
| 32 | ASTContext &Context = *static_cast<ASTContext*>(Cookie); |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 34 | std::string S; |
| 35 | if (Kind == Diagnostic::ak_qualtype) { |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 36 | assert(ModLen == 0 && ArgLen == 0 && |
| 37 | "Invalid modifier for QualType argument"); |
| 38 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 39 | QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 41 | // FIXME: Playing with std::string is really slow. |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 42 | S = Ty.getAsString(Context.PrintingPolicy); |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 43 | |
| 44 | // If this is a sugared type (like a typedef, typeof, etc), then unwrap one |
| 45 | // level of the sugar so that the type is more obvious to the user. |
Douglas Gregor | 969c689 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 46 | QualType DesugaredTy = Ty->getDesugaredType(true); |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 47 | DesugaredTy.setCVRQualifiers(DesugaredTy.getCVRQualifiers() | |
| 48 | Ty.getCVRQualifiers()); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 49 | |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 50 | if (Ty != DesugaredTy && |
| 51 | // If the desugared type is a vector type, we don't want to expand it, |
| 52 | // it will turn into an attribute mess. People want their "vec4". |
| 53 | !isa<VectorType>(DesugaredTy) && |
| 54 | |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 55 | // Don't desugar magic Objective-C types. |
| 56 | Ty.getUnqualifiedType() != Context.getObjCIdType() && |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 57 | Ty.getUnqualifiedType() != Context.getObjCClassType() && |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 58 | Ty.getUnqualifiedType() != Context.getObjCSelType() && |
| 59 | Ty.getUnqualifiedType() != Context.getObjCProtoType() && |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 60 | |
| 61 | // Not va_list. |
| 62 | Ty.getUnqualifiedType() != Context.getBuiltinVaListType()) { |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 63 | S = "'"+S+"' (aka '"; |
Argyrios Kyrtzidis | 7c94c4b | 2009-06-03 02:06:50 +0000 | [diff] [blame] | 64 | S += DesugaredTy.getAsString(Context.PrintingPolicy); |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 65 | S += "')"; |
| 66 | Output.append(S.begin(), S.end()); |
| 67 | return; |
| 68 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 70 | } else if (Kind == Diagnostic::ak_declarationname) { |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 71 | |
| 72 | DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); |
| 73 | S = N.getAsString(); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 74 | |
| 75 | if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0) |
| 76 | S = '+' + S; |
| 77 | else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0) |
| 78 | S = '-' + S; |
| 79 | else |
| 80 | assert(ModLen == 0 && ArgLen == 0 && |
| 81 | "Invalid modifier for DeclarationName argument"); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 82 | } else { |
| 83 | assert(Kind == Diagnostic::ak_nameddecl); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 84 | if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0) |
| 85 | S = reinterpret_cast<NamedDecl*>(Val)->getQualifiedNameAsString(); |
| 86 | else { |
| 87 | assert(ModLen == 0 && ArgLen == 0 && |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 88 | "Invalid modifier for NamedDecl* argument"); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 89 | S = reinterpret_cast<NamedDecl*>(Val)->getNameAsString(); |
| 90 | } |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 91 | } |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 92 | |
| 93 | Output.push_back('\''); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 94 | Output.append(S.begin(), S.end()); |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 95 | Output.push_back('\''); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 99 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 100 | if (C.getLangOptions().CPlusPlus) |
| 101 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
| 102 | C.getTranslationUnitDecl(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 103 | SourceLocation(), &C.Idents.get(Name)); |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 104 | |
| 105 | return RecordDecl::Create(C, TagDecl::TK_struct, |
| 106 | C.getTranslationUnitDecl(), |
| 107 | SourceLocation(), &C.Idents.get(Name)); |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 110 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 111 | TUScope = S; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 112 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 114 | if (PP.getTargetInfo().getPointerWidth(0) >= 64) { |
| 115 | // Install [u]int128_t for 64-bit targets. |
| 116 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 117 | SourceLocation(), |
| 118 | &Context.Idents.get("__int128_t"), |
| 119 | Context.Int128Ty), TUScope); |
| 120 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 121 | SourceLocation(), |
| 122 | &Context.Idents.get("__uint128_t"), |
| 123 | Context.UnsignedInt128Ty), TUScope); |
| 124 | } |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 125 | |
| 126 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 127 | if (!PP.getLangOptions().ObjC1) return; |
| 128 | |
Steve Naroff | cb83c53 | 2009-06-16 00:20:10 +0000 | [diff] [blame] | 129 | // 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] | 130 | if (Context.getObjCSelType().isNull()) { |
| 131 | // Synthesize "typedef struct objc_selector *SEL;" |
| 132 | RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); |
| 133 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 134 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 135 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
| 136 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 137 | SourceLocation(), |
| 138 | &Context.Idents.get("SEL"), |
| 139 | SelT); |
| 140 | PushOnScopeChains(SelTypedef, TUScope); |
| 141 | Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); |
| 142 | } |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 144 | // Synthesize "@class Protocol; |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 145 | if (Context.getObjCProtoType().isNull()) { |
| 146 | ObjCInterfaceDecl *ProtocolDecl = |
| 147 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
| 148 | &Context.Idents.get("Protocol"), |
| 149 | SourceLocation(), true); |
| 150 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 151 | PushOnScopeChains(ProtocolDecl, TUScope); |
| 152 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 153 | // Create the built-in typedef for 'id'. |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 154 | if (Context.getObjCIdType().isNull()) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 155 | TypedefDecl *IdTypedef = |
| 156 | TypedefDecl::Create( |
| 157 | Context, CurContext, SourceLocation(), &Context.Idents.get("id"), |
| 158 | Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy) |
| 159 | ); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 160 | PushOnScopeChains(IdTypedef, TUScope); |
| 161 | Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); |
| 162 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 163 | // Create the built-in typedef for 'Class'. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 164 | if (Context.getObjCClassType().isNull()) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 165 | TypedefDecl *ClassTypedef = |
| 166 | TypedefDecl::Create( |
| 167 | Context, CurContext, SourceLocation(), &Context.Idents.get("Class"), |
| 168 | Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy) |
| 169 | ); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 170 | PushOnScopeChains(ClassTypedef, TUScope); |
| 171 | Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); |
| 172 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 175 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
| 176 | bool CompleteTranslationUnit) |
Chris Lattner | 53ebff3 | 2009-01-22 19:21:44 +0000 | [diff] [blame] | 177 | : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 178 | Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), |
| 179 | ExternalSource(0), CurContext(0), PreDeclaratorDC(0), |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 180 | CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()), |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 181 | GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated), |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 182 | CompleteTranslationUnit(CompleteTranslationUnit), |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 183 | NumSFINAEErrors(0), CurrentInstantiationScope(0) { |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 184 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 185 | StdNamespace = 0; |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 186 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 187 | if (getLangOptions().CPlusPlus) |
| 188 | FieldCollector.reset(new CXXFieldCollector()); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 189 | |
| 190 | // Tell diagnostics how to render things from the AST library. |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 191 | PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 194 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
| 195 | /// If there is already an implicit cast, merge into the existing one. |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 196 | /// If isLvalue, the result of the cast is an lvalue. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 197 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 198 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 199 | QualType TypeTy = Context.getCanonicalType(Ty); |
| 200 | |
| 201 | if (ExprTy == TypeTy) |
| 202 | return; |
| 203 | |
| 204 | if (Expr->getType().getTypePtr()->isPointerType() && |
| 205 | Ty.getTypePtr()->isPointerType()) { |
| 206 | QualType ExprBaseType = |
| 207 | cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType(); |
| 208 | QualType BaseType = |
| 209 | cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType(); |
| 210 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 211 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 212 | << Expr->getSourceRange(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 215 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 216 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 217 | ImpCast->setType(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 218 | ImpCast->setLvalueCast(isLvalue); |
| 219 | } else |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame^] | 220 | Expr = new (Context) ImplicitCastExpr(Ty, CastExpr::CK_Unknown, Expr, |
| 221 | isLvalue); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 224 | void Sema::DeleteExpr(ExprTy *E) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 225 | if (E) static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 226 | } |
| 227 | void Sema::DeleteStmt(StmtTy *S) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 228 | if (S) static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 231 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 232 | /// translation unit when EOF is reached and all but the top-level scope is |
| 233 | /// popped. |
| 234 | void Sema::ActOnEndOfTranslationUnit() { |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 235 | // C++: Perform implicit template instantiations. |
| 236 | // |
| 237 | // FIXME: When we perform these implicit instantiations, we do not carefully |
| 238 | // keep track of the point of instantiation (C++ [temp.point]). This means |
| 239 | // that name lookup that occurs within the template instantiation will |
| 240 | // always happen at the end of the translation unit, so it will find |
| 241 | // some names that should not be found. Although this is common behavior |
| 242 | // for C++ compilers, it is technically wrong. In the future, we either need |
| 243 | // to be able to filter the results of name lookup or we need to perform |
| 244 | // template instantiations earlier. |
| 245 | PerformPendingImplicitInstantiations(); |
| 246 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 247 | // check for #pragma weak identifiers that were never declared |
| 248 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
| 249 | I = WeakUndeclaredIdentifiers.begin(), |
| 250 | E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 251 | if (!I->second.getUsed()) |
| 252 | Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 253 | << I->first; |
| 254 | } |
| 255 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 256 | if (!CompleteTranslationUnit) |
| 257 | return; |
| 258 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 259 | // C99 6.9.2p2: |
| 260 | // A declaration of an identifier for an object that has file |
| 261 | // scope without an initializer, and without a storage-class |
| 262 | // specifier or with the storage-class specifier static, |
| 263 | // constitutes a tentative definition. If a translation unit |
| 264 | // contains one or more tentative definitions for an identifier, |
| 265 | // and the translation unit contains no external definition for |
| 266 | // that identifier, then the behavior is exactly as if the |
| 267 | // translation unit contains a file scope declaration of that |
| 268 | // identifier, with the composite type as of the end of the |
| 269 | // translation unit, with an initializer equal to 0. |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 270 | for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator |
| 271 | D = TentativeDefinitions.begin(), |
| 272 | DEnd = TentativeDefinitions.end(); |
| 273 | D != DEnd; ++D) { |
| 274 | VarDecl *VD = D->second; |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 276 | if (VD->isInvalidDecl() || !VD->isTentativeDefinition(Context)) |
| 277 | continue; |
| 278 | |
| 279 | if (const IncompleteArrayType *ArrayT |
| 280 | = Context.getAsIncompleteArrayType(VD->getType())) { |
| 281 | if (RequireCompleteType(VD->getLocation(), |
| 282 | ArrayT->getElementType(), |
| 283 | diag::err_tentative_def_incomplete_type_arr)) |
| 284 | VD->setInvalidDecl(); |
| 285 | else { |
| 286 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 287 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 288 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), |
| 289 | true); |
| 290 | QualType T |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 291 | = Context.getConstantArrayWithoutExprType(ArrayT->getElementType(), |
| 292 | One, ArrayType::Normal, 0); |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 293 | VD->setType(T); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 294 | } |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 295 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
| 296 | diag::err_tentative_def_incomplete_type)) |
| 297 | VD->setInvalidDecl(); |
| 298 | |
| 299 | // Notify the consumer that we've completed a tentative definition. |
| 300 | if (!VD->isInvalidDecl()) |
| 301 | Consumer.CompleteTentativeDefinition(VD); |
| 302 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 303 | } |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | //===----------------------------------------------------------------------===// |
| 308 | // Helper functions. |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 311 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 312 | /// to the function decl for the function being parsed. If we're currently |
| 313 | /// in a 'block', this returns the containing context. |
| 314 | FunctionDecl *Sema::getCurFunctionDecl() { |
| 315 | DeclContext *DC = CurContext; |
| 316 | while (isa<BlockDecl>(DC)) |
| 317 | DC = DC->getParent(); |
| 318 | return dyn_cast<FunctionDecl>(DC); |
| 319 | } |
| 320 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 321 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Steve Naroff | d7612e1 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 322 | DeclContext *DC = CurContext; |
| 323 | while (isa<BlockDecl>(DC)) |
| 324 | DC = DC->getParent(); |
| 325 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 327 | |
| 328 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
| 329 | DeclContext *DC = CurContext; |
| 330 | while (isa<BlockDecl>(DC)) |
| 331 | DC = DC->getParent(); |
| 332 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 333 | return cast<NamedDecl>(DC); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 337 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 338 | if (!this->Emit()) |
| 339 | return; |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 340 | |
| 341 | // If this is not a note, and we're in a template instantiation |
| 342 | // that is different from the last template instantiation where |
| 343 | // we emitted an error, print a template instantiation |
| 344 | // backtrace. |
| 345 | if (!SemaRef.Diags.isBuiltinNote(DiagID) && |
| 346 | !SemaRef.ActiveTemplateInstantiations.empty() && |
| 347 | SemaRef.ActiveTemplateInstantiations.back() |
| 348 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 349 | SemaRef.PrintInstantiationStack(); |
| 350 | SemaRef.LastTemplateInstantiationErrorContext |
| 351 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 352 | } |
| 353 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 354 | |
| 355 | void Sema::ActOnComment(SourceRange Comment) { |
| 356 | Context.Comments.push_back(Comment); |
| 357 | } |