blob: a73743fef69c659363778ef29e38389138dea88c [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"
Chris Lattner2e64c072007-08-10 20:18:51 +000021
Chris Lattner4b009652007-07-25 00:24:17 +000022using namespace clang;
23
Ted Kremenek42730c52008-01-07 19:49:32 +000024bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroffae84af82007-10-31 18:42:27 +000025 const char *typeName = TD->getIdentifier()->getName();
26 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000027 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroffae84af82007-10-31 18:42:27 +000028}
29
Steve Naroff9637a9b2007-10-09 22:01:59 +000030void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
31 TUScope = S;
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000032 CurContext = Context.getTranslationUnitDecl();
Chris Lattnera8c2d592008-02-06 00:46:58 +000033 if (!PP.getLangOptions().ObjC1) return;
34
Steve Naroff8f635e02008-02-24 16:25:02 +000035 // Synthesize "typedef struct objc_selector *SEL;"
Chris Lattner5cece462008-07-21 07:06:49 +000036 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct,
37 CurContext,
Chris Lattner58114f02008-03-15 21:32:50 +000038 SourceLocation(),
Chris Lattnere4650482008-03-15 06:12:44 +000039 &Context.Idents.get("objc_selector"),
Chris Lattner58114f02008-03-15 21:32:50 +000040 0);
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000041 PushOnScopeChains(SelTag, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000042
43 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnereee57c02008-04-04 06:12:32 +000044 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
45 SourceLocation(),
Chris Lattnere4650482008-03-15 06:12:44 +000046 &Context.Idents.get("SEL"),
Chris Lattner58114f02008-03-15 21:32:50 +000047 SelT, 0);
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000048 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000049 Context.setObjCSelType(SelTypedef);
Chris Lattner4e9553a2008-06-21 20:20:39 +000050
Chris Lattner1062d3a2008-06-21 22:44:51 +000051 // FIXME: Make sure these don't leak!
Chris Lattner4e9553a2008-06-21 20:20:39 +000052 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
53 CurContext,
54 SourceLocation(),
55 &Context.Idents.get("objc_class"),
56 0);
57 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
58 TypedefDecl *ClassTypedef =
59 TypedefDecl::Create(Context, CurContext, SourceLocation(),
60 &Context.Idents.get("Class"), ClassT, 0);
61 PushOnScopeChains(ClassTag, TUScope);
62 PushOnScopeChains(ClassTypedef, TUScope);
63 Context.setObjCClassType(ClassTypedef);
64 // Synthesize "@class Protocol;
65 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattner5cece462008-07-21 07:06:49 +000066 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner4e9553a2008-06-21 20:20:39 +000067 &Context.Idents.get("Protocol"),
68 SourceLocation(), true);
69 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
70 PushOnScopeChains(ProtocolDecl, TUScope);
71
72 // Synthesize "typedef struct objc_object { Class isa; } *id;"
73 RecordDecl *ObjectTag =
74 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
75 SourceLocation(),
76 &Context.Idents.get("objc_object"), 0);
77 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
78 PushOnScopeChains(ObjectTag, TUScope);
79 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
80 SourceLocation(),
81 &Context.Idents.get("id"),
82 ObjT, 0);
83 PushOnScopeChains(IdTypedef, TUScope);
84 Context.setObjCIdType(IdTypedef);
Steve Naroffee1de132007-10-10 21:53:07 +000085}
86
Chris Lattnera8c2d592008-02-06 00:46:58 +000087Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000088 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattner2e64c072007-08-10 20:18:51 +000089
90 // Get IdentifierInfo objects for known functions for which we
91 // do extra checking.
Chris Lattnera8c2d592008-02-06 00:46:58 +000092 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner2e64c072007-08-10 20:18:51 +000093
Chris Lattnera8c2d592008-02-06 00:46:58 +000094 KnownFunctionIDs[id_printf] = &IT.get("printf");
95 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
96 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
97 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
98 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek225a14c2008-06-16 18:00:42 +000099 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattnera8c2d592008-02-06 00:46:58 +0000100 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
101 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
102 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
103 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
104 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroffae84af82007-10-31 18:42:27 +0000105
Daniel Dunbar4837ae72008-08-14 22:04:54 +0000106 SuperID = &IT.get("super");
107
Steve Naroffee1de132007-10-10 21:53:07 +0000108 TUScope = 0;
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000109 if (getLangOptions().CPlusPlus)
110 FieldCollector.reset(new CXXFieldCollector());
Chris Lattner4b009652007-07-25 00:24:17 +0000111}
112
Chris Lattnere992d6c2008-01-16 19:17:22 +0000113/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
114/// If there is already an implicit cast, merge into the existing one.
115void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000116 if (Context.getCanonicalType(Expr->getType()) ==
117 Context.getCanonicalType(Type)) return;
Chris Lattnere992d6c2008-01-16 19:17:22 +0000118
119 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
120 ImpCast->setType(Type);
121 else
122 Expr = new ImplicitCastExpr(Type, Expr);
123}
124
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000125void Sema::DeleteExpr(ExprTy *E) {
126 delete static_cast<Expr*>(E);
127}
128void Sema::DeleteStmt(StmtTy *S) {
129 delete static_cast<Stmt*>(S);
130}
131
Chris Lattnerc1aea812008-08-23 03:19:52 +0000132/// ActOnEndOfTranslationUnit - This is called at the very end of the
133/// translation unit when EOF is reached and all but the top-level scope is
134/// popped.
135void Sema::ActOnEndOfTranslationUnit() {
136
137}
138
139
Chris Lattner4b009652007-07-25 00:24:17 +0000140//===----------------------------------------------------------------------===//
141// Helper functions.
142//===----------------------------------------------------------------------===//
143
144bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000145 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000146 return true;
147}
148
149bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000150 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000151 return true;
152}
153
154bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
155 const std::string &Msg2) {
156 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000157 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000158 return true;
159}
160
161bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000162 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000163 return true;
164}
165
166bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
167 SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000168 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000169 return true;
170}
171
172bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
173 const std::string &Msg2, SourceRange Range) {
174 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000175 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000176 return true;
177}
178
Chris Lattner01e854d2008-01-03 23:38:43 +0000179bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
180 const std::string &Msg2, const std::string &Msg3,
181 SourceRange R1) {
182 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
183 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
184 return true;
185}
186
Chris Lattner4b009652007-07-25 00:24:17 +0000187bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
188 SourceRange R1, SourceRange R2) {
189 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000190 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000191 return true;
192}
193
194bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
195 SourceRange R1, SourceRange R2) {
196 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000197 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000198 return true;
199}
200
201bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
202 const std::string &Msg2, SourceRange R1, SourceRange R2) {
203 std::string MsgArr[] = { Msg1, Msg2 };
204 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000205 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000206 return true;
207}
208
209const LangOptions &Sema::getLangOptions() const {
210 return PP.getLangOptions();
211}
Daniel Dunbar64789f82008-08-11 05:35:13 +0000212
213ObjCMethodDecl *Sema::getCurMethodDecl() {
214 return dyn_cast<ObjCMethodDecl>(CurContext);
215}