blob: 2d72c3e1be2ab0b33c4eecc173b18fb35f708109 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +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 Flynn52900802009-07-30 03:15:39 +000016#include "llvm/ADT/DenseMap.h"
Douglas Gregor9cdb4a12009-04-21 17:11:58 +000017#include "clang/AST/ASTConsumer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/Lex/Preprocessor.h"
Anders Carlsson9454a9d2009-08-26 22:33:56 +000022#include "clang/Basic/PartialDiagnostic.h"
Chris Lattnerd196f752009-04-30 06:18:40 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
Chris Lattnerda5c0872008-11-23 09:13:29 +000026/// ConvertQualTypeToStringFn - This function is used to pretty print the
27/// specified QualType as a string in diagnostics.
Chris Lattner254de7d2008-11-23 20:28:15 +000028static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattner2bd4a5a2009-02-19 23:45:49 +000029 const char *Modifier, unsigned ModLen,
30 const char *Argument, unsigned ArgLen,
Chris Lattner0b53af22009-02-19 23:53:20 +000031 llvm::SmallVectorImpl<char> &Output,
32 void *Cookie) {
33 ASTContext &Context = *static_cast<ASTContext*>(Cookie);
Chris Lattnerf5b269a2008-11-23 09:21:17 +000034
Chris Lattner254de7d2008-11-23 20:28:15 +000035 std::string S;
36 if (Kind == Diagnostic::ak_qualtype) {
Chris Lattner2bd4a5a2009-02-19 23:45:49 +000037 assert(ModLen == 0 && ArgLen == 0 &&
38 "Invalid modifier for QualType argument");
39
Chris Lattner254de7d2008-11-23 20:28:15 +000040 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner3a8f2942008-11-24 03:33:13 +000041
Chris Lattner254de7d2008-11-23 20:28:15 +000042 // FIXME: Playing with std::string is really slow.
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +000043 S = Ty.getAsString(Context.PrintingPolicy);
Chris Lattner2bd4a5a2009-02-19 23:45:49 +000044
45 // 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 Gregorda0c4d22009-04-01 15:47:24 +000047 QualType DesugaredTy = Ty->getDesugaredType(true);
Chris Lattner2bd4a5a2009-02-19 23:45:49 +000048 DesugaredTy.setCVRQualifiers(DesugaredTy.getCVRQualifiers() |
49 Ty.getCVRQualifiers());
Chris Lattner3a8f2942008-11-24 03:33:13 +000050
Chris Lattner2bd4a5a2009-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 McCallfbec6a92009-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) &&
Chris Lattner2bd4a5a2009-02-19 23:45:49 +000059
Chris Lattner0b53af22009-02-19 23:53:20 +000060 // Don't desugar magic Objective-C types.
61 Ty.getUnqualifiedType() != Context.getObjCIdType() &&
Steve Naroff329ec222009-07-10 23:34:53 +000062 Ty.getUnqualifiedType() != Context.getObjCClassType() &&
Chris Lattner0b53af22009-02-19 23:53:20 +000063 Ty.getUnqualifiedType() != Context.getObjCSelType() &&
64 Ty.getUnqualifiedType() != Context.getObjCProtoType() &&
Chris Lattner0b53af22009-02-19 23:53:20 +000065
66 // Not va_list.
67 Ty.getUnqualifiedType() != Context.getBuiltinVaListType()) {
Chris Lattner2bd4a5a2009-02-19 23:45:49 +000068 S = "'"+S+"' (aka '";
Argiris Kirtzidis31231ee2009-06-03 02:06:50 +000069 S += DesugaredTy.getAsString(Context.PrintingPolicy);
Chris Lattner2bd4a5a2009-02-19 23:45:49 +000070 S += "')";
71 Output.append(S.begin(), S.end());
72 return;
73 }
Chris Lattner3a8f2942008-11-24 03:33:13 +000074
Douglas Gregor09be81b2009-02-04 17:27:36 +000075 } else if (Kind == Diagnostic::ak_declarationname) {
Chris Lattner254de7d2008-11-23 20:28:15 +000076
77 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
78 S = N.getAsString();
Chris Lattner3a8f2942008-11-24 03:33:13 +000079
80 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 Gregor3e208d82009-08-26 00:04:55 +000087 } else if (Kind == Diagnostic::ak_nameddecl) {
Douglas Gregorbe69b162009-02-04 22:46:25 +000088 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
89 S = reinterpret_cast<NamedDecl*>(Val)->getQualifiedNameAsString();
90 else {
91 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor09be81b2009-02-04 17:27:36 +000092 "Invalid modifier for NamedDecl* argument");
Douglas Gregorbe69b162009-02-04 22:46:25 +000093 S = reinterpret_cast<NamedDecl*>(Val)->getNameAsString();
94 }
Douglas Gregor3e208d82009-08-26 00:04:55 +000095 } else {
96 llvm::raw_string_ostream OS(S);
97 assert(Kind == Diagnostic::ak_nestednamespec);
98 reinterpret_cast<NestedNameSpecifier*> (Val)->print(OS,
99 Context.PrintingPolicy);
Chris Lattner254de7d2008-11-23 20:28:15 +0000100 }
Chris Lattner2bd4a5a2009-02-19 23:45:49 +0000101
102 Output.push_back('\'');
Chris Lattnerda5c0872008-11-23 09:13:29 +0000103 Output.append(S.begin(), S.end());
Chris Lattner2bd4a5a2009-02-19 23:45:49 +0000104 Output.push_back('\'');
Chris Lattnerda5c0872008-11-23 09:13:29 +0000105}
106
107
Chris Lattner6948ae62008-11-18 07:04:44 +0000108static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlsson8930fb52008-08-23 22:20:38 +0000109 if (C.getLangOptions().CPlusPlus)
110 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
111 C.getTranslationUnitDecl(),
Ted Kremenek2c984042008-09-05 01:34:33 +0000112 SourceLocation(), &C.Idents.get(Name));
Chris Lattner8ba580c2008-11-19 05:08:23 +0000113
114 return RecordDecl::Create(C, TagDecl::TK_struct,
115 C.getTranslationUnitDecl(),
116 SourceLocation(), &C.Idents.get(Name));
Anders Carlsson8930fb52008-08-23 22:20:38 +0000117}
118
Steve Naroff9637a9b2007-10-09 22:01:59 +0000119void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
120 TUScope = S;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000121 PushDeclContext(S, Context.getTranslationUnitDecl());
Chris Lattner6cc7e412009-04-30 02:43:43 +0000122
Chris Lattnerd196f752009-04-30 06:18:40 +0000123 if (PP.getTargetInfo().getPointerWidth(0) >= 64) {
124 // Install [u]int128_t for 64-bit targets.
125 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
126 SourceLocation(),
127 &Context.Idents.get("__int128_t"),
128 Context.Int128Ty), TUScope);
129 PushOnScopeChains(TypedefDecl::Create(Context, CurContext,
130 SourceLocation(),
131 &Context.Idents.get("__uint128_t"),
132 Context.UnsignedInt128Ty), TUScope);
133 }
Chris Lattner6cc7e412009-04-30 02:43:43 +0000134
135
Chris Lattnera8c2d592008-02-06 00:46:58 +0000136 if (!PP.getLangOptions().ObjC1) return;
137
Steve Naroff89b0a052009-06-16 00:20:10 +0000138 // Built-in ObjC types may already be set by PCHReader (hence isNull checks).
Douglas Gregorbb21d4b2009-04-23 22:29:11 +0000139 if (Context.getObjCSelType().isNull()) {
140 // Synthesize "typedef struct objc_selector *SEL;"
141 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
142 PushOnScopeChains(SelTag, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +0000143
Douglas Gregorbb21d4b2009-04-23 22:29:11 +0000144 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
145 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
146 SourceLocation(),
147 &Context.Idents.get("SEL"),
148 SelT);
149 PushOnScopeChains(SelTypedef, TUScope);
150 Context.setObjCSelType(Context.getTypeDeclType(SelTypedef));
151 }
Chris Lattner4e9553a2008-06-21 20:20:39 +0000152
Chris Lattner4e9553a2008-06-21 20:20:39 +0000153 // Synthesize "@class Protocol;
Douglas Gregorbb21d4b2009-04-23 22:29:11 +0000154 if (Context.getObjCProtoType().isNull()) {
155 ObjCInterfaceDecl *ProtocolDecl =
156 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
157 &Context.Idents.get("Protocol"),
158 SourceLocation(), true);
159 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
160 PushOnScopeChains(ProtocolDecl, TUScope);
161 }
Steve Naroff7bffd372009-07-15 18:40:39 +0000162 // Create the built-in typedef for 'id'.
Douglas Gregorbb21d4b2009-04-23 22:29:11 +0000163 if (Context.getObjCIdType().isNull()) {
Steve Naroff7bffd372009-07-15 18:40:39 +0000164 TypedefDecl *IdTypedef =
165 TypedefDecl::Create(
166 Context, CurContext, SourceLocation(), &Context.Idents.get("id"),
167 Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy)
168 );
Douglas Gregorbb21d4b2009-04-23 22:29:11 +0000169 PushOnScopeChains(IdTypedef, TUScope);
170 Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
David Chisnall44663db2009-08-17 16:35:33 +0000171 Context.ObjCIdRedefinitionType = Context.getObjCIdType();
Douglas Gregorbb21d4b2009-04-23 22:29:11 +0000172 }
Steve Naroff7bffd372009-07-15 18:40:39 +0000173 // Create the built-in typedef for 'Class'.
Steve Naroff329ec222009-07-10 23:34:53 +0000174 if (Context.getObjCClassType().isNull()) {
Steve Naroff7bffd372009-07-15 18:40:39 +0000175 TypedefDecl *ClassTypedef =
176 TypedefDecl::Create(
177 Context, CurContext, SourceLocation(), &Context.Idents.get("Class"),
178 Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy)
179 );
Steve Naroff329ec222009-07-10 23:34:53 +0000180 PushOnScopeChains(ClassTypedef, TUScope);
181 Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
David Chisnall44663db2009-08-17 16:35:33 +0000182 Context.ObjCClassRedefinitionType = Context.getObjCClassType();
Steve Naroff329ec222009-07-10 23:34:53 +0000183 }
Steve Naroffee1de132007-10-10 21:53:07 +0000184}
185
Douglas Gregore0d5c562009-04-14 16:27:31 +0000186Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
187 bool CompleteTranslationUnit)
Chris Lattner6d89a8a2009-01-22 19:21:44 +0000188 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000189 Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
190 ExternalSource(0), CurContext(0), PreDeclaratorDC(0),
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000191 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()),
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000192 GlobalNewDeleteDeclared(false), ExprEvalContext(PotentiallyEvaluated),
Douglas Gregorf036aa72009-05-14 21:44:34 +0000193 CompleteTranslationUnit(CompleteTranslationUnit),
Douglas Gregor003d7402009-06-14 08:02:22 +0000194 NumSFINAEErrors(0), CurrentInstantiationScope(0) {
Chris Lattner2e64c072007-08-10 20:18:51 +0000195
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000196 StdNamespace = 0;
Steve Naroffee1de132007-10-10 21:53:07 +0000197 TUScope = 0;
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000198 if (getLangOptions().CPlusPlus)
199 FieldCollector.reset(new CXXFieldCollector());
Chris Lattnerda5c0872008-11-23 09:13:29 +0000200
201 // Tell diagnostics how to render things from the AST library.
Chris Lattner0b53af22009-02-19 23:53:20 +0000202 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn, &Context);
Chris Lattner4b009652007-07-25 00:24:17 +0000203}
204
Chris Lattnere992d6c2008-01-16 19:17:22 +0000205/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
206/// If there is already an implicit cast, merge into the existing one.
Nate Begeman7903d052009-01-18 06:42:49 +0000207/// If isLvalue, the result of the cast is an lvalue.
Anders Carlsson85186942009-07-31 01:23:52 +0000208void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty,
Anders Carlssonc0b03962009-08-10 21:30:22 +0000209 const CastExpr::CastInfo &Info, bool isLvalue) {
Mon P Wangc6c92742008-09-04 08:38:01 +0000210 QualType ExprTy = Context.getCanonicalType(Expr->getType());
211 QualType TypeTy = Context.getCanonicalType(Ty);
212
213 if (ExprTy == TypeTy)
214 return;
215
216 if (Expr->getType().getTypePtr()->isPointerType() &&
217 Ty.getTypePtr()->isPointerType()) {
218 QualType ExprBaseType =
219 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
220 QualType BaseType =
221 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
222 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000223 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
224 << Expr->getSourceRange();
Mon P Wangc6c92742008-09-04 08:38:01 +0000225 }
226 }
Chris Lattnere992d6c2008-01-16 19:17:22 +0000227
Douglas Gregor70d26122008-11-12 17:17:38 +0000228 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wangc6c92742008-09-04 08:38:01 +0000229 ImpCast->setType(Ty);
Douglas Gregor70d26122008-11-12 17:17:38 +0000230 ImpCast->setLvalueCast(isLvalue);
231 } else
Anders Carlssonc0b03962009-08-10 21:30:22 +0000232 Expr = new (Context) ImplicitCastExpr(Ty, Info, Expr,
Anders Carlsson7ef181c2009-07-31 00:48:10 +0000233 isLvalue);
Chris Lattnere992d6c2008-01-16 19:17:22 +0000234}
235
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000236void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000237 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000238}
239void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000240 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000241}
242
Chris Lattnerc1aea812008-08-23 03:19:52 +0000243/// ActOnEndOfTranslationUnit - This is called at the very end of the
244/// translation unit when EOF is reached and all but the top-level scope is
245/// popped.
246void Sema::ActOnEndOfTranslationUnit() {
Douglas Gregorcad27f62009-06-22 23:06:13 +0000247 // C++: Perform implicit template instantiations.
248 //
249 // FIXME: When we perform these implicit instantiations, we do not carefully
250 // keep track of the point of instantiation (C++ [temp.point]). This means
251 // that name lookup that occurs within the template instantiation will
252 // always happen at the end of the translation unit, so it will find
253 // some names that should not be found. Although this is common behavior
254 // for C++ compilers, it is technically wrong. In the future, we either need
255 // to be able to filter the results of name lookup or we need to perform
256 // template instantiations earlier.
257 PerformPendingImplicitInstantiations();
258
Chris Lattner814b4252009-09-08 18:19:27 +0000259 // Check for #pragma weak identifiers that were never declared
260 // FIXME: This will cause diagnostics to be emitted in a non-determinstic
261 // order! Iterating over a densemap like this is bad.
Ryan Flynn52900802009-07-30 03:15:39 +0000262 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Chris Lattner814b4252009-09-08 18:19:27 +0000263 I = WeakUndeclaredIdentifiers.begin(),
264 E = WeakUndeclaredIdentifiers.end(); I != E; ++I) {
265 if (I->second.getUsed()) continue;
266
267 Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared)
268 << I->first;
Ryan Flynn52900802009-07-30 03:15:39 +0000269 }
270
Douglas Gregore0d5c562009-04-14 16:27:31 +0000271 if (!CompleteTranslationUnit)
272 return;
273
Douglas Gregor2f728b22009-03-10 23:43:53 +0000274 // C99 6.9.2p2:
275 // A declaration of an identifier for an object that has file
276 // scope without an initializer, and without a storage-class
277 // specifier or with the storage-class specifier static,
278 // constitutes a tentative definition. If a translation unit
279 // contains one or more tentative definitions for an identifier,
280 // and the translation unit contains no external definition for
281 // that identifier, then the behavior is exactly as if the
282 // translation unit contains a file scope declaration of that
283 // identifier, with the composite type as of the end of the
284 // translation unit, with an initializer equal to 0.
Chris Lattner814b4252009-09-08 18:19:27 +0000285 for (unsigned i = 0, e = TentativeDefinitionList.size(); i != e; ++i) {
286 VarDecl *VD = TentativeDefinitions.lookup(TentativeDefinitionList[i]);
287
288 // If the tentative definition was completed, it will be in the list, but
289 // not the map.
290 if (VD == 0 || VD->isInvalidDecl() || !VD->isTentativeDefinition(Context))
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000291 continue;
292
293 if (const IncompleteArrayType *ArrayT
294 = Context.getAsIncompleteArrayType(VD->getType())) {
295 if (RequireCompleteType(VD->getLocation(),
296 ArrayT->getElementType(),
Chris Lattner814b4252009-09-08 18:19:27 +0000297 diag::err_tentative_def_incomplete_type_arr)) {
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000298 VD->setInvalidDecl();
Chris Lattner814b4252009-09-08 18:19:27 +0000299 continue;
Douglas Gregor2f728b22009-03-10 23:43:53 +0000300 }
Chris Lattner814b4252009-09-08 18:19:27 +0000301
302 // Set the length of the array to 1 (C99 6.9.2p5).
303 Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
304 llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
305 QualType T
306 = Context.getConstantArrayWithoutExprType(ArrayT->getElementType(),
307 One, ArrayType::Normal, 0);
308 VD->setType(T);
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000309 } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
310 diag::err_tentative_def_incomplete_type))
311 VD->setInvalidDecl();
312
313 // Notify the consumer that we've completed a tentative definition.
314 if (!VD->isInvalidDecl())
315 Consumer.CompleteTentativeDefinition(VD);
316
Douglas Gregor2f728b22009-03-10 23:43:53 +0000317 }
Chris Lattnerc1aea812008-08-23 03:19:52 +0000318}
319
320
Chris Lattner4b009652007-07-25 00:24:17 +0000321//===----------------------------------------------------------------------===//
322// Helper functions.
323//===----------------------------------------------------------------------===//
324
Anders Carlsson2f19dce2009-08-08 17:45:02 +0000325DeclContext *Sema::getFunctionLevelDeclContext() {
Anders Carlsson84ee3492009-08-08 17:48:49 +0000326 DeclContext *DC = PreDeclaratorDC ? PreDeclaratorDC : CurContext;
327
Anders Carlsson2f19dce2009-08-08 17:45:02 +0000328 while (isa<BlockDecl>(DC))
329 DC = DC->getParent();
330
331 return DC;
332}
333
Chris Lattnere5cb5862008-12-04 23:50:19 +0000334/// getCurFunctionDecl - If inside of a function body, this returns a pointer
335/// to the function decl for the function being parsed. If we're currently
336/// in a 'block', this returns the containing context.
337FunctionDecl *Sema::getCurFunctionDecl() {
Anders Carlsson2f19dce2009-08-08 17:45:02 +0000338 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattnere5cb5862008-12-04 23:50:19 +0000339 return dyn_cast<FunctionDecl>(DC);
340}
341
Daniel Dunbar64789f82008-08-11 05:35:13 +0000342ObjCMethodDecl *Sema::getCurMethodDecl() {
Anders Carlsson2f19dce2009-08-08 17:45:02 +0000343 DeclContext *DC = getFunctionLevelDeclContext();
Steve Naroff55debea2008-11-17 16:28:52 +0000344 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbar64789f82008-08-11 05:35:13 +0000345}
Chris Lattnere5cb5862008-12-04 23:50:19 +0000346
347NamedDecl *Sema::getCurFunctionOrMethodDecl() {
Anders Carlsson2f19dce2009-08-08 17:45:02 +0000348 DeclContext *DC = getFunctionLevelDeclContext();
Chris Lattnere5cb5862008-12-04 23:50:19 +0000349 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000350 return cast<NamedDecl>(DC);
Chris Lattnere5cb5862008-12-04 23:50:19 +0000351 return 0;
352}
353
Anders Carlsson4355a392009-08-30 00:54:35 +0000354void Sema::DiagnoseMissingMember(SourceLocation MemberLoc,
355 DeclarationName Member,
356 NestedNameSpecifier *NNS, SourceRange Range) {
357 switch (NNS->getKind()) {
358 default: assert(0 && "Unexpected nested name specifier kind!");
359 case NestedNameSpecifier::TypeSpec: {
360 const Type *Ty = Context.getCanonicalType(NNS->getAsType());
361 RecordDecl *RD = cast<RecordType>(Ty)->getDecl();
362 Diag(MemberLoc, diag::err_typecheck_record_no_member)
Anders Carlssonf1cb8352009-08-30 06:49:43 +0000363 << Member << RD->getTagKind() << RD << Range;
Anders Carlsson4355a392009-08-30 00:54:35 +0000364 break;
365 }
366 case NestedNameSpecifier::Namespace: {
367 Diag(MemberLoc, diag::err_typecheck_namespace_no_member)
Anders Carlssonf1cb8352009-08-30 06:49:43 +0000368 << Member << NNS->getAsNamespace() << Range;
Anders Carlsson4355a392009-08-30 00:54:35 +0000369 break;
370 }
371 case NestedNameSpecifier::Global: {
Anders Carlssonf1cb8352009-08-30 06:49:43 +0000372 Diag(MemberLoc, diag::err_typecheck_global_namespace_no_member)
Anders Carlsson4355a392009-08-30 00:54:35 +0000373 << Member << Range;
374 break;
375 }
376 }
377}
378
Douglas Gregor46970ed2009-03-20 22:48:49 +0000379Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
Douglas Gregor95d6c952009-06-14 07:33:30 +0000380 if (!this->Emit())
381 return;
Douglas Gregor46970ed2009-03-20 22:48:49 +0000382
383 // If this is not a note, and we're in a template instantiation
384 // that is different from the last template instantiation where
385 // we emitted an error, print a template instantiation
386 // backtrace.
387 if (!SemaRef.Diags.isBuiltinNote(DiagID) &&
388 !SemaRef.ActiveTemplateInstantiations.empty() &&
389 SemaRef.ActiveTemplateInstantiations.back()
390 != SemaRef.LastTemplateInstantiationErrorContext) {
391 SemaRef.PrintInstantiationStack();
392 SemaRef.LastTemplateInstantiationErrorContext
393 = SemaRef.ActiveTemplateInstantiations.back();
394 }
395}
Douglas Gregora252b232009-07-02 17:08:52 +0000396
Anders Carlsson9454a9d2009-08-26 22:33:56 +0000397Sema::SemaDiagnosticBuilder
398Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) {
399 SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID()));
400 PD.Emit(Builder);
401
402 return Builder;
403}
404
Douglas Gregora252b232009-07-02 17:08:52 +0000405void Sema::ActOnComment(SourceRange Comment) {
406 Context.Comments.push_back(Comment);
407}
Anders Carlsson9454a9d2009-08-26 22:33:56 +0000408