blob: 24c5e09f434b8250dc9a64a53107cf5b31d909d4 [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"
Chris Lattner4d150c82009-04-30 06:18:40 +000022#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Chris Lattner22caddc2008-11-23 09:13:29 +000025/// ConvertQualTypeToStringFn - This function is used to pretty print the
26/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +000027static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattnerd0344a42009-02-19 23:45:49 +000028 const char *Modifier, unsigned ModLen,
29 const char *Argument, unsigned ArgLen,
Chris Lattner92dd3862009-02-19 23:53:20 +000030 llvm::SmallVectorImpl<char> &Output,
31 void *Cookie) {
32 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Chris Lattner3fdf4b02008-11-23 09:21:17 +000033
Chris Lattner011bb4e2008-11-23 20:28:15 +000034 std::string S;
35 if (Kind == Diagnostic::ak_qualtype) {
Chris Lattnerd0344a42009-02-19 23:45:49 +000036 assert(ModLen == 0 && ArgLen == 0 &&
37 "Invalid modifier for QualType argument");
38
Chris Lattner011bb4e2008-11-23 20:28:15 +000039 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner077bf5e2008-11-24 03:33:13 +000040
Chris Lattner011bb4e2008-11-23 20:28:15 +000041 // FIXME: Playing with std::string is really slow.
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000042 S = Ty.getAsString(Context.PrintingPolicy);
Chris Lattnerd0344a42009-02-19 23:45:49 +000043
44 // If this is a sugared type (like a typedef, typeof, etc), then unwrap one
45 // level of the sugar so that the type is more obvious to the user.
Douglas Gregor969c6892009-04-01 15:47:24 +000046 QualType DesugaredTy = Ty->getDesugaredType(true);
Chris Lattnerd0344a42009-02-19 23:45:49 +000047 DesugaredTy.setCVRQualifiers(DesugaredTy.getCVRQualifiers() |
48 Ty.getCVRQualifiers());
Chris Lattner077bf5e2008-11-24 03:33:13 +000049
Chris Lattnerd0344a42009-02-19 23:45:49 +000050 if (Ty != DesugaredTy &&
51 // If the desugared type is a vector type, we don't want to expand it,
52 // it will turn into an attribute mess. People want their "vec4".
53 !isa<VectorType>(DesugaredTy) &&
54
Chris Lattner92dd3862009-02-19 23:53:20 +000055 // Don't desugar magic Objective-C types.
56 Ty.getUnqualifiedType() != Context.getObjCIdType() &&
Steve Naroff14108da2009-07-10 23:34:53 +000057 Ty.getUnqualifiedType() != Context.getObjCClassType() &&
Chris Lattner92dd3862009-02-19 23:53:20 +000058 Ty.getUnqualifiedType() != Context.getObjCSelType() &&
59 Ty.getUnqualifiedType() != Context.getObjCProtoType() &&
Chris Lattner92dd3862009-02-19 23:53:20 +000060
61 // Not va_list.
62 Ty.getUnqualifiedType() != Context.getBuiltinVaListType()) {
Chris Lattnerd0344a42009-02-19 23:45:49 +000063 S = "'"+S+"' (aka '";
Argyrios Kyrtzidis7c94c4b2009-06-03 02:06:50 +000064 S += DesugaredTy.getAsString(Context.PrintingPolicy);
Chris Lattnerd0344a42009-02-19 23:45:49 +000065 S += "')";
66 Output.append(S.begin(), S.end());
67 return;
68 }
Chris Lattner077bf5e2008-11-24 03:33:13 +000069
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000070 } else if (Kind == Diagnostic::ak_declarationname) {
Chris Lattner011bb4e2008-11-23 20:28:15 +000071
72 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
73 S = N.getAsString();
Chris Lattner077bf5e2008-11-24 03:33:13 +000074
75 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
76 S = '+' + S;
77 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
78 S = '-' + S;
79 else
80 assert(ModLen == 0 && ArgLen == 0 &&
81 "Invalid modifier for DeclarationName argument");
Douglas Gregordacd4342009-08-26 00:04:55 +000082 } else if (Kind == Diagnostic::ak_nameddecl) {
Douglas Gregoreeb15d42009-02-04 22:46:25 +000083 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
84 S = reinterpret_cast<NamedDecl*>(Val)->getQualifiedNameAsString();
85 else {
86 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000087 "Invalid modifier for NamedDecl* argument");
Douglas Gregoreeb15d42009-02-04 22:46:25 +000088 S = reinterpret_cast<NamedDecl*>(Val)->getNameAsString();
89 }
Douglas Gregordacd4342009-08-26 00:04:55 +000090 } else {
91 llvm::raw_string_ostream OS(S);
92 assert(Kind == Diagnostic::ak_nestednamespec);
93 reinterpret_cast<NestedNameSpecifier*> (Val)->print(OS,
94 Context.PrintingPolicy);
Chris Lattner011bb4e2008-11-23 20:28:15 +000095 }
Chris Lattnerd0344a42009-02-19 23:45:49 +000096
97 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +000098 Output.append(S.begin(), S.end());
Chris Lattnerd0344a42009-02-19 23:45:49 +000099 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000100}
101
102
Chris Lattner0a14eee2008-11-18 07:04:44 +0000103static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +0000104 if (C.getLangOptions().CPlusPlus)
105 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
106 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +0000107 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000108
109 return RecordDecl::Create(C, TagDecl::TK_struct,
110 C.getTranslationUnitDecl(),
111 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +0000112}
113
Steve Naroffb216c882007-10-09 22:01:59 +0000114void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
115 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000116 PushDeclContext(S, Context.getTranslationUnitDecl());
Chris Lattner2df9ced2009-04-30 02:43:43 +0000117
Chris Lattner4d150c82009-04-30 06:18:40 +0000118 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
119 // Install [u]int128_t for 64-bit targets.
120 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
121 SourceLocation(),
122 &Context.Idents.get("__int128_t"),
123 Context.Int128Ty), TUScope);
124 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
125 SourceLocation(),
126 &Context.Idents.get("__uint128_t"),
127 Context.UnsignedInt128Ty), TUScope);
128 }
Chris Lattner2df9ced2009-04-30 02:43:43 +0000129
130
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000131 if (!PP.getLangOptions().ObjC1) return;
132
Steve Naroffcb83c532009-06-16 00:20:10 +0000133 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +0000134 if (Context.getObjCSelType().isNull()) {
135 // Synthesize "typedef struct objc_selector *SEL;"
136 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
137 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +0000138
Douglas Gregor319ac892009-04-23 22:29:11 +0000139 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
140 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
141 SourceLocation(),
142 &Context.Idents.get("SEL"),
143 SelT);
144 PushOnScopeChains(SelTypedef, TUScope);
145 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
146 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000147
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000148 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +0000149 if (Context.getObjCProtoType().isNull()) {
150 ObjCInterfaceDecl *ProtocolDecl =
151 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
152 &Context.Idents.get("Protocol"),
153 SourceLocation(), true);
154 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
155 PushOnScopeChains(ProtocolDecl, TUScope);
156 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000157 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +0000158 if (Context.getObjCIdType().isNull()) {
Steve Naroffde2e22d2009-07-15 18:40:39 +0000159 TypedefDecl *IdTypedef =
160 TypedefDecl::Create(
161 Context, CurContext, SourceLocation(), &Context.Idents.get("id"),
162 Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy)
163 );
Douglas Gregor319ac892009-04-23 22:29:11 +0000164 PushOnScopeChains(IdTypedef, TUScope);
165 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000166 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000167 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000168 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +0000169 if (Context.getObjCClassType().isNull()) {
Steve Naroffde2e22d2009-07-15 18:40:39 +0000170 TypedefDecl *ClassTypedef =
171 TypedefDecl::Create(
172 Context, CurContext, SourceLocation(), &Context.Idents.get("Class"),
173 Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy)
174 );
Steve Naroff14108da2009-07-10 23:34:53 +0000175 PushOnScopeChains(ClassTypedef, TUScope);
176 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000177 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000178 }
Steve Naroff3b950172007-10-10 21:53:07 +0000179}
180
Douglas Gregorf807fe02009-04-14 16:27:31 +0000181Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
182 bool CompleteTranslationUnit)
Chris Lattner53ebff32009-01-22 19:21:44 +0000183 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000184 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
185 ExternalSource(0), CurContext(0), PreDeclaratorDC(0),
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000186 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()),
Douglas Gregorac7610d2009-06-22 20:57:11 +0000187 GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000188 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorbb260412009-06-14 08:02:22 +0000189 NumSFINAEErrors(0), CurrentInstantiationScope(0) {
Chris Lattner59907c42007-08-10 20:18:51 +0000190
Sebastian Redlc42e1182008-11-11 11:37:55 +0000191 StdNamespace = 0;
Steve Naroff3b950172007-10-10 21:53:07 +0000192 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000193 if (getLangOptions().CPlusPlus)
194 FieldCollector.reset(new CXXFieldCollector());
Chris Lattner22caddc2008-11-23 09:13:29 +0000195
196 // Tell diagnostics how to render things from the AST library.
Chris Lattner92dd3862009-02-19 23:53:20 +0000197 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
Chris Lattner1e0a3902008-01-16 19:17:22 +0000200/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
201/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000202/// If isLvalue, the result of the cast is an lvalue.
Anders Carlsson3503d042009-07-31 01:23:52 +0000203void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonbd0fb302009-08-10 21:30:22 +0000204 const CastExpr::CastInfo &Info, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000205 QualType ExprTy = Context.getCanonicalType(Expr->getType());
206 QualType TypeTy = Context.getCanonicalType(Ty);
207
208 if (ExprTy == TypeTy)
209 return;
210
211 if (Expr->getType().getTypePtr()->isPointerType() &&
212 Ty.getTypePtr()->isPointerType()) {
213 QualType ExprBaseType =
214 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
215 QualType BaseType =
216 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
217 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000218 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
219 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000220 }
221 }
Chris Lattner1e0a3902008-01-16 19:17:22 +0000222
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000223 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000224 ImpCast->setType(Ty);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000225 ImpCast->setLvalueCast(isLvalue);
226 } else
Anders Carlssonbd0fb302009-08-10 21:30:22 +0000227 Expr = new (Context) ImplicitCastExpr(Ty, Info, Expr,
Anders Carlssoncdef2b72009-07-31 00:48:10 +0000228 isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000229}
230
Chris Lattner394a3fd2007-08-31 04:53:24 +0000231void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000232 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000233}
234void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000235 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000236}
237
Chris Lattner9299f3f2008-08-23 03:19:52 +0000238/// ActOnEndOfTranslationUnit - This is called at the very end of the
239/// translation unit when EOF is reached and all but the top-level scope is
240/// popped.
241void Sema::ActOnEndOfTranslationUnit() {
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000242 // C++: Perform implicit template instantiations.
243 //
244 // FIXME: When we perform these implicit instantiations, we do not carefully
245 // keep track of the point of instantiation (C++ [temp.point]). This means
246 // that name lookup that occurs within the template instantiation will
247 // always happen at the end of the translation unit, so it will find
248 // some names that should not be found. Although this is common behavior
249 // for C++ compilers, it is technically wrong. In the future, we either need
250 // to be able to filter the results of name lookup or we need to perform
251 // template instantiations earlier.
252 PerformPendingImplicitInstantiations();
253
Ryan Flynne25ff832009-07-30 03:15:39 +0000254 // check for #pragma weak identifiers that were never declared
255 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
256 I = WeakUndeclaredIdentifiers.begin(),
257 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
258 if (!I->second.getUsed())
259 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
260 << I->first;
261 }
262
Douglas Gregorf807fe02009-04-14 16:27:31 +0000263 if (!CompleteTranslationUnit)
264 return;
265
Douglas Gregor275a3692009-03-10 23:43:53 +0000266 // C99 6.9.2p2:
267 // A declaration of an identifier for an object that has file
268 // scope without an initializer, and without a storage-class
269 // specifier or with the storage-class specifier static,
270 // constitutes a tentative definition. If a translation unit
271 // contains one or more tentative definitions for an identifier,
272 // and the translation unit contains no external definition for
273 // that identifier, then the behavior is exactly as if the
274 // translation unit contains a file scope declaration of that
275 // identifier, with the composite type as of the end of the
276 // translation unit, with an initializer equal to 0.
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000277 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
278 D = TentativeDefinitions.begin(),
279 DEnd = TentativeDefinitions.end();
280 D != DEnd; ++D) {
281 VarDecl *VD = D->second;
Chris Lattner9299f3f2008-08-23 03:19:52 +0000282
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000283 if (VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
284 continue;
285
286 if (const IncompleteArrayType *ArrayT
287 = Context.getAsIncompleteArrayType(VD->getType())) {
288 if (RequireCompleteType(VD->getLocation(),
289 ArrayT->getElementType(),
290 diag::err_tentative_def_incomplete_type_arr))
291 VD->setInvalidDecl();
292 else {
293 // Set the length of the array to 1 (C99 6.9.2p5).
294 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
295 llvm::APInt One(Context.getTypeSize(Context.getSizeType()),
296 true);
297 QualType T
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000298 = Context.getConstantArrayWithoutExprType(ArrayT->getElementType(),
299 One, ArrayType::Normal, 0);
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000300 VD->setType(T);
Douglas Gregor275a3692009-03-10 23:43:53 +0000301 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000302 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
303 diag::err_tentative_def_incomplete_type))
304 VD->setInvalidDecl();
305
306 // Notify the consumer that we've completed a tentative definition.
307 if (!VD->isInvalidDecl())
308 Consumer.CompleteTentativeDefinition(VD);
309
Douglas Gregor275a3692009-03-10 23:43:53 +0000310 }
Chris Lattner9299f3f2008-08-23 03:19:52 +0000311}
312
313
Reid Spencer5f016e22007-07-11 17:01:13 +0000314//===----------------------------------------------------------------------===//
315// Helper functions.
316//===----------------------------------------------------------------------===//
317
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000318DeclContext *Sema::getFunctionLevelDeclContext() {
Anders Carlssonfb7ef752009-08-08 17:48:49 +0000319 DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext;
320
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000321 while (isa<BlockDecl>(DC))
322 DC = DC->getParent();
323
324 return DC;
325}
326
Chris Lattner371f2582008-12-04 23:50:19 +0000327/// getCurFunctionDecl - If inside of a function body, this returns a pointer
328/// to the function decl for the function being parsed. If we're currently
329/// in a 'block', this returns the containing context.
330FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000331 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000332 return dyn_cast<FunctionDecl>(DC);
333}
334
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000335ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000336 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000337 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000338}
Chris Lattner371f2582008-12-04 23:50:19 +0000339
340NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000341 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000342 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000343 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000344 return 0;
345}
346
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000347Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000348 if (!this->Emit())
349 return;
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000350
351 // If this is not a note, and we're in a template instantiation
352 // that is different from the last template instantiation where
353 // we emitted an error, print a template instantiation
354 // backtrace.
355 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
356 !SemaRef.ActiveTemplateInstantiations.empty() &&
357 SemaRef.ActiveTemplateInstantiations.back()
358 != SemaRef.LastTemplateInstantiationErrorContext) {
359 SemaRef.PrintInstantiationStack();
360 SemaRef.LastTemplateInstantiationErrorContext
361 = SemaRef.ActiveTemplateInstantiations.back();
362 }
363}
Douglas Gregor2e222532009-07-02 17:08:52 +0000364
365void Sema::ActOnComment(SourceRange Comment) {
366 Context.Comments.push_back(Comment);
367}