blob: c289abfea869c9e59e29c567ae744b32e74cac2a [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 Gregor3f093272009-10-13 21:16:44 +0000120 } else {
121 assert(Kind == Diagnostic::ak_declcontext);
122 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
123 NeedQuotes = false;
124 if (!DC) {
125 assert(false && "Should never have a null declaration context");
126 S = "unknown context";
127 } else if (DC->isTranslationUnit()) {
128 // FIXME: Get these strings from some localized place
129 if (Context.getLangOptions().CPlusPlus)
130 S = "the global namespace";
131 else
132 S = "the global scope";
133 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
134 S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type));
135 NeedQuotes = false;
136 } else {
137 // FIXME: Get these strings from some localized place
138 NamedDecl *ND = cast<NamedDecl>(DC);
139 if (isa<NamespaceDecl>(ND))
140 S += "namespace ";
141 else if (isa<ObjCMethodDecl>(ND))
142 S += "method ";
143 else if (isa<FunctionDecl>(ND))
144 S += "function ";
145
146 S += "'";
147 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
148 S += "'";
149 NeedQuotes = false;
150 }
Chris Lattner011bb4e2008-11-23 20:28:15 +0000151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Douglas Gregor3f093272009-10-13 21:16:44 +0000153 if (NeedQuotes)
154 Output.push_back('\'');
155
Chris Lattner22caddc2008-11-23 09:13:29 +0000156 Output.append(S.begin(), S.end());
Douglas Gregor3f093272009-10-13 21:16:44 +0000157
158 if (NeedQuotes)
159 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000160}
161
162
Chris Lattner0a14eee2008-11-18 07:04:44 +0000163static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +0000164 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000165 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +0000166 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +0000167 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000168
Mike Stump1eb44332009-09-09 15:08:12 +0000169 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000170 C.getTranslationUnitDecl(),
171 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +0000172}
173
Steve Naroffb216c882007-10-09 22:01:59 +0000174void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
175 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000176 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Chris Lattner4d150c82009-04-30 06:18:40 +0000178 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
179 // Install [u]int128_t for 64-bit targets.
180 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
181 SourceLocation(),
182 &Context.Idents.get("__int128_t"),
183 Context.Int128Ty), TUScope);
184 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
185 SourceLocation(),
186 &Context.Idents.get("__uint128_t"),
187 Context.UnsignedInt128Ty), TUScope);
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
190
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000191 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Steve Naroffcb83c532009-06-16 00:20:10 +0000193 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +0000194 if (Context.getObjCSelType().isNull()) {
195 // Synthesize "typedef struct objc_selector *SEL;"
196 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
197 PushOnScopeChains(SelTag, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Douglas Gregor319ac892009-04-23 22:29:11 +0000199 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
200 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
201 SourceLocation(),
202 &Context.Idents.get("SEL"),
203 SelT);
204 PushOnScopeChains(SelTypedef, TUScope);
205 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
206 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000207
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000208 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +0000209 if (Context.getObjCProtoType().isNull()) {
210 ObjCInterfaceDecl *ProtocolDecl =
211 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000212 &Context.Idents.get("Protocol"),
Douglas Gregor319ac892009-04-23 22:29:11 +0000213 SourceLocation(), true);
214 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
215 PushOnScopeChains(ProtocolDecl, TUScope);
216 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000217 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +0000218 if (Context.getObjCIdType().isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000219 TypedefDecl *IdTypedef =
220 TypedefDecl::Create(
Steve Naroffde2e22d2009-07-15 18:40:39 +0000221 Context, CurContext, SourceLocation(), &Context.Idents.get("id"),
222 Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy)
223 );
Douglas Gregor319ac892009-04-23 22:29:11 +0000224 PushOnScopeChains(IdTypedef, TUScope);
225 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000226 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000227 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000228 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +0000229 if (Context.getObjCClassType().isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000230 TypedefDecl *ClassTypedef =
231 TypedefDecl::Create(
Steve Naroffde2e22d2009-07-15 18:40:39 +0000232 Context, CurContext, SourceLocation(), &Context.Idents.get("Class"),
233 Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy)
234 );
Steve Naroff14108da2009-07-10 23:34:53 +0000235 PushOnScopeChains(ClassTypedef, TUScope);
236 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000237 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000238 }
Steve Naroff3b950172007-10-10 21:53:07 +0000239}
240
Douglas Gregorf807fe02009-04-14 16:27:31 +0000241Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
242 bool CompleteTranslationUnit)
Chris Lattner53ebff32009-01-22 19:21:44 +0000243 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump1eb44332009-09-09 15:08:12 +0000244 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Douglas Gregor81b747b2009-09-17 21:32:03 +0000245 ExternalSource(0), CodeCompleter(0), CurContext(0),
246 PreDeclaratorDC(0), CurBlock(0), PackContext(0),
247 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregorac7610d2009-06-22 20:57:11 +0000248 GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000249 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorbb260412009-06-14 08:02:22 +0000250 NumSFINAEErrors(0), CurrentInstantiationScope(0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Steve Naroff3b950172007-10-10 21:53:07 +0000252 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000253 if (getLangOptions().CPlusPlus)
254 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Chris Lattner22caddc2008-11-23 09:13:29 +0000256 // Tell diagnostics how to render things from the AST library.
Chris Lattner92dd3862009-02-19 23:53:20 +0000257 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
Mike Stump1eb44332009-09-09 15:08:12 +0000260/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000261/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000262/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000263void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonc0a2fd82009-09-15 05:13:45 +0000264 CastExpr::CastKind Kind, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000265 QualType ExprTy = Context.getCanonicalType(Expr->getType());
266 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Mon P Wang3a2c7442008-09-04 08:38:01 +0000268 if (ExprTy == TypeTy)
269 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Mon P Wang3a2c7442008-09-04 08:38:01 +0000271 if (Expr->getType().getTypePtr()->isPointerType() &&
272 Ty.getTypePtr()->isPointerType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000273 QualType ExprBaseType =
Mon P Wang3a2c7442008-09-04 08:38:01 +0000274 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
275 QualType BaseType =
276 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
277 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000278 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
279 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000280 }
281 }
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000283 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000284 if (ImpCast->getCastKind() == Kind) {
285 ImpCast->setType(Ty);
286 ImpCast->setLvalueCast(isLvalue);
287 return;
288 }
289 }
290
291 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000292}
293
Chris Lattner394a3fd2007-08-31 04:53:24 +0000294void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000295 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000296}
297void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000298 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000299}
300
Chris Lattner9299f3f2008-08-23 03:19:52 +0000301/// ActOnEndOfTranslationUnit - This is called at the very end of the
302/// translation unit when EOF is reached and all but the top-level scope is
303/// popped.
304void Sema::ActOnEndOfTranslationUnit() {
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000305 // C++: Perform implicit template instantiations.
306 //
307 // FIXME: When we perform these implicit instantiations, we do not carefully
308 // keep track of the point of instantiation (C++ [temp.point]). This means
309 // that name lookup that occurs within the template instantiation will
310 // always happen at the end of the translation unit, so it will find
Mike Stump1eb44332009-09-09 15:08:12 +0000311 // some names that should not be found. Although this is common behavior
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000312 // for C++ compilers, it is technically wrong. In the future, we either need
313 // to be able to filter the results of name lookup or we need to perform
314 // template instantiations earlier.
315 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Chris Lattner63d65f82009-09-08 18:19:27 +0000317 // Check for #pragma weak identifiers that were never declared
318 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
319 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000320 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000321 I = WeakUndeclaredIdentifiers.begin(),
322 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
323 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Chris Lattner63d65f82009-09-08 18:19:27 +0000325 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
326 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000327 }
328
Douglas Gregorf807fe02009-04-14 16:27:31 +0000329 if (!CompleteTranslationUnit)
330 return;
331
Douglas Gregor275a3692009-03-10 23:43:53 +0000332 // C99 6.9.2p2:
333 // A declaration of an identifier for an object that has file
334 // scope without an initializer, and without a storage-class
335 // specifier or with the storage-class specifier static,
336 // constitutes a tentative definition. If a translation unit
337 // contains one or more tentative definitions for an identifier,
338 // and the translation unit contains no external definition for
339 // that identifier, then the behavior is exactly as if the
340 // translation unit contains a file scope declaration of that
341 // identifier, with the composite type as of the end of the
342 // translation unit, with an initializer equal to 0.
Chris Lattner63d65f82009-09-08 18:19:27 +0000343 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
344 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattner63d65f82009-09-08 18:19:27 +0000346 // If the tentative definition was completed, it will be in the list, but
347 // not the map.
348 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000349 continue;
350
Mike Stump1eb44332009-09-09 15:08:12 +0000351 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000352 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000353 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000354 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000355 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000356 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000357 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattner63d65f82009-09-08 18:19:27 +0000360 // Set the length of the array to 1 (C99 6.9.2p5).
361 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
362 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
Mike Stump1eb44332009-09-09 15:08:12 +0000363 QualType T
Chris Lattner63d65f82009-09-08 18:19:27 +0000364 = Context.getConstantArrayWithoutExprType(ArrayT->getElementType(),
365 One, ArrayType::Normal, 0);
366 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000367 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000368 diag::err_tentative_def_incomplete_type))
369 VD->setInvalidDecl();
370
371 // Notify the consumer that we've completed a tentative definition.
372 if (!VD->isInvalidDecl())
373 Consumer.CompleteTentativeDefinition(VD);
374
Douglas Gregor275a3692009-03-10 23:43:53 +0000375 }
Chris Lattner9299f3f2008-08-23 03:19:52 +0000376}
377
378
Reid Spencer5f016e22007-07-11 17:01:13 +0000379//===----------------------------------------------------------------------===//
380// Helper functions.
381//===----------------------------------------------------------------------===//
382
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000383DeclContext *Sema::getFunctionLevelDeclContext() {
Anders Carlssonfb7ef752009-08-08 17:48:49 +0000384 DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000386 while (isa<BlockDecl>(DC))
387 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000389 return DC;
390}
391
Chris Lattner371f2582008-12-04 23:50:19 +0000392/// getCurFunctionDecl - If inside of a function body, this returns a pointer
393/// to the function decl for the function being parsed. If we're currently
394/// in a 'block', this returns the containing context.
395FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000396 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000397 return dyn_cast<FunctionDecl>(DC);
398}
399
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000400ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000401 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000402 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000403}
Chris Lattner371f2582008-12-04 23:50:19 +0000404
405NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000406 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000407 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000408 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000409 return 0;
410}
411
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000412Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000413 if (!this->Emit())
414 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000416 // If this is not a note, and we're in a template instantiation
417 // that is different from the last template instantiation where
418 // we emitted an error, print a template instantiation
419 // backtrace.
420 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
421 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000422 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000423 != SemaRef.LastTemplateInstantiationErrorContext) {
424 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000425 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000426 = SemaRef.ActiveTemplateInstantiations.back();
427 }
428}
Douglas Gregor2e222532009-07-02 17:08:52 +0000429
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000430Sema::SemaDiagnosticBuilder
431Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
432 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
433 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000435 return Builder;
436}
437
Douglas Gregor2e222532009-07-02 17:08:52 +0000438void Sema::ActOnComment(SourceRange Comment) {
439 Context.Comments.push_back(Comment);
440}
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000441