Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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" |
Douglas Gregor | 9cdb4a1 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 23 | /// ConvertQualTypeToStringFn - This function is used to pretty print the |
| 24 | /// specified QualType as a string in diagnostics. |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 25 | static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val, |
Chris Lattner | 2bd4a5a | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 26 | const char *Modifier, unsigned ModLen, |
| 27 | const char *Argument, unsigned ArgLen, |
Chris Lattner | 0b53af2 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 28 | llvm::SmallVectorImpl<char> &Output, |
| 29 | void *Cookie) { |
| 30 | ASTContext &Context = *static_cast<ASTContext*>(Cookie); |
Chris Lattner | f5b269a | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 32 | std::string S; |
| 33 | if (Kind == Diagnostic::ak_qualtype) { |
Chris Lattner | 2bd4a5a | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 34 | assert(ModLen == 0 && ArgLen == 0 && |
| 35 | "Invalid modifier for QualType argument"); |
| 36 | |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 37 | QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 39 | // FIXME: Playing with std::string is really slow. |
| 40 | S = Ty.getAsString(); |
Chris Lattner | 2bd4a5a | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 41 | |
| 42 | // If this is a sugared type (like a typedef, typeof, etc), then unwrap one |
| 43 | // level of the sugar so that the type is more obvious to the user. |
Douglas Gregor | da0c4d2 | 2009-04-01 15:47:24 +0000 | [diff] [blame] | 44 | QualType DesugaredTy = Ty->getDesugaredType(true); |
Chris Lattner | 2bd4a5a | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 45 | DesugaredTy.setCVRQualifiers(DesugaredTy.getCVRQualifiers() | |
| 46 | Ty.getCVRQualifiers()); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 2bd4a5a | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 48 | if (Ty != DesugaredTy && |
| 49 | // If the desugared type is a vector type, we don't want to expand it, |
| 50 | // it will turn into an attribute mess. People want their "vec4". |
| 51 | !isa<VectorType>(DesugaredTy) && |
| 52 | |
Chris Lattner | 0b53af2 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 53 | // Don't desugar magic Objective-C types. |
| 54 | Ty.getUnqualifiedType() != Context.getObjCIdType() && |
| 55 | Ty.getUnqualifiedType() != Context.getObjCSelType() && |
| 56 | Ty.getUnqualifiedType() != Context.getObjCProtoType() && |
| 57 | Ty.getUnqualifiedType() != Context.getObjCClassType() && |
| 58 | |
| 59 | // Not va_list. |
| 60 | Ty.getUnqualifiedType() != Context.getBuiltinVaListType()) { |
Chris Lattner | 2bd4a5a | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 61 | S = "'"+S+"' (aka '"; |
| 62 | S += DesugaredTy.getAsString(); |
| 63 | S += "')"; |
| 64 | Output.append(S.begin(), S.end()); |
| 65 | return; |
| 66 | } |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 67 | |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 68 | } else if (Kind == Diagnostic::ak_declarationname) { |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 69 | |
| 70 | DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); |
| 71 | S = N.getAsString(); |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 72 | |
| 73 | if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0) |
| 74 | S = '+' + S; |
| 75 | else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0) |
| 76 | S = '-' + S; |
| 77 | else |
| 78 | assert(ModLen == 0 && ArgLen == 0 && |
| 79 | "Invalid modifier for DeclarationName argument"); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 80 | } else { |
| 81 | assert(Kind == Diagnostic::ak_nameddecl); |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 82 | if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0) |
| 83 | S = reinterpret_cast<NamedDecl*>(Val)->getQualifiedNameAsString(); |
| 84 | else { |
| 85 | assert(ModLen == 0 && ArgLen == 0 && |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 86 | "Invalid modifier for NamedDecl* argument"); |
Douglas Gregor | be69b16 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 87 | S = reinterpret_cast<NamedDecl*>(Val)->getNameAsString(); |
| 88 | } |
Chris Lattner | 254de7d | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 89 | } |
Chris Lattner | 2bd4a5a | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 90 | |
| 91 | Output.push_back('\''); |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 92 | Output.append(S.begin(), S.end()); |
Chris Lattner | 2bd4a5a | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 93 | Output.push_back('\''); |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 97 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 98 | if (C.getLangOptions().CPlusPlus) |
| 99 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
| 100 | C.getTranslationUnitDecl(), |
Ted Kremenek | 2c98404 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 101 | SourceLocation(), &C.Idents.get(Name)); |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 102 | |
| 103 | return RecordDecl::Create(C, TagDecl::TK_struct, |
| 104 | C.getTranslationUnitDecl(), |
| 105 | SourceLocation(), &C.Idents.get(Name)); |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 108 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 109 | TUScope = S; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 110 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Chris Lattner | 6cc7e41 | 2009-04-30 02:43:43 +0000 | [diff] [blame^] | 111 | |
| 112 | // Install [u]int128_t. |
| 113 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 114 | SourceLocation(), |
| 115 | &Context.Idents.get("__int128_t"), |
| 116 | Context.Int128Ty), TUScope); |
| 117 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 118 | SourceLocation(), |
| 119 | &Context.Idents.get("__uint128_t"), |
| 120 | Context.UnsignedInt128Ty), TUScope); |
| 121 | |
| 122 | |
Chris Lattner | a8c2d59 | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 123 | if (!PP.getLangOptions().ObjC1) return; |
| 124 | |
Douglas Gregor | bb21d4b | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 125 | if (Context.getObjCSelType().isNull()) { |
| 126 | // Synthesize "typedef struct objc_selector *SEL;" |
| 127 | RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); |
| 128 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | 8f635e0 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 129 | |
Douglas Gregor | bb21d4b | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 130 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
| 131 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 132 | SourceLocation(), |
| 133 | &Context.Idents.get("SEL"), |
| 134 | SelT); |
| 135 | PushOnScopeChains(SelTypedef, TUScope); |
| 136 | Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); |
| 137 | } |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 138 | |
Douglas Gregor | bb21d4b | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 139 | if (Context.getObjCClassType().isNull()) { |
| 140 | RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class"); |
| 141 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 142 | TypedefDecl *ClassTypedef = |
| 143 | TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 144 | &Context.Idents.get("Class"), ClassT); |
| 145 | PushOnScopeChains(ClassTag, TUScope); |
| 146 | PushOnScopeChains(ClassTypedef, TUScope); |
| 147 | Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); |
| 148 | } |
| 149 | |
Chris Lattner | 4e9553a | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 150 | // Synthesize "@class Protocol; |
Douglas Gregor | bb21d4b | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 151 | if (Context.getObjCProtoType().isNull()) { |
| 152 | ObjCInterfaceDecl *ProtocolDecl = |
| 153 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
| 154 | &Context.Idents.get("Protocol"), |
| 155 | SourceLocation(), true); |
| 156 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 157 | PushOnScopeChains(ProtocolDecl, TUScope); |
| 158 | } |
Anders Carlsson | 8930fb5 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | bb21d4b | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 160 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
| 161 | if (Context.getObjCIdType().isNull()) { |
| 162 | RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object"); |
| 163 | |
| 164 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 165 | PushOnScopeChains(ObjectTag, TUScope); |
| 166 | TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, |
| 167 | SourceLocation(), |
| 168 | &Context.Idents.get("id"), |
| 169 | ObjT); |
| 170 | PushOnScopeChains(IdTypedef, TUScope); |
| 171 | Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); |
| 172 | } |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Douglas Gregor | e0d5c56 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 175 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
| 176 | bool CompleteTranslationUnit) |
Chris Lattner | 6d89a8a | 2009-01-22 19:21:44 +0000 | [diff] [blame] | 177 | : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Douglas Gregor | c3221aa | 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 | b5ee874 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 180 | CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()), |
Douglas Gregor | e0d5c56 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 181 | GlobalNewDeleteDeclared(false), |
| 182 | CompleteTranslationUnit(CompleteTranslationUnit) { |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 183 | |
Sebastian Redl | b93b49c | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 184 | StdNamespace = 0; |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 185 | TUScope = 0; |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 186 | if (getLangOptions().CPlusPlus) |
| 187 | FieldCollector.reset(new CXXFieldCollector()); |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 188 | |
| 189 | // Tell diagnostics how to render things from the AST library. |
Chris Lattner | 0b53af2 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 190 | PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 193 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
| 194 | /// If there is already an implicit cast, merge into the existing one. |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 195 | /// If isLvalue, the result of the cast is an lvalue. |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 196 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) { |
Mon P Wang | c6c9274 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 197 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 198 | QualType TypeTy = Context.getCanonicalType(Ty); |
| 199 | |
| 200 | if (ExprTy == TypeTy) |
| 201 | return; |
| 202 | |
| 203 | if (Expr->getType().getTypePtr()->isPointerType() && |
| 204 | Ty.getTypePtr()->isPointerType()) { |
| 205 | QualType ExprBaseType = |
| 206 | cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType(); |
| 207 | QualType BaseType = |
| 208 | cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType(); |
| 209 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 210 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 211 | << Expr->getSourceRange(); |
Mon P Wang | c6c9274 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 214 | |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 215 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Mon P Wang | c6c9274 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 216 | ImpCast->setType(Ty); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 217 | ImpCast->setLvalueCast(isLvalue); |
| 218 | } else |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 219 | Expr = new (Context) ImplicitCastExpr(Ty, Expr, isLvalue); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 222 | void Sema::DeleteExpr(ExprTy *E) { |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 223 | if (E) static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 224 | } |
| 225 | void Sema::DeleteStmt(StmtTy *S) { |
Douglas Gregor | c5a6bdc | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 226 | if (S) static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Chris Lattner | c1aea81 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 229 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 230 | /// translation unit when EOF is reached and all but the top-level scope is |
| 231 | /// popped. |
| 232 | void Sema::ActOnEndOfTranslationUnit() { |
Douglas Gregor | e0d5c56 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 233 | if (!CompleteTranslationUnit) |
| 234 | return; |
| 235 | |
Douglas Gregor | 2f728b2 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 236 | // C99 6.9.2p2: |
| 237 | // A declaration of an identifier for an object that has file |
| 238 | // scope without an initializer, and without a storage-class |
| 239 | // specifier or with the storage-class specifier static, |
| 240 | // constitutes a tentative definition. If a translation unit |
| 241 | // contains one or more tentative definitions for an identifier, |
| 242 | // and the translation unit contains no external definition for |
| 243 | // that identifier, then the behavior is exactly as if the |
| 244 | // translation unit contains a file scope declaration of that |
| 245 | // identifier, with the composite type as of the end of the |
| 246 | // translation unit, with an initializer equal to 0. |
Douglas Gregor | 9cdb4a1 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 247 | for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator |
| 248 | D = TentativeDefinitions.begin(), |
| 249 | DEnd = TentativeDefinitions.end(); |
| 250 | D != DEnd; ++D) { |
| 251 | VarDecl *VD = D->second; |
Chris Lattner | c1aea81 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | 9cdb4a1 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 253 | if (VD->isInvalidDecl() || !VD->isTentativeDefinition(Context)) |
| 254 | continue; |
| 255 | |
| 256 | if (const IncompleteArrayType *ArrayT |
| 257 | = Context.getAsIncompleteArrayType(VD->getType())) { |
| 258 | if (RequireCompleteType(VD->getLocation(), |
| 259 | ArrayT->getElementType(), |
| 260 | diag::err_tentative_def_incomplete_type_arr)) |
| 261 | VD->setInvalidDecl(); |
| 262 | else { |
| 263 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 264 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 265 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), |
| 266 | true); |
| 267 | QualType T |
| 268 | = Context.getConstantArrayType(ArrayT->getElementType(), |
| 269 | One, ArrayType::Normal, 0); |
| 270 | VD->setType(T); |
Douglas Gregor | 2f728b2 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 271 | } |
Douglas Gregor | 9cdb4a1 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 272 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
| 273 | diag::err_tentative_def_incomplete_type)) |
| 274 | VD->setInvalidDecl(); |
| 275 | |
| 276 | // Notify the consumer that we've completed a tentative definition. |
| 277 | if (!VD->isInvalidDecl()) |
| 278 | Consumer.CompleteTentativeDefinition(VD); |
| 279 | |
Douglas Gregor | 2f728b2 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 280 | } |
Chris Lattner | c1aea81 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 284 | //===----------------------------------------------------------------------===// |
| 285 | // Helper functions. |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 288 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 289 | /// to the function decl for the function being parsed. If we're currently |
| 290 | /// in a 'block', this returns the containing context. |
| 291 | FunctionDecl *Sema::getCurFunctionDecl() { |
| 292 | DeclContext *DC = CurContext; |
| 293 | while (isa<BlockDecl>(DC)) |
| 294 | DC = DC->getParent(); |
| 295 | return dyn_cast<FunctionDecl>(DC); |
| 296 | } |
| 297 | |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 298 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Steve Naroff | 55debea | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 299 | DeclContext *DC = CurContext; |
| 300 | while (isa<BlockDecl>(DC)) |
| 301 | DC = DC->getParent(); |
| 302 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 303 | } |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 304 | |
| 305 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
| 306 | DeclContext *DC = CurContext; |
| 307 | while (isa<BlockDecl>(DC)) |
| 308 | DC = DC->getParent(); |
| 309 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 310 | return cast<NamedDecl>(DC); |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
Douglas Gregor | 46970ed | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 314 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
| 315 | this->Emit(); |
| 316 | |
| 317 | // If this is not a note, and we're in a template instantiation |
| 318 | // that is different from the last template instantiation where |
| 319 | // we emitted an error, print a template instantiation |
| 320 | // backtrace. |
| 321 | if (!SemaRef.Diags.isBuiltinNote(DiagID) && |
| 322 | !SemaRef.ActiveTemplateInstantiations.empty() && |
| 323 | SemaRef.ActiveTemplateInstantiations.back() |
| 324 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 325 | SemaRef.PrintInstantiationStack(); |
| 326 | SemaRef.LastTemplateInstantiationErrorContext |
| 327 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 328 | } |
| 329 | } |