blob: a51b0751f5d1b1a0895c14d95724de17d2a56cba [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 Lattnerb26b7ad2007-08-31 04:53:24 +0000123void Sema::DeleteExpr(ExprTy *E) {
124 delete static_cast<Expr*>(E);
125}
126void Sema::DeleteStmt(StmtTy *S) {
127 delete static_cast<Stmt*>(S);
128}
129
Chris Lattner4b009652007-07-25 00:24:17 +0000130//===----------------------------------------------------------------------===//
131// Helper functions.
132//===----------------------------------------------------------------------===//
133
134bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000135 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000136 return true;
137}
138
139bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000140 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000141 return true;
142}
143
144bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
145 const std::string &Msg2) {
146 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000147 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 return true;
149}
150
151bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000152 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000153 return true;
154}
155
156bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
157 SourceRange Range) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000158 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Chris Lattner4b009652007-07-25 00:24:17 +0000159 return true;
160}
161
162bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
163 const std::string &Msg2, SourceRange Range) {
164 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000165 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Chris Lattner4b009652007-07-25 00:24:17 +0000166 return true;
167}
168
Chris Lattner01e854d2008-01-03 23:38:43 +0000169bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
170 const std::string &Msg2, const std::string &Msg3,
171 SourceRange R1) {
172 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
173 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
174 return true;
175}
176
Chris Lattner4b009652007-07-25 00:24:17 +0000177bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
178 SourceRange R1, SourceRange R2) {
179 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000180 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000181 return true;
182}
183
184bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
185 SourceRange R1, SourceRange R2) {
186 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000187 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000188 return true;
189}
190
191bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
192 const std::string &Msg2, SourceRange R1, SourceRange R2) {
193 std::string MsgArr[] = { Msg1, Msg2 };
194 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000195 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000196 return true;
197}
198
199const LangOptions &Sema::getLangOptions() const {
200 return PP.getLangOptions();
201}