blob: 171101bb96de6e68ae873944756ba108f09eeab6 [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"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000016#include "TargetAttributesSema.h"
Ryan Flynne25ff832009-07-30 03:15:39 +000017#include "llvm/ADT/DenseMap.h"
John McCall680523a2009-11-07 03:30:10 +000018#include "llvm/ADT/APFloat.h"
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +000019#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000021#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/Lex/Preprocessor.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000024#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner4d150c82009-04-30 06:18:40 +000025#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
John McCall49a832b2009-10-18 09:09:24 +000028/// Determines whether we should have an a.k.a. clause when
Chris Lattner0a026af2009-10-20 05:36:05 +000029/// pretty-printing a type. There are three main criteria:
John McCall49a832b2009-10-18 09:09:24 +000030///
31/// 1) Some types provide very minimal sugar that doesn't impede the
32/// user's understanding --- for example, elaborated type
33/// specifiers. If this is all the sugar we see, we don't want an
34/// a.k.a. clause.
35/// 2) Some types are technically sugared but are much more familiar
36/// when seen in their sugared form --- for example, va_list,
37/// vector types, and the magic Objective C types. We don't
38/// want to desugar these, even if we do produce an a.k.a. clause.
Chris Lattner0a026af2009-10-20 05:36:05 +000039/// 3) Some types may have already been desugared previously in this diagnostic.
40/// if this is the case, doing another "aka" would just be clutter.
41///
John McCall49a832b2009-10-18 09:09:24 +000042static bool ShouldAKA(ASTContext &Context, QualType QT,
Chris Lattner0a026af2009-10-20 05:36:05 +000043 const Diagnostic::ArgumentValue *PrevArgs,
44 unsigned NumPrevArgs,
45 QualType &DesugaredQT) {
46 QualType InputTy = QT;
47
John McCall49a832b2009-10-18 09:09:24 +000048 bool AKA = false;
49 QualifierCollector Qc;
50
51 while (true) {
52 const Type *Ty = Qc.strip(QT);
53
54 // Don't aka just because we saw an elaborated type...
55 if (isa<ElaboratedType>(Ty)) {
56 QT = cast<ElaboratedType>(Ty)->desugar();
57 continue;
58 }
59
60 // ...or a qualified name type...
61 if (isa<QualifiedNameType>(Ty)) {
62 QT = cast<QualifiedNameType>(Ty)->desugar();
63 continue;
64 }
65
66 // ...or a substituted template type parameter.
67 if (isa<SubstTemplateTypeParmType>(Ty)) {
68 QT = cast<SubstTemplateTypeParmType>(Ty)->desugar();
69 continue;
70 }
71
72 // Don't desugar template specializations.
73 if (isa<TemplateSpecializationType>(Ty))
74 break;
75
76 // Don't desugar magic Objective-C types.
77 if (QualType(Ty,0) == Context.getObjCIdType() ||
78 QualType(Ty,0) == Context.getObjCClassType() ||
79 QualType(Ty,0) == Context.getObjCSelType() ||
80 QualType(Ty,0) == Context.getObjCProtoType())
81 break;
82
83 // Don't desugar va_list.
84 if (QualType(Ty,0) == Context.getBuiltinVaListType())
85 break;
86
87 // Otherwise, do a single-step desugar.
88 QualType Underlying;
89 bool IsSugar = false;
90 switch (Ty->getTypeClass()) {
91#define ABSTRACT_TYPE(Class, Base)
92#define TYPE(Class, Base) \
93 case Type::Class: { \
94 const Class##Type *CTy = cast<Class##Type>(Ty); \
95 if (CTy->isSugared()) { \
96 IsSugar = true; \
97 Underlying = CTy->desugar(); \
98 } \
99 break; \
100 }
101#include "clang/AST/TypeNodes.def"
102 }
103
104 // If it wasn't sugared, we're done.
105 if (!IsSugar)
106 break;
107
108 // If the desugared type is a vector type, we don't want to expand
109 // it, it will turn into an attribute mess. People want their "vec4".
110 if (isa<VectorType>(Underlying))
111 break;
112
John McCall64f7e252010-01-13 22:07:44 +0000113 // Don't desugar through the primary typedef of an anonymous type.
114 if (isa<TagType>(Underlying) && isa<TypedefType>(QT))
115 if (cast<TagType>(Underlying)->getDecl()->getTypedefForAnonDecl() ==
116 cast<TypedefType>(QT)->getDecl())
117 break;
118
John McCall49a832b2009-10-18 09:09:24 +0000119 // Otherwise, we're tearing through something opaque; note that
120 // we'll eventually need an a.k.a. clause and keep going.
121 AKA = true;
122 QT = Underlying;
123 continue;
124 }
125
Chris Lattner0a026af2009-10-20 05:36:05 +0000126 // If we never tore through opaque sugar, don't print aka.
127 if (!AKA) return false;
John McCall49a832b2009-10-18 09:09:24 +0000128
Chris Lattner0a026af2009-10-20 05:36:05 +0000129 // If we did, check to see if we already desugared this type in this
130 // diagnostic. If so, don't do it again.
131 for (unsigned i = 0; i != NumPrevArgs; ++i) {
132 // TODO: Handle ak_declcontext case.
133 if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
134 void *Ptr = (void*)PrevArgs[i].second;
135 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
136 if (PrevTy == InputTy)
137 return false;
138 }
139 }
140
141 DesugaredQT = Qc.apply(QT);
142 return true;
John McCall49a832b2009-10-18 09:09:24 +0000143}
144
Douglas Gregor3f093272009-10-13 21:16:44 +0000145/// \brief Convert the given type to a string suitable for printing as part of
146/// a diagnostic.
147///
148/// \param Context the context in which the type was allocated
149/// \param Ty the type to print
Chris Lattner0a026af2009-10-20 05:36:05 +0000150static std::string
151ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
152 const Diagnostic::ArgumentValue *PrevArgs,
153 unsigned NumPrevArgs) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000154 // FIXME: Playing with std::string is really slow.
155 std::string S = Ty.getAsString(Context.PrintingPolicy);
156
John McCall49a832b2009-10-18 09:09:24 +0000157 // Consider producing an a.k.a. clause if removing all the direct
158 // sugar gives us something "significantly different".
159
160 QualType DesugaredTy;
Chris Lattner0a026af2009-10-20 05:36:05 +0000161 if (ShouldAKA(Context, Ty, PrevArgs, NumPrevArgs, DesugaredTy)) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000162 S = "'"+S+"' (aka '";
163 S += DesugaredTy.getAsString(Context.PrintingPolicy);
164 S += "')";
165 return S;
166 }
167
168 S = "'" + S + "'";
169 return S;
170}
171
Mike Stump1eb44332009-09-09 15:08:12 +0000172/// ConvertQualTypeToStringFn - This function is used to pretty print the
Chris Lattner22caddc2008-11-23 09:13:29 +0000173/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +0000174static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattnerd0344a42009-02-19 23:45:49 +0000175 const char *Modifier, unsigned ModLen,
176 const char *Argument, unsigned ArgLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000177 const Diagnostic::ArgumentValue *PrevArgs,
178 unsigned NumPrevArgs,
Chris Lattner92dd3862009-02-19 23:53:20 +0000179 llvm::SmallVectorImpl<char> &Output,
180 void *Cookie) {
181 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattner011bb4e2008-11-23 20:28:15 +0000183 std::string S;
Douglas Gregor3f093272009-10-13 21:16:44 +0000184 bool NeedQuotes = true;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000185
186 switch (Kind) {
187 default: assert(0 && "unknown ArgumentKind");
188 case Diagnostic::ak_qualtype: {
Chris Lattnerd0344a42009-02-19 23:45:49 +0000189 assert(ModLen == 0 && ArgLen == 0 &&
190 "Invalid modifier for QualType argument");
191
Chris Lattner011bb4e2008-11-23 20:28:15 +0000192 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner0a026af2009-10-20 05:36:05 +0000193 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
Douglas Gregor3f093272009-10-13 21:16:44 +0000194 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000195 break;
196 }
197 case Diagnostic::ak_declarationname: {
Chris Lattner011bb4e2008-11-23 20:28:15 +0000198 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
199 S = N.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattner077bf5e2008-11-24 03:33:13 +0000201 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
202 S = '+' + S;
203 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
204 S = '-' + S;
205 else
206 assert(ModLen == 0 && ArgLen == 0 &&
207 "Invalid modifier for DeclarationName argument");
Chris Lattner9cf9f862009-10-20 05:12:36 +0000208 break;
209 }
210 case Diagnostic::ak_nameddecl: {
John McCall136a6982009-09-11 06:45:03 +0000211 bool Qualified;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000212 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
John McCall136a6982009-09-11 06:45:03 +0000213 Qualified = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000214 else {
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000215 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000216 "Invalid modifier for NamedDecl* argument");
John McCall136a6982009-09-11 06:45:03 +0000217 Qualified = false;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000218 }
Chris Lattner9cf9f862009-10-20 05:12:36 +0000219 reinterpret_cast<NamedDecl*>(Val)->
220 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
221 break;
222 }
223 case Diagnostic::ak_nestednamespec: {
Douglas Gregordacd4342009-08-26 00:04:55 +0000224 llvm::raw_string_ostream OS(S);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000225 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
226 Context.PrintingPolicy);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000227 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000228 break;
229 }
230 case Diagnostic::ak_declcontext: {
Douglas Gregor3f093272009-10-13 21:16:44 +0000231 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000232 assert(DC && "Should never have a null declaration context");
233
234 if (DC->isTranslationUnit()) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000235 // FIXME: Get these strings from some localized place
236 if (Context.getLangOptions().CPlusPlus)
237 S = "the global namespace";
238 else
239 S = "the global scope";
240 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
Chris Lattner0a026af2009-10-20 05:36:05 +0000241 S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type),
242 PrevArgs, NumPrevArgs);
Douglas Gregor3f093272009-10-13 21:16:44 +0000243 } else {
244 // FIXME: Get these strings from some localized place
245 NamedDecl *ND = cast<NamedDecl>(DC);
246 if (isa<NamespaceDecl>(ND))
247 S += "namespace ";
248 else if (isa<ObjCMethodDecl>(ND))
249 S += "method ";
250 else if (isa<FunctionDecl>(ND))
251 S += "function ";
252
253 S += "'";
254 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
255 S += "'";
Douglas Gregor3f093272009-10-13 21:16:44 +0000256 }
Chris Lattner9cf9f862009-10-20 05:12:36 +0000257 NeedQuotes = false;
258 break;
259 }
Chris Lattner011bb4e2008-11-23 20:28:15 +0000260 }
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Douglas Gregor3f093272009-10-13 21:16:44 +0000262 if (NeedQuotes)
263 Output.push_back('\'');
264
Chris Lattner22caddc2008-11-23 09:13:29 +0000265 Output.append(S.begin(), S.end());
Douglas Gregor3f093272009-10-13 21:16:44 +0000266
267 if (NeedQuotes)
268 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000269}
270
271
Chris Lattner0a14eee2008-11-18 07:04:44 +0000272static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +0000273 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000274 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +0000275 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +0000276 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000277
Mike Stump1eb44332009-09-09 15:08:12 +0000278 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000279 C.getTranslationUnitDecl(),
280 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +0000281}
282
Steve Naroffb216c882007-10-09 22:01:59 +0000283void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
284 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000285 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Chris Lattner4d150c82009-04-30 06:18:40 +0000287 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
John McCalla93c9342009-12-07 02:54:59 +0000288 TypeSourceInfo *TInfo;
John McCallba6a9bd2009-10-24 08:00:42 +0000289
Chris Lattner4d150c82009-04-30 06:18:40 +0000290 // Install [u]int128_t for 64-bit targets.
John McCalla93c9342009-12-07 02:54:59 +0000291 TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +0000292 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
293 SourceLocation(),
294 &Context.Idents.get("__int128_t"),
John McCalla93c9342009-12-07 02:54:59 +0000295 TInfo), TUScope);
John McCallba6a9bd2009-10-24 08:00:42 +0000296
John McCalla93c9342009-12-07 02:54:59 +0000297 TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +0000298 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
299 SourceLocation(),
300 &Context.Idents.get("__uint128_t"),
John McCalla93c9342009-12-07 02:54:59 +0000301 TInfo), TUScope);
Chris Lattner4d150c82009-04-30 06:18:40 +0000302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303
304
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000305 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Steve Naroffcb83c532009-06-16 00:20:10 +0000307 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +0000308 if (Context.getObjCSelType().isNull()) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000309 // Create the built-in typedef for 'SEL'.
Fariborz Jahanian04765ac2009-11-23 18:04:25 +0000310 QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
John McCalla93c9342009-12-07 02:54:59 +0000311 TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
John McCallba6a9bd2009-10-24 08:00:42 +0000312 TypedefDecl *SelTypedef
313 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
314 &Context.Idents.get("SEL"), SelInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +0000315 PushOnScopeChains(SelTypedef, TUScope);
316 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +0000317 Context.ObjCSelRedefinitionType = Context.getObjCSelType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000318 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000319
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000320 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +0000321 if (Context.getObjCProtoType().isNull()) {
322 ObjCInterfaceDecl *ProtocolDecl =
323 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000324 &Context.Idents.get("Protocol"),
Douglas Gregor319ac892009-04-23 22:29:11 +0000325 SourceLocation(), true);
326 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian10324db2009-11-18 23:15:37 +0000327 PushOnScopeChains(ProtocolDecl, TUScope, false);
Douglas Gregor319ac892009-04-23 22:29:11 +0000328 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000329 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +0000330 if (Context.getObjCIdType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000331 QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy);
John McCalla93c9342009-12-07 02:54:59 +0000332 TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(IdT);
John McCallba6a9bd2009-10-24 08:00:42 +0000333 TypedefDecl *IdTypedef
334 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
335 &Context.Idents.get("id"), IdInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +0000336 PushOnScopeChains(IdTypedef, TUScope);
337 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000338 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000339 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000340 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +0000341 if (Context.getObjCClassType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000342 QualType ClassType
343 = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy);
John McCalla93c9342009-12-07 02:54:59 +0000344 TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(ClassType);
John McCallba6a9bd2009-10-24 08:00:42 +0000345 TypedefDecl *ClassTypedef
346 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
347 &Context.Idents.get("Class"), ClassInfo);
Steve Naroff14108da2009-07-10 23:34:53 +0000348 PushOnScopeChains(ClassTypedef, TUScope);
349 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000350 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000351 }
Steve Naroff3b950172007-10-10 21:53:07 +0000352}
353
Douglas Gregorf807fe02009-04-14 16:27:31 +0000354Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000355 bool CompleteTranslationUnit,
356 CodeCompleteConsumer *CodeCompleter)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000357 : TheTargetAttributesSema(0),
358 LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump1eb44332009-09-09 15:08:12 +0000359 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000360 ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
John McCalldb0ee1d2009-12-19 10:53:49 +0000361 CurBlock(0), PackContext(0), ParsingDeclDepth(0),
Douglas Gregor81b747b2009-09-17 21:32:03 +0000362 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregor2afce722009-11-26 00:44:06 +0000363 GlobalNewDeleteDeclared(false),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000364 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorf35f8282009-11-11 21:54:23 +0000365 NumSFINAEErrors(0), NonInstantiationEntries(0),
366 CurrentInstantiationScope(0)
367{
Steve Naroff3b950172007-10-10 21:53:07 +0000368 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000369 if (getLangOptions().CPlusPlus)
370 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner22caddc2008-11-23 09:13:29 +0000372 // Tell diagnostics how to render things from the AST library.
Chris Lattner92dd3862009-02-19 23:53:20 +0000373 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Douglas Gregor2afce722009-11-26 00:44:06 +0000374
375 ExprEvalContexts.push_back(
376 ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
Reid Spencer5f016e22007-07-11 17:01:13 +0000377}
378
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000379Sema::~Sema() {
380 if (PackContext) FreePackedContext();
381 delete TheTargetAttributesSema;
382}
383
Mike Stump1eb44332009-09-09 15:08:12 +0000384/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000385/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000386/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000387void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonc0a2fd82009-09-15 05:13:45 +0000388 CastExpr::CastKind Kind, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000389 QualType ExprTy = Context.getCanonicalType(Expr->getType());
390 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Mon P Wang3a2c7442008-09-04 08:38:01 +0000392 if (ExprTy == TypeTy)
393 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000394
John McCall680523a2009-11-07 03:30:10 +0000395 if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
396 QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
397 QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000398 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000399 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
400 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000401 }
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
John McCall51313c32010-01-04 23:31:57 +0000404 CheckImplicitConversion(Expr, Ty);
John McCall680523a2009-11-07 03:30:10 +0000405
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000406 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000407 if (ImpCast->getCastKind() == Kind) {
408 ImpCast->setType(Ty);
409 ImpCast->setLvalueCast(isLvalue);
410 return;
411 }
412 }
413
414 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000415}
416
Chris Lattner394a3fd2007-08-31 04:53:24 +0000417void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000418 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000419}
420void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000421 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000422}
423
Chris Lattner9299f3f2008-08-23 03:19:52 +0000424/// ActOnEndOfTranslationUnit - This is called at the very end of the
425/// translation unit when EOF is reached and all but the top-level scope is
426/// popped.
427void Sema::ActOnEndOfTranslationUnit() {
Anders Carlssond6a637f2009-12-07 08:24:59 +0000428
429 while (1) {
430 // C++: Perform implicit template instantiations.
431 //
432 // FIXME: When we perform these implicit instantiations, we do not carefully
433 // keep track of the point of instantiation (C++ [temp.point]). This means
434 // that name lookup that occurs within the template instantiation will
435 // always happen at the end of the translation unit, so it will find
436 // some names that should not be found. Although this is common behavior
437 // for C++ compilers, it is technically wrong. In the future, we either need
438 // to be able to filter the results of name lookup or we need to perform
439 // template instantiations earlier.
440 PerformPendingImplicitInstantiations();
441
442 /// If ProcessPendingClassesWithUnmarkedVirtualMembers ends up marking
443 /// any virtual member functions it might lead to more pending template
444 /// instantiations, which is why we need to loop here.
445 if (!ProcessPendingClassesWithUnmarkedVirtualMembers())
446 break;
447 }
448
Chris Lattner63d65f82009-09-08 18:19:27 +0000449 // Check for #pragma weak identifiers that were never declared
450 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
451 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000452 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000453 I = WeakUndeclaredIdentifiers.begin(),
454 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
455 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner63d65f82009-09-08 18:19:27 +0000457 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
458 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000459 }
460
Douglas Gregorf807fe02009-04-14 16:27:31 +0000461 if (!CompleteTranslationUnit)
462 return;
463
Douglas Gregor275a3692009-03-10 23:43:53 +0000464 // C99 6.9.2p2:
465 // A declaration of an identifier for an object that has file
466 // scope without an initializer, and without a storage-class
467 // specifier or with the storage-class specifier static,
468 // constitutes a tentative definition. If a translation unit
469 // contains one or more tentative definitions for an identifier,
470 // and the translation unit contains no external definition for
471 // that identifier, then the behavior is exactly as if the
472 // translation unit contains a file scope declaration of that
473 // identifier, with the composite type as of the end of the
474 // translation unit, with an initializer equal to 0.
Chris Lattner63d65f82009-09-08 18:19:27 +0000475 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
476 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattner63d65f82009-09-08 18:19:27 +0000478 // If the tentative definition was completed, it will be in the list, but
479 // not the map.
480 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000481 continue;
482
Mike Stump1eb44332009-09-09 15:08:12 +0000483 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000484 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000485 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000486 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000487 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000488 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000489 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Chris Lattner63d65f82009-09-08 18:19:27 +0000492 // Set the length of the array to 1 (C99 6.9.2p5).
493 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
494 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCall46a617a2009-10-16 00:14:28 +0000495 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
496 One, ArrayType::Normal, 0);
Chris Lattner63d65f82009-09-08 18:19:27 +0000497 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000498 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000499 diag::err_tentative_def_incomplete_type))
500 VD->setInvalidDecl();
501
502 // Notify the consumer that we've completed a tentative definition.
503 if (!VD->isInvalidDecl())
504 Consumer.CompleteTentativeDefinition(VD);
505
Douglas Gregor275a3692009-03-10 23:43:53 +0000506 }
Chris Lattner9299f3f2008-08-23 03:19:52 +0000507}
508
509
Reid Spencer5f016e22007-07-11 17:01:13 +0000510//===----------------------------------------------------------------------===//
511// Helper functions.
512//===----------------------------------------------------------------------===//
513
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000514DeclContext *Sema::getFunctionLevelDeclContext() {
John McCalldb0ee1d2009-12-19 10:53:49 +0000515 DeclContext *DC = CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000517 while (isa<BlockDecl>(DC))
518 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000520 return DC;
521}
522
Chris Lattner371f2582008-12-04 23:50:19 +0000523/// getCurFunctionDecl - If inside of a function body, this returns a pointer
524/// to the function decl for the function being parsed. If we're currently
525/// in a 'block', this returns the containing context.
526FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000527 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000528 return dyn_cast<FunctionDecl>(DC);
529}
530
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000531ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000532 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000533 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000534}
Chris Lattner371f2582008-12-04 23:50:19 +0000535
536NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000537 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000538 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000539 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000540 return 0;
541}
542
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000543Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000544 if (!this->Emit())
545 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000547 // If this is not a note, and we're in a template instantiation
548 // that is different from the last template instantiation where
549 // we emitted an error, print a template instantiation
550 // backtrace.
551 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
552 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000553 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000554 != SemaRef.LastTemplateInstantiationErrorContext) {
555 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000556 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000557 = SemaRef.ActiveTemplateInstantiations.back();
558 }
559}
Douglas Gregor2e222532009-07-02 17:08:52 +0000560
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000561Sema::SemaDiagnosticBuilder
562Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
563 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
564 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000566 return Builder;
567}
568
Douglas Gregor2e222532009-07-02 17:08:52 +0000569void Sema::ActOnComment(SourceRange Comment) {
570 Context.Comments.push_back(Comment);
571}
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000572