blob: 33a111f395ca79f959c78fb7d759fbcc8582bf9f [file] [log] [blame]
Chris Lattnerddd6fc82006-11-10 04:58:55 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerddd6fc82006-11-10 04:58:55 +000010// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
Chris Lattner3e7bd4e2006-08-17 05:51:27 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerddd6fc82006-11-10 04:58:55 +000015#include "Sema.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000016#include "TargetAttributesSema.h"
Ryan Flynn7d470f32009-07-30 03:15:39 +000017#include "llvm/ADT/DenseMap.h"
Sebastian Redl35351a92010-01-31 22:27:38 +000018#include "llvm/ADT/SmallSet.h"
John McCallfceb64b2009-11-07 03:30:10 +000019#include "llvm/ADT/APFloat.h"
John McCall75b960e2010-06-01 09:23:16 +000020#include "clang/Sema/ExternalSemaSource.h"
Douglas Gregorbeecd582009-04-21 17:11:58 +000021#include "clang/AST/ASTConsumer.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000022#include "clang/AST/ASTContext.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000023#include "clang/AST/ASTDiagnostic.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000024#include "clang/AST/DeclObjC.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000025#include "clang/AST/Expr.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000026#include "clang/Lex/Preprocessor.h"
Anders Carlssonf68079e2009-08-26 22:33:56 +000027#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner7d4f5c42009-04-30 06:18:40 +000028#include "clang/Basic/TargetInfo.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000029using namespace clang;
Douglas Gregor9a28e842010-03-01 23:15:13 +000030
31FunctionScopeInfo::~FunctionScopeInfo() { }
32
33void FunctionScopeInfo::Clear(unsigned NumErrors) {
34 NeedsScopeChecking = false;
35 LabelMap.clear();
36 SwitchStack.clear();
Douglas Gregor6fd1b182010-05-15 06:01:05 +000037 Returns.clear();
Douglas Gregor9a28e842010-03-01 23:15:13 +000038 NumErrorsAtStartOfFunction = NumErrors;
39}
40
41BlockScopeInfo::~BlockScopeInfo() { }
42
Steve Naroffc62adb62007-10-09 22:01:59 +000043void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
44 TUScope = S;
Douglas Gregor91f84212008-12-11 16:49:14 +000045 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump11289f42009-09-09 15:08:12 +000046
John McCallf24d7bb2010-05-28 18:45:08 +000047 VAListTagName = PP.getIdentifierInfo("__va_list_tag");
48
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +000049 if (!Context.isInt128Installed() && // May be set by PCHReader.
50 PP.getTargetInfo().getPointerWidth(0) >= 64) {
John McCallbcd03502009-12-07 02:54:59 +000051 TypeSourceInfo *TInfo;
John McCall703a3f82009-10-24 08:00:42 +000052
Chris Lattner7d4f5c42009-04-30 06:18:40 +000053 // Install [u]int128_t for 64-bit targets.
John McCallbcd03502009-12-07 02:54:59 +000054 TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
Chris Lattner7d4f5c42009-04-30 06:18:40 +000055 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
56 SourceLocation(),
57 &Context.Idents.get("__int128_t"),
John McCallbcd03502009-12-07 02:54:59 +000058 TInfo), TUScope);
John McCall703a3f82009-10-24 08:00:42 +000059
John McCallbcd03502009-12-07 02:54:59 +000060 TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
Chris Lattner7d4f5c42009-04-30 06:18:40 +000061 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
62 SourceLocation(),
63 &Context.Idents.get("__uint128_t"),
John McCallbcd03502009-12-07 02:54:59 +000064 TInfo), TUScope);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +000065 Context.setInt128Installed();
Chris Lattner7d4f5c42009-04-30 06:18:40 +000066 }
Mike Stump11289f42009-09-09 15:08:12 +000067
68
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000069 if (!PP.getLangOptions().ObjC1) return;
Mike Stump11289f42009-09-09 15:08:12 +000070
Steve Naroff853308d2009-06-16 00:20:10 +000071 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor512b0772009-04-23 22:29:11 +000072 if (Context.getObjCSelType().isNull()) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +000073 // Create the built-in typedef for 'SEL'.
Fariborz Jahanian0afc5552009-11-23 18:04:25 +000074 QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
John McCallbcd03502009-12-07 02:54:59 +000075 TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
John McCall703a3f82009-10-24 08:00:42 +000076 TypedefDecl *SelTypedef
77 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
78 &Context.Idents.get("SEL"), SelInfo);
Douglas Gregor512b0772009-04-23 22:29:11 +000079 PushOnScopeChains(SelTypedef, TUScope);
80 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
Fariborz Jahanian04b258c2009-11-25 23:07:42 +000081 Context.ObjCSelRedefinitionType = Context.getObjCSelType();
Douglas Gregor512b0772009-04-23 22:29:11 +000082 }
Chris Lattner5a92bab2008-06-21 20:20:39 +000083
Chris Lattner5a92bab2008-06-21 20:20:39 +000084 // Synthesize "@class Protocol;
Douglas Gregor512b0772009-04-23 22:29:11 +000085 if (Context.getObjCProtoType().isNull()) {
86 ObjCInterfaceDecl *ProtocolDecl =
87 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump11289f42009-09-09 15:08:12 +000088 &Context.Idents.get("Protocol"),
Douglas Gregor512b0772009-04-23 22:29:11 +000089 SourceLocation(), true);
90 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian1e3609f2009-11-18 23:15:37 +000091 PushOnScopeChains(ProtocolDecl, TUScope, false);
Douglas Gregor512b0772009-04-23 22:29:11 +000092 }
Steve Naroff1329fa02009-07-15 18:40:39 +000093 // Create the built-in typedef for 'id'.
Douglas Gregor512b0772009-04-23 22:29:11 +000094 if (Context.getObjCIdType().isNull()) {
John McCall8b07ec22010-05-15 11:32:37 +000095 QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0);
96 T = Context.getObjCObjectPointerType(T);
97 TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T);
John McCall703a3f82009-10-24 08:00:42 +000098 TypedefDecl *IdTypedef
99 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
100 &Context.Idents.get("id"), IdInfo);
Douglas Gregor512b0772009-04-23 22:29:11 +0000101 PushOnScopeChains(IdTypedef, TUScope);
102 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall9f57c292009-08-17 16:35:33 +0000103 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor512b0772009-04-23 22:29:11 +0000104 }
Steve Naroff1329fa02009-07-15 18:40:39 +0000105 // Create the built-in typedef for 'Class'.
Steve Naroff7cae42b2009-07-10 23:34:53 +0000106 if (Context.getObjCClassType().isNull()) {
John McCall8b07ec22010-05-15 11:32:37 +0000107 QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0);
108 T = Context.getObjCObjectPointerType(T);
109 TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T);
John McCall703a3f82009-10-24 08:00:42 +0000110 TypedefDecl *ClassTypedef
111 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
112 &Context.Idents.get("Class"), ClassInfo);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000113 PushOnScopeChains(ClassTypedef, TUScope);
114 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall9f57c292009-08-17 16:35:33 +0000115 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff7cae42b2009-07-10 23:34:53 +0000116 }
Steve Naroff7f549f12007-10-10 21:53:07 +0000117}
118
Douglas Gregor54feb842009-04-14 16:27:31 +0000119Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000120 bool CompleteTranslationUnit,
121 CodeCompleteConsumer *CodeCompleter)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000122 : TheTargetAttributesSema(0),
123 LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump11289f42009-09-09 15:08:12 +0000124 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000125 ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
Douglas Gregor9a28e842010-03-01 23:15:13 +0000126 PackContext(0), TopFunctionScope(0), ParsingDeclDepth(0),
Douglas Gregor2436e712009-09-17 21:32:03 +0000127 IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0),
Douglas Gregorff790f12009-11-26 00:44:06 +0000128 GlobalNewDeleteDeclared(false),
Douglas Gregor37256522009-05-14 21:44:34 +0000129 CompleteTranslationUnit(CompleteTranslationUnit),
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000130 NumSFINAEErrors(0), SuppressAccessChecking(false),
131 NonInstantiationEntries(0), CurrentInstantiationScope(0), TyposCorrected(0),
Ted Kremenek0b405322010-03-23 00:13:23 +0000132 AnalysisWarnings(*this)
Douglas Gregor84d49a22009-11-11 21:54:23 +0000133{
Steve Naroff7f549f12007-10-10 21:53:07 +0000134 TUScope = 0;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000135 if (getLangOptions().CPlusPlus)
136 FieldCollector.reset(new CXXFieldCollector());
Mike Stump11289f42009-09-09 15:08:12 +0000137
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000138 // Tell diagnostics how to render things from the AST library.
Douglas Gregor639cccc2010-02-09 22:26:47 +0000139 PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
140 &Context);
Douglas Gregorff790f12009-11-26 00:44:06 +0000141
142 ExprEvalContexts.push_back(
143 ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
Steve Naroff38d31b42007-02-28 01:22:02 +0000144}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000145
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000146Sema::~Sema() {
147 if (PackContext) FreePackedContext();
148 delete TheTargetAttributesSema;
Douglas Gregor9a28e842010-03-01 23:15:13 +0000149 while (!FunctionScopes.empty())
150 PopFunctionOrBlockScope();
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000151}
152
Mike Stump11289f42009-09-09 15:08:12 +0000153/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattnera65e1f32008-01-16 19:17:22 +0000154/// If there is already an implicit cast, merge into the existing one.
Sebastian Redlc57d34b2010-07-20 04:20:21 +0000155/// The result is of the given category.
Mike Stump11289f42009-09-09 15:08:12 +0000156void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlsson975979382010-04-23 22:18:37 +0000157 CastExpr::CastKind Kind,
Sebastian Redlc57d34b2010-07-20 04:20:21 +0000158 ImplicitCastExpr::ResultCategory Category,
159 CXXBaseSpecifierArray BasePath) {
Mon P Wang74b32072008-09-04 08:38:01 +0000160 QualType ExprTy = Context.getCanonicalType(Expr->getType());
161 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000162
Mon P Wang74b32072008-09-04 08:38:01 +0000163 if (ExprTy == TypeTy)
164 return;
Mike Stump11289f42009-09-09 15:08:12 +0000165
John McCallfceb64b2009-11-07 03:30:10 +0000166 if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
167 QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
168 QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
Mon P Wang74b32072008-09-04 08:38:01 +0000169 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerf490e152008-11-19 05:27:50 +0000170 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
171 << Expr->getSourceRange();
Mon P Wang74b32072008-09-04 08:38:01 +0000172 }
173 }
Mike Stump11289f42009-09-09 15:08:12 +0000174
Douglas Gregor88d292c2010-05-13 16:44:06 +0000175 // If this is a derived-to-base cast to a through a virtual base, we
176 // need a vtable.
177 if (Kind == CastExpr::CK_DerivedToBase &&
178 BasePathInvolvesVirtualBase(BasePath)) {
179 QualType T = Expr->getType();
180 if (const PointerType *Pointer = T->getAs<PointerType>())
181 T = Pointer->getPointeeType();
182 if (const RecordType *RecordTy = T->getAs<RecordType>())
183 MarkVTableUsed(Expr->getLocStart(),
184 cast<CXXRecordDecl>(RecordTy->getDecl()));
185 }
186
Douglas Gregora11693b2008-11-12 17:17:38 +0000187 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Anders Carlssonb78feca2010-04-24 19:22:20 +0000188 if (ImpCast->getCastKind() == Kind && BasePath.empty()) {
Anders Carlsson4e66cda2009-09-15 05:28:24 +0000189 ImpCast->setType(Ty);
Sebastian Redlc57d34b2010-07-20 04:20:21 +0000190 ImpCast->setCategory(Category);
Anders Carlsson4e66cda2009-09-15 05:28:24 +0000191 return;
192 }
193 }
194
Sebastian Redlc57d34b2010-07-20 04:20:21 +0000195 Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, BasePath, Category);
196}
197
198ImplicitCastExpr::ResultCategory Sema::CastCategory(Expr *E) {
199 Expr::Classification Classification = E->Classify(Context);
200 return Classification.isRValue() ?
201 ImplicitCastExpr::RValue :
202 (Classification.isLValue() ?
203 ImplicitCastExpr::LValue :
204 ImplicitCastExpr::XValue);
Chris Lattnera65e1f32008-01-16 19:17:22 +0000205}
206
Chris Lattner57c523f2007-08-31 04:53:24 +0000207void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000208 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner57c523f2007-08-31 04:53:24 +0000209}
210void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000211 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner57c523f2007-08-31 04:53:24 +0000212}
213
Chris Lattnerf4404402008-08-23 03:19:52 +0000214/// ActOnEndOfTranslationUnit - This is called at the very end of the
215/// translation unit when EOF is reached and all but the top-level scope is
216/// popped.
Douglas Gregorfb8b27d2010-04-09 17:41:13 +0000217void Sema::ActOnEndOfTranslationUnit() {
Anders Carlsson82fccd02009-12-07 08:24:59 +0000218 while (1) {
219 // C++: Perform implicit template instantiations.
220 //
221 // FIXME: When we perform these implicit instantiations, we do not carefully
222 // keep track of the point of instantiation (C++ [temp.point]). This means
223 // that name lookup that occurs within the template instantiation will
224 // always happen at the end of the translation unit, so it will find
225 // some names that should not be found. Although this is common behavior
226 // for C++ compilers, it is technically wrong. In the future, we either need
227 // to be able to filter the results of name lookup or we need to perform
228 // template instantiations earlier.
229 PerformPendingImplicitInstantiations();
230
Douglas Gregor88d292c2010-05-13 16:44:06 +0000231 /// If DefinedUsedVTables ends up marking any virtual member
232 /// functions it might lead to more pending template
Anders Carlsson82fccd02009-12-07 08:24:59 +0000233 /// instantiations, which is why we need to loop here.
Douglas Gregor88d292c2010-05-13 16:44:06 +0000234 if (!DefineUsedVTables())
Anders Carlsson82fccd02009-12-07 08:24:59 +0000235 break;
236 }
237
Douglas Gregorfb8b27d2010-04-09 17:41:13 +0000238 // Remove functions that turned out to be used.
239 UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(),
240 UnusedStaticFuncs.end(),
Douglas Gregorebada0772010-06-17 23:14:26 +0000241 std::bind2nd(std::mem_fun(&FunctionDecl::isUsed),
242 true)),
Douglas Gregorfb8b27d2010-04-09 17:41:13 +0000243 UnusedStaticFuncs.end());
244
Chris Lattner0c797362009-09-08 18:19:27 +0000245 // Check for #pragma weak identifiers that were never declared
246 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
247 // order! Iterating over a densemap like this is bad.
Ryan Flynn7d470f32009-07-30 03:15:39 +0000248 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner0c797362009-09-08 18:19:27 +0000249 I = WeakUndeclaredIdentifiers.begin(),
250 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
251 if (I->second.getUsed()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000252
Chris Lattner0c797362009-09-08 18:19:27 +0000253 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
254 << I->first;
Ryan Flynn7d470f32009-07-30 03:15:39 +0000255 }
256
Douglas Gregor54feb842009-04-14 16:27:31 +0000257 if (!CompleteTranslationUnit)
258 return;
259
Douglas Gregor0760fa12009-03-10 23:43:53 +0000260 // C99 6.9.2p2:
261 // A declaration of an identifier for an object that has file
262 // scope without an initializer, and without a storage-class
263 // specifier or with the storage-class specifier static,
264 // constitutes a tentative definition. If a translation unit
265 // contains one or more tentative definitions for an identifier,
266 // and the translation unit contains no external definition for
267 // that identifier, then the behavior is exactly as if the
268 // translation unit contains a file scope declaration of that
269 // identifier, with the composite type as of the end of the
270 // translation unit, with an initializer equal to 0.
Sebastian Redl35351a92010-01-31 22:27:38 +0000271 llvm::SmallSet<VarDecl *, 32> Seen;
272 for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
273 VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
Mike Stump11289f42009-09-09 15:08:12 +0000274
Sebastian Redl35351a92010-01-31 22:27:38 +0000275 // If the tentative definition was completed, getActingDefinition() returns
276 // null. If we've already seen this variable before, insert()'s second
277 // return value is false.
278 if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
Douglas Gregorbeecd582009-04-21 17:11:58 +0000279 continue;
280
Mike Stump11289f42009-09-09 15:08:12 +0000281 if (const IncompleteArrayType *ArrayT
Douglas Gregorbeecd582009-04-21 17:11:58 +0000282 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000283 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorbeecd582009-04-21 17:11:58 +0000284 ArrayT->getElementType(),
Chris Lattner0c797362009-09-08 18:19:27 +0000285 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorbeecd582009-04-21 17:11:58 +0000286 VD->setInvalidDecl();
Chris Lattner0c797362009-09-08 18:19:27 +0000287 continue;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000288 }
Mike Stump11289f42009-09-09 15:08:12 +0000289
Chris Lattner0c797362009-09-08 18:19:27 +0000290 // Set the length of the array to 1 (C99 6.9.2p5).
291 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
292 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCallc5b82252009-10-16 00:14:28 +0000293 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
294 One, ArrayType::Normal, 0);
Chris Lattner0c797362009-09-08 18:19:27 +0000295 VD->setType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000296 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorbeecd582009-04-21 17:11:58 +0000297 diag::err_tentative_def_incomplete_type))
298 VD->setInvalidDecl();
299
300 // Notify the consumer that we've completed a tentative definition.
301 if (!VD->isInvalidDecl())
302 Consumer.CompleteTentativeDefinition(VD);
303
Douglas Gregor0760fa12009-03-10 23:43:53 +0000304 }
Tanya Lattner90073802010-02-12 00:07:30 +0000305
306 // Output warning for unused functions.
307 for (std::vector<FunctionDecl*>::iterator
308 F = UnusedStaticFuncs.begin(),
309 FEnd = UnusedStaticFuncs.end();
310 F != FEnd;
311 ++F)
312 Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName();
313
Chris Lattnerf4404402008-08-23 03:19:52 +0000314}
315
316
Chris Lattnerc11438c2006-08-18 05:17:52 +0000317//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000318// Helper functions.
319//===----------------------------------------------------------------------===//
320
Anders Carlssonb26ab812009-08-08 17:45:02 +0000321DeclContext *Sema::getFunctionLevelDeclContext() {
John McCallb8788012009-12-19 10:53:49 +0000322 DeclContext *DC = CurContext;
Mike Stump11289f42009-09-09 15:08:12 +0000323
Douglas Gregor0c6f5392010-05-22 16:25:05 +0000324 while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
Anders Carlssonb26ab812009-08-08 17:45:02 +0000325 DC = DC->getParent();
Mike Stump11289f42009-09-09 15:08:12 +0000326
Anders Carlssonb26ab812009-08-08 17:45:02 +0000327 return DC;
328}
329
Chris Lattner79413952008-12-04 23:50:19 +0000330/// getCurFunctionDecl - If inside of a function body, this returns a pointer
331/// to the function decl for the function being parsed. If we're currently
332/// in a 'block', this returns the containing context.
333FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlssonb26ab812009-08-08 17:45:02 +0000334 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner79413952008-12-04 23:50:19 +0000335 return dyn_cast<FunctionDecl>(DC);
336}
337
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000338ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlssonb26ab812009-08-08 17:45:02 +0000339 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffecf2bb82008-11-17 16:28:52 +0000340 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000341}
Chris Lattner79413952008-12-04 23:50:19 +0000342
343NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlssonb26ab812009-08-08 17:45:02 +0000344 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner79413952008-12-04 23:50:19 +0000345 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000346 return cast<NamedDecl>(DC);
Chris Lattner79413952008-12-04 23:50:19 +0000347 return 0;
348}
349
Douglas Gregorda17bd32009-03-20 22:48:49 +0000350Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor33834512009-06-14 07:33:30 +0000351 if (!this->Emit())
352 return;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Douglas Gregorda17bd32009-03-20 22:48:49 +0000354 // If this is not a note, and we're in a template instantiation
355 // that is different from the last template instantiation where
356 // we emitted an error, print a template instantiation
357 // backtrace.
358 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
359 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump11289f42009-09-09 15:08:12 +0000360 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregorda17bd32009-03-20 22:48:49 +0000361 != SemaRef.LastTemplateInstantiationErrorContext) {
362 SemaRef.PrintInstantiationStack();
Mike Stump11289f42009-09-09 15:08:12 +0000363 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregorda17bd32009-03-20 22:48:49 +0000364 = SemaRef.ActiveTemplateInstantiations.back();
365 }
366}
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000367
Douglas Gregor210b5902010-03-25 22:17:48 +0000368Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) {
369 if (isSFINAEContext()) {
370 switch (Diagnostic::getDiagnosticSFINAEResponse(DiagID)) {
371 case Diagnostic::SFINAE_Report:
372 // Fall through; we'll report the diagnostic below.
373 break;
374
375 case Diagnostic::SFINAE_SubstitutionFailure:
376 // Count this failure so that we know that template argument deduction
377 // has failed.
378 ++NumSFINAEErrors;
379 // Fall through
380
381 case Diagnostic::SFINAE_Suppress:
382 // Suppress this diagnostic.
383 Diags.setLastDiagnosticIgnored();
384 return SemaDiagnosticBuilder(*this);
385 }
386 }
387
388 DiagnosticBuilder DB = Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
389 return SemaDiagnosticBuilder(DB, *this, DiagID);
390}
391
Anders Carlssonf68079e2009-08-26 22:33:56 +0000392Sema::SemaDiagnosticBuilder
393Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
394 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
395 PD.Emit(Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000396
Anders Carlssonf68079e2009-08-26 22:33:56 +0000397 return Builder;
398}
399
Douglas Gregor0be31a22010-07-02 17:43:08 +0000400/// \brief Determines the active Scope associated with the given declaration
401/// context.
402///
403/// This routine maps a declaration context to the active Scope object that
404/// represents that declaration context in the parser. It is typically used
405/// from "scope-less" code (e.g., template instantiation, lazy creation of
406/// declarations) that injects a name for name-lookup purposes and, therefore,
407/// must update the Scope.
408///
409/// \returns The scope corresponding to the given declaraion context, or NULL
410/// if no such scope is open.
411Scope *Sema::getScopeForContext(DeclContext *Ctx) {
412
413 if (!Ctx)
414 return 0;
415
416 Ctx = Ctx->getPrimaryContext();
417 for (Scope *S = getCurScope(); S; S = S->getParent()) {
Sebastian Redlcaef9ab2010-07-08 23:07:34 +0000418 // Ignore scopes that cannot have declarations. This is important for
419 // out-of-line definitions of static class members.
420 if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
421 if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity()))
422 if (Ctx == Entity->getPrimaryContext())
423 return S;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000424 }
425
426 return 0;
427}
Douglas Gregor9a28e842010-03-01 23:15:13 +0000428
429/// \brief Enter a new function scope
430void Sema::PushFunctionScope() {
431 if (FunctionScopes.empty()) {
432 // Use the "top" function scope rather than having to allocate memory for
433 // a new scope.
434 TopFunctionScope.Clear(getDiagnostics().getNumErrors());
435 FunctionScopes.push_back(&TopFunctionScope);
436 return;
437 }
438
439 FunctionScopes.push_back(
440 new FunctionScopeInfo(getDiagnostics().getNumErrors()));
441}
442
443void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
444 FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics().getNumErrors(),
445 BlockScope, Block));
446}
447
448void Sema::PopFunctionOrBlockScope() {
449 if (FunctionScopes.back() != &TopFunctionScope)
450 delete FunctionScopes.back();
451 else
452 TopFunctionScope.Clear(getDiagnostics().getNumErrors());
453
454 FunctionScopes.pop_back();
455}
456
457/// \brief Determine whether any errors occurred within this function/method/
458/// block.
459bool Sema::hasAnyErrorsInThisFunction() const {
460 unsigned NumErrors = TopFunctionScope.NumErrorsAtStartOfFunction;
461 if (!FunctionScopes.empty())
462 NumErrors = FunctionScopes.back()->NumErrorsAtStartOfFunction;
463 return NumErrors != getDiagnostics().getNumErrors();
464}
465
466BlockScopeInfo *Sema::getCurBlock() {
467 if (FunctionScopes.empty())
468 return 0;
469
470 return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
471}
John McCall75b960e2010-06-01 09:23:16 +0000472
473// Pin this vtable to this file.
474ExternalSemaSource::~ExternalSemaSource() {}