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" |
| 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 22 | /// ConvertQualTypeToStringFn - This function is used to pretty print the |
| 23 | /// specified QualType as a string in diagnostics. |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 24 | static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val, |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 25 | const char *Modifier, unsigned ModLen, |
| 26 | const char *Argument, unsigned ArgLen, |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 27 | llvm::SmallVectorImpl<char> &Output, |
| 28 | void *Cookie) { |
| 29 | ASTContext &Context = *static_cast<ASTContext*>(Cookie); |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 31 | std::string S; |
| 32 | if (Kind == Diagnostic::ak_qualtype) { |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 33 | assert(ModLen == 0 && ArgLen == 0 && |
| 34 | "Invalid modifier for QualType argument"); |
| 35 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 36 | QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val))); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 38 | // FIXME: Playing with std::string is really slow. |
| 39 | S = Ty.getAsString(); |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 40 | |
| 41 | // If this is a sugared type (like a typedef, typeof, etc), then unwrap one |
| 42 | // 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^] | 43 | QualType DesugaredTy = Ty->getDesugaredType(true); |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 44 | DesugaredTy.setCVRQualifiers(DesugaredTy.getCVRQualifiers() | |
| 45 | Ty.getCVRQualifiers()); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 46 | |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 47 | if (Ty != DesugaredTy && |
| 48 | // If the desugared type is a vector type, we don't want to expand it, |
| 49 | // it will turn into an attribute mess. People want their "vec4". |
| 50 | !isa<VectorType>(DesugaredTy) && |
| 51 | |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 52 | // Don't desugar magic Objective-C types. |
| 53 | Ty.getUnqualifiedType() != Context.getObjCIdType() && |
| 54 | Ty.getUnqualifiedType() != Context.getObjCSelType() && |
| 55 | Ty.getUnqualifiedType() != Context.getObjCProtoType() && |
| 56 | Ty.getUnqualifiedType() != Context.getObjCClassType() && |
| 57 | |
| 58 | // Not va_list. |
| 59 | Ty.getUnqualifiedType() != Context.getBuiltinVaListType()) { |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 60 | S = "'"+S+"' (aka '"; |
| 61 | S += DesugaredTy.getAsString(); |
| 62 | S += "')"; |
| 63 | Output.append(S.begin(), S.end()); |
| 64 | return; |
| 65 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 67 | } else if (Kind == Diagnostic::ak_declarationname) { |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 68 | |
| 69 | DeclarationName N = DeclarationName::getFromOpaqueInteger(Val); |
| 70 | S = N.getAsString(); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 71 | |
| 72 | if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0) |
| 73 | S = '+' + S; |
| 74 | else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0) |
| 75 | S = '-' + S; |
| 76 | else |
| 77 | assert(ModLen == 0 && ArgLen == 0 && |
| 78 | "Invalid modifier for DeclarationName argument"); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 79 | } else { |
| 80 | assert(Kind == Diagnostic::ak_nameddecl); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 81 | if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0) |
| 82 | S = reinterpret_cast<NamedDecl*>(Val)->getQualifiedNameAsString(); |
| 83 | else { |
| 84 | assert(ModLen == 0 && ArgLen == 0 && |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 85 | "Invalid modifier for NamedDecl* argument"); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 86 | S = reinterpret_cast<NamedDecl*>(Val)->getNameAsString(); |
| 87 | } |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 89 | |
| 90 | Output.push_back('\''); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 91 | Output.append(S.begin(), S.end()); |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 92 | Output.push_back('\''); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 96 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) { |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 97 | if (C.getLangOptions().CPlusPlus) |
| 98 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
| 99 | C.getTranslationUnitDecl(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 100 | SourceLocation(), &C.Idents.get(Name)); |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 101 | |
| 102 | return RecordDecl::Create(C, TagDecl::TK_struct, |
| 103 | C.getTranslationUnitDecl(), |
| 104 | SourceLocation(), &C.Idents.get(Name)); |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 107 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 108 | TUScope = S; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 109 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 110 | if (!PP.getLangOptions().ObjC1) return; |
| 111 | |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 112 | // Synthesize "typedef struct objc_selector *SEL;" |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 113 | RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 114 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 115 | |
| 116 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 117 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 118 | SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 119 | &Context.Idents.get("SEL"), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 120 | SelT); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 121 | PushOnScopeChains(SelTypedef, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 122 | Context.setObjCSelType(SelTypedef); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 27933c1 | 2008-06-21 22:44:51 +0000 | [diff] [blame] | 124 | // FIXME: Make sure these don't leak! |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 125 | RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class"); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 126 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 127 | TypedefDecl *ClassTypedef = |
| 128 | TypedefDecl::Create(Context, CurContext, SourceLocation(), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 129 | &Context.Idents.get("Class"), ClassT); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 130 | PushOnScopeChains(ClassTag, TUScope); |
| 131 | PushOnScopeChains(ClassTypedef, TUScope); |
| 132 | Context.setObjCClassType(ClassTypedef); |
| 133 | // Synthesize "@class Protocol; |
| 134 | ObjCInterfaceDecl *ProtocolDecl = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 135 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 136 | &Context.Idents.get("Protocol"), |
| 137 | SourceLocation(), true); |
| 138 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 139 | PushOnScopeChains(ProtocolDecl, TUScope); |
| 140 | |
| 141 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 142 | RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object"); |
| 143 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 144 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 145 | PushOnScopeChains(ObjectTag, TUScope); |
| 146 | TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, |
| 147 | SourceLocation(), |
| 148 | &Context.Idents.get("id"), |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 149 | ObjT); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 150 | PushOnScopeChains(IdTypedef, TUScope); |
| 151 | Context.setObjCIdType(IdTypedef); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 154 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) |
Chris Lattner | 53ebff3 | 2009-01-22 19:21:44 +0000 | [diff] [blame] | 155 | : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
| 156 | Diags(PP.getDiagnostics()), |
Chris Lattner | 3cfa928 | 2008-11-22 08:28:49 +0000 | [diff] [blame] | 157 | SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0), |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 158 | CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()), |
| 159 | GlobalNewDeleteDeclared(false) { |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 160 | |
| 161 | // Get IdentifierInfo objects for known functions for which we |
| 162 | // do extra checking. |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 163 | IdentifierTable &IT = PP.getIdentifierTable(); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 164 | |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 165 | KnownFunctionIDs[id_NSLog] = &IT.get("NSLog"); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 166 | KnownFunctionIDs[id_NSLogv] = &IT.get("NSLogv"); |
Douglas Gregor | a316e7b | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 167 | KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 168 | KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 169 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 170 | StdNamespace = 0; |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 171 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 172 | if (getLangOptions().CPlusPlus) |
| 173 | FieldCollector.reset(new CXXFieldCollector()); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 174 | |
| 175 | // Tell diagnostics how to render things from the AST library. |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 176 | PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 179 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
| 180 | /// If there is already an implicit cast, merge into the existing one. |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 181 | /// If isLvalue, the result of the cast is an lvalue. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 182 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 183 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 184 | QualType TypeTy = Context.getCanonicalType(Ty); |
| 185 | |
| 186 | if (ExprTy == TypeTy) |
| 187 | return; |
| 188 | |
| 189 | if (Expr->getType().getTypePtr()->isPointerType() && |
| 190 | Ty.getTypePtr()->isPointerType()) { |
| 191 | QualType ExprBaseType = |
| 192 | cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType(); |
| 193 | QualType BaseType = |
| 194 | cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType(); |
| 195 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 196 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 197 | << Expr->getSourceRange(); |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 201 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 202 | ImpCast->setType(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 203 | ImpCast->setLvalueCast(isLvalue); |
| 204 | } else |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 205 | Expr = new (Context) ImplicitCastExpr(Ty, Expr, isLvalue); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 208 | void Sema::DeleteExpr(ExprTy *E) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 209 | if (E) static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 210 | } |
| 211 | void Sema::DeleteStmt(StmtTy *S) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 212 | if (S) static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 215 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 216 | /// translation unit when EOF is reached and all but the top-level scope is |
| 217 | /// popped. |
| 218 | void Sema::ActOnEndOfTranslationUnit() { |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 219 | // C99 6.9.2p2: |
| 220 | // A declaration of an identifier for an object that has file |
| 221 | // scope without an initializer, and without a storage-class |
| 222 | // specifier or with the storage-class specifier static, |
| 223 | // constitutes a tentative definition. If a translation unit |
| 224 | // contains one or more tentative definitions for an identifier, |
| 225 | // and the translation unit contains no external definition for |
| 226 | // that identifier, then the behavior is exactly as if the |
| 227 | // translation unit contains a file scope declaration of that |
| 228 | // identifier, with the composite type as of the end of the |
| 229 | // translation unit, with an initializer equal to 0. |
| 230 | if (!getLangOptions().CPlusPlus) { |
| 231 | // Note: we traverse the scope's list of declarations rather than |
| 232 | // the DeclContext's list, because we only want to see the most |
| 233 | // recent declaration of each identifier. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 234 | for (Scope::decl_iterator I = TUScope->decl_begin(), |
| 235 | IEnd = TUScope->decl_end(); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 236 | I != IEnd; ++I) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 237 | Decl *D = (*I).getAs<Decl>(); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 238 | if (D->isInvalidDecl()) |
| 239 | continue; |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 240 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 241 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 242 | if (VD->isTentativeDefinition(Context)) { |
| 243 | if (const IncompleteArrayType *ArrayT |
| 244 | = Context.getAsIncompleteArrayType(VD->getType())) { |
| 245 | if (RequireCompleteType(VD->getLocation(), |
| 246 | ArrayT->getElementType(), |
| 247 | diag::err_tentative_def_incomplete_type_arr)) |
| 248 | VD->setInvalidDecl(); |
| 249 | else { |
| 250 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 251 | llvm::APSInt One(Context.getTypeSize(Context.getSizeType()), |
| 252 | true); |
| 253 | QualType T |
| 254 | = Context.getConstantArrayType(ArrayT->getElementType(), |
| 255 | One, ArrayType::Normal, 0); |
| 256 | VD->setType(T); |
| 257 | } |
| 258 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
| 259 | diag::err_tentative_def_incomplete_type)) |
| 260 | VD->setInvalidDecl(); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
| 269 | // Helper functions. |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 272 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 273 | /// to the function decl for the function being parsed. If we're currently |
| 274 | /// in a 'block', this returns the containing context. |
| 275 | FunctionDecl *Sema::getCurFunctionDecl() { |
| 276 | DeclContext *DC = CurContext; |
| 277 | while (isa<BlockDecl>(DC)) |
| 278 | DC = DC->getParent(); |
| 279 | return dyn_cast<FunctionDecl>(DC); |
| 280 | } |
| 281 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 282 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Steve Naroff | d7612e1 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 283 | DeclContext *DC = CurContext; |
| 284 | while (isa<BlockDecl>(DC)) |
| 285 | DC = DC->getParent(); |
| 286 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 287 | } |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 288 | |
| 289 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
| 290 | DeclContext *DC = CurContext; |
| 291 | while (isa<BlockDecl>(DC)) |
| 292 | DC = DC->getParent(); |
| 293 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 294 | return cast<NamedDecl>(DC); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 298 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
| 299 | this->Emit(); |
| 300 | |
| 301 | // If this is not a note, and we're in a template instantiation |
| 302 | // that is different from the last template instantiation where |
| 303 | // we emitted an error, print a template instantiation |
| 304 | // backtrace. |
| 305 | if (!SemaRef.Diags.isBuiltinNote(DiagID) && |
| 306 | !SemaRef.ActiveTemplateInstantiations.empty() && |
| 307 | SemaRef.ActiveTemplateInstantiations.back() |
| 308 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 309 | SemaRef.PrintInstantiationStack(); |
| 310 | SemaRef.LastTemplateInstantiationErrorContext |
| 311 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 312 | } |
| 313 | } |