blob: 8104dd39d052836389e1399a82e8f4055a182622 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
Ryan Flynne25ff832009-07-30 03:15:39 +000016#include "llvm/ADT/DenseMap.h"
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +000017#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Lex/Preprocessor.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000022#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner4d150c82009-04-30 06:18:40 +000023#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
John McCall49a832b2009-10-18 09:09:24 +000026/// Determines whether we should have an a.k.a. clause when
Chris Lattner0a026af2009-10-20 05:36:05 +000027/// pretty-printing a type. There are three main criteria:
John McCall49a832b2009-10-18 09:09:24 +000028///
29/// 1) Some types provide very minimal sugar that doesn't impede the
30/// user's understanding --- for example, elaborated type
31/// specifiers. If this is all the sugar we see, we don't want an
32/// a.k.a. clause.
33/// 2) Some types are technically sugared but are much more familiar
34/// when seen in their sugared form --- for example, va_list,
35/// vector types, and the magic Objective C types. We don't
36/// want to desugar these, even if we do produce an a.k.a. clause.
Chris Lattner0a026af2009-10-20 05:36:05 +000037/// 3) Some types may have already been desugared previously in this diagnostic.
38/// if this is the case, doing another "aka" would just be clutter.
39///
John McCall49a832b2009-10-18 09:09:24 +000040static bool ShouldAKA(ASTContext &Context, QualType QT,
Chris Lattner0a026af2009-10-20 05:36:05 +000041 const Diagnostic::ArgumentValue *PrevArgs,
42 unsigned NumPrevArgs,
43 QualType &DesugaredQT) {
44 QualType InputTy = QT;
45
John McCall49a832b2009-10-18 09:09:24 +000046 bool AKA = false;
47 QualifierCollector Qc;
48
49 while (true) {
50 const Type *Ty = Qc.strip(QT);
51
52 // Don't aka just because we saw an elaborated type...
53 if (isa<ElaboratedType>(Ty)) {
54 QT = cast<ElaboratedType>(Ty)->desugar();
55 continue;
56 }
57
58 // ...or a qualified name type...
59 if (isa<QualifiedNameType>(Ty)) {
60 QT = cast<QualifiedNameType>(Ty)->desugar();
61 continue;
62 }
63
64 // ...or a substituted template type parameter.
65 if (isa<SubstTemplateTypeParmType>(Ty)) {
66 QT = cast<SubstTemplateTypeParmType>(Ty)->desugar();
67 continue;
68 }
69
70 // Don't desugar template specializations.
71 if (isa<TemplateSpecializationType>(Ty))
72 break;
73
74 // Don't desugar magic Objective-C types.
75 if (QualType(Ty,0) == Context.getObjCIdType() ||
76 QualType(Ty,0) == Context.getObjCClassType() ||
77 QualType(Ty,0) == Context.getObjCSelType() ||
78 QualType(Ty,0) == Context.getObjCProtoType())
79 break;
80
81 // Don't desugar va_list.
82 if (QualType(Ty,0) == Context.getBuiltinVaListType())
83 break;
84
85 // Otherwise, do a single-step desugar.
86 QualType Underlying;
87 bool IsSugar = false;
88 switch (Ty->getTypeClass()) {
89#define ABSTRACT_TYPE(Class, Base)
90#define TYPE(Class, Base) \
91 case Type::Class: { \
92 const Class##Type *CTy = cast<Class##Type>(Ty); \
93 if (CTy->isSugared()) { \
94 IsSugar = true; \
95 Underlying = CTy->desugar(); \
96 } \
97 break; \
98 }
99#include "clang/AST/TypeNodes.def"
100 }
101
102 // If it wasn't sugared, we're done.
103 if (!IsSugar)
104 break;
105
106 // If the desugared type is a vector type, we don't want to expand
107 // it, it will turn into an attribute mess. People want their "vec4".
108 if (isa<VectorType>(Underlying))
109 break;
110
111 // Otherwise, we're tearing through something opaque; note that
112 // we'll eventually need an a.k.a. clause and keep going.
113 AKA = true;
114 QT = Underlying;
115 continue;
116 }
117
Chris Lattner0a026af2009-10-20 05:36:05 +0000118 // If we never tore through opaque sugar, don't print aka.
119 if (!AKA) return false;
John McCall49a832b2009-10-18 09:09:24 +0000120
Chris Lattner0a026af2009-10-20 05:36:05 +0000121 // If we did, check to see if we already desugared this type in this
122 // diagnostic. If so, don't do it again.
123 for (unsigned i = 0; i != NumPrevArgs; ++i) {
124 // TODO: Handle ak_declcontext case.
125 if (PrevArgs[i].first == Diagnostic::ak_qualtype) {
126 void *Ptr = (void*)PrevArgs[i].second;
127 QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
128 if (PrevTy == InputTy)
129 return false;
130 }
131 }
132
133 DesugaredQT = Qc.apply(QT);
134 return true;
John McCall49a832b2009-10-18 09:09:24 +0000135}
136
Douglas Gregor3f093272009-10-13 21:16:44 +0000137/// \brief Convert the given type to a string suitable for printing as part of
138/// a diagnostic.
139///
140/// \param Context the context in which the type was allocated
141/// \param Ty the type to print
Chris Lattner0a026af2009-10-20 05:36:05 +0000142static std::string
143ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
144 const Diagnostic::ArgumentValue *PrevArgs,
145 unsigned NumPrevArgs) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000146 // FIXME: Playing with std::string is really slow.
147 std::string S = Ty.getAsString(Context.PrintingPolicy);
148
John McCall49a832b2009-10-18 09:09:24 +0000149 // Consider producing an a.k.a. clause if removing all the direct
150 // sugar gives us something "significantly different".
151
152 QualType DesugaredTy;
Chris Lattner0a026af2009-10-20 05:36:05 +0000153 if (ShouldAKA(Context, Ty, PrevArgs, NumPrevArgs, DesugaredTy)) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000154 S = "'"+S+"' (aka '";
155 S += DesugaredTy.getAsString(Context.PrintingPolicy);
156 S += "')";
157 return S;
158 }
159
160 S = "'" + S + "'";
161 return S;
162}
163
Mike Stump1eb44332009-09-09 15:08:12 +0000164/// ConvertQualTypeToStringFn - This function is used to pretty print the
Chris Lattner22caddc2008-11-23 09:13:29 +0000165/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +0000166static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattnerd0344a42009-02-19 23:45:49 +0000167 const char *Modifier, unsigned ModLen,
168 const char *Argument, unsigned ArgLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000169 const Diagnostic::ArgumentValue *PrevArgs,
170 unsigned NumPrevArgs,
Chris Lattner92dd3862009-02-19 23:53:20 +0000171 llvm::SmallVectorImpl<char> &Output,
172 void *Cookie) {
173 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Chris Lattner011bb4e2008-11-23 20:28:15 +0000175 std::string S;
Douglas Gregor3f093272009-10-13 21:16:44 +0000176 bool NeedQuotes = true;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000177
178 switch (Kind) {
179 default: assert(0 && "unknown ArgumentKind");
180 case Diagnostic::ak_qualtype: {
Chris Lattnerd0344a42009-02-19 23:45:49 +0000181 assert(ModLen == 0 && ArgLen == 0 &&
182 "Invalid modifier for QualType argument");
183
Chris Lattner011bb4e2008-11-23 20:28:15 +0000184 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner0a026af2009-10-20 05:36:05 +0000185 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
Douglas Gregor3f093272009-10-13 21:16:44 +0000186 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000187 break;
188 }
189 case Diagnostic::ak_declarationname: {
Chris Lattner011bb4e2008-11-23 20:28:15 +0000190 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
191 S = N.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattner077bf5e2008-11-24 03:33:13 +0000193 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
194 S = '+' + S;
195 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
196 S = '-' + S;
197 else
198 assert(ModLen == 0 && ArgLen == 0 &&
199 "Invalid modifier for DeclarationName argument");
Chris Lattner9cf9f862009-10-20 05:12:36 +0000200 break;
201 }
202 case Diagnostic::ak_nameddecl: {
John McCall136a6982009-09-11 06:45:03 +0000203 bool Qualified;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000204 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
John McCall136a6982009-09-11 06:45:03 +0000205 Qualified = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000206 else {
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000207 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000208 "Invalid modifier for NamedDecl* argument");
John McCall136a6982009-09-11 06:45:03 +0000209 Qualified = false;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000210 }
Chris Lattner9cf9f862009-10-20 05:12:36 +0000211 reinterpret_cast<NamedDecl*>(Val)->
212 getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
213 break;
214 }
215 case Diagnostic::ak_nestednamespec: {
Douglas Gregordacd4342009-08-26 00:04:55 +0000216 llvm::raw_string_ostream OS(S);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000217 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
218 Context.PrintingPolicy);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000219 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000220 break;
221 }
222 case Diagnostic::ak_declcontext: {
Douglas Gregor3f093272009-10-13 21:16:44 +0000223 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000224 assert(DC && "Should never have a null declaration context");
225
226 if (DC->isTranslationUnit()) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000227 // FIXME: Get these strings from some localized place
228 if (Context.getLangOptions().CPlusPlus)
229 S = "the global namespace";
230 else
231 S = "the global scope";
232 } else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
Chris Lattner0a026af2009-10-20 05:36:05 +0000233 S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type),
234 PrevArgs, NumPrevArgs);
Douglas Gregor3f093272009-10-13 21:16:44 +0000235 } else {
236 // FIXME: Get these strings from some localized place
237 NamedDecl *ND = cast<NamedDecl>(DC);
238 if (isa<NamespaceDecl>(ND))
239 S += "namespace ";
240 else if (isa<ObjCMethodDecl>(ND))
241 S += "method ";
242 else if (isa<FunctionDecl>(ND))
243 S += "function ";
244
245 S += "'";
246 ND->getNameForDiagnostic(S, Context.PrintingPolicy, true);
247 S += "'";
Douglas Gregor3f093272009-10-13 21:16:44 +0000248 }
Chris Lattner9cf9f862009-10-20 05:12:36 +0000249 NeedQuotes = false;
250 break;
251 }
Chris Lattner011bb4e2008-11-23 20:28:15 +0000252 }
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Douglas Gregor3f093272009-10-13 21:16:44 +0000254 if (NeedQuotes)
255 Output.push_back('\'');
256
Chris Lattner22caddc2008-11-23 09:13:29 +0000257 Output.append(S.begin(), S.end());
Douglas Gregor3f093272009-10-13 21:16:44 +0000258
259 if (NeedQuotes)
260 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000261}
262
263
Chris Lattner0a14eee2008-11-18 07:04:44 +0000264static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +0000265 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000266 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +0000267 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +0000268 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000269
Mike Stump1eb44332009-09-09 15:08:12 +0000270 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000271 C.getTranslationUnitDecl(),
272 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +0000273}
274
Steve Naroffb216c882007-10-09 22:01:59 +0000275void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
276 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000277 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattner4d150c82009-04-30 06:18:40 +0000279 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
John McCallba6a9bd2009-10-24 08:00:42 +0000280 DeclaratorInfo *DInfo;
281
Chris Lattner4d150c82009-04-30 06:18:40 +0000282 // Install [u]int128_t for 64-bit targets.
John McCallba6a9bd2009-10-24 08:00:42 +0000283 DInfo = Context.getTrivialDeclaratorInfo(Context.Int128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +0000284 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
285 SourceLocation(),
286 &Context.Idents.get("__int128_t"),
John McCallba6a9bd2009-10-24 08:00:42 +0000287 DInfo), TUScope);
288
289 DInfo = Context.getTrivialDeclaratorInfo(Context.UnsignedInt128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +0000290 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
291 SourceLocation(),
292 &Context.Idents.get("__uint128_t"),
John McCallba6a9bd2009-10-24 08:00:42 +0000293 DInfo), TUScope);
Chris Lattner4d150c82009-04-30 06:18:40 +0000294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
296
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000297 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Steve Naroffcb83c532009-06-16 00:20:10 +0000299 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +0000300 if (Context.getObjCSelType().isNull()) {
301 // Synthesize "typedef struct objc_selector *SEL;"
302 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
303 PushOnScopeChains(SelTag, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Douglas Gregor319ac892009-04-23 22:29:11 +0000305 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
John McCallba6a9bd2009-10-24 08:00:42 +0000306 DeclaratorInfo *SelInfo = Context.getTrivialDeclaratorInfo(SelT);
307 TypedefDecl *SelTypedef
308 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
309 &Context.Idents.get("SEL"), SelInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +0000310 PushOnScopeChains(SelTypedef, TUScope);
311 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
312 }
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));
321 PushOnScopeChains(ProtocolDecl, TUScope);
322 }
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);
326 DeclaratorInfo *IdInfo = Context.getTrivialDeclaratorInfo(IdT);
327 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);
338 DeclaratorInfo *ClassInfo = Context.getTrivialDeclaratorInfo(ClassType);
339 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,
349 bool CompleteTranslationUnit)
Chris Lattner53ebff32009-01-22 19:21:44 +0000350 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump1eb44332009-09-09 15:08:12 +0000351 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Douglas Gregor81b747b2009-09-17 21:32:03 +0000352 ExternalSource(0), CodeCompleter(0), CurContext(0),
John McCall54abf7d2009-11-04 02:18:39 +0000353 PreDeclaratorDC(0), CurBlock(0), PackContext(0), ParsingDeclDepth(0),
Douglas Gregor81b747b2009-09-17 21:32:03 +0000354 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregorac7610d2009-06-22 20:57:11 +0000355 GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000356 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorbb260412009-06-14 08:02:22 +0000357 NumSFINAEErrors(0), CurrentInstantiationScope(0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Steve Naroff3b950172007-10-10 21:53:07 +0000359 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000360 if (getLangOptions().CPlusPlus)
361 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Chris Lattner22caddc2008-11-23 09:13:29 +0000363 // Tell diagnostics how to render things from the AST library.
Chris Lattner92dd3862009-02-19 23:53:20 +0000364 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Reid Spencer5f016e22007-07-11 17:01:13 +0000365}
366
Mike Stump1eb44332009-09-09 15:08:12 +0000367/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000368/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000369/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000370void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonc0a2fd82009-09-15 05:13:45 +0000371 CastExpr::CastKind Kind, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000372 QualType ExprTy = Context.getCanonicalType(Expr->getType());
373 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Mon P Wang3a2c7442008-09-04 08:38:01 +0000375 if (ExprTy == TypeTy)
376 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Mon P Wang3a2c7442008-09-04 08:38:01 +0000378 if (Expr->getType().getTypePtr()->isPointerType() &&
379 Ty.getTypePtr()->isPointerType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000380 QualType ExprBaseType =
Mon P Wang3a2c7442008-09-04 08:38:01 +0000381 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
382 QualType BaseType =
383 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
384 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000385 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
386 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000387 }
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000390 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000391 if (ImpCast->getCastKind() == Kind) {
392 ImpCast->setType(Ty);
393 ImpCast->setLvalueCast(isLvalue);
394 return;
395 }
396 }
397
398 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000399}
400
Chris Lattner394a3fd2007-08-31 04:53:24 +0000401void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000402 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000403}
404void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000405 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000406}
407
Chris Lattner9299f3f2008-08-23 03:19:52 +0000408/// ActOnEndOfTranslationUnit - This is called at the very end of the
409/// translation unit when EOF is reached and all but the top-level scope is
410/// popped.
411void Sema::ActOnEndOfTranslationUnit() {
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000412 // C++: Perform implicit template instantiations.
413 //
414 // FIXME: When we perform these implicit instantiations, we do not carefully
415 // keep track of the point of instantiation (C++ [temp.point]). This means
416 // that name lookup that occurs within the template instantiation will
417 // always happen at the end of the translation unit, so it will find
Mike Stump1eb44332009-09-09 15:08:12 +0000418 // some names that should not be found. Although this is common behavior
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000419 // for C++ compilers, it is technically wrong. In the future, we either need
420 // to be able to filter the results of name lookup or we need to perform
421 // template instantiations earlier.
422 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner63d65f82009-09-08 18:19:27 +0000424 // Check for #pragma weak identifiers that were never declared
425 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
426 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000427 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000428 I = WeakUndeclaredIdentifiers.begin(),
429 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
430 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattner63d65f82009-09-08 18:19:27 +0000432 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
433 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000434 }
435
Douglas Gregorf807fe02009-04-14 16:27:31 +0000436 if (!CompleteTranslationUnit)
437 return;
438
Douglas Gregor275a3692009-03-10 23:43:53 +0000439 // C99 6.9.2p2:
440 // A declaration of an identifier for an object that has file
441 // scope without an initializer, and without a storage-class
442 // specifier or with the storage-class specifier static,
443 // constitutes a tentative definition. If a translation unit
444 // contains one or more tentative definitions for an identifier,
445 // and the translation unit contains no external definition for
446 // that identifier, then the behavior is exactly as if the
447 // translation unit contains a file scope declaration of that
448 // identifier, with the composite type as of the end of the
449 // translation unit, with an initializer equal to 0.
Chris Lattner63d65f82009-09-08 18:19:27 +0000450 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
451 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner63d65f82009-09-08 18:19:27 +0000453 // If the tentative definition was completed, it will be in the list, but
454 // not the map.
455 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000456 continue;
457
Mike Stump1eb44332009-09-09 15:08:12 +0000458 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000459 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000460 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000461 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000462 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000463 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000464 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattner63d65f82009-09-08 18:19:27 +0000467 // Set the length of the array to 1 (C99 6.9.2p5).
468 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
469 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCall46a617a2009-10-16 00:14:28 +0000470 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
471 One, ArrayType::Normal, 0);
Chris Lattner63d65f82009-09-08 18:19:27 +0000472 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000473 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000474 diag::err_tentative_def_incomplete_type))
475 VD->setInvalidDecl();
476
477 // Notify the consumer that we've completed a tentative definition.
478 if (!VD->isInvalidDecl())
479 Consumer.CompleteTentativeDefinition(VD);
480
Douglas Gregor275a3692009-03-10 23:43:53 +0000481 }
Chris Lattner9299f3f2008-08-23 03:19:52 +0000482}
483
484
Reid Spencer5f016e22007-07-11 17:01:13 +0000485//===----------------------------------------------------------------------===//
486// Helper functions.
487//===----------------------------------------------------------------------===//
488
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000489DeclContext *Sema::getFunctionLevelDeclContext() {
Anders Carlssonfb7ef752009-08-08 17:48:49 +0000490 DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000492 while (isa<BlockDecl>(DC))
493 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000495 return DC;
496}
497
Chris Lattner371f2582008-12-04 23:50:19 +0000498/// getCurFunctionDecl - If inside of a function body, this returns a pointer
499/// to the function decl for the function being parsed. If we're currently
500/// in a 'block', this returns the containing context.
501FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000502 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000503 return dyn_cast<FunctionDecl>(DC);
504}
505
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000506ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000507 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000508 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000509}
Chris Lattner371f2582008-12-04 23:50:19 +0000510
511NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000512 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000513 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000514 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000515 return 0;
516}
517
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000518Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000519 if (!this->Emit())
520 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000522 // If this is not a note, and we're in a template instantiation
523 // that is different from the last template instantiation where
524 // we emitted an error, print a template instantiation
525 // backtrace.
526 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
527 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000528 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000529 != SemaRef.LastTemplateInstantiationErrorContext) {
530 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000531 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000532 = SemaRef.ActiveTemplateInstantiations.back();
533 }
534}
Douglas Gregor2e222532009-07-02 17:08:52 +0000535
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000536Sema::SemaDiagnosticBuilder
537Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
538 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
539 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000541 return Builder;
542}
543
Douglas Gregor2e222532009-07-02 17:08:52 +0000544void Sema::ActOnComment(SourceRange Comment) {
545 Context.Comments.push_back(Comment);
546}
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000547