blob: ba05a07f26540068a876881fa6bad5e3e8b091c8 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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 Flynne25ff832009-07-30 03:15:39 +000016#include "llvm/ADT/DenseMap.h"
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +000017#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Lex/Preprocessor.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000022#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner4d150c82009-04-30 06:18:40 +000023#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Douglas Gregor3f093272009-10-13 21:16:44 +000026/// \brief Convert the given type to a string suitable for printing as part of
27/// a diagnostic.
28///
29/// \param Context the context in which the type was allocated
30/// \param Ty the type to print
31static std::string ConvertTypeToDiagnosticString(ASTContext &Context,
32 QualType Ty) {
33 // FIXME: Playing with std::string is really slow.
34 std::string S = Ty.getAsString(Context.PrintingPolicy);
35
36 // If this is a sugared type (like a typedef, typeof, etc), then unwrap one
37 // level of the sugar so that the type is more obvious to the user.
38 QualType DesugaredTy = Ty.getDesugaredType();
39
40 if (Ty != DesugaredTy &&
41 // If the desugared type is a vector type, we don't want to expand it,
42 // it will turn into an attribute mess. People want their "vec4".
43 !isa<VectorType>(DesugaredTy) &&
44
45 // Don't aka just because we saw an elaborated type...
46 (!isa<ElaboratedType>(Ty) ||
47 cast<ElaboratedType>(Ty)->desugar() != DesugaredTy) &&
48
49 // ...or a qualified name type...
50 (!isa<QualifiedNameType>(Ty) ||
51 cast<QualifiedNameType>(Ty)->desugar() != DesugaredTy) &&
52
53 // ...or a non-dependent template specialization.
54 (!isa<TemplateSpecializationType>(Ty) || Ty->isDependentType()) &&
55
56 // Don't desugar magic Objective-C types.
57 Ty.getUnqualifiedType() != Context.getObjCIdType() &&
58 Ty.getUnqualifiedType() != Context.getObjCClassType() &&
59 Ty.getUnqualifiedType() != Context.getObjCSelType() &&
60 Ty.getUnqualifiedType() != Context.getObjCProtoType() &&
61
62 // Not va_list.
63 Ty.getUnqualifiedType() != Context.getBuiltinVaListType()) {
64 S = "'"+S+"' (aka '";
65 S += DesugaredTy.getAsString(Context.PrintingPolicy);
66 S += "')";
67 return S;
68 }
69
70 S = "'" + S + "'";
71 return S;
72}
73
Mike Stump1eb44332009-09-09 15:08:12 +000074/// ConvertQualTypeToStringFn - This function is used to pretty print the
Chris Lattner22caddc2008-11-23 09:13:29 +000075/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +000076static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattnerd0344a42009-02-19 23:45:49 +000077 const char *Modifier, unsigned ModLen,
78 const char *Argument, unsigned ArgLen,
Chris Lattner92dd3862009-02-19 23:53:20 +000079 llvm::SmallVectorImpl<char> &Output,
80 void *Cookie) {
81 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattner011bb4e2008-11-23 20:28:15 +000083 std::string S;
Douglas Gregor3f093272009-10-13 21:16:44 +000084 bool NeedQuotes = true;
Chris Lattner011bb4e2008-11-23 20:28:15 +000085 if (Kind == Diagnostic::ak_qualtype) {
Chris Lattnerd0344a42009-02-19 23:45:49 +000086 assert(ModLen == 0 && ArgLen == 0 &&
87 "Invalid modifier for QualType argument");
88
Chris Lattner011bb4e2008-11-23 20:28:15 +000089 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Douglas Gregor3f093272009-10-13 21:16:44 +000090 S = ConvertTypeToDiagnosticString(Context, Ty);
91 NeedQuotes = false;
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000092 } else if (Kind == Diagnostic::ak_declarationname) {
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner011bb4e2008-11-23 20:28:15 +000094 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
95 S = N.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattner077bf5e2008-11-24 03:33:13 +000097 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
98 S = '+' + S;
99 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
100 S = '-' + S;
101 else
102 assert(ModLen == 0 && ArgLen == 0 &&
103 "Invalid modifier for DeclarationName argument");
Douglas Gregordacd4342009-08-26 00:04:55 +0000104 } else if (Kind == Diagnostic::ak_nameddecl) {
John McCall136a6982009-09-11 06:45:03 +0000105 bool Qualified;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000106 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
John McCall136a6982009-09-11 06:45:03 +0000107 Qualified = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000108 else {
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000109 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000110 "Invalid modifier for NamedDecl* argument");
John McCall136a6982009-09-11 06:45:03 +0000111 Qualified = false;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000112 }
John McCall136a6982009-09-11 06:45:03 +0000113 reinterpret_cast<NamedDecl*>(Val)->getNameForDiagnostic(S,
114 Context.PrintingPolicy,
115 Qualified);
Douglas Gregor3f093272009-10-13 21:16:44 +0000116 } else if (Kind == Diagnostic::ak_nestednamespec) {
Douglas Gregordacd4342009-08-26 00:04:55 +0000117 llvm::raw_string_ostream OS(S);
Mike Stump1eb44332009-09-09 15:08:12 +0000118 reinterpret_cast<NestedNameSpecifier*> (Val)->print(OS,
Douglas Gregordacd4342009-08-26 00:04:55 +0000119 Context.PrintingPolicy);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000120 NeedQuotes = false;
Douglas Gregor3f093272009-10-13 21:16:44 +0000121 } else {
122 assert(Kind == Diagnostic::ak_declcontext);
123 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
124 NeedQuotes = false;
125 if (!DC) {
126 assert(false && "Should never have a null declaration context");
127 S = "unknown context";
128 } else if (DC->isTranslationUnit()) {
129 // FIXME: Get these strings from some localized place
130 if (Context.getLangOptions().CPlusPlus)
131 S = "the global namespace";
132 else
133 S = "the global scope";
134 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
135 S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type));
136 NeedQuotes = false;
137 } else {
138 // FIXME: Get these strings from some localized place
139 NamedDecl *ND = cast<NamedDecl>(DC);
140 if (isa<NamespaceDecl>(ND))
141 S += "namespace ";
142 else if (isa<ObjCMethodDecl>(ND))
143 S += "method ";
144 else if (isa<FunctionDecl>(ND))
145 S += "function ";
146
147 S += "'";
148 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
149 S += "'";
150 NeedQuotes = false;
151 }
Chris Lattner011bb4e2008-11-23 20:28:15 +0000152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Douglas Gregor3f093272009-10-13 21:16:44 +0000154 if (NeedQuotes)
155 Output.push_back('\'');
156
Chris Lattner22caddc2008-11-23 09:13:29 +0000157 Output.append(S.begin(), S.end());
Douglas Gregor3f093272009-10-13 21:16:44 +0000158
159 if (NeedQuotes)
160 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000161}
162
163
Chris Lattner0a14eee2008-11-18 07:04:44 +0000164static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +0000165 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000166 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +0000167 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +0000168 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000169
Mike Stump1eb44332009-09-09 15:08:12 +0000170 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000171 C.getTranslationUnitDecl(),
172 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +0000173}
174
Steve Naroffb216c882007-10-09 22:01:59 +0000175void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
176 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000177 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner4d150c82009-04-30 06:18:40 +0000179 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
180 // Install [u]int128_t for 64-bit targets.
181 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
182 SourceLocation(),
183 &Context.Idents.get("__int128_t"),
184 Context.Int128Ty), TUScope);
185 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
186 SourceLocation(),
187 &Context.Idents.get("__uint128_t"),
188 Context.UnsignedInt128Ty), TUScope);
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
191
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000192 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Steve Naroffcb83c532009-06-16 00:20:10 +0000194 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +0000195 if (Context.getObjCSelType().isNull()) {
196 // Synthesize "typedef struct objc_selector *SEL;"
197 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
198 PushOnScopeChains(SelTag, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Douglas Gregor319ac892009-04-23 22:29:11 +0000200 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
201 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
202 SourceLocation(),
203 &Context.Idents.get("SEL"),
204 SelT);
205 PushOnScopeChains(SelTypedef, TUScope);
206 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
207 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000208
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000209 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +0000210 if (Context.getObjCProtoType().isNull()) {
211 ObjCInterfaceDecl *ProtocolDecl =
212 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000213 &Context.Idents.get("Protocol"),
Douglas Gregor319ac892009-04-23 22:29:11 +0000214 SourceLocation(), true);
215 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
216 PushOnScopeChains(ProtocolDecl, TUScope);
217 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000218 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +0000219 if (Context.getObjCIdType().isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000220 TypedefDecl *IdTypedef =
221 TypedefDecl::Create(
Steve Naroffde2e22d2009-07-15 18:40:39 +0000222 Context, CurContext, SourceLocation(), &Context.Idents.get("id"),
223 Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy)
224 );
Douglas Gregor319ac892009-04-23 22:29:11 +0000225 PushOnScopeChains(IdTypedef, TUScope);
226 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000227 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000228 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000229 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +0000230 if (Context.getObjCClassType().isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000231 TypedefDecl *ClassTypedef =
232 TypedefDecl::Create(
Steve Naroffde2e22d2009-07-15 18:40:39 +0000233 Context, CurContext, SourceLocation(), &Context.Idents.get("Class"),
234 Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy)
235 );
Steve Naroff14108da2009-07-10 23:34:53 +0000236 PushOnScopeChains(ClassTypedef, TUScope);
237 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000238 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000239 }
Steve Naroff3b950172007-10-10 21:53:07 +0000240}
241
Douglas Gregorf807fe02009-04-14 16:27:31 +0000242Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
243 bool CompleteTranslationUnit)
Chris Lattner53ebff32009-01-22 19:21:44 +0000244 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump1eb44332009-09-09 15:08:12 +0000245 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Douglas Gregor81b747b2009-09-17 21:32:03 +0000246 ExternalSource(0), CodeCompleter(0), CurContext(0),
247 PreDeclaratorDC(0), CurBlock(0), PackContext(0),
248 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregorac7610d2009-06-22 20:57:11 +0000249 GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000250 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorbb260412009-06-14 08:02:22 +0000251 NumSFINAEErrors(0), CurrentInstantiationScope(0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Steve Naroff3b950172007-10-10 21:53:07 +0000253 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000254 if (getLangOptions().CPlusPlus)
255 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattner22caddc2008-11-23 09:13:29 +0000257 // Tell diagnostics how to render things from the AST library.
Chris Lattner92dd3862009-02-19 23:53:20 +0000258 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Reid Spencer5f016e22007-07-11 17:01:13 +0000259}
260
Mike Stump1eb44332009-09-09 15:08:12 +0000261/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000262/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000263/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000264void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonc0a2fd82009-09-15 05:13:45 +0000265 CastExpr::CastKind Kind, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000266 QualType ExprTy = Context.getCanonicalType(Expr->getType());
267 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Mon P Wang3a2c7442008-09-04 08:38:01 +0000269 if (ExprTy == TypeTy)
270 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Mon P Wang3a2c7442008-09-04 08:38:01 +0000272 if (Expr->getType().getTypePtr()->isPointerType() &&
273 Ty.getTypePtr()->isPointerType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000274 QualType ExprBaseType =
Mon P Wang3a2c7442008-09-04 08:38:01 +0000275 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
276 QualType BaseType =
277 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
278 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000279 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
280 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000281 }
282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000284 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000285 if (ImpCast->getCastKind() == Kind) {
286 ImpCast->setType(Ty);
287 ImpCast->setLvalueCast(isLvalue);
288 return;
289 }
290 }
291
292 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000293}
294
Chris Lattner394a3fd2007-08-31 04:53:24 +0000295void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000296 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000297}
298void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000299 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000300}
301
Chris Lattner9299f3f2008-08-23 03:19:52 +0000302/// ActOnEndOfTranslationUnit - This is called at the very end of the
303/// translation unit when EOF is reached and all but the top-level scope is
304/// popped.
305void Sema::ActOnEndOfTranslationUnit() {
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000306 // C++: Perform implicit template instantiations.
307 //
308 // FIXME: When we perform these implicit instantiations, we do not carefully
309 // keep track of the point of instantiation (C++ [temp.point]). This means
310 // that name lookup that occurs within the template instantiation will
311 // always happen at the end of the translation unit, so it will find
Mike Stump1eb44332009-09-09 15:08:12 +0000312 // some names that should not be found. Although this is common behavior
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000313 // for C++ compilers, it is technically wrong. In the future, we either need
314 // to be able to filter the results of name lookup or we need to perform
315 // template instantiations earlier.
316 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Chris Lattner63d65f82009-09-08 18:19:27 +0000318 // Check for #pragma weak identifiers that were never declared
319 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
320 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000321 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000322 I = WeakUndeclaredIdentifiers.begin(),
323 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
324 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner63d65f82009-09-08 18:19:27 +0000326 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
327 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000328 }
329
Douglas Gregorf807fe02009-04-14 16:27:31 +0000330 if (!CompleteTranslationUnit)
331 return;
332
Douglas Gregor275a3692009-03-10 23:43:53 +0000333 // C99 6.9.2p2:
334 // A declaration of an identifier for an object that has file
335 // scope without an initializer, and without a storage-class
336 // specifier or with the storage-class specifier static,
337 // constitutes a tentative definition. If a translation unit
338 // contains one or more tentative definitions for an identifier,
339 // and the translation unit contains no external definition for
340 // that identifier, then the behavior is exactly as if the
341 // translation unit contains a file scope declaration of that
342 // identifier, with the composite type as of the end of the
343 // translation unit, with an initializer equal to 0.
Chris Lattner63d65f82009-09-08 18:19:27 +0000344 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
345 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner63d65f82009-09-08 18:19:27 +0000347 // If the tentative definition was completed, it will be in the list, but
348 // not the map.
349 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000350 continue;
351
Mike Stump1eb44332009-09-09 15:08:12 +0000352 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000353 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000354 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000355 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000356 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000357 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000358 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner63d65f82009-09-08 18:19:27 +0000361 // Set the length of the array to 1 (C99 6.9.2p5).
362 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
363 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
Mike Stump1eb44332009-09-09 15:08:12 +0000364 QualType T
Chris Lattner63d65f82009-09-08 18:19:27 +0000365 = Context.getConstantArrayWithoutExprType(ArrayT->getElementType(),
366 One, ArrayType::Normal, 0);
367 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000368 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000369 diag::err_tentative_def_incomplete_type))
370 VD->setInvalidDecl();
371
372 // Notify the consumer that we've completed a tentative definition.
373 if (!VD->isInvalidDecl())
374 Consumer.CompleteTentativeDefinition(VD);
375
Douglas Gregor275a3692009-03-10 23:43:53 +0000376 }
Chris Lattner9299f3f2008-08-23 03:19:52 +0000377}
378
379
Reid Spencer5f016e22007-07-11 17:01:13 +0000380//===----------------------------------------------------------------------===//
381// Helper functions.
382//===----------------------------------------------------------------------===//
383
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000384DeclContext *Sema::getFunctionLevelDeclContext() {
Anders Carlssonfb7ef752009-08-08 17:48:49 +0000385 DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000387 while (isa<BlockDecl>(DC))
388 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000390 return DC;
391}
392
Chris Lattner371f2582008-12-04 23:50:19 +0000393/// getCurFunctionDecl - If inside of a function body, this returns a pointer
394/// to the function decl for the function being parsed. If we're currently
395/// in a 'block', this returns the containing context.
396FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000397 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000398 return dyn_cast<FunctionDecl>(DC);
399}
400
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000401ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000402 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000403 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000404}
Chris Lattner371f2582008-12-04 23:50:19 +0000405
406NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000407 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000408 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000409 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000410 return 0;
411}
412
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000413Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000414 if (!this->Emit())
415 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000417 // If this is not a note, and we're in a template instantiation
418 // that is different from the last template instantiation where
419 // we emitted an error, print a template instantiation
420 // backtrace.
421 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
422 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000423 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000424 != SemaRef.LastTemplateInstantiationErrorContext) {
425 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000426 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000427 = SemaRef.ActiveTemplateInstantiations.back();
428 }
429}
Douglas Gregor2e222532009-07-02 17:08:52 +0000430
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000431Sema::SemaDiagnosticBuilder
432Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
433 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
434 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000436 return Builder;
437}
438
Douglas Gregor2e222532009-07-02 17:08:52 +0000439void Sema::ActOnComment(SourceRange Comment) {
440 Context.Comments.push_back(Comment);
441}
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000442