blob: 604173624299e9958655d134a1e4d4f7c7e25a07 [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"
16#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Lex/Preprocessor.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattnerda5c0872008-11-23 09:13:29 +000022/// ConvertQualTypeToStringFn - This function is used to pretty print the
23/// specified QualType as a string in diagnostics.
Chris Lattner254de7d2008-11-23 20:28:15 +000024static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattner3a8f2942008-11-24 03:33:13 +000025 const char *Modifier, unsigned ModLen,
Chris Lattnerda5c0872008-11-23 09:13:29 +000026 const char *Argument, unsigned ArgLen,
27 llvm::SmallVectorImpl<char> &Output) {
Chris Lattnerf5b269a2008-11-23 09:21:17 +000028
Chris Lattner254de7d2008-11-23 20:28:15 +000029 std::string S;
30 if (Kind == Diagnostic::ak_qualtype) {
31 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner3a8f2942008-11-24 03:33:13 +000032
Chris Lattner254de7d2008-11-23 20:28:15 +000033 // FIXME: Playing with std::string is really slow.
34 S = Ty.getAsString();
Chris Lattner3a8f2942008-11-24 03:33:13 +000035
36 assert(ModLen == 0 && ArgLen == 0 &&
37 "Invalid modifier for QualType argument");
38
Chris Lattner254de7d2008-11-23 20:28:15 +000039 } else {
40 assert(Kind == Diagnostic::ak_declarationname);
41
42 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
43 S = N.getAsString();
Chris Lattner3a8f2942008-11-24 03:33:13 +000044
45 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
46 S = '+' + S;
47 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
48 S = '-' + S;
49 else
50 assert(ModLen == 0 && ArgLen == 0 &&
51 "Invalid modifier for DeclarationName argument");
Chris Lattner254de7d2008-11-23 20:28:15 +000052 }
Chris Lattnerda5c0872008-11-23 09:13:29 +000053 Output.append(S.begin(), S.end());
54}
55
56
Chris Lattner6948ae62008-11-18 07:04:44 +000057static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlsson8930fb52008-08-23 22:20:38 +000058 if (C.getLangOptions().CPlusPlus)
59 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
60 C.getTranslationUnitDecl(),
Ted Kremenek2c984042008-09-05 01:34:33 +000061 SourceLocation(), &C.Idents.get(Name));
Chris Lattner8ba580c2008-11-19 05:08:23 +000062
63 return RecordDecl::Create(C, TagDecl::TK_struct,
64 C.getTranslationUnitDecl(),
65 SourceLocation(), &C.Idents.get(Name));
Anders Carlsson8930fb52008-08-23 22:20:38 +000066}
67
Steve Naroff9637a9b2007-10-09 22:01:59 +000068void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
69 TUScope = S;
Douglas Gregor8acb7272008-12-11 16:49:14 +000070 PushDeclContext(S, Context.getTranslationUnitDecl());
Chris Lattnera8c2d592008-02-06 00:46:58 +000071 if (!PP.getLangOptions().ObjC1) return;
72
Steve Naroff8f635e02008-02-24 16:25:02 +000073 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlsson8930fb52008-08-23 22:20:38 +000074 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000075 PushOnScopeChains(SelTag, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000076
77 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnereee57c02008-04-04 06:12:32 +000078 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
79 SourceLocation(),
Chris Lattnere4650482008-03-15 06:12:44 +000080 &Context.Idents.get("SEL"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000081 SelT);
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000082 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000083 Context.setObjCSelType(SelTypedef);
Chris Lattner4e9553a2008-06-21 20:20:39 +000084
Chris Lattner1062d3a2008-06-21 22:44:51 +000085 // FIXME: Make sure these don't leak!
Anders Carlsson8930fb52008-08-23 22:20:38 +000086 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner4e9553a2008-06-21 20:20:39 +000087 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
88 TypedefDecl *ClassTypedef =
89 TypedefDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000090 &Context.Idents.get("Class"), ClassT);
Chris Lattner4e9553a2008-06-21 20:20:39 +000091 PushOnScopeChains(ClassTag, TUScope);
92 PushOnScopeChains(ClassTypedef, TUScope);
93 Context.setObjCClassType(ClassTypedef);
94 // Synthesize "@class Protocol;
95 ObjCInterfaceDecl *ProtocolDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000096 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Chris Lattner4e9553a2008-06-21 20:20:39 +000097 &Context.Idents.get("Protocol"),
98 SourceLocation(), true);
99 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
100 PushOnScopeChains(ProtocolDecl, TUScope);
101
102 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlsson8930fb52008-08-23 22:20:38 +0000103 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
104
Chris Lattner4e9553a2008-06-21 20:20:39 +0000105 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
106 PushOnScopeChains(ObjectTag, TUScope);
107 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
108 SourceLocation(),
109 &Context.Idents.get("id"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000110 ObjT);
Chris Lattner4e9553a2008-06-21 20:20:39 +0000111 PushOnScopeChains(IdTypedef, TUScope);
112 Context.setObjCIdType(IdTypedef);
Steve Naroffee1de132007-10-10 21:53:07 +0000113}
114
Chris Lattnera8c2d592008-02-06 00:46:58 +0000115Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Chris Lattner6d89a8a2009-01-22 19:21:44 +0000116 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
117 Diags(PP.getDiagnostics()),
Chris Lattnerf9ea6a42008-11-22 08:28:49 +0000118 SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0),
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000119 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()),
120 GlobalNewDeleteDeclared(false) {
Chris Lattner2e64c072007-08-10 20:18:51 +0000121
122 // Get IdentifierInfo objects for known functions for which we
123 // do extra checking.
Chris Lattnera8c2d592008-02-06 00:46:58 +0000124 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner2e64c072007-08-10 20:18:51 +0000125
Daniel Dunbar0ab03e62008-10-02 18:44:07 +0000126 KnownFunctionIDs[id_printf] = &IT.get("printf");
127 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
128 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
129 KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk");
130 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
131 KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk");
132 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
133 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
134 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
135 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
136 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
137 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
138 KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk");
139 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
140 KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
141 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroffae84af82007-10-31 18:42:27 +0000142
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000143 StdNamespace = 0;
Steve Naroffee1de132007-10-10 21:53:07 +0000144 TUScope = 0;
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000145 if (getLangOptions().CPlusPlus)
146 FieldCollector.reset(new CXXFieldCollector());
Chris Lattnerda5c0872008-11-23 09:13:29 +0000147
148 // Tell diagnostics how to render things from the AST library.
Chris Lattnerf5b269a2008-11-23 09:21:17 +0000149 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn);
Chris Lattner4b009652007-07-25 00:24:17 +0000150}
151
Chris Lattnere992d6c2008-01-16 19:17:22 +0000152/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
153/// If there is already an implicit cast, merge into the existing one.
Nate Begeman7903d052009-01-18 06:42:49 +0000154/// If isLvalue, the result of the cast is an lvalue.
Douglas Gregor70d26122008-11-12 17:17:38 +0000155void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wangc6c92742008-09-04 08:38:01 +0000156 QualType ExprTy = Context.getCanonicalType(Expr->getType());
157 QualType TypeTy = Context.getCanonicalType(Ty);
158
159 if (ExprTy == TypeTy)
160 return;
161
162 if (Expr->getType().getTypePtr()->isPointerType() &&
163 Ty.getTypePtr()->isPointerType()) {
164 QualType ExprBaseType =
165 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
166 QualType BaseType =
167 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
168 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000169 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
170 << Expr->getSourceRange();
Mon P Wangc6c92742008-09-04 08:38:01 +0000171 }
172 }
Chris Lattnere992d6c2008-01-16 19:17:22 +0000173
Douglas Gregor70d26122008-11-12 17:17:38 +0000174 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wangc6c92742008-09-04 08:38:01 +0000175 ImpCast->setType(Ty);
Douglas Gregor70d26122008-11-12 17:17:38 +0000176 ImpCast->setLvalueCast(isLvalue);
177 } else
178 Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattnere992d6c2008-01-16 19:17:22 +0000179}
180
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000181void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000182 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000183}
184void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000185 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000186}
187
Chris Lattnerc1aea812008-08-23 03:19:52 +0000188/// ActOnEndOfTranslationUnit - This is called at the very end of the
189/// translation unit when EOF is reached and all but the top-level scope is
190/// popped.
191void Sema::ActOnEndOfTranslationUnit() {
192
193}
194
195
Chris Lattner4b009652007-07-25 00:24:17 +0000196//===----------------------------------------------------------------------===//
197// Helper functions.
198//===----------------------------------------------------------------------===//
199
Chris Lattnere5cb5862008-12-04 23:50:19 +0000200/// getCurFunctionDecl - If inside of a function body, this returns a pointer
201/// to the function decl for the function being parsed. If we're currently
202/// in a 'block', this returns the containing context.
203FunctionDecl *Sema::getCurFunctionDecl() {
204 DeclContext *DC = CurContext;
205 while (isa<BlockDecl>(DC))
206 DC = DC->getParent();
207 return dyn_cast<FunctionDecl>(DC);
208}
209
Daniel Dunbar64789f82008-08-11 05:35:13 +0000210ObjCMethodDecl *Sema::getCurMethodDecl() {
Steve Naroff55debea2008-11-17 16:28:52 +0000211 DeclContext *DC = CurContext;
212 while (isa<BlockDecl>(DC))
213 DC = DC->getParent();
214 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbar64789f82008-08-11 05:35:13 +0000215}
Chris Lattnere5cb5862008-12-04 23:50:19 +0000216
217NamedDecl *Sema::getCurFunctionOrMethodDecl() {
218 DeclContext *DC = CurContext;
219 while (isa<BlockDecl>(DC))
220 DC = DC->getParent();
221 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000222 return cast<NamedDecl>(DC);
Chris Lattnere5cb5862008-12-04 23:50:19 +0000223 return 0;
224}
225