blob: fefe924bc46f7ccfb917807aa636dceaa771d4bb [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
113 // Otherwise, we're tearing through something opaque; note that
114 // we'll eventually need an a.k.a. clause and keep going.
115 AKA = true;
116 QT = Underlying;
117 continue;
118 }
119
Chris Lattner0a026af2009-10-20 05:36:05 +0000120 // If we never tore through opaque sugar, don't print aka.
121 if (!AKA) return false;
John McCall49a832b2009-10-18 09:09:24 +0000122
Chris Lattner0a026af2009-10-20 05:36:05 +0000123 // If we did, check to see if we already desugared this type in this
124 // diagnostic. If so, don't do it again.
125 for (unsigned i = 0; i != NumPrevArgs; ++i) {
126 // TODO: Handle ak_declcontext case.
127 if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
128 void *Ptr = (void*)PrevArgs[i].second;
129 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
130 if (PrevTy == InputTy)
131 return false;
132 }
133 }
134
135 DesugaredQT = Qc.apply(QT);
136 return true;
John McCall49a832b2009-10-18 09:09:24 +0000137}
138
Douglas Gregor3f093272009-10-13 21:16:44 +0000139/// \brief Convert the given type to a string suitable for printing as part of
140/// a diagnostic.
141///
142/// \param Context the context in which the type was allocated
143/// \param Ty the type to print
Chris Lattner0a026af2009-10-20 05:36:05 +0000144static std::string
145ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
146 const Diagnostic::ArgumentValue *PrevArgs,
147 unsigned NumPrevArgs) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000148 // FIXME: Playing with std::string is really slow.
149 std::string S = Ty.getAsString(Context.PrintingPolicy);
150
John McCall49a832b2009-10-18 09:09:24 +0000151 // Consider producing an a.k.a. clause if removing all the direct
152 // sugar gives us something "significantly different".
153
154 QualType DesugaredTy;
Chris Lattner0a026af2009-10-20 05:36:05 +0000155 if (ShouldAKA(Context, Ty, PrevArgs, NumPrevArgs, DesugaredTy)) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000156 S = "'"+S+"' (aka '";
157 S += DesugaredTy.getAsString(Context.PrintingPolicy);
158 S += "')";
159 return S;
160 }
161
162 S = "'" + S + "'";
163 return S;
164}
165
Mike Stump1eb44332009-09-09 15:08:12 +0000166/// ConvertQualTypeToStringFn - This function is used to pretty print the
Chris Lattner22caddc2008-11-23 09:13:29 +0000167/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +0000168static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattnerd0344a42009-02-19 23:45:49 +0000169 const char *Modifier, unsigned ModLen,
170 const char *Argument, unsigned ArgLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000171 const Diagnostic::ArgumentValue *PrevArgs,
172 unsigned NumPrevArgs,
Chris Lattner92dd3862009-02-19 23:53:20 +0000173 llvm::SmallVectorImpl<char> &Output,
174 void *Cookie) {
175 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Chris Lattner011bb4e2008-11-23 20:28:15 +0000177 std::string S;
Douglas Gregor3f093272009-10-13 21:16:44 +0000178 bool NeedQuotes = true;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000179
180 switch (Kind) {
181 default: assert(0 && "unknown ArgumentKind");
182 case Diagnostic::ak_qualtype: {
Chris Lattnerd0344a42009-02-19 23:45:49 +0000183 assert(ModLen == 0 && ArgLen == 0 &&
184 "Invalid modifier for QualType argument");
185
Chris Lattner011bb4e2008-11-23 20:28:15 +0000186 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner0a026af2009-10-20 05:36:05 +0000187 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
Douglas Gregor3f093272009-10-13 21:16:44 +0000188 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000189 break;
190 }
191 case Diagnostic::ak_declarationname: {
Chris Lattner011bb4e2008-11-23 20:28:15 +0000192 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
193 S = N.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Chris Lattner077bf5e2008-11-24 03:33:13 +0000195 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
196 S = '+' + S;
197 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
198 S = '-' + S;
199 else
200 assert(ModLen == 0 && ArgLen == 0 &&
201 "Invalid modifier for DeclarationName argument");
Chris Lattner9cf9f862009-10-20 05:12:36 +0000202 break;
203 }
204 case Diagnostic::ak_nameddecl: {
John McCall136a6982009-09-11 06:45:03 +0000205 bool Qualified;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000206 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
John McCall136a6982009-09-11 06:45:03 +0000207 Qualified = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000208 else {
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000209 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000210 "Invalid modifier for NamedDecl* argument");
John McCall136a6982009-09-11 06:45:03 +0000211 Qualified = false;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000212 }
Chris Lattner9cf9f862009-10-20 05:12:36 +0000213 reinterpret_cast<NamedDecl*>(Val)->
214 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
215 break;
216 }
217 case Diagnostic::ak_nestednamespec: {
Douglas Gregordacd4342009-08-26 00:04:55 +0000218 llvm::raw_string_ostream OS(S);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000219 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
220 Context.PrintingPolicy);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000221 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000222 break;
223 }
224 case Diagnostic::ak_declcontext: {
Douglas Gregor3f093272009-10-13 21:16:44 +0000225 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000226 assert(DC && "Should never have a null declaration context");
227
228 if (DC->isTranslationUnit()) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000229 // FIXME: Get these strings from some localized place
230 if (Context.getLangOptions().CPlusPlus)
231 S = "the global namespace";
232 else
233 S = "the global scope";
234 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
Chris Lattner0a026af2009-10-20 05:36:05 +0000235 S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type),
236 PrevArgs, NumPrevArgs);
Douglas Gregor3f093272009-10-13 21:16:44 +0000237 } else {
238 // FIXME: Get these strings from some localized place
239 NamedDecl *ND = cast<NamedDecl>(DC);
240 if (isa<NamespaceDecl>(ND))
241 S += "namespace ";
242 else if (isa<ObjCMethodDecl>(ND))
243 S += "method ";
244 else if (isa<FunctionDecl>(ND))
245 S += "function ";
246
247 S += "'";
248 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
249 S += "'";
Douglas Gregor3f093272009-10-13 21:16:44 +0000250 }
Chris Lattner9cf9f862009-10-20 05:12:36 +0000251 NeedQuotes = false;
252 break;
253 }
Chris Lattner011bb4e2008-11-23 20:28:15 +0000254 }
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Douglas Gregor3f093272009-10-13 21:16:44 +0000256 if (NeedQuotes)
257 Output.push_back('\'');
258
Chris Lattner22caddc2008-11-23 09:13:29 +0000259 Output.append(S.begin(), S.end());
Douglas Gregor3f093272009-10-13 21:16:44 +0000260
261 if (NeedQuotes)
262 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000263}
264
265
Chris Lattner0a14eee2008-11-18 07:04:44 +0000266static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +0000267 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000268 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +0000269 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +0000270 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000271
Mike Stump1eb44332009-09-09 15:08:12 +0000272 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000273 C.getTranslationUnitDecl(),
274 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +0000275}
276
Steve Naroffb216c882007-10-09 22:01:59 +0000277void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
278 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000279 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattner4d150c82009-04-30 06:18:40 +0000281 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
John McCalla93c9342009-12-07 02:54:59 +0000282 TypeSourceInfo *TInfo;
John McCallba6a9bd2009-10-24 08:00:42 +0000283
Chris Lattner4d150c82009-04-30 06:18:40 +0000284 // Install [u]int128_t for 64-bit targets.
John McCalla93c9342009-12-07 02:54:59 +0000285 TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +0000286 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
287 SourceLocation(),
288 &Context.Idents.get("__int128_t"),
John McCalla93c9342009-12-07 02:54:59 +0000289 TInfo), TUScope);
John McCallba6a9bd2009-10-24 08:00:42 +0000290
John McCalla93c9342009-12-07 02:54:59 +0000291 TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +0000292 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
293 SourceLocation(),
294 &Context.Idents.get("__uint128_t"),
John McCalla93c9342009-12-07 02:54:59 +0000295 TInfo), TUScope);
Chris Lattner4d150c82009-04-30 06:18:40 +0000296 }
Mike Stump1eb44332009-09-09 15:08:12 +0000297
298
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000299 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Steve Naroffcb83c532009-06-16 00:20:10 +0000301 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +0000302 if (Context.getObjCSelType().isNull()) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000303 // Create the built-in typedef for 'SEL'.
Fariborz Jahanian04765ac2009-11-23 18:04:25 +0000304 QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
John McCalla93c9342009-12-07 02:54:59 +0000305 TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
John McCallba6a9bd2009-10-24 08:00:42 +0000306 TypedefDecl *SelTypedef
307 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
308 &Context.Idents.get("SEL"), SelInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +0000309 PushOnScopeChains(SelTypedef, TUScope);
310 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +0000311 Context.ObjCSelRedefinitionType = Context.getObjCSelType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000312 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000313
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000314 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +0000315 if (Context.getObjCProtoType().isNull()) {
316 ObjCInterfaceDecl *ProtocolDecl =
317 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000318 &Context.Idents.get("Protocol"),
Douglas Gregor319ac892009-04-23 22:29:11 +0000319 SourceLocation(), true);
320 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian10324db2009-11-18 23:15:37 +0000321 PushOnScopeChains(ProtocolDecl, TUScope, false);
Douglas Gregor319ac892009-04-23 22:29:11 +0000322 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000323 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +0000324 if (Context.getObjCIdType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000325 QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy);
John McCalla93c9342009-12-07 02:54:59 +0000326 TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(IdT);
John McCallba6a9bd2009-10-24 08:00:42 +0000327 TypedefDecl *IdTypedef
328 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
329 &Context.Idents.get("id"), IdInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +0000330 PushOnScopeChains(IdTypedef, TUScope);
331 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000332 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000333 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000334 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +0000335 if (Context.getObjCClassType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000336 QualType ClassType
337 = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy);
John McCalla93c9342009-12-07 02:54:59 +0000338 TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(ClassType);
John McCallba6a9bd2009-10-24 08:00:42 +0000339 TypedefDecl *ClassTypedef
340 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
341 &Context.Idents.get("Class"), ClassInfo);
Steve Naroff14108da2009-07-10 23:34:53 +0000342 PushOnScopeChains(ClassTypedef, TUScope);
343 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000344 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000345 }
Steve Naroff3b950172007-10-10 21:53:07 +0000346}
347
Douglas Gregorf807fe02009-04-14 16:27:31 +0000348Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000349 bool CompleteTranslationUnit,
350 CodeCompleteConsumer *CodeCompleter)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000351 : TheTargetAttributesSema(0),
352 LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump1eb44332009-09-09 15:08:12 +0000353 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000354 ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
John McCalldb0ee1d2009-12-19 10:53:49 +0000355 CurBlock(0), PackContext(0), ParsingDeclDepth(0),
Douglas Gregor81b747b2009-09-17 21:32:03 +0000356 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregor2afce722009-11-26 00:44:06 +0000357 GlobalNewDeleteDeclared(false),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000358 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorf35f8282009-11-11 21:54:23 +0000359 NumSFINAEErrors(0), NonInstantiationEntries(0),
360 CurrentInstantiationScope(0)
361{
Steve Naroff3b950172007-10-10 21:53:07 +0000362 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000363 if (getLangOptions().CPlusPlus)
364 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattner22caddc2008-11-23 09:13:29 +0000366 // Tell diagnostics how to render things from the AST library.
Chris Lattner92dd3862009-02-19 23:53:20 +0000367 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Douglas Gregor2afce722009-11-26 00:44:06 +0000368
369 ExprEvalContexts.push_back(
370 ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
Reid Spencer5f016e22007-07-11 17:01:13 +0000371}
372
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000373Sema::~Sema() {
374 if (PackContext) FreePackedContext();
375 delete TheTargetAttributesSema;
376}
377
Mike Stump1eb44332009-09-09 15:08:12 +0000378/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000379/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000380/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000381void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonc0a2fd82009-09-15 05:13:45 +0000382 CastExpr::CastKind Kind, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000383 QualType ExprTy = Context.getCanonicalType(Expr->getType());
384 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Mon P Wang3a2c7442008-09-04 08:38:01 +0000386 if (ExprTy == TypeTy)
387 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000388
John McCall680523a2009-11-07 03:30:10 +0000389 if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
390 QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
391 QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000392 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000393 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
394 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000395 }
396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
John McCall51313c32010-01-04 23:31:57 +0000398 CheckImplicitConversion(Expr, Ty);
John McCall680523a2009-11-07 03:30:10 +0000399
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000400 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000401 if (ImpCast->getCastKind() == Kind) {
402 ImpCast->setType(Ty);
403 ImpCast->setLvalueCast(isLvalue);
404 return;
405 }
406 }
407
408 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000409}
410
Chris Lattner394a3fd2007-08-31 04:53:24 +0000411void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000412 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000413}
414void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000415 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000416}
417
Chris Lattner9299f3f2008-08-23 03:19:52 +0000418/// ActOnEndOfTranslationUnit - This is called at the very end of the
419/// translation unit when EOF is reached and all but the top-level scope is
420/// popped.
421void Sema::ActOnEndOfTranslationUnit() {
Anders Carlssond6a637f2009-12-07 08:24:59 +0000422
423 while (1) {
424 // C++: Perform implicit template instantiations.
425 //
426 // FIXME: When we perform these implicit instantiations, we do not carefully
427 // keep track of the point of instantiation (C++ [temp.point]). This means
428 // that name lookup that occurs within the template instantiation will
429 // always happen at the end of the translation unit, so it will find
430 // some names that should not be found. Although this is common behavior
431 // for C++ compilers, it is technically wrong. In the future, we either need
432 // to be able to filter the results of name lookup or we need to perform
433 // template instantiations earlier.
434 PerformPendingImplicitInstantiations();
435
436 /// If ProcessPendingClassesWithUnmarkedVirtualMembers ends up marking
437 /// any virtual member functions it might lead to more pending template
438 /// instantiations, which is why we need to loop here.
439 if (!ProcessPendingClassesWithUnmarkedVirtualMembers())
440 break;
441 }
442
Chris Lattner63d65f82009-09-08 18:19:27 +0000443 // Check for #pragma weak identifiers that were never declared
444 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
445 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000446 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000447 I = WeakUndeclaredIdentifiers.begin(),
448 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
449 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner63d65f82009-09-08 18:19:27 +0000451 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
452 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000453 }
454
Douglas Gregorf807fe02009-04-14 16:27:31 +0000455 if (!CompleteTranslationUnit)
456 return;
457
Douglas Gregor275a3692009-03-10 23:43:53 +0000458 // C99 6.9.2p2:
459 // A declaration of an identifier for an object that has file
460 // scope without an initializer, and without a storage-class
461 // specifier or with the storage-class specifier static,
462 // constitutes a tentative definition. If a translation unit
463 // contains one or more tentative definitions for an identifier,
464 // and the translation unit contains no external definition for
465 // that identifier, then the behavior is exactly as if the
466 // translation unit contains a file scope declaration of that
467 // identifier, with the composite type as of the end of the
468 // translation unit, with an initializer equal to 0.
Chris Lattner63d65f82009-09-08 18:19:27 +0000469 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
470 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattner63d65f82009-09-08 18:19:27 +0000472 // If the tentative definition was completed, it will be in the list, but
473 // not the map.
474 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000475 continue;
476
Mike Stump1eb44332009-09-09 15:08:12 +0000477 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000478 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000479 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000480 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000481 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000482 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000483 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner63d65f82009-09-08 18:19:27 +0000486 // Set the length of the array to 1 (C99 6.9.2p5).
487 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
488 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCall46a617a2009-10-16 00:14:28 +0000489 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
490 One, ArrayType::Normal, 0);
Chris Lattner63d65f82009-09-08 18:19:27 +0000491 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000492 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000493 diag::err_tentative_def_incomplete_type))
494 VD->setInvalidDecl();
495
496 // Notify the consumer that we've completed a tentative definition.
497 if (!VD->isInvalidDecl())
498 Consumer.CompleteTentativeDefinition(VD);
499
Douglas Gregor275a3692009-03-10 23:43:53 +0000500 }
Chris Lattner9299f3f2008-08-23 03:19:52 +0000501}
502
503
Reid Spencer5f016e22007-07-11 17:01:13 +0000504//===----------------------------------------------------------------------===//
505// Helper functions.
506//===----------------------------------------------------------------------===//
507
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000508DeclContext *Sema::getFunctionLevelDeclContext() {
John McCalldb0ee1d2009-12-19 10:53:49 +0000509 DeclContext *DC = CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000511 while (isa<BlockDecl>(DC))
512 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000514 return DC;
515}
516
Chris Lattner371f2582008-12-04 23:50:19 +0000517/// getCurFunctionDecl - If inside of a function body, this returns a pointer
518/// to the function decl for the function being parsed. If we're currently
519/// in a 'block', this returns the containing context.
520FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000521 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000522 return dyn_cast<FunctionDecl>(DC);
523}
524
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000525ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000526 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000527 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000528}
Chris Lattner371f2582008-12-04 23:50:19 +0000529
530NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000531 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000532 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000533 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000534 return 0;
535}
536
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000537Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000538 if (!this->Emit())
539 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000541 // If this is not a note, and we're in a template instantiation
542 // that is different from the last template instantiation where
543 // we emitted an error, print a template instantiation
544 // backtrace.
545 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
546 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000547 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000548 != SemaRef.LastTemplateInstantiationErrorContext) {
549 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000550 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000551 = SemaRef.ActiveTemplateInstantiations.back();
552 }
553}
Douglas Gregor2e222532009-07-02 17:08:52 +0000554
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000555Sema::SemaDiagnosticBuilder
556Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
557 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
558 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000560 return Builder;
561}
562
Douglas Gregor2e222532009-07-02 17:08:52 +0000563void Sema::ActOnComment(SourceRange Comment) {
564 Context.Comments.push_back(Comment);
565}
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000566