blob: 028c9ebf4f3e12329a6b4cb9094f5efe3141ff27 [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
Chris Lattnerddd6fc82006-11-10 04:58:55 +000015#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000019#include "clang/Lex/Preprocessor.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000020using namespace clang;
21
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000022/// ConvertQualTypeToStringFn - This function is used to pretty print the
23/// specified QualType as a string in diagnostics.
Chris Lattnerf7e69d52008-11-23 20:28:15 +000024static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattnere4b95692008-11-24 03:33:13 +000025 const char *Modifier, unsigned ModLen,
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000026 const char *Argument, unsigned ArgLen,
27 llvm::SmallVectorImpl<char> &Output) {
Chris Lattner63ecc502008-11-23 09:21:17 +000028
Chris Lattnerf7e69d52008-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 Lattnere4b95692008-11-24 03:33:13 +000032
Chris Lattnerf7e69d52008-11-23 20:28:15 +000033 // FIXME: Playing with std::string is really slow.
34 S = Ty.getAsString();
Chris Lattnere4b95692008-11-24 03:33:13 +000035
36 assert(ModLen == 0 && ArgLen == 0 &&
37 "Invalid modifier for QualType argument");
38
Douglas Gregor2ada0482009-02-04 17:27:36 +000039 } else if (Kind == Diagnostic::ak_declarationname) {
Chris Lattnerf7e69d52008-11-23 20:28:15 +000040
41 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
42 S = N.getAsString();
Chris Lattnere4b95692008-11-24 03:33:13 +000043
44 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
45 S = '+' + S;
46 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
47 S = '-' + S;
48 else
49 assert(ModLen == 0 && ArgLen == 0 &&
50 "Invalid modifier for DeclarationName argument");
Douglas Gregor2ada0482009-02-04 17:27:36 +000051 } else {
52 assert(Kind == Diagnostic::ak_nameddecl);
Douglas Gregorfc4f8a12009-02-04 22:46:25 +000053 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
54 S = reinterpret_cast<NamedDecl*>(Val)->getQualifiedNameAsString();
55 else {
56 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor2ada0482009-02-04 17:27:36 +000057 "Invalid modifier for NamedDecl* argument");
Douglas Gregorfc4f8a12009-02-04 22:46:25 +000058 S = reinterpret_cast<NamedDecl*>(Val)->getNameAsString();
59 }
Chris Lattnerf7e69d52008-11-23 20:28:15 +000060 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000061 Output.append(S.begin(), S.end());
62}
63
64
Chris Lattner8488c822008-11-18 07:04:44 +000065static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonfbcd8512008-08-23 22:20:38 +000066 if (C.getLangOptions().CPlusPlus)
67 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
68 C.getTranslationUnitDecl(),
Ted Kremenek47923c72008-09-05 01:34:33 +000069 SourceLocation(), &C.Idents.get(Name));
Chris Lattner3b054132008-11-19 05:08:23 +000070
71 return RecordDecl::Create(C, TagDecl::TK_struct,
72 C.getTranslationUnitDecl(),
73 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonfbcd8512008-08-23 22:20:38 +000074}
75
Steve Naroffc62adb62007-10-09 22:01:59 +000076void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
77 TUScope = S;
Douglas Gregor91f84212008-12-11 16:49:14 +000078 PushDeclContext(S, Context.getTranslationUnitDecl());
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000079 if (!PP.getLangOptions().ObjC1) return;
80
Steve Narofff0ed31a2008-02-24 16:25:02 +000081 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonfbcd8512008-08-23 22:20:38 +000082 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000083 PushOnScopeChains(SelTag, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000084
85 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnerc5ffed42008-04-04 06:12:32 +000086 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
87 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +000088 &Context.Idents.get("SEL"),
Douglas Gregor6e6ad602009-01-20 01:17:11 +000089 SelT);
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000090 PushOnScopeChains(SelTypedef, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000091 Context.setObjCSelType(SelTypedef);
Chris Lattner5a92bab2008-06-21 20:20:39 +000092
Chris Lattner7fa27582008-06-21 22:44:51 +000093 // FIXME: Make sure these don't leak!
Anders Carlssonfbcd8512008-08-23 22:20:38 +000094 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner5a92bab2008-06-21 20:20:39 +000095 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
96 TypedefDecl *ClassTypedef =
97 TypedefDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor6e6ad602009-01-20 01:17:11 +000098 &Context.Idents.get("Class"), ClassT);
Chris Lattner5a92bab2008-06-21 20:20:39 +000099 PushOnScopeChains(ClassTag, TUScope);
100 PushOnScopeChains(ClassTypedef, TUScope);
101 Context.setObjCClassType(ClassTypedef);
102 // Synthesize "@class Protocol;
103 ObjCInterfaceDecl *ProtocolDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000104 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Chris Lattner5a92bab2008-06-21 20:20:39 +0000105 &Context.Idents.get("Protocol"),
106 SourceLocation(), true);
107 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
108 PushOnScopeChains(ProtocolDecl, TUScope);
109
110 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlssonfbcd8512008-08-23 22:20:38 +0000111 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
112
Chris Lattner5a92bab2008-06-21 20:20:39 +0000113 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
114 PushOnScopeChains(ObjectTag, TUScope);
115 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
116 SourceLocation(),
117 &Context.Idents.get("id"),
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000118 ObjT);
Chris Lattner5a92bab2008-06-21 20:20:39 +0000119 PushOnScopeChains(IdTypedef, TUScope);
120 Context.setObjCIdType(IdTypedef);
Steve Naroff7f549f12007-10-10 21:53:07 +0000121}
122
Chris Lattnerfe0e0af2008-02-06 00:46:58 +0000123Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Chris Lattner4da04a4e2009-01-22 19:21:44 +0000124 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
125 Diags(PP.getDiagnostics()),
Chris Lattnerbc495d72008-11-22 08:28:49 +0000126 SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0),
Sebastian Redlfaf68082008-12-03 20:26:15 +0000127 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()),
128 GlobalNewDeleteDeclared(false) {
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000129
130 // Get IdentifierInfo objects for known functions for which we
131 // do extra checking.
Chris Lattnerfe0e0af2008-02-06 00:46:58 +0000132 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000133
Daniel Dunbardd9b2d12008-10-02 18:44:07 +0000134 KnownFunctionIDs[id_printf] = &IT.get("printf");
135 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
136 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
137 KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk");
138 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
139 KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk");
140 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
141 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
142 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
143 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
144 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
145 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
146 KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk");
147 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
148 KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
149 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff6d40db02007-10-31 18:42:27 +0000150
Sebastian Redlc4704762008-11-11 11:37:55 +0000151 StdNamespace = 0;
Steve Naroff7f549f12007-10-10 21:53:07 +0000152 TUScope = 0;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000153 if (getLangOptions().CPlusPlus)
154 FieldCollector.reset(new CXXFieldCollector());
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000155
156 // Tell diagnostics how to render things from the AST library.
Chris Lattner63ecc502008-11-23 09:21:17 +0000157 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn);
Steve Naroff38d31b42007-02-28 01:22:02 +0000158}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000159
Chris Lattnera65e1f32008-01-16 19:17:22 +0000160/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
161/// If there is already an implicit cast, merge into the existing one.
Nate Begemanb699c9b2009-01-18 06:42:49 +0000162/// If isLvalue, the result of the cast is an lvalue.
Douglas Gregora11693b2008-11-12 17:17:38 +0000163void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wang74b32072008-09-04 08:38:01 +0000164 QualType ExprTy = Context.getCanonicalType(Expr->getType());
165 QualType TypeTy = Context.getCanonicalType(Ty);
166
167 if (ExprTy == TypeTy)
168 return;
169
170 if (Expr->getType().getTypePtr()->isPointerType() &&
171 Ty.getTypePtr()->isPointerType()) {
172 QualType ExprBaseType =
173 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
174 QualType BaseType =
175 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
176 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerf490e152008-11-19 05:27:50 +0000177 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
178 << Expr->getSourceRange();
Mon P Wang74b32072008-09-04 08:38:01 +0000179 }
180 }
Chris Lattnera65e1f32008-01-16 19:17:22 +0000181
Douglas Gregora11693b2008-11-12 17:17:38 +0000182 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wang74b32072008-09-04 08:38:01 +0000183 ImpCast->setType(Ty);
Douglas Gregora11693b2008-11-12 17:17:38 +0000184 ImpCast->setLvalueCast(isLvalue);
185 } else
186 Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattnera65e1f32008-01-16 19:17:22 +0000187}
188
Chris Lattner57c523f2007-08-31 04:53:24 +0000189void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000190 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner57c523f2007-08-31 04:53:24 +0000191}
192void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000193 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner57c523f2007-08-31 04:53:24 +0000194}
195
Chris Lattnerf4404402008-08-23 03:19:52 +0000196/// ActOnEndOfTranslationUnit - This is called at the very end of the
197/// translation unit when EOF is reached and all but the top-level scope is
198/// popped.
199void Sema::ActOnEndOfTranslationUnit() {
200
201}
202
203
Chris Lattnerc11438c2006-08-18 05:17:52 +0000204//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000205// Helper functions.
206//===----------------------------------------------------------------------===//
207
Chris Lattner79413952008-12-04 23:50:19 +0000208/// getCurFunctionDecl - If inside of a function body, this returns a pointer
209/// to the function decl for the function being parsed. If we're currently
210/// in a 'block', this returns the containing context.
211FunctionDecl *Sema::getCurFunctionDecl() {
212 DeclContext *DC = CurContext;
213 while (isa<BlockDecl>(DC))
214 DC = DC->getParent();
215 return dyn_cast<FunctionDecl>(DC);
216}
217
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000218ObjCMethodDecl *Sema::getCurMethodDecl() {
Steve Naroffecf2bb82008-11-17 16:28:52 +0000219 DeclContext *DC = CurContext;
220 while (isa<BlockDecl>(DC))
221 DC = DC->getParent();
222 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000223}
Chris Lattner79413952008-12-04 23:50:19 +0000224
225NamedDecl *Sema::getCurFunctionOrMethodDecl() {
226 DeclContext *DC = CurContext;
227 while (isa<BlockDecl>(DC))
228 DC = DC->getParent();
229 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000230 return cast<NamedDecl>(DC);
Chris Lattner79413952008-12-04 23:50:19 +0000231 return 0;
232}
233