blob: 080266c75fb65333c1d7af0a535e20ba195587ae [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
Ryan Flynne25ff832009-07-30 03:15:39 +000016#include "llvm/ADT/DenseMap.h"
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +000017#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Lex/Preprocessor.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000022#include "clang/Basic/PartialDiagnostic.h"
Chris Lattner4d150c82009-04-30 06:18:40 +000023#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Mike Stump1eb44332009-09-09 15:08:12 +000026/// ConvertQualTypeToStringFn - This function is used to pretty print the
Chris Lattner22caddc2008-11-23 09:13:29 +000027/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +000028static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattnerd0344a42009-02-19 23:45:49 +000029 const char *Modifier, unsigned ModLen,
30 const char *Argument, unsigned ArgLen,
Chris Lattner92dd3862009-02-19 23:53:20 +000031 llvm::SmallVectorImpl<char> &Output,
32 void *Cookie) {
33 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Mike Stump1eb44332009-09-09 15:08:12 +000034
Chris Lattner011bb4e2008-11-23 20:28:15 +000035 std::string S;
36 if (Kind == Diagnostic::ak_qualtype) {
Chris Lattnerd0344a42009-02-19 23:45:49 +000037 assert(ModLen == 0 && ArgLen == 0 &&
38 "Invalid modifier for QualType argument");
39
Chris Lattner011bb4e2008-11-23 20:28:15 +000040 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner077bf5e2008-11-24 03:33:13 +000041
Chris Lattner011bb4e2008-11-23 20:28:15 +000042 // FIXME: Playing with std::string is really slow.
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000043 S = Ty.getAsString(Context.PrintingPolicy);
Mike Stump1eb44332009-09-09 15:08:12 +000044
Chris Lattnerd0344a42009-02-19 23:45:49 +000045 // If this is a sugared type (like a typedef, typeof, etc), then unwrap one
46 // level of the sugar so that the type is more obvious to the user.
Douglas Gregor969c6892009-04-01 15:47:24 +000047 QualType DesugaredTy = Ty->getDesugaredType(true);
Chris Lattnerd0344a42009-02-19 23:45:49 +000048 DesugaredTy.setCVRQualifiers(DesugaredTy.getCVRQualifiers() |
49 Ty.getCVRQualifiers());
Chris Lattner077bf5e2008-11-24 03:33:13 +000050
Chris Lattnerd0344a42009-02-19 23:45:49 +000051 if (Ty != DesugaredTy &&
52 // If the desugared type is a vector type, we don't want to expand it,
53 // it will turn into an attribute mess. People want their "vec4".
54 !isa<VectorType>(DesugaredTy) &&
John McCall2191b202009-09-05 06:31:47 +000055
56 // Don't aka just because we saw an elaborated type.
57 (!isa<ElaboratedType>(Ty) ||
58 cast<ElaboratedType>(Ty)->getUnderlyingType() != DesugaredTy) &&
Mike Stump1eb44332009-09-09 15:08:12 +000059
Chris Lattner92dd3862009-02-19 23:53:20 +000060 // Don't desugar magic Objective-C types.
61 Ty.getUnqualifiedType() != Context.getObjCIdType() &&
Steve Naroff14108da2009-07-10 23:34:53 +000062 Ty.getUnqualifiedType() != Context.getObjCClassType() &&
Chris Lattner92dd3862009-02-19 23:53:20 +000063 Ty.getUnqualifiedType() != Context.getObjCSelType() &&
64 Ty.getUnqualifiedType() != Context.getObjCProtoType() &&
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner92dd3862009-02-19 23:53:20 +000066 // Not va_list.
67 Ty.getUnqualifiedType() != Context.getBuiltinVaListType()) {
Chris Lattnerd0344a42009-02-19 23:45:49 +000068 S = "'"+S+"' (aka '";
Argyrios Kyrtzidis7c94c4b2009-06-03 02:06:50 +000069 S += DesugaredTy.getAsString(Context.PrintingPolicy);
Chris Lattnerd0344a42009-02-19 23:45:49 +000070 S += "')";
71 Output.append(S.begin(), S.end());
72 return;
73 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000075 } else if (Kind == Diagnostic::ak_declarationname) {
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chris Lattner011bb4e2008-11-23 20:28:15 +000077 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
78 S = N.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +000079
Chris Lattner077bf5e2008-11-24 03:33:13 +000080 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
81 S = '+' + S;
82 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
83 S = '-' + S;
84 else
85 assert(ModLen == 0 && ArgLen == 0 &&
86 "Invalid modifier for DeclarationName argument");
Douglas Gregordacd4342009-08-26 00:04:55 +000087 } else if (Kind == Diagnostic::ak_nameddecl) {
John McCall136a6982009-09-11 06:45:03 +000088 bool Qualified;
Douglas Gregoreeb15d42009-02-04 22:46:25 +000089 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
John McCall136a6982009-09-11 06:45:03 +000090 Qualified = true;
Mike Stump1eb44332009-09-09 15:08:12 +000091 else {
Douglas Gregoreeb15d42009-02-04 22:46:25 +000092 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000093 "Invalid modifier for NamedDecl* argument");
John McCall136a6982009-09-11 06:45:03 +000094 Qualified = false;
Douglas Gregoreeb15d42009-02-04 22:46:25 +000095 }
John McCall136a6982009-09-11 06:45:03 +000096 reinterpret_cast<NamedDecl*>(Val)->getNameForDiagnostic(S,
97 Context.PrintingPolicy,
98 Qualified);
Douglas Gregordacd4342009-08-26 00:04:55 +000099 } else {
100 llvm::raw_string_ostream OS(S);
101 assert(Kind == Diagnostic::ak_nestednamespec);
Mike Stump1eb44332009-09-09 15:08:12 +0000102 reinterpret_cast<NestedNameSpecifier*> (Val)->print(OS,
Douglas Gregordacd4342009-08-26 00:04:55 +0000103 Context.PrintingPolicy);
Chris Lattner011bb4e2008-11-23 20:28:15 +0000104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Chris Lattnerd0344a42009-02-19 23:45:49 +0000106 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000107 Output.append(S.begin(), S.end());
Chris Lattnerd0344a42009-02-19 23:45:49 +0000108 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +0000109}
110
111
Chris Lattner0a14eee2008-11-18 07:04:44 +0000112static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +0000113 if (C.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +0000114 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
Anders Carlssonc3036062008-08-23 22:20:38 +0000115 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +0000116 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000117
Mike Stump1eb44332009-09-09 15:08:12 +0000118 return RecordDecl::Create(C, TagDecl::TK_struct,
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000119 C.getTranslationUnitDecl(),
120 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +0000121}
122
Steve Naroffb216c882007-10-09 22:01:59 +0000123void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
124 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000125 PushDeclContext(S, Context.getTranslationUnitDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattner4d150c82009-04-30 06:18:40 +0000127 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
128 // Install [u]int128_t for 64-bit targets.
129 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
130 SourceLocation(),
131 &Context.Idents.get("__int128_t"),
132 Context.Int128Ty), TUScope);
133 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
134 SourceLocation(),
135 &Context.Idents.get("__uint128_t"),
136 Context.UnsignedInt128Ty), TUScope);
137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
139
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000140 if (!PP.getLangOptions().ObjC1) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Steve Naroffcb83c532009-06-16 00:20:10 +0000142 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregor319ac892009-04-23 22:29:11 +0000143 if (Context.getObjCSelType().isNull()) {
144 // Synthesize "typedef struct objc_selector *SEL;"
145 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
146 PushOnScopeChains(SelTag, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Douglas Gregor319ac892009-04-23 22:29:11 +0000148 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
149 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
150 SourceLocation(),
151 &Context.Idents.get("SEL"),
152 SelT);
153 PushOnScopeChains(SelTypedef, TUScope);
154 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
155 }
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000156
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000157 // Synthesize "@class Protocol;
Douglas Gregor319ac892009-04-23 22:29:11 +0000158 if (Context.getObjCProtoType().isNull()) {
159 ObjCInterfaceDecl *ProtocolDecl =
160 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000161 &Context.Idents.get("Protocol"),
Douglas Gregor319ac892009-04-23 22:29:11 +0000162 SourceLocation(), true);
163 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
164 PushOnScopeChains(ProtocolDecl, TUScope);
165 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000166 // Create the built-in typedef for 'id'.
Douglas Gregor319ac892009-04-23 22:29:11 +0000167 if (Context.getObjCIdType().isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000168 TypedefDecl *IdTypedef =
169 TypedefDecl::Create(
Steve Naroffde2e22d2009-07-15 18:40:39 +0000170 Context, CurContext, SourceLocation(), &Context.Idents.get("id"),
171 Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy)
172 );
Douglas Gregor319ac892009-04-23 22:29:11 +0000173 PushOnScopeChains(IdTypedef, TUScope);
174 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000175 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregor319ac892009-04-23 22:29:11 +0000176 }
Steve Naroffde2e22d2009-07-15 18:40:39 +0000177 // Create the built-in typedef for 'Class'.
Steve Naroff14108da2009-07-10 23:34:53 +0000178 if (Context.getObjCClassType().isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000179 TypedefDecl *ClassTypedef =
180 TypedefDecl::Create(
Steve Naroffde2e22d2009-07-15 18:40:39 +0000181 Context, CurContext, SourceLocation(), &Context.Idents.get("Class"),
182 Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy)
183 );
Steve Naroff14108da2009-07-10 23:34:53 +0000184 PushOnScopeChains(ClassTypedef, TUScope);
185 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall0f436562009-08-17 16:35:33 +0000186 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff14108da2009-07-10 23:34:53 +0000187 }
Steve Naroff3b950172007-10-10 21:53:07 +0000188}
189
Douglas Gregorf807fe02009-04-14 16:27:31 +0000190Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
191 bool CompleteTranslationUnit)
Chris Lattner53ebff32009-01-22 19:21:44 +0000192 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Mike Stump1eb44332009-09-09 15:08:12 +0000193 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000194 ExternalSource(0), CurContext(0), PreDeclaratorDC(0),
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000195 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()),
Douglas Gregorac7610d2009-06-22 20:57:11 +0000196 GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated),
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000197 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregorbb260412009-06-14 08:02:22 +0000198 NumSFINAEErrors(0), CurrentInstantiationScope(0) {
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Sebastian Redlc42e1182008-11-11 11:37:55 +0000200 StdNamespace = 0;
Steve Naroff3b950172007-10-10 21:53:07 +0000201 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000202 if (getLangOptions().CPlusPlus)
203 FieldCollector.reset(new CXXFieldCollector());
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattner22caddc2008-11-23 09:13:29 +0000205 // Tell diagnostics how to render things from the AST library.
Chris Lattner92dd3862009-02-19 23:53:20 +0000206 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207}
208
Mike Stump1eb44332009-09-09 15:08:12 +0000209/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Chris Lattner1e0a3902008-01-16 19:17:22 +0000210/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000211/// If isLvalue, the result of the cast is an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000212void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonbd0fb302009-08-10 21:30:22 +0000213 const CastExpr::CastInfo &Info, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000214 QualType ExprTy = Context.getCanonicalType(Expr->getType());
215 QualType TypeTy = Context.getCanonicalType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Mon P Wang3a2c7442008-09-04 08:38:01 +0000217 if (ExprTy == TypeTy)
218 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Mon P Wang3a2c7442008-09-04 08:38:01 +0000220 if (Expr->getType().getTypePtr()->isPointerType() &&
221 Ty.getTypePtr()->isPointerType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000222 QualType ExprBaseType =
Mon P Wang3a2c7442008-09-04 08:38:01 +0000223 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
224 QualType BaseType =
225 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
226 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000227 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
228 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000229 }
230 }
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000232 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000233 ImpCast->setType(Ty);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000234 ImpCast->setLvalueCast(isLvalue);
Mike Stump1eb44332009-09-09 15:08:12 +0000235 } else
236 Expr = new (Context) ImplicitCastExpr(Ty, Info, Expr,
Anders Carlssoncdef2b72009-07-31 00:48:10 +0000237 isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000238}
239
Chris Lattner394a3fd2007-08-31 04:53:24 +0000240void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000241 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000242}
243void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000244 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000245}
246
Chris Lattner9299f3f2008-08-23 03:19:52 +0000247/// ActOnEndOfTranslationUnit - This is called at the very end of the
248/// translation unit when EOF is reached and all but the top-level scope is
249/// popped.
250void Sema::ActOnEndOfTranslationUnit() {
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000251 // C++: Perform implicit template instantiations.
252 //
253 // FIXME: When we perform these implicit instantiations, we do not carefully
254 // keep track of the point of instantiation (C++ [temp.point]). This means
255 // that name lookup that occurs within the template instantiation will
256 // always happen at the end of the translation unit, so it will find
Mike Stump1eb44332009-09-09 15:08:12 +0000257 // some names that should not be found. Although this is common behavior
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000258 // for C++ compilers, it is technically wrong. In the future, we either need
259 // to be able to filter the results of name lookup or we need to perform
260 // template instantiations earlier.
261 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Chris Lattner63d65f82009-09-08 18:19:27 +0000263 // Check for #pragma weak identifiers that were never declared
264 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
265 // order! Iterating over a densemap like this is bad.
Ryan Flynne25ff832009-07-30 03:15:39 +0000266 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner63d65f82009-09-08 18:19:27 +0000267 I = WeakUndeclaredIdentifiers.begin(),
268 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
269 if (I->second.getUsed()) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattner63d65f82009-09-08 18:19:27 +0000271 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
272 << I->first;
Ryan Flynne25ff832009-07-30 03:15:39 +0000273 }
274
Douglas Gregorf807fe02009-04-14 16:27:31 +0000275 if (!CompleteTranslationUnit)
276 return;
277
Douglas Gregor275a3692009-03-10 23:43:53 +0000278 // C99 6.9.2p2:
279 // A declaration of an identifier for an object that has file
280 // scope without an initializer, and without a storage-class
281 // specifier or with the storage-class specifier static,
282 // constitutes a tentative definition. If a translation unit
283 // contains one or more tentative definitions for an identifier,
284 // and the translation unit contains no external definition for
285 // that identifier, then the behavior is exactly as if the
286 // translation unit contains a file scope declaration of that
287 // identifier, with the composite type as of the end of the
288 // translation unit, with an initializer equal to 0.
Chris Lattner63d65f82009-09-08 18:19:27 +0000289 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
290 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattner63d65f82009-09-08 18:19:27 +0000292 // If the tentative definition was completed, it will be in the list, but
293 // not the map.
294 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000295 continue;
296
Mike Stump1eb44332009-09-09 15:08:12 +0000297 if (const IncompleteArrayType *ArrayT
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000298 = Context.getAsIncompleteArrayType(VD->getType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000299 if (RequireCompleteType(VD->getLocation(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000300 ArrayT->getElementType(),
Chris Lattner63d65f82009-09-08 18:19:27 +0000301 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000302 VD->setInvalidDecl();
Chris Lattner63d65f82009-09-08 18:19:27 +0000303 continue;
Douglas Gregor275a3692009-03-10 23:43:53 +0000304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattner63d65f82009-09-08 18:19:27 +0000306 // Set the length of the array to 1 (C99 6.9.2p5).
307 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
308 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
Mike Stump1eb44332009-09-09 15:08:12 +0000309 QualType T
Chris Lattner63d65f82009-09-08 18:19:27 +0000310 = Context.getConstantArrayWithoutExprType(ArrayT->getElementType(),
311 One, ArrayType::Normal, 0);
312 VD->setType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000313 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000314 diag::err_tentative_def_incomplete_type))
315 VD->setInvalidDecl();
316
317 // Notify the consumer that we've completed a tentative definition.
318 if (!VD->isInvalidDecl())
319 Consumer.CompleteTentativeDefinition(VD);
320
Douglas Gregor275a3692009-03-10 23:43:53 +0000321 }
Chris Lattner9299f3f2008-08-23 03:19:52 +0000322}
323
324
Reid Spencer5f016e22007-07-11 17:01:13 +0000325//===----------------------------------------------------------------------===//
326// Helper functions.
327//===----------------------------------------------------------------------===//
328
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000329DeclContext *Sema::getFunctionLevelDeclContext() {
Anders Carlssonfb7ef752009-08-08 17:48:49 +0000330 DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000332 while (isa<BlockDecl>(DC))
333 DC = DC->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000335 return DC;
336}
337
Chris Lattner371f2582008-12-04 23:50:19 +0000338/// getCurFunctionDecl - If inside of a function body, this returns a pointer
339/// to the function decl for the function being parsed. If we're currently
340/// in a 'block', this returns the containing context.
341FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000342 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000343 return dyn_cast<FunctionDecl>(DC);
344}
345
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000346ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000347 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroffd7612e12008-11-17 16:28:52 +0000348 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000349}
Chris Lattner371f2582008-12-04 23:50:19 +0000350
351NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson8517d9b2009-08-08 17:45:02 +0000352 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattner371f2582008-12-04 23:50:19 +0000353 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000354 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000355 return 0;
356}
357
Mike Stump1eb44332009-09-09 15:08:12 +0000358void Sema::DiagnoseMissingMember(SourceLocation MemberLoc,
Anders Carlssonf4d84b62009-08-30 00:54:35 +0000359 DeclarationName Member,
360 NestedNameSpecifier *NNS, SourceRange Range) {
361 switch (NNS->getKind()) {
362 default: assert(0 && "Unexpected nested name specifier kind!");
363 case NestedNameSpecifier::TypeSpec: {
364 const Type *Ty = Context.getCanonicalType(NNS->getAsType());
365 RecordDecl *RD = cast<RecordType>(Ty)->getDecl();
366 Diag(MemberLoc, diag::err_typecheck_record_no_member)
Anders Carlsson639bfc72009-08-30 06:49:43 +0000367 << Member << RD->getTagKind() << RD << Range;
Anders Carlssonf4d84b62009-08-30 00:54:35 +0000368 break;
369 }
370 case NestedNameSpecifier::Namespace: {
371 Diag(MemberLoc, diag::err_typecheck_namespace_no_member)
Anders Carlsson639bfc72009-08-30 06:49:43 +0000372 << Member << NNS->getAsNamespace() << Range;
Anders Carlssonf4d84b62009-08-30 00:54:35 +0000373 break;
374 }
375 case NestedNameSpecifier::Global: {
Anders Carlsson639bfc72009-08-30 06:49:43 +0000376 Diag(MemberLoc, diag::err_typecheck_global_namespace_no_member)
Anders Carlssonf4d84b62009-08-30 00:54:35 +0000377 << Member << Range;
378 break;
379 }
380 }
381}
382
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000383Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000384 if (!this->Emit())
385 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000387 // If this is not a note, and we're in a template instantiation
388 // that is different from the last template instantiation where
389 // we emitted an error, print a template instantiation
390 // backtrace.
391 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
392 !SemaRef.ActiveTemplateInstantiations.empty() &&
Mike Stump1eb44332009-09-09 15:08:12 +0000393 SemaRef.ActiveTemplateInstantiations.back()
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000394 != SemaRef.LastTemplateInstantiationErrorContext) {
395 SemaRef.PrintInstantiationStack();
Mike Stump1eb44332009-09-09 15:08:12 +0000396 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregor25a88bb2009-03-20 22:48:49 +0000397 = SemaRef.ActiveTemplateInstantiations.back();
398 }
399}
Douglas Gregor2e222532009-07-02 17:08:52 +0000400
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000401Sema::SemaDiagnosticBuilder
402Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
403 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
404 PD.Emit(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000406 return Builder;
407}
408
Douglas Gregor2e222532009-07-02 17:08:52 +0000409void Sema::ActOnComment(SourceRange Comment) {
410 Context.Comments.push_back(Comment);
411}
Anders Carlsson91a0cc92009-08-26 22:33:56 +0000412