blob: ffa81a2736e6b96cf96f27937d97c1d31b1515ee [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;
Chris Lattnera8c2d592008-02-06 00:46:58 +000045 if (!PP.getLangOptions().ObjC1) return;
46
47 TypedefType *t;
48
49 // Add the built-in ObjC types.
50 t = cast<TypedefType>(Context.getObjCIdType().getTypePtr());
51 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
52 TUScope->AddDecl(t->getDecl());
53 t = cast<TypedefType>(Context.getObjCClassType().getTypePtr());
54 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
55 TUScope->AddDecl(t->getDecl());
56 ObjCInterfaceType *it = cast<ObjCInterfaceType>(Context.getObjCProtoType());
57 ObjCInterfaceDecl *IDecl = it->getDecl();
58 IDecl->getIdentifier()->setFETokenInfo(IDecl);
59 TUScope->AddDecl(IDecl);
60 t = cast<TypedefType>(Context.getObjCSelType().getTypePtr());
61 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
62 TUScope->AddDecl(t->getDecl());
Steve Naroffee1de132007-10-10 21:53:07 +000063}
64
Chris Lattnera8c2d592008-02-06 00:46:58 +000065Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
66 : PP(pp), Context(ctxt), Consumer(consumer),
67 CurFunctionDecl(0), CurMethodDecl(0) {
Chris Lattner2e64c072007-08-10 20:18:51 +000068
69 // Get IdentifierInfo objects for known functions for which we
70 // do extra checking.
Chris Lattnera8c2d592008-02-06 00:46:58 +000071 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner2e64c072007-08-10 20:18:51 +000072
Chris Lattnera8c2d592008-02-06 00:46:58 +000073 KnownFunctionIDs[id_printf] = &IT.get("printf");
74 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
75 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
76 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
77 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
78 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
79 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
80 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
81 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
82 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroffae84af82007-10-31 18:42:27 +000083
84 if (PP.getLangOptions().ObjC1) {
85 // Synthesize "typedef struct objc_class *Class;"
86 RecordDecl *ClassTag = new RecordDecl(Decl::Struct, SourceLocation(),
87 &IT.get("objc_class"), 0);
88 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
89 TypedefDecl *ClassTypedef = new TypedefDecl(SourceLocation(),
90 &Context.Idents.get("Class"),
91 ClassT, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +000092 Context.setObjCClassType(ClassTypedef);
Steve Naroffae84af82007-10-31 18:42:27 +000093
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000094 // Synthesize "@class Protocol;
Ted Kremenek42730c52008-01-07 19:49:32 +000095 ObjCInterfaceDecl *ProtocolDecl = new ObjCInterfaceDecl(SourceLocation(), 0,
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000096 &Context.Idents.get("Protocol"), true);
Ted Kremenek42730c52008-01-07 19:49:32 +000097 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000098
Steve Naroffae84af82007-10-31 18:42:27 +000099 // Synthesize "typedef struct objc_object { Class isa; } *id;"
100 RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(),
101 &IT.get("objc_object"), 0);
102 FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0,
Ted Kremenek42730c52008-01-07 19:49:32 +0000103 Context.getObjCClassType());
Steve Naroffae84af82007-10-31 18:42:27 +0000104 ObjectTag->defineBody(&IsaDecl, 1);
105 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
106 TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(),
107 &Context.Idents.get("id"),
108 ObjT, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 Context.setObjCIdType(IdTypedef);
Steve Naroffae84af82007-10-31 18:42:27 +0000110
111 // Synthesize "typedef struct objc_selector *SEL;"
112 RecordDecl *SelTag = new RecordDecl(Decl::Struct, SourceLocation(),
113 &IT.get("objc_selector"), 0);
114 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
115 TypedefDecl *SelTypedef = new TypedefDecl(SourceLocation(),
116 &Context.Idents.get("SEL"),
117 SelT, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +0000118 Context.setObjCSelType(SelTypedef);
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +0000119
Steve Naroffae84af82007-10-31 18:42:27 +0000120 }
Steve Naroffee1de132007-10-10 21:53:07 +0000121 TUScope = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000122}
123
Chris Lattnere992d6c2008-01-16 19:17:22 +0000124/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
125/// If there is already an implicit cast, merge into the existing one.
126void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
127 if (Expr->getType() == Type) return;
128
129 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
130 ImpCast->setType(Type);
131 else
132 Expr = new ImplicitCastExpr(Type, Expr);
133}
134
135
136
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000137void Sema::DeleteExpr(ExprTy *E) {
138 delete static_cast<Expr*>(E);
139}
140void Sema::DeleteStmt(StmtTy *S) {
141 delete static_cast<Stmt*>(S);
142}
143
Chris Lattner4b009652007-07-25 00:24:17 +0000144//===----------------------------------------------------------------------===//
145// Helper functions.
146//===----------------------------------------------------------------------===//
147
148bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000149 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000150 return true;
151}
152
153bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000154 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000155 return true;
156}
157
158bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
159 const std::string &Msg2) {
160 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000161 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000162 return true;
163}
164
165bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000166 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000167 return true;
168}
169
170bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
171 SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000172 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000173 return true;
174}
175
176bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
177 const std::string &Msg2, SourceRange Range) {
178 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000179 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000180 return true;
181}
182
Chris Lattner01e854d2008-01-03 23:38:43 +0000183bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
184 const std::string &Msg2, const std::string &Msg3,
185 SourceRange R1) {
186 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
187 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
188 return true;
189}
190
Chris Lattner4b009652007-07-25 00:24:17 +0000191bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
192 SourceRange R1, SourceRange R2) {
193 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000194 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000195 return true;
196}
197
198bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
199 SourceRange R1, SourceRange R2) {
200 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000201 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000202 return true;
203}
204
205bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
206 const std::string &Msg2, SourceRange R1, SourceRange R2) {
207 std::string MsgArr[] = { Msg1, Msg2 };
208 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000209 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000210 return true;
211}
212
213const LangOptions &Sema::getLangOptions() const {
214 return PP.getLangOptions();
215}