blob: 3296bf3550a46f9b07f646b78cefe4399829a4ae [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"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/Diagnostic.h"
Steve Naroffae84af82007-10-31 18:42:27 +000019#include "clang/Parse/Scope.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000020
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Ted Kremenek42730c52008-01-07 19:49:32 +000023bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroffae84af82007-10-31 18:42:27 +000024 const char *typeName = TD->getIdentifier()->getName();
25 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000026 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroffae84af82007-10-31 18:42:27 +000027}
28
Ted Kremenek42730c52008-01-07 19:49:32 +000029bool Sema::isObjCObjectPointerType(QualType type) const {
30 if (!type->isPointerType() && !type->isObjCQualifiedIdType())
Fariborz Jahanianfe0982d2008-01-04 00:27:46 +000031 return false;
Ted Kremenek42730c52008-01-07 19:49:32 +000032 if (type == Context.getObjCIdType() || type == Context.getObjCClassType() ||
33 type->isObjCQualifiedIdType())
Fariborz Jahanianfe0982d2008-01-04 00:27:46 +000034 return true;
35
Fariborz Jahanianc7612612008-01-07 18:14:04 +000036 if (type->isPointerType()) {
Fariborz Jahanianfe0982d2008-01-04 00:27:46 +000037 PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
38 type = pointerType->getPointeeType();
39 }
Ted Kremenek42730c52008-01-07 19:49:32 +000040 return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType());
Fariborz Jahanianfe0982d2008-01-04 00:27:46 +000041}
42
Steve Naroff9637a9b2007-10-09 22:01:59 +000043void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
44 TUScope = S;
Argiris Kirtzidisd3586002008-04-17 14:40:12 +000045 CurContext = Context.getTranslationUnitDecl();
Chris Lattnera8c2d592008-02-06 00:46:58 +000046 if (!PP.getLangOptions().ObjC1) return;
47
Steve Naroff8f635e02008-02-24 16:25:02 +000048 // Synthesize "typedef struct objc_selector *SEL;"
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000049 RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
Chris Lattner58114f02008-03-15 21:32:50 +000050 SourceLocation(),
Chris Lattnere4650482008-03-15 06:12:44 +000051 &Context.Idents.get("objc_selector"),
Chris Lattner58114f02008-03-15 21:32:50 +000052 0);
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000053 PushOnScopeChains(SelTag, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000054
55 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattnereee57c02008-04-04 06:12:32 +000056 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
57 SourceLocation(),
Chris Lattnere4650482008-03-15 06:12:44 +000058 &Context.Idents.get("SEL"),
Chris Lattner58114f02008-03-15 21:32:50 +000059 SelT, 0);
Argiris Kirtzidis951f25b2008-04-12 00:47:19 +000060 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff8f635e02008-02-24 16:25:02 +000061 Context.setObjCSelType(SelTypedef);
Chris Lattner4e9553a2008-06-21 20:20:39 +000062
63 RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
64 CurContext,
65 SourceLocation(),
66 &Context.Idents.get("objc_class"),
67 0);
68 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
69 TypedefDecl *ClassTypedef =
70 TypedefDecl::Create(Context, CurContext, SourceLocation(),
71 &Context.Idents.get("Class"), ClassT, 0);
72 PushOnScopeChains(ClassTag, TUScope);
73 PushOnScopeChains(ClassTypedef, TUScope);
74 Context.setObjCClassType(ClassTypedef);
75 // Synthesize "@class Protocol;
76 ObjCInterfaceDecl *ProtocolDecl =
77 ObjCInterfaceDecl::Create(Context, SourceLocation(), 0,
78 &Context.Idents.get("Protocol"),
79 SourceLocation(), true);
80 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
81 PushOnScopeChains(ProtocolDecl, TUScope);
82
83 // Synthesize "typedef struct objc_object { Class isa; } *id;"
84 RecordDecl *ObjectTag =
85 RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
86 SourceLocation(),
87 &Context.Idents.get("objc_object"), 0);
88 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
89 PushOnScopeChains(ObjectTag, TUScope);
90 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
91 SourceLocation(),
92 &Context.Idents.get("id"),
93 ObjT, 0);
94 PushOnScopeChains(IdTypedef, TUScope);
95 Context.setObjCIdType(IdTypedef);
Steve Naroffee1de132007-10-10 21:53:07 +000096}
97
Chris Lattnera8c2d592008-02-06 00:46:58 +000098Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
99 : PP(pp), Context(ctxt), Consumer(consumer),
Chris Lattnereee57c02008-04-04 06:12:32 +0000100 CurFunctionDecl(0), CurMethodDecl(0), CurContext(0) {
Chris Lattner2e64c072007-08-10 20:18:51 +0000101
102 // Get IdentifierInfo objects for known functions for which we
103 // do extra checking.
Chris Lattnera8c2d592008-02-06 00:46:58 +0000104 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner2e64c072007-08-10 20:18:51 +0000105
Chris Lattnera8c2d592008-02-06 00:46:58 +0000106 KnownFunctionIDs[id_printf] = &IT.get("printf");
107 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
108 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
109 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
110 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek225a14c2008-06-16 18:00:42 +0000111 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattnera8c2d592008-02-06 00:46:58 +0000112 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
113 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
114 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
115 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
116 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroffae84af82007-10-31 18:42:27 +0000117
Steve Naroffee1de132007-10-10 21:53:07 +0000118 TUScope = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000119}
120
Chris Lattnere992d6c2008-01-16 19:17:22 +0000121/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
122/// If there is already an implicit cast, merge into the existing one.
123void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattner9764da92008-02-13 18:01:07 +0000124 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
Chris Lattnere992d6c2008-01-16 19:17:22 +0000125
126 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
127 ImpCast->setType(Type);
128 else
129 Expr = new ImplicitCastExpr(Type, Expr);
130}
131
132
133
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000134void Sema::DeleteExpr(ExprTy *E) {
135 delete static_cast<Expr*>(E);
136}
137void Sema::DeleteStmt(StmtTy *S) {
138 delete static_cast<Stmt*>(S);
139}
140
Chris Lattner4b009652007-07-25 00:24:17 +0000141//===----------------------------------------------------------------------===//
142// Helper functions.
143//===----------------------------------------------------------------------===//
144
145bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000146 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000147 return true;
148}
149
150bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000151 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000152 return true;
153}
154
155bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
156 const std::string &Msg2) {
157 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000158 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000159 return true;
160}
161
162bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000163 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000164 return true;
165}
166
167bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
168 SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000169 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000170 return true;
171}
172
173bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
174 const std::string &Msg2, SourceRange Range) {
175 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000176 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000177 return true;
178}
179
Chris Lattner01e854d2008-01-03 23:38:43 +0000180bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
181 const std::string &Msg2, const std::string &Msg3,
182 SourceRange R1) {
183 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
184 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
185 return true;
186}
187
Chris Lattner4b009652007-07-25 00:24:17 +0000188bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
189 SourceRange R1, SourceRange R2) {
190 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000191 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000192 return true;
193}
194
195bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
196 SourceRange R1, SourceRange R2) {
197 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000198 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000199 return true;
200}
201
202bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
203 const std::string &Msg2, SourceRange R1, SourceRange R2) {
204 std::string MsgArr[] = { Msg1, Msg2 };
205 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000206 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000207 return true;
208}
209
210const LangOptions &Sema::getLangOptions() const {
211 return PP.getLangOptions();
212}