1 //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the actions class which performs semantic analysis and
11 // builds an AST out of a parse stream.
13 //===----------------------------------------------------------------------===//
16 #include "clang/AST/ASTContext.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Parse/Scope.h"
21 using namespace clang;
23 bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
24 const char *typeName = TD->getIdentifier()->getName();
25 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
26 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
29 bool Sema::isObjCObjectPointerType(QualType type) const {
30 if (!type->isPointerType() && !type->isObjCQualifiedIdType())
32 if (type == Context.getObjCIdType() || type == Context.getObjCClassType() ||
33 type->isObjCQualifiedIdType())
36 if (type->isPointerType()) {
37 PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
38 type = pointerType->getPointeeType();
40 return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType());
43 void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
45 if (!PP.getLangOptions().ObjC1) return;
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());
65 Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
66 : PP(pp), Context(ctxt), Consumer(consumer),
67 CurFunctionDecl(0), CurMethodDecl(0) {
69 // Get IdentifierInfo objects for known functions for which we
71 IdentifierTable &IT = PP.getIdentifierTable();
73 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");
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"),
92 Context.setObjCClassType(ClassTypedef);
94 // Synthesize "@class Protocol;
95 ObjCInterfaceDecl *ProtocolDecl = new ObjCInterfaceDecl(SourceLocation(), 0,
96 &Context.Idents.get("Protocol"), true);
97 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
99 // 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,
103 Context.getObjCClassType());
104 ObjectTag->defineBody(&IsaDecl, 1);
105 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
106 TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(),
107 &Context.Idents.get("id"),
109 Context.setObjCIdType(IdTypedef);
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"),
118 Context.setObjCSelType(SelTypedef);
124 /// 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.
126 void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
127 if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
129 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
130 ImpCast->setType(Type);
132 Expr = new ImplicitCastExpr(Type, Expr);
137 void Sema::DeleteExpr(ExprTy *E) {
138 delete static_cast<Expr*>(E);
140 void Sema::DeleteStmt(StmtTy *S) {
141 delete static_cast<Stmt*>(S);
144 //===----------------------------------------------------------------------===//
146 //===----------------------------------------------------------------------===//
148 bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
149 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
153 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
154 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
158 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
159 const std::string &Msg2) {
160 std::string MsgArr[] = { Msg1, Msg2 };
161 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
165 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
166 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
170 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
172 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
176 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
177 const std::string &Msg2, SourceRange Range) {
178 std::string MsgArr[] = { Msg1, Msg2 };
179 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
183 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
184 const std::string &Msg2, const std::string &Msg3,
186 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
187 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
191 bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
192 SourceRange R1, SourceRange R2) {
193 SourceRange RangeArr[] = { R1, R2 };
194 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
198 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
199 SourceRange R1, SourceRange R2) {
200 SourceRange RangeArr[] = { R1, R2 };
201 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
205 bool 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 };
209 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
213 const LangOptions &Sema::getLangOptions() const {
214 return PP.getLangOptions();