blob: b32ddfd2093ac71c03872d9264cc6801503849e8 [file] [log] [blame]
Chris Lattnerddd6fc82006-11-10 04:58:55 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerddd6fc82006-11-10 04:58:55 +000010// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerddd6fc82006-11-10 04:58:55 +000015#include "Sema.h"
Ryan Flynn7d470f32009-07-30 03:15:39 +000016#include "llvm/ADT/DenseMap.h"
John McCallfceb64b2009-11-07 03:30:10 +000017#include "llvm/ADT/APFloat.h"
Douglas Gregorbeecd582009-04-21 17:11:58 +000018#include "clang/AST/ASTConsumer.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000021#include "clang/AST/Expr.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000022#include "clang/Lex/Preprocessor.h"
Anders Carlssonf68079e2009-08-26 22:33:56 +000023#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner7d4f5c42009-04-30 06:18:40 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000025using namespace clang;
26
John McCallcebee162009-10-18 09:09:24 +000027/// Determines whether we should have an a.k.a. clause when
Chris Lattnerbd19b182009-10-20 05:36:05 +000028/// pretty-printing a type. There are three main criteria:
John McCallcebee162009-10-18 09:09:24 +000029///
30/// 1) Some types provide very minimal sugar that doesn't impede the
31/// user's understanding --- for example, elaborated type
32/// specifiers. If this is all the sugar we see, we don't want an
33/// a.k.a. clause.
34/// 2) Some types are technically sugared but are much more familiar
35/// when seen in their sugared form --- for example, va_list,
36/// vector types, and the magic Objective C types. We don't
37/// want to desugar these, even if we do produce an a.k.a. clause.
Chris Lattnerbd19b182009-10-20 05:36:05 +000038/// 3) Some types may have already been desugared previously in this diagnostic.
39/// if this is the case, doing another "aka" would just be clutter.
40///
John McCallcebee162009-10-18 09:09:24 +000041static bool ShouldAKA(ASTContext &Context, QualType QT,
Chris Lattnerbd19b182009-10-20 05:36:05 +000042 const Diagnostic::ArgumentValue *PrevArgs,
43 unsigned NumPrevArgs,
44 QualType &DesugaredQT) {
45 QualType InputTy = QT;
46
John McCallcebee162009-10-18 09:09:24 +000047 bool AKA = false;
48 QualifierCollector Qc;
49
50 while (true) {
51 const Type *Ty = Qc.strip(QT);
52
53 // Don't aka just because we saw an elaborated type...
54 if (isa<ElaboratedType>(Ty)) {
55 QT = cast<ElaboratedType>(Ty)->desugar();
56 continue;
57 }
58
59 // ...or a qualified name type...
60 if (isa<QualifiedNameType>(Ty)) {
61 QT = cast<QualifiedNameType>(Ty)->desugar();
62 continue;
63 }
64
65 // ...or a substituted template type parameter.
66 if (isa<SubstTemplateTypeParmType>(Ty)) {
67 QT = cast<SubstTemplateTypeParmType>(Ty)->desugar();
68 continue;
69 }
70
71 // Don't desugar template specializations.
72 if (isa<TemplateSpecializationType>(Ty))
73 break;
74
75 // Don't desugar magic Objective-C types.
76 if (QualType(Ty,0) == Context.getObjCIdType() ||
77 QualType(Ty,0) == Context.getObjCClassType() ||
78 QualType(Ty,0) == Context.getObjCSelType() ||
79 QualType(Ty,0) == Context.getObjCProtoType())
80 break;
81
82 // Don't desugar va_list.
83 if (QualType(Ty,0) == Context.getBuiltinVaListType())
84 break;
85
86 // Otherwise, do a single-step desugar.
87 QualType Underlying;
88 bool IsSugar = false;
89 switch (Ty->getTypeClass()) {
90#define ABSTRACT_TYPE(Class, Base)
91#define TYPE(Class, Base) \
92 case Type::Class: { \
93 const Class##Type *CTy = cast<Class##Type>(Ty); \
94 if (CTy->isSugared()) { \
95 IsSugar = true; \
96 Underlying = CTy->desugar(); \
97 } \
98 break; \
99 }
100#include "clang/AST/TypeNodes.def"
101 }
102
103 // If it wasn't sugared, we're done.
104 if (!IsSugar)
105 break;
106
107 // If the desugared type is a vector type, we don't want to expand
108 // it, it will turn into an attribute mess. People want their "vec4".
109 if (isa<VectorType>(Underlying))
110 break;
111
112 // Otherwise, we're tearing through something opaque; note that
113 // we'll eventually need an a.k.a. clause and keep going.
114 AKA = true;
115 QT = Underlying;
116 continue;
117 }
118
Chris Lattnerbd19b182009-10-20 05:36:05 +0000119 // If we never tore through opaque sugar, don't print aka.
120 if (!AKA) return false;
John McCallcebee162009-10-18 09:09:24 +0000121
Chris Lattnerbd19b182009-10-20 05:36:05 +0000122 // If we did, check to see if we already desugared this type in this
123 // diagnostic. If so, don't do it again.
124 for (unsigned i = 0; i != NumPrevArgs; ++i) {
125 // TODO: Handle ak_declcontext case.
126 if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
127 void *Ptr = (void*)PrevArgs[i].second;
128 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
129 if (PrevTy == InputTy)
130 return false;
131 }
132 }
133
134 DesugaredQT = Qc.apply(QT);
135 return true;
John McCallcebee162009-10-18 09:09:24 +0000136}
137
Douglas Gregore40876a2009-10-13 21:16:44 +0000138/// \brief Convert the given type to a string suitable for printing as part of
139/// a diagnostic.
140///
141/// \param Context the context in which the type was allocated
142/// \param Ty the type to print
Chris Lattnerbd19b182009-10-20 05:36:05 +0000143static std::string
144ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
145 const Diagnostic::ArgumentValue *PrevArgs,
146 unsigned NumPrevArgs) {
Douglas Gregore40876a2009-10-13 21:16:44 +0000147 // FIXME: Playing with std::string is really slow.
148 std::string S = Ty.getAsString(Context.PrintingPolicy);
149
John McCallcebee162009-10-18 09:09:24 +0000150 // Consider producing an a.k.a. clause if removing all the direct
151 // sugar gives us something "significantly different".
152
153 QualType DesugaredTy;
Chris Lattnerbd19b182009-10-20 05:36:05 +0000154 if (ShouldAKA(Context, Ty, PrevArgs, NumPrevArgs, DesugaredTy)) {
Douglas Gregore40876a2009-10-13 21:16:44 +0000155 S = "'"+S+"' (aka '";
156 S += DesugaredTy.getAsString(Context.PrintingPolicy);
157 S += "')";
158 return S;
159 }
160
161 S = "'" + S + "'";
162 return S;
163}
164
Mike Stump11289f42009-09-09 15:08:12 +0000165/// ConvertQualTypeToStringFn - This function is used to pretty print the
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000166/// specified QualType as a string in diagnostics.
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000167static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattner810d3302009-02-19 23:45:49 +0000168 const char *Modifier, unsigned ModLen,
169 const char *Argument, unsigned ArgLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000170 const Diagnostic::ArgumentValue *PrevArgs,
171 unsigned NumPrevArgs,
Chris Lattnercf868c42009-02-19 23:53:20 +0000172 llvm::SmallVectorImpl<char> &Output,
173 void *Cookie) {
174 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Mike Stump11289f42009-09-09 15:08:12 +0000175
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000176 std::string S;
Douglas Gregore40876a2009-10-13 21:16:44 +0000177 bool NeedQuotes = true;
Chris Lattnerdac91472009-10-20 05:12:36 +0000178
179 switch (Kind) {
180 default: assert(0 && "unknown ArgumentKind");
181 case Diagnostic::ak_qualtype: {
Chris Lattner810d3302009-02-19 23:45:49 +0000182 assert(ModLen == 0 && ArgLen == 0 &&
183 "Invalid modifier for QualType argument");
184
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000185 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattnerbd19b182009-10-20 05:36:05 +0000186 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
Douglas Gregore40876a2009-10-13 21:16:44 +0000187 NeedQuotes = false;
Chris Lattnerdac91472009-10-20 05:12:36 +0000188 break;
189 }
190 case Diagnostic::ak_declarationname: {
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000191 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
192 S = N.getAsString();
Mike Stump11289f42009-09-09 15:08:12 +0000193
Chris Lattnere4b95692008-11-24 03:33:13 +0000194 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
195 S = '+' + S;
196 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
197 S = '-' + S;
198 else
199 assert(ModLen == 0 && ArgLen == 0 &&
200 "Invalid modifier for DeclarationName argument");
Chris Lattnerdac91472009-10-20 05:12:36 +0000201 break;
202 }
203 case Diagnostic::ak_nameddecl: {
John McCalle1f2ec22009-09-11 06:45:03 +0000204 bool Qualified;
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000205 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
John McCalle1f2ec22009-09-11 06:45:03 +0000206 Qualified = true;
Mike Stump11289f42009-09-09 15:08:12 +0000207 else {
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000208 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor2ada0482009-02-04 17:27:36 +0000209 "Invalid modifier for NamedDecl* argument");
John McCalle1f2ec22009-09-11 06:45:03 +0000210 Qualified = false;
Douglas Gregorfc4f8a12009-02-04 22:46:25 +0000211 }
Chris Lattnerdac91472009-10-20 05:12:36 +0000212 reinterpret_cast<NamedDecl*>(Val)->
213 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
214 break;
215 }
216 case Diagnostic::ak_nestednamespec: {
Douglas Gregor053f6912009-08-26 00:04:55 +0000217 llvm::raw_string_ostream OS(S);
Chris Lattnerdac91472009-10-20 05:12:36 +0000218 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
219 Context.PrintingPolicy);
Douglas Gregor15e56022009-10-13 23:27:22 +0000220 NeedQuotes = false;
Chris Lattnerdac91472009-10-20 05:12:36 +0000221 break;
222 }
223 case Diagnostic::ak_declcontext: {
Douglas Gregore40876a2009-10-13 21:16:44 +0000224 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
Chris Lattnerdac91472009-10-20 05:12:36 +0000225 assert(DC && "Should never have a null declaration context");
226
227 if (DC->isTranslationUnit()) {
Douglas Gregore40876a2009-10-13 21:16:44 +0000228 // FIXME: Get these strings from some localized place
229 if (Context.getLangOptions().CPlusPlus)
230 S = "the global namespace";
231 else
232 S = "the global scope";
233 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
Chris Lattnerbd19b182009-10-20 05:36:05 +0000234 S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type),
235 PrevArgs, NumPrevArgs);
Douglas Gregore40876a2009-10-13 21:16:44 +0000236 } else {
237 // FIXME: Get these strings from some localized place
238 NamedDecl *ND = cast<NamedDecl>(DC);
239 if (isa<NamespaceDecl>(ND))
240 S += "namespace ";
241 else if (isa<ObjCMethodDecl>(ND))
242 S += "method ";
243 else if (isa<FunctionDecl>(ND))
244 S += "function ";
245
246 S += "'";
247 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
248 S += "'";
Douglas Gregore40876a2009-10-13 21:16:44 +0000249 }
Chris Lattnerdac91472009-10-20 05:12:36 +0000250 NeedQuotes = false;
251 break;
252 }
Chris Lattnerf7e69d52008-11-23 20:28:15 +0000253 }
Mike Stump11289f42009-09-09 15:08:12 +0000254
Douglas Gregore40876a2009-10-13 21:16:44 +0000255 if (NeedQuotes)
256 Output.push_back('\'');
257
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000258 Output.append(S.begin(), S.end());
Douglas Gregore40876a2009-10-13 21:16:44 +0000259
260 if (NeedQuotes)
261 Output.push_back('\'');
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000262}
263
264
Chris Lattner8488c822008-11-18 07:04:44 +0000265static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonfbcd8512008-08-23 22:20:38 +0000266 if (C.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +0000267 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonfbcd8512008-08-23 22:20:38 +0000268 C.getTranslationUnitDecl(),
Ted Kremenek47923c72008-09-05 01:34:33 +0000269 SourceLocation(), &C.Idents.get(Name));
Chris Lattner3b054132008-11-19 05:08:23 +0000270
Mike Stump11289f42009-09-09 15:08:12 +0000271 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattner3b054132008-11-19 05:08:23 +0000272 C.getTranslationUnitDecl(),
273 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonfbcd8512008-08-23 22:20:38 +0000274}
275
Steve Naroffc62adb62007-10-09 22:01:59 +0000276void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
277 TUScope = S;
Douglas Gregor91f84212008-12-11 16:49:14 +0000278 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000279
Chris Lattner7d4f5c42009-04-30 06:18:40 +0000280 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
John McCallbcd03502009-12-07 02:54:59 +0000281 TypeSourceInfo *TInfo;
John McCall703a3f82009-10-24 08:00:42 +0000282
Chris Lattner7d4f5c42009-04-30 06:18:40 +0000283 // Install [u]int128_t for 64-bit targets.
John McCallbcd03502009-12-07 02:54:59 +0000284 TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
Chris Lattner7d4f5c42009-04-30 06:18:40 +0000285 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
286 SourceLocation(),
287 &Context.Idents.get("__int128_t"),
John McCallbcd03502009-12-07 02:54:59 +0000288 TInfo), TUScope);
John McCall703a3f82009-10-24 08:00:42 +0000289
John McCallbcd03502009-12-07 02:54:59 +0000290 TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
Chris Lattner7d4f5c42009-04-30 06:18:40 +0000291 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
292 SourceLocation(),
293 &Context.Idents.get("__uint128_t"),
John McCallbcd03502009-12-07 02:54:59 +0000294 TInfo), TUScope);
Chris Lattner7d4f5c42009-04-30 06:18:40 +0000295 }
Mike Stump11289f42009-09-09 15:08:12 +0000296
297
Chris Lattnerfe0e0af2008-02-06 00:46:58 +0000298 if (!PP.getLangOptions().ObjC1) return;
Mike Stump11289f42009-09-09 15:08:12 +0000299
Steve Naroff853308d2009-06-16 00:20:10 +0000300 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor512b0772009-04-23 22:29:11 +0000301 if (Context.getObjCSelType().isNull()) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000302 // Create the built-in typedef for 'SEL'.
Fariborz Jahanian0afc5552009-11-23 18:04:25 +0000303 QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
John McCallbcd03502009-12-07 02:54:59 +0000304 TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
John McCall703a3f82009-10-24 08:00:42 +0000305 TypedefDecl *SelTypedef
306 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
307 &Context.Idents.get("SEL"), SelInfo);
Douglas Gregor512b0772009-04-23 22:29:11 +0000308 PushOnScopeChains(SelTypedef, TUScope);
309 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
Fariborz Jahanian04b258c2009-11-25 23:07:42 +0000310 Context.ObjCSelRedefinitionType = Context.getObjCSelType();
Douglas Gregor512b0772009-04-23 22:29:11 +0000311 }
Chris Lattner5a92bab2008-06-21 20:20:39 +0000312
Chris Lattner5a92bab2008-06-21 20:20:39 +0000313 // Synthesize "@class Protocol;
Douglas Gregor512b0772009-04-23 22:29:11 +0000314 if (Context.getObjCProtoType().isNull()) {
315 ObjCInterfaceDecl *ProtocolDecl =
316 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump11289f42009-09-09 15:08:12 +0000317 &Context.Idents.get("Protocol"),
Douglas Gregor512b0772009-04-23 22:29:11 +0000318 SourceLocation(), true);
319 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian1e3609f2009-11-18 23:15:37 +0000320 PushOnScopeChains(ProtocolDecl, TUScope, false);
Douglas Gregor512b0772009-04-23 22:29:11 +0000321 }
Steve Naroff1329fa02009-07-15 18:40:39 +0000322 // Create the built-in typedef for 'id'.
Douglas Gregor512b0772009-04-23 22:29:11 +0000323 if (Context.getObjCIdType().isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000324 QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy);
John McCallbcd03502009-12-07 02:54:59 +0000325 TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(IdT);
John McCall703a3f82009-10-24 08:00:42 +0000326 TypedefDecl *IdTypedef
327 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
328 &Context.Idents.get("id"), IdInfo);
Douglas Gregor512b0772009-04-23 22:29:11 +0000329 PushOnScopeChains(IdTypedef, TUScope);
330 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall9f57c292009-08-17 16:35:33 +0000331 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor512b0772009-04-23 22:29:11 +0000332 }
Steve Naroff1329fa02009-07-15 18:40:39 +0000333 // Create the built-in typedef for 'Class'.
Steve Naroff7cae42b2009-07-10 23:34:53 +0000334 if (Context.getObjCClassType().isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000335 QualType ClassType
336 = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy);
John McCallbcd03502009-12-07 02:54:59 +0000337 TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(ClassType);
John McCall703a3f82009-10-24 08:00:42 +0000338 TypedefDecl *ClassTypedef
339 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
340 &Context.Idents.get("Class"), ClassInfo);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000341 PushOnScopeChains(ClassTypedef, TUScope);
342 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall9f57c292009-08-17 16:35:33 +0000343 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000344 }
Steve Naroff7f549f12007-10-10 21:53:07 +0000345}
346
Douglas Gregor54feb842009-04-14 16:27:31 +0000347Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000348 bool CompleteTranslationUnit,
349 CodeCompleteConsumer *CodeCompleter)
Chris Lattner4da04a4e2009-01-22 19:21:44 +0000350 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump11289f42009-09-09 15:08:12 +0000351 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000352 ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
John McCallb8788012009-12-19 10:53:49 +0000353 CurBlock(0), PackContext(0), ParsingDeclDepth(0),
Douglas Gregor2436e712009-09-17 21:32:03 +0000354 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregorff790f12009-11-26 00:44:06 +0000355 GlobalNewDeleteDeclared(false),
Douglas Gregor37256522009-05-14 21:44:34 +0000356 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregor84d49a22009-11-11 21:54:23 +0000357 NumSFINAEErrors(0), NonInstantiationEntries(0),
358 CurrentInstantiationScope(0)
359{
Steve Naroff7f549f12007-10-10 21:53:07 +0000360 TUScope = 0;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000361 if (getLangOptions().CPlusPlus)
362 FieldCollector.reset(new CXXFieldCollector());
Mike Stump11289f42009-09-09 15:08:12 +0000363
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000364 // Tell diagnostics how to render things from the AST library.
Chris Lattnercf868c42009-02-19 23:53:20 +0000365 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Douglas Gregorff790f12009-11-26 00:44:06 +0000366
367 ExprEvalContexts.push_back(
368 ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
Steve Naroff38d31b42007-02-28 01:22:02 +0000369}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000370
Mike Stump11289f42009-09-09 15:08:12 +0000371/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattnera65e1f32008-01-16 19:17:22 +0000372/// If there is already an implicit cast, merge into the existing one.
Nate Begemanb699c9b2009-01-18 06:42:49 +0000373/// If isLvalue, the result of the cast is an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +0000374void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssond3bc31f2009-09-15 05:13:45 +0000375 CastExpr::CastKind Kind, bool isLvalue) {
Mon P Wang74b32072008-09-04 08:38:01 +0000376 QualType ExprTy = Context.getCanonicalType(Expr->getType());
377 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000378
Mon P Wang74b32072008-09-04 08:38:01 +0000379 if (ExprTy == TypeTy)
380 return;
Mike Stump11289f42009-09-09 15:08:12 +0000381
John McCallfceb64b2009-11-07 03:30:10 +0000382 if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
383 QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
384 QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
Mon P Wang74b32072008-09-04 08:38:01 +0000385 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerf490e152008-11-19 05:27:50 +0000386 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
387 << Expr->getSourceRange();
Mon P Wang74b32072008-09-04 08:38:01 +0000388 }
389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
John McCall263a48b2010-01-04 23:31:57 +0000391 CheckImplicitConversion(Expr, Ty);
John McCallfceb64b2009-11-07 03:30:10 +0000392
Douglas Gregora11693b2008-11-12 17:17:38 +0000393 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4e66cda2009-09-15 05:28:24 +0000394 if (ImpCast->getCastKind() == Kind) {
395 ImpCast->setType(Ty);
396 ImpCast->setLvalueCast(isLvalue);
397 return;
398 }
399 }
400
401 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
Chris Lattnera65e1f32008-01-16 19:17:22 +0000402}
403
Chris Lattner57c523f2007-08-31 04:53:24 +0000404void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000405 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner57c523f2007-08-31 04:53:24 +0000406}
407void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000408 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner57c523f2007-08-31 04:53:24 +0000409}
410
Chris Lattnerf4404402008-08-23 03:19:52 +0000411/// ActOnEndOfTranslationUnit - This is called at the very end of the
412/// translation unit when EOF is reached and all but the top-level scope is
413/// popped.
414void Sema::ActOnEndOfTranslationUnit() {
Anders Carlsson82fccd02009-12-07 08:24:59 +0000415
416 while (1) {
417 // C++: Perform implicit template instantiations.
418 //
419 // FIXME: When we perform these implicit instantiations, we do not carefully
420 // keep track of the point of instantiation (C++ [temp.point]). This means
421 // that name lookup that occurs within the template instantiation will
422 // always happen at the end of the translation unit, so it will find
423 // some names that should not be found. Although this is common behavior
424 // for C++ compilers, it is technically wrong. In the future, we either need
425 // to be able to filter the results of name lookup or we need to perform
426 // template instantiations earlier.
427 PerformPendingImplicitInstantiations();
428
429 /// If ProcessPendingClassesWithUnmarkedVirtualMembers ends up marking
430 /// any virtual member functions it might lead to more pending template
431 /// instantiations, which is why we need to loop here.
432 if (!ProcessPendingClassesWithUnmarkedVirtualMembers())
433 break;
434 }
435
Chris Lattner0c797362009-09-08 18:19:27 +0000436 // Check for #pragma weak identifiers that were never declared
437 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
438 // order! Iterating over a densemap like this is bad.
Ryan Flynn7d470f32009-07-30 03:15:39 +0000439 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner0c797362009-09-08 18:19:27 +0000440 I = WeakUndeclaredIdentifiers.begin(),
441 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
442 if (I->second.getUsed()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattner0c797362009-09-08 18:19:27 +0000444 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
445 << I->first;
Ryan Flynn7d470f32009-07-30 03:15:39 +0000446 }
447
Douglas Gregor54feb842009-04-14 16:27:31 +0000448 if (!CompleteTranslationUnit)
449 return;
450
Douglas Gregor0760fa12009-03-10 23:43:53 +0000451 // C99 6.9.2p2:
452 // A declaration of an identifier for an object that has file
453 // scope without an initializer, and without a storage-class
454 // specifier or with the storage-class specifier static,
455 // constitutes a tentative definition. If a translation unit
456 // contains one or more tentative definitions for an identifier,
457 // and the translation unit contains no external definition for
458 // that identifier, then the behavior is exactly as if the
459 // translation unit contains a file scope declaration of that
460 // identifier, with the composite type as of the end of the
461 // translation unit, with an initializer equal to 0.
Chris Lattner0c797362009-09-08 18:19:27 +0000462 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
463 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000464
Chris Lattner0c797362009-09-08 18:19:27 +0000465 // If the tentative definition was completed, it will be in the list, but
466 // not the map.
467 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregorbeecd582009-04-21 17:11:58 +0000468 continue;
469
Mike Stump11289f42009-09-09 15:08:12 +0000470 if (const IncompleteArrayType *ArrayT
Douglas Gregorbeecd582009-04-21 17:11:58 +0000471 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000472 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorbeecd582009-04-21 17:11:58 +0000473 ArrayT->getElementType(),
Chris Lattner0c797362009-09-08 18:19:27 +0000474 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorbeecd582009-04-21 17:11:58 +0000475 VD->setInvalidDecl();
Chris Lattner0c797362009-09-08 18:19:27 +0000476 continue;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattner0c797362009-09-08 18:19:27 +0000479 // Set the length of the array to 1 (C99 6.9.2p5).
480 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
481 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCallc5b82252009-10-16 00:14:28 +0000482 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
483 One, ArrayType::Normal, 0);
Chris Lattner0c797362009-09-08 18:19:27 +0000484 VD->setType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000485 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorbeecd582009-04-21 17:11:58 +0000486 diag::err_tentative_def_incomplete_type))
487 VD->setInvalidDecl();
488
489 // Notify the consumer that we've completed a tentative definition.
490 if (!VD->isInvalidDecl())
491 Consumer.CompleteTentativeDefinition(VD);
492
Douglas Gregor0760fa12009-03-10 23:43:53 +0000493 }
Chris Lattnerf4404402008-08-23 03:19:52 +0000494}
495
496
Chris Lattnerc11438c2006-08-18 05:17:52 +0000497//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000498// Helper functions.
499//===----------------------------------------------------------------------===//
500
Anders Carlssonb26ab812009-08-08 17:45:02 +0000501DeclContext *Sema::getFunctionLevelDeclContext() {
John McCallb8788012009-12-19 10:53:49 +0000502 DeclContext *DC = CurContext;
Mike Stump11289f42009-09-09 15:08:12 +0000503
Anders Carlssonb26ab812009-08-08 17:45:02 +0000504 while (isa<BlockDecl>(DC))
505 DC = DC->getParent();
Mike Stump11289f42009-09-09 15:08:12 +0000506
Anders Carlssonb26ab812009-08-08 17:45:02 +0000507 return DC;
508}
509
Chris Lattner79413952008-12-04 23:50:19 +0000510/// getCurFunctionDecl - If inside of a function body, this returns a pointer
511/// to the function decl for the function being parsed. If we're currently
512/// in a 'block', this returns the containing context.
513FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlssonb26ab812009-08-08 17:45:02 +0000514 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner79413952008-12-04 23:50:19 +0000515 return dyn_cast<FunctionDecl>(DC);
516}
517
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000518ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlssonb26ab812009-08-08 17:45:02 +0000519 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffecf2bb82008-11-17 16:28:52 +0000520 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000521}
Chris Lattner79413952008-12-04 23:50:19 +0000522
523NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlssonb26ab812009-08-08 17:45:02 +0000524 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner79413952008-12-04 23:50:19 +0000525 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000526 return cast<NamedDecl>(DC);
Chris Lattner79413952008-12-04 23:50:19 +0000527 return 0;
528}
529
Douglas Gregorda17bd32009-03-20 22:48:49 +0000530Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor33834512009-06-14 07:33:30 +0000531 if (!this->Emit())
532 return;
Mike Stump11289f42009-09-09 15:08:12 +0000533
Douglas Gregorda17bd32009-03-20 22:48:49 +0000534 // If this is not a note, and we're in a template instantiation
535 // that is different from the last template instantiation where
536 // we emitted an error, print a template instantiation
537 // backtrace.
538 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
539 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump11289f42009-09-09 15:08:12 +0000540 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregorda17bd32009-03-20 22:48:49 +0000541 != SemaRef.LastTemplateInstantiationErrorContext) {
542 SemaRef.PrintInstantiationStack();
Mike Stump11289f42009-09-09 15:08:12 +0000543 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregorda17bd32009-03-20 22:48:49 +0000544 = SemaRef.ActiveTemplateInstantiations.back();
545 }
546}
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000547
Anders Carlssonf68079e2009-08-26 22:33:56 +0000548Sema::SemaDiagnosticBuilder
549Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
550 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
551 PD.Emit(Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000552
Anders Carlssonf68079e2009-08-26 22:33:56 +0000553 return Builder;
554}
555
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000556void Sema::ActOnComment(SourceRange Comment) {
557 Context.Comments.push_back(Comment);
558}
Anders Carlssonf68079e2009-08-26 22:33:56 +0000559