blob: b53147f6eb7d6016817894287bcb2078ba457116 [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;
Steve Naroffae84af82007-10-31 18:42:27 +000045 if (PP.getLangOptions().ObjC1) {
46 TypedefType *t;
Chris Lattner77470dc2007-10-30 20:57:56 +000047
Steve Naroffae84af82007-10-31 18:42:27 +000048 // Add the built-in ObjC types.
Ted Kremenek42730c52008-01-07 19:49:32 +000049 t = dyn_cast<TypedefType>(Context.getObjCIdType().getTypePtr());
Steve Naroffae84af82007-10-31 18:42:27 +000050 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
51 TUScope->AddDecl(t->getDecl());
Ted Kremenek42730c52008-01-07 19:49:32 +000052 t = dyn_cast<TypedefType>(Context.getObjCClassType().getTypePtr());
Steve Naroffae84af82007-10-31 18:42:27 +000053 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
54 TUScope->AddDecl(t->getDecl());
Ted Kremenek42730c52008-01-07 19:49:32 +000055 ObjCInterfaceType *it = dyn_cast<ObjCInterfaceType>(Context.getObjCProtoType());
56 ObjCInterfaceDecl *IDecl = it->getDecl();
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000057 IDecl->getIdentifier()->setFETokenInfo(IDecl);
58 TUScope->AddDecl(IDecl);
Ted Kremenek42730c52008-01-07 19:49:32 +000059 t = dyn_cast<TypedefType>(Context.getObjCSelType().getTypePtr());
Steve Naroffae84af82007-10-31 18:42:27 +000060 t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl());
61 TUScope->AddDecl(t->getDecl());
Steve Naroffee1de132007-10-10 21:53:07 +000062 }
Steve Naroffee1de132007-10-10 21:53:07 +000063}
64
Steve Naroffca44ffd2007-11-29 23:05:20 +000065Sema::Sema(Preprocessor &pp, ASTContext &ctxt)
Anders Carlssonc9225262007-11-30 20:01:38 +000066 : PP(pp), Context(ctxt), CurFunctionDecl(0), CurMethodDecl(0) {
Chris Lattner2e64c072007-08-10 20:18:51 +000067
68 // Get IdentifierInfo objects for known functions for which we
69 // do extra checking.
70 IdentifierTable& IT = PP.getIdentifierTable();
71
72 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
73 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
74 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
75 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000076 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenek2d7e9532007-08-10 21:13:51 +000077 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000078 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
79 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
80 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
81 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Steve Naroffae84af82007-10-31 18:42:27 +000082
83 if (PP.getLangOptions().ObjC1) {
84 // Synthesize "typedef struct objc_class *Class;"
85 RecordDecl *ClassTag = new RecordDecl(Decl::Struct, SourceLocation(),
86 &IT.get("objc_class"), 0);
87 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
88 TypedefDecl *ClassTypedef = new TypedefDecl(SourceLocation(),
89 &Context.Idents.get("Class"),
90 ClassT, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +000091 Context.setObjCClassType(ClassTypedef);
Steve Naroffae84af82007-10-31 18:42:27 +000092
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000093 // Synthesize "@class Protocol;
Ted Kremenek42730c52008-01-07 19:49:32 +000094 ObjCInterfaceDecl *ProtocolDecl = new ObjCInterfaceDecl(SourceLocation(), 0,
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000095 &Context.Idents.get("Protocol"), true);
Ted Kremenek42730c52008-01-07 19:49:32 +000096 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +000097
Steve Naroffae84af82007-10-31 18:42:27 +000098 // Synthesize "typedef struct objc_object { Class isa; } *id;"
99 RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(),
100 &IT.get("objc_object"), 0);
101 FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0,
Ted Kremenek42730c52008-01-07 19:49:32 +0000102 Context.getObjCClassType());
Steve Naroffae84af82007-10-31 18:42:27 +0000103 ObjectTag->defineBody(&IsaDecl, 1);
104 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
105 TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(),
106 &Context.Idents.get("id"),
107 ObjT, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +0000108 Context.setObjCIdType(IdTypedef);
Steve Naroffae84af82007-10-31 18:42:27 +0000109
110 // Synthesize "typedef struct objc_selector *SEL;"
111 RecordDecl *SelTag = new RecordDecl(Decl::Struct, SourceLocation(),
112 &IT.get("objc_selector"), 0);
113 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
114 TypedefDecl *SelTypedef = new TypedefDecl(SourceLocation(),
115 &Context.Idents.get("SEL"),
116 SelT, 0);
Ted Kremenek42730c52008-01-07 19:49:32 +0000117 Context.setObjCSelType(SelTypedef);
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +0000118
Steve Naroffae84af82007-10-31 18:42:27 +0000119 }
Steve Naroffee1de132007-10-10 21:53:07 +0000120 TUScope = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000121}
122
Chris Lattnere992d6c2008-01-16 19:17:22 +0000123/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
124/// If there is already an implicit cast, merge into the existing one.
125void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
126 if (Expr->getType() == Type) return;
127
128 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
129 ImpCast->setType(Type);
130 else
131 Expr = new ImplicitCastExpr(Type, Expr);
132}
133
134
135
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000136void Sema::DeleteExpr(ExprTy *E) {
137 delete static_cast<Expr*>(E);
138}
139void Sema::DeleteStmt(StmtTy *S) {
140 delete static_cast<Stmt*>(S);
141}
142
Chris Lattner4b009652007-07-25 00:24:17 +0000143//===----------------------------------------------------------------------===//
144// Helper functions.
145//===----------------------------------------------------------------------===//
146
147bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000148 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000149 return true;
150}
151
152bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000153 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000154 return true;
155}
156
157bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
158 const std::string &Msg2) {
159 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000160 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000161 return true;
162}
163
164bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000165 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000166 return true;
167}
168
169bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
170 SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000171 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000172 return true;
173}
174
175bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
176 const std::string &Msg2, SourceRange Range) {
177 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000178 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000179 return true;
180}
181
Chris Lattner01e854d2008-01-03 23:38:43 +0000182bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
183 const std::string &Msg2, const std::string &Msg3,
184 SourceRange R1) {
185 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
186 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
187 return true;
188}
189
Chris Lattner4b009652007-07-25 00:24:17 +0000190bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
191 SourceRange R1, SourceRange R2) {
192 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000193 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000194 return true;
195}
196
197bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
198 SourceRange R1, SourceRange R2) {
199 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000200 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000201 return true;
202}
203
204bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
205 const std::string &Msg2, SourceRange R1, SourceRange R2) {
206 std::string MsgArr[] = { Msg1, Msg2 };
207 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000208 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000209 return true;
210}
211
212const LangOptions &Sema::getLangOptions() const {
213 return PP.getLangOptions();
214}