blob: 2e6617be1034a693fdb84b9ec0a28a2c9c120e0a [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"
Steve Naroffae84af82007-10-31 18:42:27 +000021#include "clang/Parse/Scope.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
Ted Kremenek42730c52008-01-07 19:49:32 +000025bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroffae84af82007-10-31 18:42:27 +000026 const char *typeName = TD->getIdentifier()->getName();
27 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000028 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroffae84af82007-10-31 18:42:27 +000029}
30
Steve Naroff9637a9b2007-10-09 22:01:59 +000031void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
32 TUScope = S;
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000033 CurContext = Context.getTranslationUnitDecl();
Chris Lattnera8c2d592008-02-06 00:46:58 +000034 if (!PP.getLangOptions().ObjC1) return;
35
Steve Naroff8f635e02008-02-24 16:25:02 +000036 // Synthesize "typedef struct objc_selector *SEL;"
Chris Lattner5cece462008-07-21 07:06:49 +000037 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct,
38 CurContext,
Chris Lattner58114f02008-03-15 21:32:50 +000039 SourceLocation(),
Chris Lattnere4650482008-03-15 06:12:44 +000040 &Context.Idents.get("objc_selector"),
Chris Lattner58114f02008-03-15 21:32:50 +000041 0);
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000042 PushOnScopeChains(SelTag, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000043
44 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnereee57c02008-04-04 06:12:32 +000045 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
46 SourceLocation(),
Chris Lattnere4650482008-03-15 06:12:44 +000047 &Context.Idents.get("SEL"),
Chris Lattner58114f02008-03-15 21:32:50 +000048 SelT, 0);
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000049 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000050 Context.setObjCSelType(SelTypedef);
Chris Lattner4e9553a2008-06-21 20:20:39 +000051
Chris Lattner1062d3a2008-06-21 22:44:51 +000052 // FIXME: Make sure these don't leak!
Chris Lattner4e9553a2008-06-21 20:20:39 +000053 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
54 CurContext,
55 SourceLocation(),
56 &Context.Idents.get("objc_class"),
57 0);
58 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
59 TypedefDecl *ClassTypedef =
60 TypedefDecl::Create(Context, CurContext, SourceLocation(),
61 &Context.Idents.get("Class"), ClassT, 0);
62 PushOnScopeChains(ClassTag, TUScope);
63 PushOnScopeChains(ClassTypedef, TUScope);
64 Context.setObjCClassType(ClassTypedef);
65 // Synthesize "@class Protocol;
66 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattner5cece462008-07-21 07:06:49 +000067 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner4e9553a2008-06-21 20:20:39 +000068 &Context.Idents.get("Protocol"),
69 SourceLocation(), true);
70 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
71 PushOnScopeChains(ProtocolDecl, TUScope);
72
73 // Synthesize "typedef struct objc_object { Class isa; } *id;"
74 RecordDecl *ObjectTag =
75 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
76 SourceLocation(),
77 &Context.Idents.get("objc_object"), 0);
78 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
79 PushOnScopeChains(ObjectTag, TUScope);
80 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
81 SourceLocation(),
82 &Context.Idents.get("id"),
83 ObjT, 0);
84 PushOnScopeChains(IdTypedef, TUScope);
85 Context.setObjCIdType(IdTypedef);
Steve Naroffee1de132007-10-10 21:53:07 +000086}
87
Chris Lattnera8c2d592008-02-06 00:46:58 +000088Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000089 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattner2e64c072007-08-10 20:18:51 +000090
91 // Get IdentifierInfo objects for known functions for which we
92 // do extra checking.
Chris Lattnera8c2d592008-02-06 00:46:58 +000093 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner2e64c072007-08-10 20:18:51 +000094
Chris Lattnera8c2d592008-02-06 00:46:58 +000095 KnownFunctionIDs[id_printf] = &IT.get("printf");
96 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
97 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
98 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
99 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek225a14c2008-06-16 18:00:42 +0000100 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattnera8c2d592008-02-06 00:46:58 +0000101 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
102 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
103 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
104 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
105 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroffae84af82007-10-31 18:42:27 +0000106
Steve Naroffee1de132007-10-10 21:53:07 +0000107 TUScope = 0;
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000108 if (getLangOptions().CPlusPlus)
109 FieldCollector.reset(new CXXFieldCollector());
Chris Lattner4b009652007-07-25 00:24:17 +0000110}
111
Chris Lattnere992d6c2008-01-16 19:17:22 +0000112/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
113/// If there is already an implicit cast, merge into the existing one.
114void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000115 if (Context.getCanonicalType(Expr->getType()) ==
116 Context.getCanonicalType(Type)) return;
Chris Lattnere992d6c2008-01-16 19:17:22 +0000117
118 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
119 ImpCast->setType(Type);
120 else
121 Expr = new ImplicitCastExpr(Type, Expr);
122}
123
124
125
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000126void Sema::DeleteExpr(ExprTy *E) {
127 delete static_cast<Expr*>(E);
128}
129void Sema::DeleteStmt(StmtTy *S) {
130 delete static_cast<Stmt*>(S);
131}
132
Chris Lattner4b009652007-07-25 00:24:17 +0000133//===----------------------------------------------------------------------===//
134// Helper functions.
135//===----------------------------------------------------------------------===//
136
137bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000138 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000139 return true;
140}
141
142bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000143 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000144 return true;
145}
146
147bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
148 const std::string &Msg2) {
149 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000150 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000151 return true;
152}
153
154bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000155 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000156 return true;
157}
158
159bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
160 SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000161 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000162 return true;
163}
164
165bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
166 const std::string &Msg2, SourceRange Range) {
167 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000168 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000169 return true;
170}
171
Chris Lattner01e854d2008-01-03 23:38:43 +0000172bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
173 const std::string &Msg2, const std::string &Msg3,
174 SourceRange R1) {
175 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
176 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
177 return true;
178}
179
Chris Lattner4b009652007-07-25 00:24:17 +0000180bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
181 SourceRange R1, SourceRange R2) {
182 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000183 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000184 return true;
185}
186
187bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
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, &Msg, 1, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000191 return true;
192}
193
194bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
195 const std::string &Msg2, SourceRange R1, SourceRange R2) {
196 std::string MsgArr[] = { Msg1, Msg2 };
197 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000198 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000199 return true;
200}
201
202const LangOptions &Sema::getLangOptions() const {
203 return PP.getLangOptions();
204}
Daniel Dunbar64789f82008-08-11 05:35:13 +0000205
206ObjCMethodDecl *Sema::getCurMethodDecl() {
207 return dyn_cast<ObjCMethodDecl>(CurContext);
208}