blob: 7e61bbd14c5f86c913190a533f17e08a7139b122 [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"
20#include "clang/Basic/Diagnostic.h"
21using namespace clang;
22
Chris Lattnerda5c0872008-11-23 09:13:29 +000023/// ConvertQualTypeToStringFn - This function is used to pretty print the
24/// specified QualType as a string in diagnostics.
Chris Lattner254de7d2008-11-23 20:28:15 +000025static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattner3a8f2942008-11-24 03:33:13 +000026 const char *Modifier, unsigned ModLen,
Chris Lattnerda5c0872008-11-23 09:13:29 +000027 const char *Argument, unsigned ArgLen,
28 llvm::SmallVectorImpl<char> &Output) {
Chris Lattnerf5b269a2008-11-23 09:21:17 +000029
Chris Lattner254de7d2008-11-23 20:28:15 +000030 std::string S;
31 if (Kind == Diagnostic::ak_qualtype) {
32 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner3a8f2942008-11-24 03:33:13 +000033
Chris Lattner254de7d2008-11-23 20:28:15 +000034 // FIXME: Playing with std::string is really slow.
35 S = Ty.getAsString();
Chris Lattner3a8f2942008-11-24 03:33:13 +000036
37 assert(ModLen == 0 && ArgLen == 0 &&
38 "Invalid modifier for QualType argument");
39
Chris Lattner254de7d2008-11-23 20:28:15 +000040 } else {
41 assert(Kind == Diagnostic::ak_declarationname);
42
43 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
44 S = N.getAsString();
Chris Lattner3a8f2942008-11-24 03:33:13 +000045
46 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
47 S = '+' + S;
48 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
49 S = '-' + S;
50 else
51 assert(ModLen == 0 && ArgLen == 0 &&
52 "Invalid modifier for DeclarationName argument");
Chris Lattner254de7d2008-11-23 20:28:15 +000053 }
Chris Lattnerda5c0872008-11-23 09:13:29 +000054 Output.append(S.begin(), S.end());
55}
56
57
Chris Lattner6948ae62008-11-18 07:04:44 +000058static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlsson8930fb52008-08-23 22:20:38 +000059 if (C.getLangOptions().CPlusPlus)
60 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
61 C.getTranslationUnitDecl(),
Ted Kremenek2c984042008-09-05 01:34:33 +000062 SourceLocation(), &C.Idents.get(Name));
Chris Lattner8ba580c2008-11-19 05:08:23 +000063
64 return RecordDecl::Create(C, TagDecl::TK_struct,
65 C.getTranslationUnitDecl(),
66 SourceLocation(), &C.Idents.get(Name));
Anders Carlsson8930fb52008-08-23 22:20:38 +000067}
68
Steve Naroff9637a9b2007-10-09 22:01:59 +000069void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
70 TUScope = S;
Douglas Gregor8acb7272008-12-11 16:49:14 +000071 PushDeclContext(S, Context.getTranslationUnitDecl());
Chris Lattnera8c2d592008-02-06 00:46:58 +000072 if (!PP.getLangOptions().ObjC1) return;
73
Steve Naroff8f635e02008-02-24 16:25:02 +000074 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlsson8930fb52008-08-23 22:20:38 +000075 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000076 PushOnScopeChains(SelTag, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000077
78 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnereee57c02008-04-04 06:12:32 +000079 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
80 SourceLocation(),
Chris Lattnere4650482008-03-15 06:12:44 +000081 &Context.Idents.get("SEL"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000082 SelT);
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000083 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000084 Context.setObjCSelType(SelTypedef);
Chris Lattner4e9553a2008-06-21 20:20:39 +000085
Chris Lattner1062d3a2008-06-21 22:44:51 +000086 // FIXME: Make sure these don't leak!
Anders Carlsson8930fb52008-08-23 22:20:38 +000087 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner4e9553a2008-06-21 20:20:39 +000088 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
89 TypedefDecl *ClassTypedef =
90 TypedefDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000091 &Context.Idents.get("Class"), ClassT);
Chris Lattner4e9553a2008-06-21 20:20:39 +000092 PushOnScopeChains(ClassTag, TUScope);
93 PushOnScopeChains(ClassTypedef, TUScope);
94 Context.setObjCClassType(ClassTypedef);
95 // Synthesize "@class Protocol;
96 ObjCInterfaceDecl *ProtocolDecl =
Douglas Gregor6e4fa2c2009-01-09 00:49:46 +000097 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Chris Lattner4e9553a2008-06-21 20:20:39 +000098 &Context.Idents.get("Protocol"),
99 SourceLocation(), true);
100 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
101 PushOnScopeChains(ProtocolDecl, TUScope);
102
103 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlsson8930fb52008-08-23 22:20:38 +0000104 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
105
Chris Lattner4e9553a2008-06-21 20:20:39 +0000106 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
107 PushOnScopeChains(ObjectTag, TUScope);
108 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
109 SourceLocation(),
110 &Context.Idents.get("id"),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000111 ObjT);
Chris Lattner4e9553a2008-06-21 20:20:39 +0000112 PushOnScopeChains(IdTypedef, TUScope);
113 Context.setObjCIdType(IdTypedef);
Steve Naroffee1de132007-10-10 21:53:07 +0000114}
115
Chris Lattnera8c2d592008-02-06 00:46:58 +0000116Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Chris Lattner6d89a8a2009-01-22 19:21:44 +0000117 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
118 Diags(PP.getDiagnostics()),
Chris Lattnerf9ea6a42008-11-22 08:28:49 +0000119 SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0),
Sebastian Redlb5ee8742008-12-03 20:26:15 +0000120 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()),
121 GlobalNewDeleteDeclared(false) {
Chris Lattner2e64c072007-08-10 20:18:51 +0000122
123 // Get IdentifierInfo objects for known functions for which we
124 // do extra checking.
Chris Lattnera8c2d592008-02-06 00:46:58 +0000125 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner2e64c072007-08-10 20:18:51 +0000126
Daniel Dunbar0ab03e62008-10-02 18:44:07 +0000127 KnownFunctionIDs[id_printf] = &IT.get("printf");
128 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
129 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
130 KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk");
131 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
132 KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk");
133 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
134 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
135 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
136 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
137 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
138 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
139 KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk");
140 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
141 KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
142 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroffae84af82007-10-31 18:42:27 +0000143
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000144 StdNamespace = 0;
Steve Naroffee1de132007-10-10 21:53:07 +0000145 TUScope = 0;
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000146 if (getLangOptions().CPlusPlus)
147 FieldCollector.reset(new CXXFieldCollector());
Chris Lattnerda5c0872008-11-23 09:13:29 +0000148
149 // Tell diagnostics how to render things from the AST library.
Chris Lattnerf5b269a2008-11-23 09:21:17 +0000150 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn);
Chris Lattner4b009652007-07-25 00:24:17 +0000151}
152
Chris Lattnere992d6c2008-01-16 19:17:22 +0000153/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
154/// If there is already an implicit cast, merge into the existing one.
Nate Begeman7903d052009-01-18 06:42:49 +0000155/// If isLvalue, the result of the cast is an lvalue.
Douglas Gregor70d26122008-11-12 17:17:38 +0000156void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wangc6c92742008-09-04 08:38:01 +0000157 QualType ExprTy = Context.getCanonicalType(Expr->getType());
158 QualType TypeTy = Context.getCanonicalType(Ty);
159
160 if (ExprTy == TypeTy)
161 return;
162
163 if (Expr->getType().getTypePtr()->isPointerType() &&
164 Ty.getTypePtr()->isPointerType()) {
165 QualType ExprBaseType =
166 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
167 QualType BaseType =
168 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
169 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000170 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
171 << Expr->getSourceRange();
Mon P Wangc6c92742008-09-04 08:38:01 +0000172 }
173 }
Chris Lattnere992d6c2008-01-16 19:17:22 +0000174
Douglas Gregor70d26122008-11-12 17:17:38 +0000175 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wangc6c92742008-09-04 08:38:01 +0000176 ImpCast->setType(Ty);
Douglas Gregor70d26122008-11-12 17:17:38 +0000177 ImpCast->setLvalueCast(isLvalue);
178 } else
179 Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattnere992d6c2008-01-16 19:17:22 +0000180}
181
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000182void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000183 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000184}
185void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000186 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000187}
188
Chris Lattnerc1aea812008-08-23 03:19:52 +0000189/// ActOnEndOfTranslationUnit - This is called at the very end of the
190/// translation unit when EOF is reached and all but the top-level scope is
191/// popped.
192void Sema::ActOnEndOfTranslationUnit() {
193
194}
195
196
Chris Lattner4b009652007-07-25 00:24:17 +0000197//===----------------------------------------------------------------------===//
198// Helper functions.
199//===----------------------------------------------------------------------===//
200
Chris Lattnere5cb5862008-12-04 23:50:19 +0000201/// getCurFunctionDecl - If inside of a function body, this returns a pointer
202/// to the function decl for the function being parsed. If we're currently
203/// in a 'block', this returns the containing context.
204FunctionDecl *Sema::getCurFunctionDecl() {
205 DeclContext *DC = CurContext;
206 while (isa<BlockDecl>(DC))
207 DC = DC->getParent();
208 return dyn_cast<FunctionDecl>(DC);
209}
210
Daniel Dunbar64789f82008-08-11 05:35:13 +0000211ObjCMethodDecl *Sema::getCurMethodDecl() {
Steve Naroff55debea2008-11-17 16:28:52 +0000212 DeclContext *DC = CurContext;
213 while (isa<BlockDecl>(DC))
214 DC = DC->getParent();
215 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbar64789f82008-08-11 05:35:13 +0000216}
Chris Lattnere5cb5862008-12-04 23:50:19 +0000217
218NamedDecl *Sema::getCurFunctionOrMethodDecl() {
219 DeclContext *DC = CurContext;
220 while (isa<BlockDecl>(DC))
221 DC = DC->getParent();
222 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000223 return cast<NamedDecl>(DC);
Chris Lattnere5cb5862008-12-04 23:50:19 +0000224 return 0;
225}
226