blob: b288c62787242ee220352e26146f37fa70bb3524 [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 Lattnerd6647d32007-05-16 17:56:50 +000020#include "clang/Basic/Diagnostic.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000021
Chris Lattnerc11438c2006-08-18 05:17:52 +000022using namespace clang;
23
Ted Kremenek1b0ea822008-01-07 19:49:32 +000024bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff6d40db02007-10-31 18:42:27 +000025 const char *typeName = TD->getIdentifier()->getName();
26 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +000027 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff6d40db02007-10-31 18:42:27 +000028}
29
Anders Carlssonfbcd8512008-08-23 22:20:38 +000030static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name)
31{
32 if (C.getLangOptions().CPlusPlus)
33 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
34 C.getTranslationUnitDecl(),
35 SourceLocation(), &C.Idents.get(Name), 0);
36 else
37 return RecordDecl::Create(C, TagDecl::TK_struct,
38 C.getTranslationUnitDecl(),
39 SourceLocation(), &C.Idents.get(Name), 0);
40}
41
Steve Naroffc62adb62007-10-09 22:01:59 +000042void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
43 TUScope = S;
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000044 CurContext = Context.getTranslationUnitDecl();
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000045 if (!PP.getLangOptions().ObjC1) return;
46
Steve Narofff0ed31a2008-02-24 16:25:02 +000047 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonfbcd8512008-08-23 22:20:38 +000048 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000049 PushOnScopeChains(SelTag, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000050
51 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnerc5ffed42008-04-04 06:12:32 +000052 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
53 SourceLocation(),
Chris Lattnera7b32872008-03-15 06:12:44 +000054 &Context.Idents.get("SEL"),
Chris Lattner96c460d2008-03-15 21:32:50 +000055 SelT, 0);
Argyrios Kyrtzidisb8a49202008-04-12 00:47:19 +000056 PushOnScopeChains(SelTypedef, TUScope);
Steve Narofff0ed31a2008-02-24 16:25:02 +000057 Context.setObjCSelType(SelTypedef);
Chris Lattner5a92bab2008-06-21 20:20:39 +000058
Chris Lattner7fa27582008-06-21 22:44:51 +000059 // FIXME: Make sure these don't leak!
Anders Carlssonfbcd8512008-08-23 22:20:38 +000060 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner5a92bab2008-06-21 20:20:39 +000061 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
62 TypedefDecl *ClassTypedef =
63 TypedefDecl::Create(Context, CurContext, SourceLocation(),
64 &Context.Idents.get("Class"), ClassT, 0);
65 PushOnScopeChains(ClassTag, TUScope);
66 PushOnScopeChains(ClassTypedef, TUScope);
67 Context.setObjCClassType(ClassTypedef);
68 // Synthesize "@class Protocol;
69 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattnerca1e8482008-07-21 07:06:49 +000070 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner5a92bab2008-06-21 20:20:39 +000071 &Context.Idents.get("Protocol"),
72 SourceLocation(), true);
73 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
74 PushOnScopeChains(ProtocolDecl, TUScope);
75
76 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlssonfbcd8512008-08-23 22:20:38 +000077 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
78
Chris Lattner5a92bab2008-06-21 20:20:39 +000079 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
80 PushOnScopeChains(ObjectTag, TUScope);
81 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
82 SourceLocation(),
83 &Context.Idents.get("id"),
84 ObjT, 0);
85 PushOnScopeChains(IdTypedef, TUScope);
86 Context.setObjCIdType(IdTypedef);
Steve Naroff7f549f12007-10-10 21:53:07 +000087}
88
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000089Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Steve Naroffc540d662008-09-03 18:15:37 +000090 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0), CurBlock(0) {
Chris Lattnerb87b1b32007-08-10 20:18:51 +000091
92 // Get IdentifierInfo objects for known functions for which we
93 // do extra checking.
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000094 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattnerb87b1b32007-08-10 20:18:51 +000095
Chris Lattnerfe0e0af2008-02-06 00:46:58 +000096 KnownFunctionIDs[id_printf] = &IT.get("printf");
97 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
98 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
99 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
100 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek34f664d2008-06-16 18:00:42 +0000101 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattnerfe0e0af2008-02-06 00:46:58 +0000102 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
103 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
104 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
105 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
106 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff6d40db02007-10-31 18:42:27 +0000107
Daniel Dunbar12c9ddc2008-08-14 22:04:54 +0000108 SuperID = &IT.get("super");
109
Steve Naroff7f549f12007-10-10 21:53:07 +0000110 TUScope = 0;
Argyrios Kyrtzidised983422008-07-01 10:37:29 +0000111 if (getLangOptions().CPlusPlus)
112 FieldCollector.reset(new CXXFieldCollector());
Steve Naroff38d31b42007-02-28 01:22:02 +0000113}
Chris Lattnercb6a3822006-11-10 06:20:45 +0000114
Chris Lattnera65e1f32008-01-16 19:17:22 +0000115/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
116/// If there is already an implicit cast, merge into the existing one.
Mon P Wang74b32072008-09-04 08:38:01 +0000117void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty) {
118 QualType ExprTy = Context.getCanonicalType(Expr->getType());
119 QualType TypeTy = Context.getCanonicalType(Ty);
120
121 if (ExprTy == TypeTy)
122 return;
123
124 if (Expr->getType().getTypePtr()->isPointerType() &&
125 Ty.getTypePtr()->isPointerType()) {
126 QualType ExprBaseType =
127 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
128 QualType BaseType =
129 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
130 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
131 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast,
132 Expr->getSourceRange());
133 }
134 }
Chris Lattnera65e1f32008-01-16 19:17:22 +0000135
136 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
Mon P Wang74b32072008-09-04 08:38:01 +0000137 ImpCast->setType(Ty);
Chris Lattnera65e1f32008-01-16 19:17:22 +0000138 else
Mon P Wang74b32072008-09-04 08:38:01 +0000139 Expr = new ImplicitCastExpr(Ty, Expr);
Chris Lattnera65e1f32008-01-16 19:17:22 +0000140}
141
Chris Lattner57c523f2007-08-31 04:53:24 +0000142void Sema::DeleteExpr(ExprTy *E) {
143 delete static_cast<Expr*>(E);
144}
145void Sema::DeleteStmt(StmtTy *S) {
146 delete static_cast<Stmt*>(S);
147}
148
Chris Lattnerf4404402008-08-23 03:19:52 +0000149/// ActOnEndOfTranslationUnit - This is called at the very end of the
150/// translation unit when EOF is reached and all but the top-level scope is
151/// popped.
152void Sema::ActOnEndOfTranslationUnit() {
153
154}
155
156
Chris Lattnerc11438c2006-08-18 05:17:52 +0000157//===----------------------------------------------------------------------===//
Chris Lattnereaafe1222006-11-10 05:17:58 +0000158// Helper functions.
159//===----------------------------------------------------------------------===//
160
Chris Lattnerd6647d32007-05-16 17:56:50 +0000161bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000162 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000163 return true;
164}
165
Steve Narofff1e53692007-03-23 22:27:02 +0000166bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000167 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattnerd6647d32007-05-16 17:56:50 +0000168 return true;
169}
170
171bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
172 const std::string &Msg2) {
173 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000174 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000175 return true;
Chris Lattnereaafe1222006-11-10 05:17:58 +0000176}
177
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000178bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange& Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000179 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Steve Naroffbc2f0992007-03-30 20:09:34 +0000180 return true;
181}
182
Steve Naroff71ce2e02007-05-18 22:53:50 +0000183bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000184 const SourceRange& Range) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000185 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000186 return true;
187}
188
189bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000190 const std::string &Msg2, const SourceRange& Range) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000191 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000192 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000193 return true;
194}
195
Chris Lattner816dea22008-01-03 23:38:43 +0000196bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
197 const std::string &Msg2, const std::string &Msg3,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000198 const SourceRange& R1) {
Chris Lattner816dea22008-01-03 23:38:43 +0000199 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
200 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
201 return true;
202}
203
Steve Naroff71ce2e02007-05-18 22:53:50 +0000204bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000205 const SourceRange& R1, const SourceRange& R2) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000206 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000207 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000208 return true;
209}
210
211bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000212 const SourceRange& R1, const SourceRange& R2) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000213 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000214 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000215 return true;
216}
217
218bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
Argyrios Kyrtzidisbf667e22008-08-24 13:14:02 +0000219 const std::string &Msg2, const SourceRange& R1,
220 const SourceRange& R2) {
Steve Naroff71ce2e02007-05-18 22:53:50 +0000221 std::string MsgArr[] = { Msg1, Msg2 };
222 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000223 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Steve Narofff1e53692007-03-23 22:27:02 +0000224 return true;
Steve Naroff8160ea22007-03-06 01:09:46 +0000225}
226
Chris Lattnerac18be92006-11-20 06:49:47 +0000227const LangOptions &Sema::getLangOptions() const {
Steve Naroff38d31b42007-02-28 01:22:02 +0000228 return PP.getLangOptions();
Chris Lattnerac18be92006-11-20 06:49:47 +0000229}
Daniel Dunbar6e8aa532008-08-11 05:35:13 +0000230
231ObjCMethodDecl *Sema::getCurMethodDecl() {
232 return dyn_cast<ObjCMethodDecl>(CurContext);
233}