blob: ca69d69959cac791ffba0e2e81e43276ffe8f79a [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
John McCallb45a1e72010-08-26 02:13:20 +000015#include "clang/Sema/SemaInternal.h"
16#include "clang/Sema/DelayedDiagnostic.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000017#include "TargetAttributesSema.h"
Ryan Flynn7d470f32009-07-30 03:15:39 +000018#include "llvm/ADT/DenseMap.h"
Sebastian Redl35351a92010-01-31 22:27:38 +000019#include "llvm/ADT/SmallSet.h"
John McCallfceb64b2009-11-07 03:30:10 +000020#include "llvm/ADT/APFloat.h"
John McCallcc14d1f2010-08-24 08:50:51 +000021#include "clang/Sema/CXXFieldCollector.h"
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +000022#include "clang/Sema/TemplateDeduction.h"
John McCall75b960e2010-06-01 09:23:16 +000023#include "clang/Sema/ExternalSemaSource.h"
Sebastian Redlc1ca90a2010-09-28 20:23:00 +000024#include "clang/Sema/ObjCMethodList.h"
John McCallfaf5fb42010-08-26 23:41:50 +000025#include "clang/Sema/PrettyDeclStackTrace.h"
John McCallcc14d1f2010-08-24 08:50:51 +000026#include "clang/Sema/Scope.h"
John McCallaab3e412010-08-25 08:40:02 +000027#include "clang/Sema/ScopeInfo.h"
Douglas Gregor5c6f10b2010-08-12 22:51:45 +000028#include "clang/Sema/SemaConsumer.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000029#include "clang/AST/ASTContext.h"
Douglas Gregor639cccc2010-02-09 22:26:47 +000030#include "clang/AST/ASTDiagnostic.h"
John McCall28a0cf72010-08-25 07:42:41 +000031#include "clang/AST/DeclCXX.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000032#include "clang/AST/DeclObjC.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000033#include "clang/AST/Expr.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000034#include "clang/Lex/Preprocessor.h"
Anders Carlssonf68079e2009-08-26 22:33:56 +000035#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner7d4f5c42009-04-30 06:18:40 +000036#include "clang/Basic/TargetInfo.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000037using namespace clang;
John McCallaab3e412010-08-25 08:40:02 +000038using namespace sema;
Douglas Gregor9a28e842010-03-01 23:15:13 +000039
40FunctionScopeInfo::~FunctionScopeInfo() { }
41
Argyrios Kyrtzidisf51ec1d2010-11-19 00:19:15 +000042void FunctionScopeInfo::Clear() {
John McCalla95172b2010-08-01 00:26:45 +000043 HasBranchProtectedScope = false;
44 HasBranchIntoScope = false;
45 HasIndirectGoto = false;
46
Douglas Gregor9a28e842010-03-01 23:15:13 +000047 LabelMap.clear();
48 SwitchStack.clear();
Douglas Gregor6fd1b182010-05-15 06:01:05 +000049 Returns.clear();
Argyrios Kyrtzidisf51ec1d2010-11-19 00:19:15 +000050 ErrorTrap.reset();
Douglas Gregor9a28e842010-03-01 23:15:13 +000051}
52
53BlockScopeInfo::~BlockScopeInfo() { }
54
Douglas Gregorf11096c2010-08-25 18:07:12 +000055void Sema::ActOnTranslationUnitScope(Scope *S) {
Steve Naroffc62adb62007-10-09 22:01:59 +000056 TUScope = S;
Douglas Gregor91f84212008-12-11 16:49:14 +000057 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump11289f42009-09-09 15:08:12 +000058
John McCallf24d7bb2010-05-28 18:45:08 +000059 VAListTagName = PP.getIdentifierInfo("__va_list_tag");
60
Sebastian Redl2c499f62010-08-18 23:56:43 +000061 if (!Context.isInt128Installed() && // May be set by ASTReader.
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +000062 PP.getTargetInfo().getPointerWidth(0) >= 64) {
John McCallbcd03502009-12-07 02:54:59 +000063 TypeSourceInfo *TInfo;
John McCall703a3f82009-10-24 08:00:42 +000064
Chris Lattner7d4f5c42009-04-30 06:18:40 +000065 // Install [u]int128_t for 64-bit targets.
John McCallbcd03502009-12-07 02:54:59 +000066 TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty);
Chris Lattner7d4f5c42009-04-30 06:18:40 +000067 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
68 SourceLocation(),
69 &Context.Idents.get("__int128_t"),
John McCallbcd03502009-12-07 02:54:59 +000070 TInfo), TUScope);
John McCall703a3f82009-10-24 08:00:42 +000071
John McCallbcd03502009-12-07 02:54:59 +000072 TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty);
Chris Lattner7d4f5c42009-04-30 06:18:40 +000073 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
74 SourceLocation(),
75 &Context.Idents.get("__uint128_t"),
John McCallbcd03502009-12-07 02:54:59 +000076 TInfo), TUScope);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +000077 Context.setInt128Installed();
Chris Lattner7d4f5c42009-04-30 06:18:40 +000078 }
Mike Stump11289f42009-09-09 15:08:12 +000079
80
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000081 if (!PP.getLangOptions().ObjC1) return;
Mike Stump11289f42009-09-09 15:08:12 +000082
Sebastian Redl2c499f62010-08-18 23:56:43 +000083 // Built-in ObjC types may already be set by ASTReader (hence isNull checks).
Douglas Gregor512b0772009-04-23 22:29:11 +000084 if (Context.getObjCSelType().isNull()) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +000085 // Create the built-in typedef for 'SEL'.
Fariborz Jahanian0afc5552009-11-23 18:04:25 +000086 QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy);
John McCallbcd03502009-12-07 02:54:59 +000087 TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT);
John McCall703a3f82009-10-24 08:00:42 +000088 TypedefDecl *SelTypedef
89 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
90 &Context.Idents.get("SEL"), SelInfo);
Douglas Gregor512b0772009-04-23 22:29:11 +000091 PushOnScopeChains(SelTypedef, TUScope);
92 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
Fariborz Jahanian04b258c2009-11-25 23:07:42 +000093 Context.ObjCSelRedefinitionType = Context.getObjCSelType();
Douglas Gregor512b0772009-04-23 22:29:11 +000094 }
Chris Lattner5a92bab2008-06-21 20:20:39 +000095
Chris Lattner5a92bab2008-06-21 20:20:39 +000096 // Synthesize "@class Protocol;
Douglas Gregor512b0772009-04-23 22:29:11 +000097 if (Context.getObjCProtoType().isNull()) {
98 ObjCInterfaceDecl *ProtocolDecl =
99 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump11289f42009-09-09 15:08:12 +0000100 &Context.Idents.get("Protocol"),
Douglas Gregor1c283312010-08-11 12:19:30 +0000101 SourceLocation(), true);
Douglas Gregor512b0772009-04-23 22:29:11 +0000102 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanian1e3609f2009-11-18 23:15:37 +0000103 PushOnScopeChains(ProtocolDecl, TUScope, false);
Douglas Gregor512b0772009-04-23 22:29:11 +0000104 }
Steve Naroff1329fa02009-07-15 18:40:39 +0000105 // Create the built-in typedef for 'id'.
Douglas Gregor512b0772009-04-23 22:29:11 +0000106 if (Context.getObjCIdType().isNull()) {
John McCall8b07ec22010-05-15 11:32:37 +0000107 QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0);
108 T = Context.getObjCObjectPointerType(T);
109 TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T);
John McCall703a3f82009-10-24 08:00:42 +0000110 TypedefDecl *IdTypedef
111 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
112 &Context.Idents.get("id"), IdInfo);
Douglas Gregor512b0772009-04-23 22:29:11 +0000113 PushOnScopeChains(IdTypedef, TUScope);
114 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall9f57c292009-08-17 16:35:33 +0000115 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor512b0772009-04-23 22:29:11 +0000116 }
Steve Naroff1329fa02009-07-15 18:40:39 +0000117 // Create the built-in typedef for 'Class'.
Steve Naroff7cae42b2009-07-10 23:34:53 +0000118 if (Context.getObjCClassType().isNull()) {
John McCall8b07ec22010-05-15 11:32:37 +0000119 QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0);
120 T = Context.getObjCObjectPointerType(T);
121 TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T);
John McCall703a3f82009-10-24 08:00:42 +0000122 TypedefDecl *ClassTypedef
123 = TypedefDecl::Create(Context, CurContext, SourceLocation(),
124 &Context.Idents.get("Class"), ClassInfo);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000125 PushOnScopeChains(ClassTypedef, TUScope);
126 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall9f57c292009-08-17 16:35:33 +0000127 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000128 }
Steve Naroff7f549f12007-10-10 21:53:07 +0000129}
130
Douglas Gregor54feb842009-04-14 16:27:31 +0000131Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000132 bool CompleteTranslationUnit,
133 CodeCompleteConsumer *CodeCompleter)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000134 : TheTargetAttributesSema(0),
135 LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump11289f42009-09-09 15:08:12 +0000136 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000137 ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0),
John McCallaab3e412010-08-25 08:40:02 +0000138 PackContext(0), VisContext(0), ParsingDeclDepth(0),
Douglas Gregor4c7c1092010-09-08 23:14:30 +0000139 IdResolver(pp.getLangOptions()), CXXTypeInfoDecl(0), MSVCGuidDecl(0),
Bill Wendling73f59c42010-09-08 21:30:16 +0000140 GlobalNewDeleteDeclared(false),
Douglas Gregor37256522009-05-14 21:44:34 +0000141 CompleteTranslationUnit(CompleteTranslationUnit),
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000142 NumSFINAEErrors(0), SuppressAccessChecking(false),
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000143 NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1),
144 CurrentInstantiationScope(0), TyposCorrected(0),
Bill Wendling73f59c42010-09-08 21:30:16 +0000145 AnalysisWarnings(*this)
Douglas Gregor84d49a22009-11-11 21:54:23 +0000146{
Steve Naroff7f549f12007-10-10 21:53:07 +0000147 TUScope = 0;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000148 if (getLangOptions().CPlusPlus)
149 FieldCollector.reset(new CXXFieldCollector());
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000151 // Tell diagnostics how to render things from the AST library.
Douglas Gregor639cccc2010-02-09 22:26:47 +0000152 PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
153 &Context);
Douglas Gregorff790f12009-11-26 00:44:06 +0000154
155 ExprEvalContexts.push_back(
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000156 ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0));
John McCallaab3e412010-08-25 08:40:02 +0000157
Argyrios Kyrtzidisf51ec1d2010-11-19 00:19:15 +0000158 FunctionScopes.push_back(new FunctionScopeInfo(Diags));
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000159}
160
161void Sema::Initialize() {
162 // Tell the AST consumer about this Sema object.
163 Consumer.Initialize(Context);
164
165 // FIXME: Isn't this redundant with the initialization above?
166 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
167 SC->InitializeSema(*this);
168
169 // Tell the external Sema source about this Sema object.
170 if (ExternalSemaSource *ExternalSema
171 = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
172 ExternalSema->InitializeSema(*this);
Steve Naroff38d31b42007-02-28 01:22:02 +0000173}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000174
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000175Sema::~Sema() {
176 if (PackContext) FreePackedContext();
Eli Friedman570024a2010-08-05 06:57:20 +0000177 if (VisContext) FreeVisContext();
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000178 delete TheTargetAttributesSema;
John McCallaab3e412010-08-25 08:40:02 +0000179
180 // Kill all the active scopes.
181 for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I)
182 delete FunctionScopes[I];
183 if (FunctionScopes.size() == 1)
184 delete FunctionScopes[0];
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000185
186 // Tell the SemaConsumer to forget about us; we're going out of scope.
187 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
188 SC->ForgetSema();
189
190 // Detach from the external Sema source.
191 if (ExternalSemaSource *ExternalSema
Douglas Gregor6fd55e02010-08-13 03:15:25 +0000192 = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
Douglas Gregor5c6f10b2010-08-12 22:51:45 +0000193 ExternalSema->ForgetSema();
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000194}
195
Mike Stump11289f42009-09-09 15:08:12 +0000196/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattnera65e1f32008-01-16 19:17:22 +0000197/// If there is already an implicit cast, merge into the existing one.
Sebastian Redlc57d34b2010-07-20 04:20:21 +0000198/// The result is of the given category.
Mike Stump11289f42009-09-09 15:08:12 +0000199void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
John McCall2536c6d2010-08-25 10:28:54 +0000200 CastKind Kind, ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000201 const CXXCastPath *BasePath) {
Mon P Wang74b32072008-09-04 08:38:01 +0000202 QualType ExprTy = Context.getCanonicalType(Expr->getType());
203 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000204
Mon P Wang74b32072008-09-04 08:38:01 +0000205 if (ExprTy == TypeTy)
206 return;
Mike Stump11289f42009-09-09 15:08:12 +0000207
John McCallfceb64b2009-11-07 03:30:10 +0000208 if (Expr->getType()->isPointerType() && Ty->isPointerType()) {
209 QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType();
210 QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType();
Mon P Wang74b32072008-09-04 08:38:01 +0000211 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerf490e152008-11-19 05:27:50 +0000212 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
213 << Expr->getSourceRange();
Mon P Wang74b32072008-09-04 08:38:01 +0000214 }
215 }
Mike Stump11289f42009-09-09 15:08:12 +0000216
Douglas Gregor88d292c2010-05-13 16:44:06 +0000217 // If this is a derived-to-base cast to a through a virtual base, we
218 // need a vtable.
John McCalle3027922010-08-25 11:45:40 +0000219 if (Kind == CK_DerivedToBase &&
John McCallcf142162010-08-07 06:22:56 +0000220 BasePathInvolvesVirtualBase(*BasePath)) {
Douglas Gregor88d292c2010-05-13 16:44:06 +0000221 QualType T = Expr->getType();
222 if (const PointerType *Pointer = T->getAs<PointerType>())
223 T = Pointer->getPointeeType();
224 if (const RecordType *RecordTy = T->getAs<RecordType>())
225 MarkVTableUsed(Expr->getLocStart(),
226 cast<CXXRecordDecl>(RecordTy->getDecl()));
227 }
228
Douglas Gregora11693b2008-11-12 17:17:38 +0000229 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
John McCallcf142162010-08-07 06:22:56 +0000230 if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
Anders Carlsson4e66cda2009-09-15 05:28:24 +0000231 ImpCast->setType(Ty);
John McCall2536c6d2010-08-25 10:28:54 +0000232 ImpCast->setValueKind(VK);
Anders Carlsson4e66cda2009-09-15 05:28:24 +0000233 return;
234 }
235 }
236
John McCall2536c6d2010-08-25 10:28:54 +0000237 Expr = ImplicitCastExpr::Create(Context, Ty, Kind, Expr, BasePath, VK);
Sebastian Redlc57d34b2010-07-20 04:20:21 +0000238}
239
John McCall2536c6d2010-08-25 10:28:54 +0000240ExprValueKind Sema::CastCategory(Expr *E) {
Sebastian Redlc57d34b2010-07-20 04:20:21 +0000241 Expr::Classification Classification = E->Classify(Context);
John McCall2536c6d2010-08-25 10:28:54 +0000242 return Classification.isRValue() ? VK_RValue :
243 (Classification.isLValue() ? VK_LValue : VK_XValue);
Chris Lattnera65e1f32008-01-16 19:17:22 +0000244}
245
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +0000246/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
247static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
248 if (D->isUsed())
249 return true;
250
251 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
252 // UnusedFileScopedDecls stores the first declaration.
253 // The declaration may have become definition so check again.
254 const FunctionDecl *DeclToCheck;
255 if (FD->hasBody(DeclToCheck))
256 return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
257
258 // Later redecls may add new information resulting in not having to warn,
259 // so check again.
260 DeclToCheck = FD->getMostRecentDeclaration();
261 if (DeclToCheck != FD)
262 return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
263 }
264
265 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
266 // UnusedFileScopedDecls stores the first declaration.
267 // The declaration may have become definition so check again.
268 const VarDecl *DeclToCheck = VD->getDefinition();
269 if (DeclToCheck)
270 return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
271
272 // Later redecls may add new information resulting in not having to warn,
273 // so check again.
274 DeclToCheck = VD->getMostRecentDeclaration();
275 if (DeclToCheck != VD)
276 return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
277 }
278
279 return false;
280}
281
Chris Lattnerf4404402008-08-23 03:19:52 +0000282/// ActOnEndOfTranslationUnit - This is called at the very end of the
283/// translation unit when EOF is reached and all but the top-level scope is
284/// popped.
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +0000285void Sema::ActOnEndOfTranslationUnit() {
286 // At PCH writing, implicit instantiations and VTable handling info are
287 // stored and performed when the PCH is included.
Nick Lewyckyef4f4562010-11-25 00:35:20 +0000288 if (CompleteTranslationUnit) {
Chandler Carruth88bfa5e2010-12-12 21:36:11 +0000289 // If any dynamic classes have their key function defined within
290 // this translation unit, then those vtables are considered "used" and must
291 // be emitted.
292 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
293 if (const CXXMethodDecl *KeyFunction
294 = Context.getKeyFunction(DynamicClasses[I])) {
295 const FunctionDecl *Definition = 0;
296 if (KeyFunction->hasBody(Definition))
297 MarkVTableUsed(Definition->getLocation(), DynamicClasses[I], true);
298 }
299 }
300
Nick Lewyckyef4f4562010-11-25 00:35:20 +0000301 // If DefinedUsedVTables ends up marking any virtual member functions it
302 // might lead to more pending template instantiations, which we then need
303 // to instantiate.
304 DefineUsedVTables();
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +0000305
Nick Lewyckyef4f4562010-11-25 00:35:20 +0000306 // C++: Perform implicit template instantiations.
307 //
308 // FIXME: When we perform these implicit instantiations, we do not
309 // carefully keep track of the point of instantiation (C++ [temp.point]).
310 // This means that name lookup that occurs within the template
311 // instantiation will always happen at the end of the translation unit,
312 // so it will find some names that should not be found. Although this is
313 // common behavior for C++ compilers, it is technically wrong. In the
314 // future, we either need to be able to filter the results of name lookup
315 // or we need to perform template instantiations earlier.
316 PerformPendingInstantiations();
317 }
Anders Carlsson82fccd02009-12-07 08:24:59 +0000318
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000319 // Remove file scoped decls that turned out to be used.
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +0000320 UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(),
321 UnusedFileScopedDecls.end(),
322 std::bind1st(std::ptr_fun(ShouldRemoveFromUnused),
323 this)),
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000324 UnusedFileScopedDecls.end());
Douglas Gregorfb8b27d2010-04-09 17:41:13 +0000325
Douglas Gregorb14904c2010-08-13 22:48:40 +0000326 if (!CompleteTranslationUnit) {
327 TUScope = 0;
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +0000328 return;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000329 }
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +0000330
Chris Lattner0c797362009-09-08 18:19:27 +0000331 // Check for #pragma weak identifiers that were never declared
332 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
333 // order! Iterating over a densemap like this is bad.
Ryan Flynn7d470f32009-07-30 03:15:39 +0000334 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner0c797362009-09-08 18:19:27 +0000335 I = WeakUndeclaredIdentifiers.begin(),
336 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
337 if (I->second.getUsed()) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000338
Chris Lattner0c797362009-09-08 18:19:27 +0000339 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
340 << I->first;
Ryan Flynn7d470f32009-07-30 03:15:39 +0000341 }
342
Douglas Gregor0760fa12009-03-10 23:43:53 +0000343 // C99 6.9.2p2:
344 // A declaration of an identifier for an object that has file
345 // scope without an initializer, and without a storage-class
346 // specifier or with the storage-class specifier static,
347 // constitutes a tentative definition. If a translation unit
348 // contains one or more tentative definitions for an identifier,
349 // and the translation unit contains no external definition for
350 // that identifier, then the behavior is exactly as if the
351 // translation unit contains a file scope declaration of that
352 // identifier, with the composite type as of the end of the
353 // translation unit, with an initializer equal to 0.
Sebastian Redl35351a92010-01-31 22:27:38 +0000354 llvm::SmallSet<VarDecl *, 32> Seen;
355 for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) {
356 VarDecl *VD = TentativeDefinitions[i]->getActingDefinition();
Mike Stump11289f42009-09-09 15:08:12 +0000357
Sebastian Redl35351a92010-01-31 22:27:38 +0000358 // If the tentative definition was completed, getActingDefinition() returns
359 // null. If we've already seen this variable before, insert()'s second
360 // return value is false.
361 if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD))
Douglas Gregorbeecd582009-04-21 17:11:58 +0000362 continue;
363
Mike Stump11289f42009-09-09 15:08:12 +0000364 if (const IncompleteArrayType *ArrayT
Douglas Gregorbeecd582009-04-21 17:11:58 +0000365 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump11289f42009-09-09 15:08:12 +0000366 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorbeecd582009-04-21 17:11:58 +0000367 ArrayT->getElementType(),
Chris Lattner0c797362009-09-08 18:19:27 +0000368 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorbeecd582009-04-21 17:11:58 +0000369 VD->setInvalidDecl();
Chris Lattner0c797362009-09-08 18:19:27 +0000370 continue;
Douglas Gregor0760fa12009-03-10 23:43:53 +0000371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
Chris Lattner0c797362009-09-08 18:19:27 +0000373 // Set the length of the array to 1 (C99 6.9.2p5).
374 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
375 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
John McCallc5b82252009-10-16 00:14:28 +0000376 QualType T = Context.getConstantArrayType(ArrayT->getElementType(),
377 One, ArrayType::Normal, 0);
Chris Lattner0c797362009-09-08 18:19:27 +0000378 VD->setType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000379 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorbeecd582009-04-21 17:11:58 +0000380 diag::err_tentative_def_incomplete_type))
381 VD->setInvalidDecl();
382
383 // Notify the consumer that we've completed a tentative definition.
384 if (!VD->isInvalidDecl())
385 Consumer.CompleteTentativeDefinition(VD);
386
Douglas Gregor0760fa12009-03-10 23:43:53 +0000387 }
Tanya Lattner90073802010-02-12 00:07:30 +0000388
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000389 // Output warning for unused file scoped decls.
John McCallafad82e2010-08-24 17:40:45 +0000390 for (llvm::SmallVectorImpl<const DeclaratorDecl*>::iterator
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000391 I = UnusedFileScopedDecls.begin(),
392 E = UnusedFileScopedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +0000393 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
394 const FunctionDecl *DiagD;
395 if (!FD->hasBody(DiagD))
396 DiagD = FD;
Argyrios Kyrtzidiscad715f2010-08-17 21:43:11 +0000397 Diag(DiagD->getLocation(),
Argyrios Kyrtzidisbeb71b32010-08-17 22:06:44 +0000398 isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function
Argyrios Kyrtzidiscad715f2010-08-17 21:43:11 +0000399 : diag::warn_unused_function)
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +0000400 << DiagD->getDeclName();
401 } else {
402 const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
403 if (!DiagD)
404 DiagD = cast<VarDecl>(*I);
405 Diag(DiagD->getLocation(), diag::warn_unused_variable)
406 << DiagD->getDeclName();
407 }
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000408 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000409
410 TUScope = 0;
Chris Lattnerf4404402008-08-23 03:19:52 +0000411}
412
413
Chris Lattnerc11438c2006-08-18 05:17:52 +0000414//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000415// Helper functions.
416//===----------------------------------------------------------------------===//
417
Anders Carlssonb26ab812009-08-08 17:45:02 +0000418DeclContext *Sema::getFunctionLevelDeclContext() {
John McCallb8788012009-12-19 10:53:49 +0000419 DeclContext *DC = CurContext;
Mike Stump11289f42009-09-09 15:08:12 +0000420
Douglas Gregor0c6f5392010-05-22 16:25:05 +0000421 while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
Anders Carlssonb26ab812009-08-08 17:45:02 +0000422 DC = DC->getParent();
Mike Stump11289f42009-09-09 15:08:12 +0000423
Anders Carlssonb26ab812009-08-08 17:45:02 +0000424 return DC;
425}
426
Chris Lattner79413952008-12-04 23:50:19 +0000427/// getCurFunctionDecl - If inside of a function body, this returns a pointer
428/// to the function decl for the function being parsed. If we're currently
429/// in a 'block', this returns the containing context.
430FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlssonb26ab812009-08-08 17:45:02 +0000431 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner79413952008-12-04 23:50:19 +0000432 return dyn_cast<FunctionDecl>(DC);
433}
434
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000435ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlssonb26ab812009-08-08 17:45:02 +0000436 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffecf2bb82008-11-17 16:28:52 +0000437 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000438}
Chris Lattner79413952008-12-04 23:50:19 +0000439
440NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlssonb26ab812009-08-08 17:45:02 +0000441 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner79413952008-12-04 23:50:19 +0000442 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000443 return cast<NamedDecl>(DC);
Chris Lattner79413952008-12-04 23:50:19 +0000444 return 0;
445}
446
Douglas Gregorda17bd32009-03-20 22:48:49 +0000447Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000448 if (!isActive())
449 return;
450
451 if (TemplateDeductionInfo *Info = SemaRef.isSFINAEContext()) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000452 switch (DiagnosticIDs::getDiagnosticSFINAEResponse(getDiagID())) {
453 case DiagnosticIDs::SFINAE_Report:
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000454 // Fall through; we'll report the diagnostic below.
455 break;
456
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000457 case DiagnosticIDs::SFINAE_SubstitutionFailure:
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000458 // Count this failure so that we know that template argument deduction
459 // has failed.
460 ++SemaRef.NumSFINAEErrors;
461 SemaRef.Diags.setLastDiagnosticIgnored();
462 SemaRef.Diags.Clear();
463 Clear();
464 return;
465
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000466 case DiagnosticIDs::SFINAE_Suppress:
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000467 // Make a copy of this suppressed diagnostic and store it with the
468 // template-deduction information;
Douglas Gregorb3921592010-10-13 17:22:14 +0000469 FlushCounts();
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000470 DiagnosticInfo DiagInfo(&SemaRef.Diags);
Douglas Gregorb3921592010-10-13 17:22:14 +0000471
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000472 Info->addSuppressedDiagnostic(DiagInfo.getLocation(),
473 PartialDiagnostic(DiagInfo,
474 SemaRef.Context.getDiagAllocator()));
475
476 // Suppress this diagnostic.
477 SemaRef.Diags.setLastDiagnosticIgnored();
478 SemaRef.Diags.Clear();
479 Clear();
480 return;
481 }
482 }
483
484 // Emit the diagnostic.
Douglas Gregor33834512009-06-14 07:33:30 +0000485 if (!this->Emit())
486 return;
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregorda17bd32009-03-20 22:48:49 +0000488 // If this is not a note, and we're in a template instantiation
489 // that is different from the last template instantiation where
490 // we emitted an error, print a template instantiation
491 // backtrace.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000492 if (!DiagnosticIDs::isBuiltinNote(DiagID) &&
Douglas Gregorda17bd32009-03-20 22:48:49 +0000493 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump11289f42009-09-09 15:08:12 +0000494 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregorda17bd32009-03-20 22:48:49 +0000495 != SemaRef.LastTemplateInstantiationErrorContext) {
496 SemaRef.PrintInstantiationStack();
Mike Stump11289f42009-09-09 15:08:12 +0000497 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregorda17bd32009-03-20 22:48:49 +0000498 = SemaRef.ActiveTemplateInstantiations.back();
499 }
500}
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000501
Douglas Gregor210b5902010-03-25 22:17:48 +0000502Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000503 DiagnosticBuilder DB = Diags.Report(Loc, DiagID);
Douglas Gregor210b5902010-03-25 22:17:48 +0000504 return SemaDiagnosticBuilder(DB, *this, DiagID);
505}
506
Anders Carlssonf68079e2009-08-26 22:33:56 +0000507Sema::SemaDiagnosticBuilder
508Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
509 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
510 PD.Emit(Builder);
Mike Stump11289f42009-09-09 15:08:12 +0000511
Anders Carlssonf68079e2009-08-26 22:33:56 +0000512 return Builder;
513}
514
Douglas Gregor0be31a22010-07-02 17:43:08 +0000515/// \brief Determines the active Scope associated with the given declaration
516/// context.
517///
518/// This routine maps a declaration context to the active Scope object that
519/// represents that declaration context in the parser. It is typically used
520/// from "scope-less" code (e.g., template instantiation, lazy creation of
521/// declarations) that injects a name for name-lookup purposes and, therefore,
522/// must update the Scope.
523///
524/// \returns The scope corresponding to the given declaraion context, or NULL
525/// if no such scope is open.
526Scope *Sema::getScopeForContext(DeclContext *Ctx) {
527
528 if (!Ctx)
529 return 0;
530
531 Ctx = Ctx->getPrimaryContext();
532 for (Scope *S = getCurScope(); S; S = S->getParent()) {
Sebastian Redlcaef9ab2010-07-08 23:07:34 +0000533 // Ignore scopes that cannot have declarations. This is important for
534 // out-of-line definitions of static class members.
535 if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
536 if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity()))
537 if (Ctx == Entity->getPrimaryContext())
538 return S;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000539 }
540
541 return 0;
542}
Douglas Gregor9a28e842010-03-01 23:15:13 +0000543
544/// \brief Enter a new function scope
545void Sema::PushFunctionScope() {
John McCallaab3e412010-08-25 08:40:02 +0000546 if (FunctionScopes.size() == 1) {
547 // Use the "top" function scope rather than having to allocate
548 // memory for a new scope.
Argyrios Kyrtzidisf51ec1d2010-11-19 00:19:15 +0000549 FunctionScopes.back()->Clear();
John McCallaab3e412010-08-25 08:40:02 +0000550 FunctionScopes.push_back(FunctionScopes.back());
Douglas Gregor9a28e842010-03-01 23:15:13 +0000551 return;
552 }
553
Argyrios Kyrtzidisf51ec1d2010-11-19 00:19:15 +0000554 FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
Douglas Gregor9a28e842010-03-01 23:15:13 +0000555}
556
557void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
Argyrios Kyrtzidisf51ec1d2010-11-19 00:19:15 +0000558 FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
Douglas Gregor9a28e842010-03-01 23:15:13 +0000559 BlockScope, Block));
560}
561
562void Sema::PopFunctionOrBlockScope() {
John McCallaab3e412010-08-25 08:40:02 +0000563 FunctionScopeInfo *Scope = FunctionScopes.pop_back_val();
564 assert(!FunctionScopes.empty() && "mismatched push/pop!");
565 if (FunctionScopes.back() != Scope)
566 delete Scope;
Douglas Gregor9a28e842010-03-01 23:15:13 +0000567}
568
569/// \brief Determine whether any errors occurred within this function/method/
570/// block.
571bool Sema::hasAnyErrorsInThisFunction() const {
Argyrios Kyrtzidisf51ec1d2010-11-19 00:19:15 +0000572 return getCurFunction()->ErrorTrap.hasErrorOccurred();
Douglas Gregor9a28e842010-03-01 23:15:13 +0000573}
574
575BlockScopeInfo *Sema::getCurBlock() {
576 if (FunctionScopes.empty())
577 return 0;
578
579 return dyn_cast<BlockScopeInfo>(FunctionScopes.back());
580}
John McCall75b960e2010-06-01 09:23:16 +0000581
582// Pin this vtable to this file.
583ExternalSemaSource::~ExternalSemaSource() {}
John McCallfaf5fb42010-08-26 23:41:50 +0000584
Sebastian Redlc1ca90a2010-09-28 20:23:00 +0000585std::pair<ObjCMethodList, ObjCMethodList>
586ExternalSemaSource::ReadMethodPool(Selector Sel) {
587 return std::pair<ObjCMethodList, ObjCMethodList>();
588}
589
John McCallfaf5fb42010-08-26 23:41:50 +0000590void PrettyDeclStackTraceEntry::print(llvm::raw_ostream &OS) const {
591 SourceLocation Loc = this->Loc;
592 if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation();
593 if (Loc.isValid()) {
594 Loc.print(OS, S.getSourceManager());
595 OS << ": ";
596 }
597 OS << Message;
598
599 if (TheDecl && isa<NamedDecl>(TheDecl)) {
600 std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString();
601 if (!Name.empty())
602 OS << " '" << Name << '\'';
603 }
604
605 OS << '\n';
606}