blob: 6975da0b3e346bb2adc8666c99ae562913f7cab7 [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 Gregor9ea9bdb2010-03-01 23:15:13 +000029
30FunctionScopeInfo::~FunctionScopeInfo() { }
31
32void FunctionScopeInfo::Clear(unsigned NumErrors) {
33 NeedsScopeChecking = false;
34 LabelMap.clear();
35 SwitchStack.clear();
36 NumErrorsAtStartOfFunction = NumErrors;
37}
38
39BlockScopeInfo::~BlockScopeInfo() { }
40
Chris Lattner0a14eee2008-11-18 07:04:44 +000041static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +000042 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +000043 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +000044 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +000045 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +000046
Mike Stump1eb44332009-09-09 15:08:12 +000047 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +000048 C.getTranslationUnitDecl(),
49 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +000050}
51
Steve Naroffb216c882007-10-09 22:01:59 +000052void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
53 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +000054 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000055
Chris Lattner4d150c82009-04-30 06:18:40 +000056 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
John McCalla93c9342009-12-07 02:54:59 +000057 TypeSourceInfo *TInfo;
John McCallba6a9bd2009-10-24 08:00:42 +000058
Chris Lattner4d150c82009-04-30 06:18:40 +000059 // Install [u]int128_t for 64-bit targets.
John McCalla93c9342009-12-07 02:54:59 +000060 TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +000061 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
62 SourceLocation(),
63 &Context.Idents.get("__int128_t"),
John McCalla93c9342009-12-07 02:54:59 +000064 TInfo), TUScope);
John McCallba6a9bd2009-10-24 08:00:42 +000065
John McCalla93c9342009-12-07 02:54:59 +000066 TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
Chris Lattner4d150c82009-04-30 06:18:40 +000067 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
68 SourceLocation(),
69 &Context.Idents.get("__uint128_t"),
John McCalla93c9342009-12-07 02:54:59 +000070 TInfo), TUScope);
Chris Lattner4d150c82009-04-30 06:18:40 +000071 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
73
Chris Lattner2ae34ed2008-02-06 00:46:58 +000074 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +000075
Steve Naroffcb83c532009-06-16 00:20:10 +000076 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +000077 if (Context.getObjCSelType().isNull()) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +000078 // Create the built-in typedef for 'SEL'.
Fariborz Jahanian04765ac2009-11-23 18:04:25 +000079 QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
John McCalla93c9342009-12-07 02:54:59 +000080 TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
John McCallba6a9bd2009-10-24 08:00:42 +000081 TypedefDecl *SelTypedef
82 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
83 &Context.Idents.get("SEL"), SelInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +000084 PushOnScopeChains(SelTypedef, TUScope);
85 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +000086 Context.ObjCSelRedefinitionType = Context.getObjCSelType();
Douglas Gregor319ac892009-04-23 22:29:11 +000087 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000088
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000089 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +000090 if (Context.getObjCProtoType().isNull()) {
91 ObjCInterfaceDecl *ProtocolDecl =
92 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +000093 &Context.Idents.get("Protocol"),
Douglas Gregor319ac892009-04-23 22:29:11 +000094 SourceLocation(), true);
95 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian10324db2009-11-18 23:15:37 +000096 PushOnScopeChains(ProtocolDecl, TUScope, false);
Douglas Gregor319ac892009-04-23 22:29:11 +000097 }
Steve Naroffde2e22d2009-07-15 18:40:39 +000098 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +000099 if (Context.getObjCIdType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000100 QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy);
John McCalla93c9342009-12-07 02:54:59 +0000101 TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(IdT);
John McCallba6a9bd2009-10-24 08:00:42 +0000102 TypedefDecl *IdTypedef
103 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
104 &Context.Idents.get("id"), IdInfo);
Douglas Gregor319ac892009-04-23 22:29:11 +0000105 PushOnScopeChains(IdTypedef, TUScope);
106 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000107 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000108 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000109 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +0000110 if (Context.getObjCClassType().isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000111 QualType ClassType
112 = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy);
John McCalla93c9342009-12-07 02:54:59 +0000113 TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(ClassType);
John McCallba6a9bd2009-10-24 08:00:42 +0000114 TypedefDecl *ClassTypedef
115 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
116 &Context.Idents.get("Class"), ClassInfo);
Steve Naroff14108da2009-07-10 23:34:53 +0000117 PushOnScopeChains(ClassTypedef, TUScope);
118 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000119 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000120 }
Steve Naroff3b950172007-10-10 21:53:07 +0000121}
122
Douglas Gregorf807fe02009-04-14 16:27:31 +0000123Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000124 bool CompleteTranslationUnit,
125 CodeCompleteConsumer *CodeCompleter)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000126 : TheTargetAttributesSema(0),
127 LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump1eb44332009-09-09 15:08:12 +0000128 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000129 ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000130 PackContext(0), TopFunctionScope(0), ParsingDeclDepth(0),
Douglas Gregor81b747b2009-09-17 21:32:03 +0000131 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregor2afce722009-11-26 00:44:06 +0000132 GlobalNewDeleteDeclared(false),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000133 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorf35f8282009-11-11 21:54:23 +0000134 NumSFINAEErrors(0), NonInstantiationEntries(0),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000135 CurrentInstantiationScope(0), TyposCorrected(0),
136 AnalysisWarnings(*this)
Douglas Gregorf35f8282009-11-11 21:54:23 +0000137{
Steve Naroff3b950172007-10-10 21:53:07 +0000138 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000139 if (getLangOptions().CPlusPlus)
140 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattner22caddc2008-11-23 09:13:29 +0000142 // Tell diagnostics how to render things from the AST library.
Douglas Gregor79a9a342010-02-09 22:26:47 +0000143 PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
144 &Context);
Douglas Gregor2afce722009-11-26 00:44:06 +0000145
146 ExprEvalContexts.push_back(
147 ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
Reid Spencer5f016e22007-07-11 17:01:13 +0000148}
149
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000150Sema::~Sema() {
151 if (PackContext) FreePackedContext();
152 delete TheTargetAttributesSema;
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000153 while (!FunctionScopes.empty())
154 PopFunctionOrBlockScope();
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000155}
156
Mike Stump1eb44332009-09-09 15:08:12 +0000157/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000158/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000159/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000160void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlsson88465d32010-04-23 22:18:37 +0000161 CastExpr::CastKind Kind,
Anders Carlssonf1b48b72010-04-24 16:57:13 +0000162 bool isLvalue, CXXBaseSpecifierArray BasePath) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000163 QualType ExprTy = Context.getCanonicalType(Expr->getType());
164 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Mon P Wang3a2c7442008-09-04 08:38:01 +0000166 if (ExprTy == TypeTy)
167 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000168
John McCall680523a2009-11-07 03:30:10 +0000169 if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
170 QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
171 QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000172 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000173 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
174 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000175 }
176 }
Mike Stump1eb44332009-09-09 15:08:12 +0000177
John McCall51313c32010-01-04 23:31:57 +0000178 CheckImplicitConversion(Expr, Ty);
John McCall680523a2009-11-07 03:30:10 +0000179
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000180 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000181 if (ImpCast->getCastKind() == Kind) {
Anders Carlssonf1b48b72010-04-24 16:57:13 +0000182 assert(BasePath.empty() && "FIXME: Merge paths!");
Anders Carlsson4c5fad32009-09-15 05:28:24 +0000183 ImpCast->setType(Ty);
184 ImpCast->setLvalueCast(isLvalue);
185 return;
186 }
187 }
188
Anders Carlsson7ab9d572010-04-24 16:34:21 +0000189 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, BasePath, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000190}
191
Chris Lattner394a3fd2007-08-31 04:53:24 +0000192void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000193 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000194}
195void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000196 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000197}
198
Chris Lattner9299f3f2008-08-23 03:19:52 +0000199/// ActOnEndOfTranslationUnit - This is called at the very end of the
200/// translation unit when EOF is reached and all but the top-level scope is
201/// popped.
Douglas Gregor47268a32010-04-09 17:41:13 +0000202void Sema::ActOnEndOfTranslationUnit() {
Anders Carlssond6a637f2009-12-07 08:24:59 +0000203 while (1) {
204 // C++: Perform implicit template instantiations.
205 //
206 // FIXME: When we perform these implicit instantiations, we do not carefully
207 // keep track of the point of instantiation (C++ [temp.point]). This means
208 // that name lookup that occurs within the template instantiation will
209 // always happen at the end of the translation unit, so it will find
210 // some names that should not be found. Although this is common behavior
211 // for C++ compilers, it is technically wrong. In the future, we either need
212 // to be able to filter the results of name lookup or we need to perform
213 // template instantiations earlier.
214 PerformPendingImplicitInstantiations();
215
216 /// If ProcessPendingClassesWithUnmarkedVirtualMembers ends up marking
217 /// any virtual member functions it might lead to more pending template
218 /// instantiations, which is why we need to loop here.
219 if (!ProcessPendingClassesWithUnmarkedVirtualMembers())
220 break;
221 }
222
Douglas Gregor47268a32010-04-09 17:41:13 +0000223 // Remove functions that turned out to be used.
224 UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(),
225 UnusedStaticFuncs.end(),
226 std::mem_fun(&FunctionDecl::isUsed)),
227 UnusedStaticFuncs.end());
228
Chris Lattner63d65f82009-09-08 18:19:27 +0000229 // Check for #pragma weak identifiers that were never declared
230 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
231 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000232 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000233 I = WeakUndeclaredIdentifiers.begin(),
234 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
235 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattner63d65f82009-09-08 18:19:27 +0000237 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
238 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000239 }
240
Douglas Gregorf807fe02009-04-14 16:27:31 +0000241 if (!CompleteTranslationUnit)
242 return;
243
Douglas Gregor275a3692009-03-10 23:43:53 +0000244 // C99 6.9.2p2:
245 // A declaration of an identifier for an object that has file
246 // scope without an initializer, and without a storage-class
247 // specifier or with the storage-class specifier static,
248 // constitutes a tentative definition. If a translation unit
249 // contains one or more tentative definitions for an identifier,
250 // and the translation unit contains no external definition for
251 // that identifier, then the behavior is exactly as if the
252 // translation unit contains a file scope declaration of that
253 // identifier, with the composite type as of the end of the
254 // translation unit, with an initializer equal to 0.
Sebastian Redle9d12b62010-01-31 22:27:38 +0000255 llvm::SmallSet<VarDecl *, 32> Seen;
256 for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
257 VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Sebastian Redle9d12b62010-01-31 22:27:38 +0000259 // If the tentative definition was completed, getActingDefinition() returns
260 // null. If we've already seen this variable before, insert()'s second
261 // return value is false.
262 if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000263 continue;
264
Mike Stump1eb44332009-09-09 15:08:12 +0000265 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000266 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000267 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000268 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000269 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000270 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000271 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000272 }
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattner63d65f82009-09-08 18:19:27 +0000274 // Set the length of the array to 1 (C99 6.9.2p5).
275 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
276 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCall46a617a2009-10-16 00:14:28 +0000277 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
278 One, ArrayType::Normal, 0);
Chris Lattner63d65f82009-09-08 18:19:27 +0000279 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000280 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000281 diag::err_tentative_def_incomplete_type))
282 VD->setInvalidDecl();
283
284 // Notify the consumer that we've completed a tentative definition.
285 if (!VD->isInvalidDecl())
286 Consumer.CompleteTentativeDefinition(VD);
287
Douglas Gregor275a3692009-03-10 23:43:53 +0000288 }
Tanya Lattnere6bbc012010-02-12 00:07:30 +0000289
290 // Output warning for unused functions.
291 for (std::vector<FunctionDecl*>::iterator
292 F = UnusedStaticFuncs.begin(),
293 FEnd = UnusedStaticFuncs.end();
294 F != FEnd;
295 ++F)
296 Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName();
297
Chris Lattner9299f3f2008-08-23 03:19:52 +0000298}
299
300
Reid Spencer5f016e22007-07-11 17:01:13 +0000301//===----------------------------------------------------------------------===//
302// Helper functions.
303//===----------------------------------------------------------------------===//
304
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000305DeclContext *Sema::getFunctionLevelDeclContext() {
John McCalldb0ee1d2009-12-19 10:53:49 +0000306 DeclContext *DC = CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000308 while (isa<BlockDecl>(DC))
309 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000311 return DC;
312}
313
Chris Lattner371f2582008-12-04 23:50:19 +0000314/// getCurFunctionDecl - If inside of a function body, this returns a pointer
315/// to the function decl for the function being parsed. If we're currently
316/// in a 'block', this returns the containing context.
317FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000318 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000319 return dyn_cast<FunctionDecl>(DC);
320}
321
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000322ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000323 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000324 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000325}
Chris Lattner371f2582008-12-04 23:50:19 +0000326
327NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000328 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000329 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000330 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000331 return 0;
332}
333
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000334Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000335 if (!this->Emit())
336 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000338 // If this is not a note, and we're in a template instantiation
339 // that is different from the last template instantiation where
340 // we emitted an error, print a template instantiation
341 // backtrace.
342 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
343 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000344 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000345 != SemaRef.LastTemplateInstantiationErrorContext) {
346 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000347 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000348 = SemaRef.ActiveTemplateInstantiations.back();
349 }
350}
Douglas Gregor2e222532009-07-02 17:08:52 +0000351
Douglas Gregoreab5d1e2010-03-25 22:17:48 +0000352Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) {
353 if (isSFINAEContext()) {
354 switch (Diagnostic::getDiagnosticSFINAEResponse(DiagID)) {
355 case Diagnostic::SFINAE_Report:
356 // Fall through; we'll report the diagnostic below.
357 break;
358
359 case Diagnostic::SFINAE_SubstitutionFailure:
360 // Count this failure so that we know that template argument deduction
361 // has failed.
362 ++NumSFINAEErrors;
363 // Fall through
364
365 case Diagnostic::SFINAE_Suppress:
366 // Suppress this diagnostic.
367 Diags.setLastDiagnosticIgnored();
368 return SemaDiagnosticBuilder(*this);
369 }
370 }
371
372 DiagnosticBuilder DB = Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
373 return SemaDiagnosticBuilder(DB, *this, DiagID);
374}
375
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000376Sema::SemaDiagnosticBuilder
377Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
378 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
379 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000381 return Builder;
382}
383
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000384
385/// \brief Enter a new function scope
386void Sema::PushFunctionScope() {
387 if (FunctionScopes.empty()) {
388 // Use the "top" function scope rather than having to allocate memory for
389 // a new scope.
390 TopFunctionScope.Clear(getDiagnostics().getNumErrors());
391 FunctionScopes.push_back(&TopFunctionScope);
392 return;
393 }
394
395 FunctionScopes.push_back(
396 new FunctionScopeInfo(getDiagnostics().getNumErrors()));
397}
398
399void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
400 FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics().getNumErrors(),
401 BlockScope, Block));
402}
403
404void Sema::PopFunctionOrBlockScope() {
405 if (FunctionScopes.back() != &TopFunctionScope)
406 delete FunctionScopes.back();
407 else
408 TopFunctionScope.Clear(getDiagnostics().getNumErrors());
409
410 FunctionScopes.pop_back();
411}
412
413/// \brief Determine whether any errors occurred within this function/method/
414/// block.
415bool Sema::hasAnyErrorsInThisFunction() const {
416 unsigned NumErrors = TopFunctionScope.NumErrorsAtStartOfFunction;
417 if (!FunctionScopes.empty())
418 NumErrors = FunctionScopes.back()->NumErrorsAtStartOfFunction;
419 return NumErrors != getDiagnostics().getNumErrors();
420}
421
422BlockScopeInfo *Sema::getCurBlock() {
423 if (FunctionScopes.empty())
424 return 0;
425
426 return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
427}