blob: 38c842eede5711aef0388156c2525bb6582571df [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"
Sebastian Redle9d12b62010-01-31 22:27:38 +000018#include "llvm/ADT/SmallSet.h"
John McCall680523a2009-11-07 03:30:10 +000019#include "llvm/ADT/APFloat.h"
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +000020#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor79a9a342010-02-09 22:26:47 +000022#include "clang/AST/ASTDiagnostic.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000023#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000024#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "clang/Lex/Preprocessor.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000026#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner4d150c82009-04-30 06:18:40 +000027#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
Douglas Gregor3f093272009-10-13 21:16:44 +000029
Chris Lattner0a14eee2008-11-18 07:04:44 +000030static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +000031 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +000032 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +000033 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +000034 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +000035
Mike Stump1eb44332009-09-09 15:08:12 +000036 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +000037 C.getTranslationUnitDecl(),
38 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +000039}
40
Steve Naroffb216c882007-10-09 22:01:59 +000041void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
42 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +000043 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000044
Chris Lattner4d150c82009-04-30 06:18:40 +000045 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
John McCalla93c9342009-12-07 02:54:59 +000046 TypeSourceInfo *TInfo;
John McCallba6a9bd2009-10-24 08:00:42 +000047
Chris Lattner4d150c82009-04-30 06:18:40 +000048 // Install [u]int128_t for 64-bit targets.
John McCalla93c9342009-12-07 02:54:59 +000049 TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +000050 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
51 SourceLocation(),
52 &Context.Idents.get("__int128_t"),
John McCalla93c9342009-12-07 02:54:59 +000053 TInfo), TUScope);
John McCallba6a9bd2009-10-24 08:00:42 +000054
John McCalla93c9342009-12-07 02:54:59 +000055 TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +000056 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
57 SourceLocation(),
58 &Context.Idents.get("__uint128_t"),
John McCalla93c9342009-12-07 02:54:59 +000059 TInfo), TUScope);
Chris Lattner4d150c82009-04-30 06:18:40 +000060 }
Mike Stump1eb44332009-09-09 15:08:12 +000061
62
Chris Lattner2ae34ed2008-02-06 00:46:58 +000063 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Steve Naroffcb83c532009-06-16 00:20:10 +000065 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +000066 if (Context.getObjCSelType().isNull()) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +000067 // Create the built-in typedef for 'SEL'.
Fariborz Jahanian04765ac2009-11-23 18:04:25 +000068 QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
John McCalla93c9342009-12-07 02:54:59 +000069 TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
John McCallba6a9bd2009-10-24 08:00:42 +000070 TypedefDecl *SelTypedef
71 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
72 &Context.Idents.get("SEL"), SelInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +000073 PushOnScopeChains(SelTypedef, TUScope);
74 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +000075 Context.ObjCSelRedefinitionType = Context.getObjCSelType();
Douglas Gregor319ac892009-04-23 22:29:11 +000076 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000077
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000078 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +000079 if (Context.getObjCProtoType().isNull()) {
80 ObjCInterfaceDecl *ProtocolDecl =
81 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +000082 &Context.Idents.get("Protocol"),
Douglas Gregor319ac892009-04-23 22:29:11 +000083 SourceLocation(), true);
84 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian10324db2009-11-18 23:15:37 +000085 PushOnScopeChains(ProtocolDecl, TUScope, false);
Douglas Gregor319ac892009-04-23 22:29:11 +000086 }
Steve Naroffde2e22d2009-07-15 18:40:39 +000087 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +000088 if (Context.getObjCIdType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +000089 QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy);
John McCalla93c9342009-12-07 02:54:59 +000090 TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(IdT);
John McCallba6a9bd2009-10-24 08:00:42 +000091 TypedefDecl *IdTypedef
92 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
93 &Context.Idents.get("id"), IdInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +000094 PushOnScopeChains(IdTypedef, TUScope);
95 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +000096 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +000097 }
Steve Naroffde2e22d2009-07-15 18:40:39 +000098 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +000099 if (Context.getObjCClassType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000100 QualType ClassType
101 = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy);
John McCalla93c9342009-12-07 02:54:59 +0000102 TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(ClassType);
John McCallba6a9bd2009-10-24 08:00:42 +0000103 TypedefDecl *ClassTypedef
104 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
105 &Context.Idents.get("Class"), ClassInfo);
Steve Naroff14108da2009-07-10 23:34:53 +0000106 PushOnScopeChains(ClassTypedef, TUScope);
107 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000108 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000109 }
Steve Naroff3b950172007-10-10 21:53:07 +0000110}
111
Douglas Gregorf807fe02009-04-14 16:27:31 +0000112Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000113 bool CompleteTranslationUnit,
114 CodeCompleteConsumer *CodeCompleter)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000115 : TheTargetAttributesSema(0),
116 LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump1eb44332009-09-09 15:08:12 +0000117 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000118 ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
John McCalldb0ee1d2009-12-19 10:53:49 +0000119 CurBlock(0), PackContext(0), ParsingDeclDepth(0),
Douglas Gregor81b747b2009-09-17 21:32:03 +0000120 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregor2afce722009-11-26 00:44:06 +0000121 GlobalNewDeleteDeclared(false),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000122 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorf35f8282009-11-11 21:54:23 +0000123 NumSFINAEErrors(0), NonInstantiationEntries(0),
Ted Kremenekd0ed4482010-02-02 02:07:01 +0000124 CurrentInstantiationScope(0), TyposCorrected(0)
Douglas Gregorf35f8282009-11-11 21:54:23 +0000125{
Steve Naroff3b950172007-10-10 21:53:07 +0000126 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000127 if (getLangOptions().CPlusPlus)
128 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Chris Lattner22caddc2008-11-23 09:13:29 +0000130 // Tell diagnostics how to render things from the AST library.
Douglas Gregor79a9a342010-02-09 22:26:47 +0000131 PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
132 &Context);
Douglas Gregor2afce722009-11-26 00:44:06 +0000133
134 ExprEvalContexts.push_back(
135 ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
Reid Spencer5f016e22007-07-11 17:01:13 +0000136}
137
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000138Sema::~Sema() {
139 if (PackContext) FreePackedContext();
140 delete TheTargetAttributesSema;
141}
142
Mike Stump1eb44332009-09-09 15:08:12 +0000143/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000144/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000145/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000146void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonc0a2fd82009-09-15 05:13:45 +0000147 CastExpr::CastKind Kind, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000148 QualType ExprTy = Context.getCanonicalType(Expr->getType());
149 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Mon P Wang3a2c7442008-09-04 08:38:01 +0000151 if (ExprTy == TypeTy)
152 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000153
John McCall680523a2009-11-07 03:30:10 +0000154 if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
155 QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
156 QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000157 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000158 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
159 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000160 }
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
John McCall51313c32010-01-04 23:31:57 +0000163 CheckImplicitConversion(Expr, Ty);
John McCall680523a2009-11-07 03:30:10 +0000164
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000165 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000166 if (ImpCast->getCastKind() == Kind) {
167 ImpCast->setType(Ty);
168 ImpCast->setLvalueCast(isLvalue);
169 return;
170 }
171 }
172
173 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000174}
175
Chris Lattner394a3fd2007-08-31 04:53:24 +0000176void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000177 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000178}
179void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000180 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000181}
182
Chris Lattner9299f3f2008-08-23 03:19:52 +0000183/// ActOnEndOfTranslationUnit - This is called at the very end of the
184/// translation unit when EOF is reached and all but the top-level scope is
185/// popped.
186void Sema::ActOnEndOfTranslationUnit() {
Anders Carlssond6a637f2009-12-07 08:24:59 +0000187
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000188 // Remove functions that turned out to be used.
189 UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(),
190 UnusedStaticFuncs.end(),
191 std::mem_fun(&FunctionDecl::isUsed)),
192 UnusedStaticFuncs.end());
193
Anders Carlssond6a637f2009-12-07 08:24:59 +0000194 while (1) {
195 // C++: Perform implicit template instantiations.
196 //
197 // FIXME: When we perform these implicit instantiations, we do not carefully
198 // keep track of the point of instantiation (C++ [temp.point]). This means
199 // that name lookup that occurs within the template instantiation will
200 // always happen at the end of the translation unit, so it will find
201 // some names that should not be found. Although this is common behavior
202 // for C++ compilers, it is technically wrong. In the future, we either need
203 // to be able to filter the results of name lookup or we need to perform
204 // template instantiations earlier.
205 PerformPendingImplicitInstantiations();
206
207 /// If ProcessPendingClassesWithUnmarkedVirtualMembers ends up marking
208 /// any virtual member functions it might lead to more pending template
209 /// instantiations, which is why we need to loop here.
210 if (!ProcessPendingClassesWithUnmarkedVirtualMembers())
211 break;
212 }
213
Chris Lattner63d65f82009-09-08 18:19:27 +0000214 // Check for #pragma weak identifiers that were never declared
215 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
216 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000217 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000218 I = WeakUndeclaredIdentifiers.begin(),
219 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
220 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattner63d65f82009-09-08 18:19:27 +0000222 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
223 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000224 }
225
Douglas Gregorf807fe02009-04-14 16:27:31 +0000226 if (!CompleteTranslationUnit)
227 return;
228
Douglas Gregor275a3692009-03-10 23:43:53 +0000229 // C99 6.9.2p2:
230 // A declaration of an identifier for an object that has file
231 // scope without an initializer, and without a storage-class
232 // specifier or with the storage-class specifier static,
233 // constitutes a tentative definition. If a translation unit
234 // contains one or more tentative definitions for an identifier,
235 // and the translation unit contains no external definition for
236 // that identifier, then the behavior is exactly as if the
237 // translation unit contains a file scope declaration of that
238 // identifier, with the composite type as of the end of the
239 // translation unit, with an initializer equal to 0.
Sebastian Redle9d12b62010-01-31 22:27:38 +0000240 llvm::SmallSet<VarDecl *, 32> Seen;
241 for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
242 VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Sebastian Redle9d12b62010-01-31 22:27:38 +0000244 // If the tentative definition was completed, getActingDefinition() returns
245 // null. If we've already seen this variable before, insert()'s second
246 // return value is false.
247 if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000248 continue;
249
Mike Stump1eb44332009-09-09 15:08:12 +0000250 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000251 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000252 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000253 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000254 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000255 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000256 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattner63d65f82009-09-08 18:19:27 +0000259 // Set the length of the array to 1 (C99 6.9.2p5).
260 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
261 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCall46a617a2009-10-16 00:14:28 +0000262 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
263 One, ArrayType::Normal, 0);
Chris Lattner63d65f82009-09-08 18:19:27 +0000264 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000265 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000266 diag::err_tentative_def_incomplete_type))
267 VD->setInvalidDecl();
268
269 // Notify the consumer that we've completed a tentative definition.
270 if (!VD->isInvalidDecl())
271 Consumer.CompleteTentativeDefinition(VD);
272
Douglas Gregor275a3692009-03-10 23:43:53 +0000273 }
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000274
275 // Output warning for unused functions.
276 for (std::vector<FunctionDecl*>::iterator
277 F = UnusedStaticFuncs.begin(),
278 FEnd = UnusedStaticFuncs.end();
279 F != FEnd;
280 ++F)
281 Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName();
282
Chris Lattner9299f3f2008-08-23 03:19:52 +0000283}
284
285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286//===----------------------------------------------------------------------===//
287// Helper functions.
288//===----------------------------------------------------------------------===//
289
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000290DeclContext *Sema::getFunctionLevelDeclContext() {
John McCalldb0ee1d2009-12-19 10:53:49 +0000291 DeclContext *DC = CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000293 while (isa<BlockDecl>(DC))
294 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000296 return DC;
297}
298
Chris Lattner371f2582008-12-04 23:50:19 +0000299/// getCurFunctionDecl - If inside of a function body, this returns a pointer
300/// to the function decl for the function being parsed. If we're currently
301/// in a 'block', this returns the containing context.
302FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000303 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000304 return dyn_cast<FunctionDecl>(DC);
305}
306
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000307ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000308 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000309 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000310}
Chris Lattner371f2582008-12-04 23:50:19 +0000311
312NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000313 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000314 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000315 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000316 return 0;
317}
318
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000319Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000320 if (!this->Emit())
321 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000323 // If this is not a note, and we're in a template instantiation
324 // that is different from the last template instantiation where
325 // we emitted an error, print a template instantiation
326 // backtrace.
327 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
328 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000329 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000330 != SemaRef.LastTemplateInstantiationErrorContext) {
331 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000332 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000333 = SemaRef.ActiveTemplateInstantiations.back();
334 }
335}
Douglas Gregor2e222532009-07-02 17:08:52 +0000336
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000337Sema::SemaDiagnosticBuilder
338Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
339 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
340 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000342 return Builder;
343}
344
Douglas Gregor2e222532009-07-02 17:08:52 +0000345void Sema::ActOnComment(SourceRange Comment) {
346 Context.Comments.push_back(Comment);
347}
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000348