blob: 6e9362f28c2968197766a9eac1e5e7e5be4e555b [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
John McCall49a832b2009-10-18 09:09:24 +000026/// Determines whether we should have an a.k.a. clause when
27/// pretty-printing a type. There are two main criteria:
28///
29/// 1) Some types provide very minimal sugar that doesn't impede the
30/// user's understanding --- for example, elaborated type
31/// specifiers. If this is all the sugar we see, we don't want an
32/// a.k.a. clause.
33/// 2) Some types are technically sugared but are much more familiar
34/// when seen in their sugared form --- for example, va_list,
35/// vector types, and the magic Objective C types. We don't
36/// want to desugar these, even if we do produce an a.k.a. clause.
37static bool ShouldAKA(ASTContext &Context, QualType QT,
38 QualType& DesugaredQT) {
39
40 bool AKA = false;
41 QualifierCollector Qc;
42
43 while (true) {
44 const Type *Ty = Qc.strip(QT);
45
46 // Don't aka just because we saw an elaborated type...
47 if (isa<ElaboratedType>(Ty)) {
48 QT = cast<ElaboratedType>(Ty)->desugar();
49 continue;
50 }
51
52 // ...or a qualified name type...
53 if (isa<QualifiedNameType>(Ty)) {
54 QT = cast<QualifiedNameType>(Ty)->desugar();
55 continue;
56 }
57
58 // ...or a substituted template type parameter.
59 if (isa<SubstTemplateTypeParmType>(Ty)) {
60 QT = cast<SubstTemplateTypeParmType>(Ty)->desugar();
61 continue;
62 }
63
64 // Don't desugar template specializations.
65 if (isa<TemplateSpecializationType>(Ty))
66 break;
67
68 // Don't desugar magic Objective-C types.
69 if (QualType(Ty,0) == Context.getObjCIdType() ||
70 QualType(Ty,0) == Context.getObjCClassType() ||
71 QualType(Ty,0) == Context.getObjCSelType() ||
72 QualType(Ty,0) == Context.getObjCProtoType())
73 break;
74
75 // Don't desugar va_list.
76 if (QualType(Ty,0) == Context.getBuiltinVaListType())
77 break;
78
79 // Otherwise, do a single-step desugar.
80 QualType Underlying;
81 bool IsSugar = false;
82 switch (Ty->getTypeClass()) {
83#define ABSTRACT_TYPE(Class, Base)
84#define TYPE(Class, Base) \
85 case Type::Class: { \
86 const Class##Type *CTy = cast<Class##Type>(Ty); \
87 if (CTy->isSugared()) { \
88 IsSugar = true; \
89 Underlying = CTy->desugar(); \
90 } \
91 break; \
92 }
93#include "clang/AST/TypeNodes.def"
94 }
95
96 // If it wasn't sugared, we're done.
97 if (!IsSugar)
98 break;
99
100 // If the desugared type is a vector type, we don't want to expand
101 // it, it will turn into an attribute mess. People want their "vec4".
102 if (isa<VectorType>(Underlying))
103 break;
104
105 // Otherwise, we're tearing through something opaque; note that
106 // we'll eventually need an a.k.a. clause and keep going.
107 AKA = true;
108 QT = Underlying;
109 continue;
110 }
111
112 // If we ever tore through opaque sugar
113 if (AKA) {
114 DesugaredQT = Qc.apply(QT);
115 return true;
116 }
117
118 return false;
119}
120
Douglas Gregor3f093272009-10-13 21:16:44 +0000121/// \brief Convert the given type to a string suitable for printing as part of
122/// a diagnostic.
123///
124/// \param Context the context in which the type was allocated
125/// \param Ty the type to print
126static std::string ConvertTypeToDiagnosticString(ASTContext &Context,
127 QualType Ty) {
128 // FIXME: Playing with std::string is really slow.
129 std::string S = Ty.getAsString(Context.PrintingPolicy);
130
John McCall49a832b2009-10-18 09:09:24 +0000131 // Consider producing an a.k.a. clause if removing all the direct
132 // sugar gives us something "significantly different".
133
134 QualType DesugaredTy;
135 if (ShouldAKA(Context, Ty, DesugaredTy)) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000136 S = "'"+S+"' (aka '";
137 S += DesugaredTy.getAsString(Context.PrintingPolicy);
138 S += "')";
139 return S;
140 }
141
142 S = "'" + S + "'";
143 return S;
144}
145
Mike Stump1eb44332009-09-09 15:08:12 +0000146/// ConvertQualTypeToStringFn - This function is used to pretty print the
Chris Lattner22caddc2008-11-23 09:13:29 +0000147/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +0000148static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattnerd0344a42009-02-19 23:45:49 +0000149 const char *Modifier, unsigned ModLen,
150 const char *Argument, unsigned ArgLen,
Chris Lattner92dd3862009-02-19 23:53:20 +0000151 llvm::SmallVectorImpl<char> &Output,
152 void *Cookie) {
153 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Chris Lattner011bb4e2008-11-23 20:28:15 +0000155 std::string S;
Douglas Gregor3f093272009-10-13 21:16:44 +0000156 bool NeedQuotes = true;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000157
158 switch (Kind) {
159 default: assert(0 && "unknown ArgumentKind");
160 case Diagnostic::ak_qualtype: {
Chris Lattnerd0344a42009-02-19 23:45:49 +0000161 assert(ModLen == 0 && ArgLen == 0 &&
162 "Invalid modifier for QualType argument");
163
Chris Lattner011bb4e2008-11-23 20:28:15 +0000164 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Douglas Gregor3f093272009-10-13 21:16:44 +0000165 S = ConvertTypeToDiagnosticString(Context, Ty);
166 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000167 break;
168 }
169 case Diagnostic::ak_declarationname: {
Chris Lattner011bb4e2008-11-23 20:28:15 +0000170 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
171 S = N.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattner077bf5e2008-11-24 03:33:13 +0000173 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
174 S = '+' + S;
175 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
176 S = '-' + S;
177 else
178 assert(ModLen == 0 && ArgLen == 0 &&
179 "Invalid modifier for DeclarationName argument");
Chris Lattner9cf9f862009-10-20 05:12:36 +0000180 break;
181 }
182 case Diagnostic::ak_nameddecl: {
John McCall136a6982009-09-11 06:45:03 +0000183 bool Qualified;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000184 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
John McCall136a6982009-09-11 06:45:03 +0000185 Qualified = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000186 else {
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000187 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000188 "Invalid modifier for NamedDecl* argument");
John McCall136a6982009-09-11 06:45:03 +0000189 Qualified = false;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000190 }
Chris Lattner9cf9f862009-10-20 05:12:36 +0000191 reinterpret_cast<NamedDecl*>(Val)->
192 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
193 break;
194 }
195 case Diagnostic::ak_nestednamespec: {
Douglas Gregordacd4342009-08-26 00:04:55 +0000196 llvm::raw_string_ostream OS(S);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000197 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
198 Context.PrintingPolicy);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000199 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000200 break;
201 }
202 case Diagnostic::ak_declcontext: {
Douglas Gregor3f093272009-10-13 21:16:44 +0000203 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000204 assert(DC && "Should never have a null declaration context");
205
206 if (DC->isTranslationUnit()) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000207 // FIXME: Get these strings from some localized place
208 if (Context.getLangOptions().CPlusPlus)
209 S = "the global namespace";
210 else
211 S = "the global scope";
212 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
213 S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type));
Douglas Gregor3f093272009-10-13 21:16:44 +0000214 } else {
215 // FIXME: Get these strings from some localized place
216 NamedDecl *ND = cast<NamedDecl>(DC);
217 if (isa<NamespaceDecl>(ND))
218 S += "namespace ";
219 else if (isa<ObjCMethodDecl>(ND))
220 S += "method ";
221 else if (isa<FunctionDecl>(ND))
222 S += "function ";
223
224 S += "'";
225 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
226 S += "'";
Douglas Gregor3f093272009-10-13 21:16:44 +0000227 }
Chris Lattner9cf9f862009-10-20 05:12:36 +0000228 NeedQuotes = false;
229 break;
230 }
Chris Lattner011bb4e2008-11-23 20:28:15 +0000231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Douglas Gregor3f093272009-10-13 21:16:44 +0000233 if (NeedQuotes)
234 Output.push_back('\'');
235
Chris Lattner22caddc2008-11-23 09:13:29 +0000236 Output.append(S.begin(), S.end());
Douglas Gregor3f093272009-10-13 21:16:44 +0000237
238 if (NeedQuotes)
239 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000240}
241
242
Chris Lattner0a14eee2008-11-18 07:04:44 +0000243static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +0000244 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000245 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +0000246 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +0000247 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000248
Mike Stump1eb44332009-09-09 15:08:12 +0000249 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000250 C.getTranslationUnitDecl(),
251 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +0000252}
253
Steve Naroffb216c882007-10-09 22:01:59 +0000254void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
255 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000256 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner4d150c82009-04-30 06:18:40 +0000258 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
259 // Install [u]int128_t for 64-bit targets.
260 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
261 SourceLocation(),
262 &Context.Idents.get("__int128_t"),
263 Context.Int128Ty), TUScope);
264 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
265 SourceLocation(),
266 &Context.Idents.get("__uint128_t"),
267 Context.UnsignedInt128Ty), TUScope);
268 }
Mike Stump1eb44332009-09-09 15:08:12 +0000269
270
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000271 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Steve Naroffcb83c532009-06-16 00:20:10 +0000273 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +0000274 if (Context.getObjCSelType().isNull()) {
275 // Synthesize "typedef struct objc_selector *SEL;"
276 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
277 PushOnScopeChains(SelTag, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Douglas Gregor319ac892009-04-23 22:29:11 +0000279 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
280 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
281 SourceLocation(),
282 &Context.Idents.get("SEL"),
283 SelT);
284 PushOnScopeChains(SelTypedef, TUScope);
285 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
286 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000287
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000288 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +0000289 if (Context.getObjCProtoType().isNull()) {
290 ObjCInterfaceDecl *ProtocolDecl =
291 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000292 &Context.Idents.get("Protocol"),
Douglas Gregor319ac892009-04-23 22:29:11 +0000293 SourceLocation(), true);
294 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
295 PushOnScopeChains(ProtocolDecl, TUScope);
296 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000297 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +0000298 if (Context.getObjCIdType().isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000299 TypedefDecl *IdTypedef =
300 TypedefDecl::Create(
Steve Naroffde2e22d2009-07-15 18:40:39 +0000301 Context, CurContext, SourceLocation(), &Context.Idents.get("id"),
302 Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy)
303 );
Douglas Gregor319ac892009-04-23 22:29:11 +0000304 PushOnScopeChains(IdTypedef, TUScope);
305 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000306 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000307 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000308 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +0000309 if (Context.getObjCClassType().isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000310 TypedefDecl *ClassTypedef =
311 TypedefDecl::Create(
Steve Naroffde2e22d2009-07-15 18:40:39 +0000312 Context, CurContext, SourceLocation(), &Context.Idents.get("Class"),
313 Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy)
314 );
Steve Naroff14108da2009-07-10 23:34:53 +0000315 PushOnScopeChains(ClassTypedef, TUScope);
316 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000317 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000318 }
Steve Naroff3b950172007-10-10 21:53:07 +0000319}
320
Douglas Gregorf807fe02009-04-14 16:27:31 +0000321Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
322 bool CompleteTranslationUnit)
Chris Lattner53ebff32009-01-22 19:21:44 +0000323 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump1eb44332009-09-09 15:08:12 +0000324 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Douglas Gregor81b747b2009-09-17 21:32:03 +0000325 ExternalSource(0), CodeCompleter(0), CurContext(0),
326 PreDeclaratorDC(0), CurBlock(0), PackContext(0),
327 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregorac7610d2009-06-22 20:57:11 +0000328 GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000329 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorbb260412009-06-14 08:02:22 +0000330 NumSFINAEErrors(0), CurrentInstantiationScope(0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Steve Naroff3b950172007-10-10 21:53:07 +0000332 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000333 if (getLangOptions().CPlusPlus)
334 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattner22caddc2008-11-23 09:13:29 +0000336 // Tell diagnostics how to render things from the AST library.
Chris Lattner92dd3862009-02-19 23:53:20 +0000337 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Reid Spencer5f016e22007-07-11 17:01:13 +0000338}
339
Mike Stump1eb44332009-09-09 15:08:12 +0000340/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000341/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000342/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000343void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonc0a2fd82009-09-15 05:13:45 +0000344 CastExpr::CastKind Kind, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000345 QualType ExprTy = Context.getCanonicalType(Expr->getType());
346 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Mon P Wang3a2c7442008-09-04 08:38:01 +0000348 if (ExprTy == TypeTy)
349 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Mon P Wang3a2c7442008-09-04 08:38:01 +0000351 if (Expr->getType().getTypePtr()->isPointerType() &&
352 Ty.getTypePtr()->isPointerType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000353 QualType ExprBaseType =
Mon P Wang3a2c7442008-09-04 08:38:01 +0000354 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
355 QualType BaseType =
356 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
357 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000358 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
359 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000360 }
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000363 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000364 if (ImpCast->getCastKind() == Kind) {
365 ImpCast->setType(Ty);
366 ImpCast->setLvalueCast(isLvalue);
367 return;
368 }
369 }
370
371 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000372}
373
Chris Lattner394a3fd2007-08-31 04:53:24 +0000374void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000375 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000376}
377void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000378 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000379}
380
Chris Lattner9299f3f2008-08-23 03:19:52 +0000381/// ActOnEndOfTranslationUnit - This is called at the very end of the
382/// translation unit when EOF is reached and all but the top-level scope is
383/// popped.
384void Sema::ActOnEndOfTranslationUnit() {
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000385 // C++: Perform implicit template instantiations.
386 //
387 // FIXME: When we perform these implicit instantiations, we do not carefully
388 // keep track of the point of instantiation (C++ [temp.point]). This means
389 // that name lookup that occurs within the template instantiation will
390 // always happen at the end of the translation unit, so it will find
Mike Stump1eb44332009-09-09 15:08:12 +0000391 // some names that should not be found. Although this is common behavior
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000392 // for C++ compilers, it is technically wrong. In the future, we either need
393 // to be able to filter the results of name lookup or we need to perform
394 // template instantiations earlier.
395 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Chris Lattner63d65f82009-09-08 18:19:27 +0000397 // Check for #pragma weak identifiers that were never declared
398 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
399 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000400 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000401 I = WeakUndeclaredIdentifiers.begin(),
402 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
403 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner63d65f82009-09-08 18:19:27 +0000405 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
406 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000407 }
408
Douglas Gregorf807fe02009-04-14 16:27:31 +0000409 if (!CompleteTranslationUnit)
410 return;
411
Douglas Gregor275a3692009-03-10 23:43:53 +0000412 // C99 6.9.2p2:
413 // A declaration of an identifier for an object that has file
414 // scope without an initializer, and without a storage-class
415 // specifier or with the storage-class specifier static,
416 // constitutes a tentative definition. If a translation unit
417 // contains one or more tentative definitions for an identifier,
418 // and the translation unit contains no external definition for
419 // that identifier, then the behavior is exactly as if the
420 // translation unit contains a file scope declaration of that
421 // identifier, with the composite type as of the end of the
422 // translation unit, with an initializer equal to 0.
Chris Lattner63d65f82009-09-08 18:19:27 +0000423 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
424 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner63d65f82009-09-08 18:19:27 +0000426 // If the tentative definition was completed, it will be in the list, but
427 // not the map.
428 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000429 continue;
430
Mike Stump1eb44332009-09-09 15:08:12 +0000431 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000432 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000433 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000434 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000435 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000436 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000437 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattner63d65f82009-09-08 18:19:27 +0000440 // Set the length of the array to 1 (C99 6.9.2p5).
441 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
442 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCall46a617a2009-10-16 00:14:28 +0000443 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
444 One, ArrayType::Normal, 0);
Chris Lattner63d65f82009-09-08 18:19:27 +0000445 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000446 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000447 diag::err_tentative_def_incomplete_type))
448 VD->setInvalidDecl();
449
450 // Notify the consumer that we've completed a tentative definition.
451 if (!VD->isInvalidDecl())
452 Consumer.CompleteTentativeDefinition(VD);
453
Douglas Gregor275a3692009-03-10 23:43:53 +0000454 }
Chris Lattner9299f3f2008-08-23 03:19:52 +0000455}
456
457
Reid Spencer5f016e22007-07-11 17:01:13 +0000458//===----------------------------------------------------------------------===//
459// Helper functions.
460//===----------------------------------------------------------------------===//
461
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000462DeclContext *Sema::getFunctionLevelDeclContext() {
Anders Carlssonfb7ef752009-08-08 17:48:49 +0000463 DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000465 while (isa<BlockDecl>(DC))
466 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000468 return DC;
469}
470
Chris Lattner371f2582008-12-04 23:50:19 +0000471/// getCurFunctionDecl - If inside of a function body, this returns a pointer
472/// to the function decl for the function being parsed. If we're currently
473/// in a 'block', this returns the containing context.
474FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000475 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000476 return dyn_cast<FunctionDecl>(DC);
477}
478
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000479ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000480 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000481 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000482}
Chris Lattner371f2582008-12-04 23:50:19 +0000483
484NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000485 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000486 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000487 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000488 return 0;
489}
490
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000491Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000492 if (!this->Emit())
493 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000495 // If this is not a note, and we're in a template instantiation
496 // that is different from the last template instantiation where
497 // we emitted an error, print a template instantiation
498 // backtrace.
499 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
500 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000501 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000502 != SemaRef.LastTemplateInstantiationErrorContext) {
503 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000504 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000505 = SemaRef.ActiveTemplateInstantiations.back();
506 }
507}
Douglas Gregor2e222532009-07-02 17:08:52 +0000508
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000509Sema::SemaDiagnosticBuilder
510Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
511 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
512 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000514 return Builder;
515}
516
Douglas Gregor2e222532009-07-02 17:08:52 +0000517void Sema::ActOnComment(SourceRange Comment) {
518 Context.Comments.push_back(Comment);
519}
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000520