blob: 62c2e25f52a8d426d37da07e6552d7bae1f3c371 [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"
John McCall680523a2009-11-07 03:30:10 +000017#include "llvm/ADT/APFloat.h"
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +000018#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Lex/Preprocessor.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000023#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner4d150c82009-04-30 06:18:40 +000024#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
John McCall49a832b2009-10-18 09:09:24 +000027/// Determines whether we should have an a.k.a. clause when
Chris Lattner0a026af2009-10-20 05:36:05 +000028/// pretty-printing a type. There are three main criteria:
John McCall49a832b2009-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 Lattner0a026af2009-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 McCall49a832b2009-10-18 09:09:24 +000041static bool ShouldAKA(ASTContext &Context, QualType QT,
Chris Lattner0a026af2009-10-20 05:36:05 +000042 const Diagnostic::ArgumentValue *PrevArgs,
43 unsigned NumPrevArgs,
44 QualType &DesugaredQT) {
45 QualType InputTy = QT;
46
John McCall49a832b2009-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 Lattner0a026af2009-10-20 05:36:05 +0000119 // If we never tore through opaque sugar, don't print aka.
120 if (!AKA) return false;
John McCall49a832b2009-10-18 09:09:24 +0000121
Chris Lattner0a026af2009-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 McCall49a832b2009-10-18 09:09:24 +0000136}
137
Douglas Gregor3f093272009-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 Lattner0a026af2009-10-20 05:36:05 +0000143static std::string
144ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
145 const Diagnostic::ArgumentValue *PrevArgs,
146 unsigned NumPrevArgs) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000147 // FIXME: Playing with std::string is really slow.
148 std::string S = Ty.getAsString(Context.PrintingPolicy);
149
John McCall49a832b2009-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 Lattner0a026af2009-10-20 05:36:05 +0000154 if (ShouldAKA(Context, Ty, PrevArgs, NumPrevArgs, DesugaredTy)) {
Douglas Gregor3f093272009-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 Stump1eb44332009-09-09 15:08:12 +0000165/// ConvertQualTypeToStringFn - This function is used to pretty print the
Chris Lattner22caddc2008-11-23 09:13:29 +0000166/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +0000167static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattnerd0344a42009-02-19 23:45:49 +0000168 const char *Modifier, unsigned ModLen,
169 const char *Argument, unsigned ArgLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000170 const Diagnostic::ArgumentValue *PrevArgs,
171 unsigned NumPrevArgs,
Chris Lattner92dd3862009-02-19 23:53:20 +0000172 llvm::SmallVectorImpl<char> &Output,
173 void *Cookie) {
174 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattner011bb4e2008-11-23 20:28:15 +0000176 std::string S;
Douglas Gregor3f093272009-10-13 21:16:44 +0000177 bool NeedQuotes = true;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000178
179 switch (Kind) {
180 default: assert(0 && "unknown ArgumentKind");
181 case Diagnostic::ak_qualtype: {
Chris Lattnerd0344a42009-02-19 23:45:49 +0000182 assert(ModLen == 0 && ArgLen == 0 &&
183 "Invalid modifier for QualType argument");
184
Chris Lattner011bb4e2008-11-23 20:28:15 +0000185 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner0a026af2009-10-20 05:36:05 +0000186 S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs);
Douglas Gregor3f093272009-10-13 21:16:44 +0000187 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000188 break;
189 }
190 case Diagnostic::ak_declarationname: {
Chris Lattner011bb4e2008-11-23 20:28:15 +0000191 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
192 S = N.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Chris Lattner077bf5e2008-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 Lattner9cf9f862009-10-20 05:12:36 +0000201 break;
202 }
203 case Diagnostic::ak_nameddecl: {
John McCall136a6982009-09-11 06:45:03 +0000204 bool Qualified;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000205 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
John McCall136a6982009-09-11 06:45:03 +0000206 Qualified = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000207 else {
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000208 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000209 "Invalid modifier for NamedDecl* argument");
John McCall136a6982009-09-11 06:45:03 +0000210 Qualified = false;
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000211 }
Chris Lattner9cf9f862009-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 Gregordacd4342009-08-26 00:04:55 +0000217 llvm::raw_string_ostream OS(S);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000218 reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
219 Context.PrintingPolicy);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000220 NeedQuotes = false;
Chris Lattner9cf9f862009-10-20 05:12:36 +0000221 break;
222 }
223 case Diagnostic::ak_declcontext: {
Douglas Gregor3f093272009-10-13 21:16:44 +0000224 DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
Chris Lattner9cf9f862009-10-20 05:12:36 +0000225 assert(DC && "Should never have a null declaration context");
226
227 if (DC->isTranslationUnit()) {
Douglas Gregor3f093272009-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 Lattner0a026af2009-10-20 05:36:05 +0000234 S = ConvertTypeToDiagnosticString(Context, Context.getTypeDeclType(Type),
235 PrevArgs, NumPrevArgs);
Douglas Gregor3f093272009-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 Gregor3f093272009-10-13 21:16:44 +0000249 }
Chris Lattner9cf9f862009-10-20 05:12:36 +0000250 NeedQuotes = false;
251 break;
252 }
Chris Lattner011bb4e2008-11-23 20:28:15 +0000253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Douglas Gregor3f093272009-10-13 21:16:44 +0000255 if (NeedQuotes)
256 Output.push_back('\'');
257
Chris Lattner22caddc2008-11-23 09:13:29 +0000258 Output.append(S.begin(), S.end());
Douglas Gregor3f093272009-10-13 21:16:44 +0000259
260 if (NeedQuotes)
261 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000262}
263
264
Chris Lattner0a14eee2008-11-18 07:04:44 +0000265static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +0000266 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000267 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +0000268 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +0000269 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000270
Mike Stump1eb44332009-09-09 15:08:12 +0000271 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000272 C.getTranslationUnitDecl(),
273 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +0000274}
275
Steve Naroffb216c882007-10-09 22:01:59 +0000276void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
277 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000278 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattner4d150c82009-04-30 06:18:40 +0000280 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
John McCallba6a9bd2009-10-24 08:00:42 +0000281 DeclaratorInfo *DInfo;
282
Chris Lattner4d150c82009-04-30 06:18:40 +0000283 // Install [u]int128_t for 64-bit targets.
John McCallba6a9bd2009-10-24 08:00:42 +0000284 DInfo = Context.getTrivialDeclaratorInfo(Context.Int128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +0000285 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
286 SourceLocation(),
287 &Context.Idents.get("__int128_t"),
John McCallba6a9bd2009-10-24 08:00:42 +0000288 DInfo), TUScope);
289
290 DInfo = Context.getTrivialDeclaratorInfo(Context.UnsignedInt128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +0000291 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
292 SourceLocation(),
293 &Context.Idents.get("__uint128_t"),
John McCallba6a9bd2009-10-24 08:00:42 +0000294 DInfo), TUScope);
Chris Lattner4d150c82009-04-30 06:18:40 +0000295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
297
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000298 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Steve Naroffcb83c532009-06-16 00:20:10 +0000300 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +0000301 if (Context.getObjCSelType().isNull()) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000302 // Create the built-in typedef for 'SEL'.
303 QualType SelT = Context.getObjCObjectPointerType(Context.ObjCBuiltinSelTy);
John McCallba6a9bd2009-10-24 08:00:42 +0000304 DeclaratorInfo *SelInfo = Context.getTrivialDeclaratorInfo(SelT);
305 TypedefDecl *SelTypedef
306 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
307 &Context.Idents.get("SEL"), SelInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +0000308 PushOnScopeChains(SelTypedef, TUScope);
309 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000310 Context.ObjCSELRedefinitionType = Context.getObjCSelType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000311 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000312
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000313 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +0000314 if (Context.getObjCProtoType().isNull()) {
315 ObjCInterfaceDecl *ProtocolDecl =
316 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000317 &Context.Idents.get("Protocol"),
Douglas Gregor319ac892009-04-23 22:29:11 +0000318 SourceLocation(), true);
319 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian10324db2009-11-18 23:15:37 +0000320 PushOnScopeChains(ProtocolDecl, TUScope, false);
Douglas Gregor319ac892009-04-23 22:29:11 +0000321 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000322 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +0000323 if (Context.getObjCIdType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000324 QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy);
325 DeclaratorInfo *IdInfo = Context.getTrivialDeclaratorInfo(IdT);
326 TypedefDecl *IdTypedef
327 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
328 &Context.Idents.get("id"), IdInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +0000329 PushOnScopeChains(IdTypedef, TUScope);
330 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000331 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000332 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000333 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +0000334 if (Context.getObjCClassType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000335 QualType ClassType
336 = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy);
337 DeclaratorInfo *ClassInfo = Context.getTrivialDeclaratorInfo(ClassType);
338 TypedefDecl *ClassTypedef
339 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
340 &Context.Idents.get("Class"), ClassInfo);
Steve Naroff14108da2009-07-10 23:34:53 +0000341 PushOnScopeChains(ClassTypedef, TUScope);
342 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000343 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000344 }
Steve Naroff3b950172007-10-10 21:53:07 +0000345}
346
Douglas Gregorf807fe02009-04-14 16:27:31 +0000347Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000348 bool CompleteTranslationUnit,
349 CodeCompleteConsumer *CodeCompleter)
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()),
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000352 ExternalSource(0), CodeCompleter(CodeCompleter), 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 Gregorf35f8282009-11-11 21:54:23 +0000357 NumSFINAEErrors(0), NonInstantiationEntries(0),
358 CurrentInstantiationScope(0)
359{
Steve Naroff3b950172007-10-10 21:53:07 +0000360 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000361 if (getLangOptions().CPlusPlus)
362 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattner22caddc2008-11-23 09:13:29 +0000364 // Tell diagnostics how to render things from the AST library.
Chris Lattner92dd3862009-02-19 23:53:20 +0000365 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Reid Spencer5f016e22007-07-11 17:01:13 +0000366}
367
John McCall680523a2009-11-07 03:30:10 +0000368/// Retrieves the width and signedness of the given integer type,
369/// or returns false if it is not an integer type.
John McCalle8babd12009-11-07 08:15:46 +0000370///
371/// \param T must be canonical
John McCall680523a2009-11-07 03:30:10 +0000372static bool getIntProperties(ASTContext &C, const Type *T,
373 unsigned &BitWidth, bool &Signed) {
John McCalle8babd12009-11-07 08:15:46 +0000374 assert(T->isCanonicalUnqualified());
375
376 if (const VectorType *VT = dyn_cast<VectorType>(T))
377 T = VT->getElementType().getTypePtr();
378 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
379 T = CT->getElementType().getTypePtr();
380
John McCall680523a2009-11-07 03:30:10 +0000381 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) {
382 if (!BT->isInteger()) return false;
383
384 BitWidth = C.getIntWidth(QualType(T, 0));
385 Signed = BT->isSignedInteger();
386 return true;
387 }
388
389 if (const FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) {
390 BitWidth = FWIT->getWidth();
391 Signed = FWIT->isSigned();
392 return true;
393 }
394
395 return false;
396}
397
398/// Checks whether the given value will have the same value if it it
399/// is truncated to the given width, then extended back to the
400/// original width.
401static bool IsSameIntAfterCast(const llvm::APSInt &value,
John McCalle8babd12009-11-07 08:15:46 +0000402 unsigned TargetWidth) {
403 unsigned SourceWidth = value.getBitWidth();
John McCall680523a2009-11-07 03:30:10 +0000404 llvm::APSInt truncated = value;
405 truncated.trunc(TargetWidth);
406 truncated.extend(SourceWidth);
407 return (truncated == value);
408}
409
410/// Checks whether the given value will have the same value if it
411/// is truncated to the given width, then extended back to the original
412/// width.
413///
414/// The value might be a vector or a complex.
John McCalle8babd12009-11-07 08:15:46 +0000415static bool IsSameIntAfterCast(const APValue &value, unsigned TargetWidth) {
John McCall680523a2009-11-07 03:30:10 +0000416 if (value.isInt())
John McCalle8babd12009-11-07 08:15:46 +0000417 return IsSameIntAfterCast(value.getInt(), TargetWidth);
John McCall680523a2009-11-07 03:30:10 +0000418
419 if (value.isVector()) {
420 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
John McCalle8babd12009-11-07 08:15:46 +0000421 if (!IsSameIntAfterCast(value.getVectorElt(i), TargetWidth))
John McCall680523a2009-11-07 03:30:10 +0000422 return false;
423 return true;
424 }
425
John McCall8406aed2009-11-11 22:52:37 +0000426 if (value.isComplexInt()) {
427 return IsSameIntAfterCast(value.getComplexIntReal(), TargetWidth) &&
428 IsSameIntAfterCast(value.getComplexIntImag(), TargetWidth);
429 }
430
431 // This can happen with lossless casts to intptr_t of "based" lvalues.
432 // Assume it might use arbitrary bits.
433 assert(value.isLValue());
434 return false;
John McCall680523a2009-11-07 03:30:10 +0000435}
436
437
438/// Checks whether the given value, which currently has the given
439/// source semantics, has the same value when coerced through the
440/// target semantics.
441static bool IsSameFloatAfterCast(const llvm::APFloat &value,
442 const llvm::fltSemantics &Src,
443 const llvm::fltSemantics &Tgt) {
444 llvm::APFloat truncated = value;
445
446 bool ignored;
447 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
448 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
449
450 return truncated.bitwiseIsEqual(value);
451}
452
453/// Checks whether the given value, which currently has the given
454/// source semantics, has the same value when coerced through the
455/// target semantics.
456///
457/// The value might be a vector of floats (or a complex number).
458static bool IsSameFloatAfterCast(const APValue &value,
459 const llvm::fltSemantics &Src,
460 const llvm::fltSemantics &Tgt) {
461 if (value.isFloat())
462 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
463
464 if (value.isVector()) {
465 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
466 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
467 return false;
468 return true;
469 }
470
471 assert(value.isComplexFloat());
472 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
473 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
474}
475
John McCalle8babd12009-11-07 08:15:46 +0000476/// Determines if it's reasonable for the given expression to be truncated
477/// down to the given integer width.
478/// * Boolean expressions are automatically white-listed.
479/// * Arithmetic operations on implicitly-promoted operands of the
480/// target width or less are okay --- not because the results are
481/// actually guaranteed to fit within the width, but because the
482/// user is effectively pretending that the operations are closed
483/// within the implicitly-promoted type.
484static bool IsExprValueWithinWidth(ASTContext &C, Expr *E, unsigned Width) {
485 E = E->IgnoreParens();
486
487#ifndef NDEBUG
488 {
489 const Type *ETy = E->getType()->getCanonicalTypeInternal().getTypePtr();
490 unsigned EWidth;
491 bool ESigned;
492
493 if (!getIntProperties(C, ETy, EWidth, ESigned))
494 assert(0 && "expression not of integer type");
495
496 // The caller should never let this happen.
497 assert(EWidth > Width && "called on expr whose type is too small");
498 }
499#endif
500
501 // Strip implicit casts off.
502 while (isa<ImplicitCastExpr>(E)) {
503 E = cast<ImplicitCastExpr>(E)->getSubExpr();
504
505 const Type *ETy = E->getType()->getCanonicalTypeInternal().getTypePtr();
506
507 unsigned EWidth;
508 bool ESigned;
509 if (!getIntProperties(C, ETy, EWidth, ESigned))
510 return false;
511
512 if (EWidth <= Width)
513 return true;
514 }
515
516 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
517 switch (BO->getOpcode()) {
518
519 // Boolean-valued operations are white-listed.
520 case BinaryOperator::LAnd:
521 case BinaryOperator::LOr:
522 case BinaryOperator::LT:
523 case BinaryOperator::GT:
524 case BinaryOperator::LE:
525 case BinaryOperator::GE:
526 case BinaryOperator::EQ:
527 case BinaryOperator::NE:
528 return true;
529
530 // Operations with opaque sources are black-listed.
531 case BinaryOperator::PtrMemD:
532 case BinaryOperator::PtrMemI:
533 return false;
534
535 // Left shift gets black-listed based on a judgement call.
536 case BinaryOperator::Shl:
537 return false;
538
539 // Various special cases.
540 case BinaryOperator::Shr:
541 return IsExprValueWithinWidth(C, BO->getLHS(), Width);
542 case BinaryOperator::Comma:
543 return IsExprValueWithinWidth(C, BO->getRHS(), Width);
544 case BinaryOperator::Sub:
545 if (BO->getLHS()->getType()->isPointerType())
546 return false;
547 // fallthrough
548
549 // Any other operator is okay if the operands are
550 // promoted from expressions of appropriate size.
551 default:
552 return IsExprValueWithinWidth(C, BO->getLHS(), Width) &&
553 IsExprValueWithinWidth(C, BO->getRHS(), Width);
554 }
555 }
556
557 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
558 switch (UO->getOpcode()) {
559 // Boolean-valued operations are white-listed.
560 case UnaryOperator::LNot:
561 return true;
562
563 // Operations with opaque sources are black-listed.
564 case UnaryOperator::Deref:
565 case UnaryOperator::AddrOf: // should be impossible
566 return false;
567
568 case UnaryOperator::OffsetOf:
569 return false;
570
571 default:
572 return IsExprValueWithinWidth(C, UO->getSubExpr(), Width);
573 }
574 }
575
576 // Don't diagnose if the expression is an integer constant
577 // whose value in the target type is the same as it was
578 // in the original type.
579 Expr::EvalResult result;
580 if (E->Evaluate(result, C))
581 if (IsSameIntAfterCast(result.Val, Width))
582 return true;
583
584 return false;
585}
586
John McCall680523a2009-11-07 03:30:10 +0000587/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
588static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, unsigned diag) {
589 S.Diag(E->getExprLoc(), diag) << E->getType() << T << E->getSourceRange();
590}
591
592/// Implements -Wconversion.
593static void CheckImplicitConversion(Sema &S, Expr *E, QualType T) {
594 // Don't diagnose in unevaluated contexts.
595 if (S.ExprEvalContext == Sema::Unevaluated)
596 return;
597
598 // Don't diagnose for value-dependent expressions.
599 if (E->isValueDependent())
600 return;
601
602 const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
603 const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
604
605 // Never diagnose implicit casts to bool.
606 if (Target->isSpecificBuiltinType(BuiltinType::Bool))
607 return;
608
609 // Strip vector types.
610 if (isa<VectorType>(Source)) {
611 if (!isa<VectorType>(Target))
612 return DiagnoseImpCast(S, E, T, diag::warn_impcast_vector_scalar);
613
614 Source = cast<VectorType>(Source)->getElementType().getTypePtr();
615 Target = cast<VectorType>(Target)->getElementType().getTypePtr();
616 }
617
618 // Strip complex types.
619 if (isa<ComplexType>(Source)) {
620 if (!isa<ComplexType>(Target))
621 return DiagnoseImpCast(S, E, T, diag::warn_impcast_complex_scalar);
622
623 Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
624 Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
625 }
626
627 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
628 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
629
630 // If the source is floating point...
631 if (SourceBT && SourceBT->isFloatingPoint()) {
632 // ...and the target is floating point...
633 if (TargetBT && TargetBT->isFloatingPoint()) {
634 // ...then warn if we're dropping FP rank.
635
636 // Builtin FP kinds are ordered by increasing FP rank.
637 if (SourceBT->getKind() > TargetBT->getKind()) {
638 // Don't warn about float constants that are precisely
639 // representable in the target type.
640 Expr::EvalResult result;
641 if (E->Evaluate(result, S.Context)) {
642 // Value might be a float, a float vector, or a float complex.
643 if (IsSameFloatAfterCast(result.Val,
644 S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
645 S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
646 return;
647 }
648
649 DiagnoseImpCast(S, E, T, diag::warn_impcast_float_precision);
650 }
651 return;
652 }
653
654 // If the target is integral, always warn.
655 if ((TargetBT && TargetBT->isInteger()) ||
656 isa<FixedWidthIntType>(Target))
657 // TODO: don't warn for integer values?
658 return DiagnoseImpCast(S, E, T, diag::warn_impcast_float_integer);
659
660 return;
661 }
662
663 unsigned SourceWidth, TargetWidth;
664 bool SourceSigned, TargetSigned;
665
666 if (!getIntProperties(S.Context, Source, SourceWidth, SourceSigned) ||
667 !getIntProperties(S.Context, Target, TargetWidth, TargetSigned))
668 return;
669
670 if (SourceWidth > TargetWidth) {
John McCalle8babd12009-11-07 08:15:46 +0000671 if (IsExprValueWithinWidth(S.Context, E, TargetWidth))
672 return;
John McCall680523a2009-11-07 03:30:10 +0000673
John McCalldc767a12009-11-07 09:03:53 +0000674 // People want to build with -Wshorten-64-to-32 and not -Wconversion
675 // and by god we'll let them.
676 if (SourceWidth == 64 && TargetWidth == 32)
677 return DiagnoseImpCast(S, E, T, diag::warn_impcast_integer_64_32);
John McCall680523a2009-11-07 03:30:10 +0000678 return DiagnoseImpCast(S, E, T, diag::warn_impcast_integer_precision);
679 }
680
681 return;
682}
683
Mike Stump1eb44332009-09-09 15:08:12 +0000684/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000685/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000686/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000687void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonc0a2fd82009-09-15 05:13:45 +0000688 CastExpr::CastKind Kind, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000689 QualType ExprTy = Context.getCanonicalType(Expr->getType());
690 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Mon P Wang3a2c7442008-09-04 08:38:01 +0000692 if (ExprTy == TypeTy)
693 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000694
John McCall680523a2009-11-07 03:30:10 +0000695 if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
696 QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
697 QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000698 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000699 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
700 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000701 }
702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
John McCall680523a2009-11-07 03:30:10 +0000704 CheckImplicitConversion(*this, Expr, Ty);
705
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000706 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000707 if (ImpCast->getCastKind() == Kind) {
708 ImpCast->setType(Ty);
709 ImpCast->setLvalueCast(isLvalue);
710 return;
711 }
712 }
713
714 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000715}
716
Chris Lattner394a3fd2007-08-31 04:53:24 +0000717void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000718 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000719}
720void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000721 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000722}
723
Chris Lattner9299f3f2008-08-23 03:19:52 +0000724/// ActOnEndOfTranslationUnit - This is called at the very end of the
725/// translation unit when EOF is reached and all but the top-level scope is
726/// popped.
727void Sema::ActOnEndOfTranslationUnit() {
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000728 // C++: Perform implicit template instantiations.
729 //
730 // FIXME: When we perform these implicit instantiations, we do not carefully
731 // keep track of the point of instantiation (C++ [temp.point]). This means
732 // that name lookup that occurs within the template instantiation will
733 // always happen at the end of the translation unit, so it will find
Mike Stump1eb44332009-09-09 15:08:12 +0000734 // some names that should not be found. Although this is common behavior
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000735 // for C++ compilers, it is technically wrong. In the future, we either need
736 // to be able to filter the results of name lookup or we need to perform
737 // template instantiations earlier.
738 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Chris Lattner63d65f82009-09-08 18:19:27 +0000740 // Check for #pragma weak identifiers that were never declared
741 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
742 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000743 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000744 I = WeakUndeclaredIdentifiers.begin(),
745 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
746 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattner63d65f82009-09-08 18:19:27 +0000748 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
749 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000750 }
751
Douglas Gregorf807fe02009-04-14 16:27:31 +0000752 if (!CompleteTranslationUnit)
753 return;
754
Douglas Gregor275a3692009-03-10 23:43:53 +0000755 // C99 6.9.2p2:
756 // A declaration of an identifier for an object that has file
757 // scope without an initializer, and without a storage-class
758 // specifier or with the storage-class specifier static,
759 // constitutes a tentative definition. If a translation unit
760 // contains one or more tentative definitions for an identifier,
761 // and the translation unit contains no external definition for
762 // that identifier, then the behavior is exactly as if the
763 // translation unit contains a file scope declaration of that
764 // identifier, with the composite type as of the end of the
765 // translation unit, with an initializer equal to 0.
Chris Lattner63d65f82009-09-08 18:19:27 +0000766 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
767 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattner63d65f82009-09-08 18:19:27 +0000769 // If the tentative definition was completed, it will be in the list, but
770 // not the map.
771 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000772 continue;
773
Mike Stump1eb44332009-09-09 15:08:12 +0000774 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000775 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000776 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000777 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000778 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000779 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000780 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattner63d65f82009-09-08 18:19:27 +0000783 // Set the length of the array to 1 (C99 6.9.2p5).
784 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
785 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCall46a617a2009-10-16 00:14:28 +0000786 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
787 One, ArrayType::Normal, 0);
Chris Lattner63d65f82009-09-08 18:19:27 +0000788 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000789 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000790 diag::err_tentative_def_incomplete_type))
791 VD->setInvalidDecl();
792
793 // Notify the consumer that we've completed a tentative definition.
794 if (!VD->isInvalidDecl())
795 Consumer.CompleteTentativeDefinition(VD);
796
Douglas Gregor275a3692009-03-10 23:43:53 +0000797 }
Chris Lattner9299f3f2008-08-23 03:19:52 +0000798}
799
800
Reid Spencer5f016e22007-07-11 17:01:13 +0000801//===----------------------------------------------------------------------===//
802// Helper functions.
803//===----------------------------------------------------------------------===//
804
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000805DeclContext *Sema::getFunctionLevelDeclContext() {
Anders Carlssonfb7ef752009-08-08 17:48:49 +0000806 DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000808 while (isa<BlockDecl>(DC))
809 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000811 return DC;
812}
813
Chris Lattner371f2582008-12-04 23:50:19 +0000814/// getCurFunctionDecl - If inside of a function body, this returns a pointer
815/// to the function decl for the function being parsed. If we're currently
816/// in a 'block', this returns the containing context.
817FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000818 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000819 return dyn_cast<FunctionDecl>(DC);
820}
821
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000822ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000823 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000824 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000825}
Chris Lattner371f2582008-12-04 23:50:19 +0000826
827NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000828 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000829 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000830 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000831 return 0;
832}
833
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000834Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000835 if (!this->Emit())
836 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000838 // If this is not a note, and we're in a template instantiation
839 // that is different from the last template instantiation where
840 // we emitted an error, print a template instantiation
841 // backtrace.
842 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
843 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000844 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000845 != SemaRef.LastTemplateInstantiationErrorContext) {
846 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000847 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000848 = SemaRef.ActiveTemplateInstantiations.back();
849 }
850}
Douglas Gregor2e222532009-07-02 17:08:52 +0000851
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000852Sema::SemaDiagnosticBuilder
853Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
854 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
855 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000857 return Builder;
858}
859
Douglas Gregor2e222532009-07-02 17:08:52 +0000860void Sema::ActOnComment(SourceRange Comment) {
861 Context.Comments.push_back(Comment);
862}
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000863