]> granicus.if.org Git - clang/blob - Sema/Sema.cpp
avoid making implicit casts that just remove typedefs.
[clang] / Sema / Sema.cpp
1 //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
19 #include "clang/Parse/Scope.h"
20
21 using namespace clang;
22
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;
27 }
28
29 bool Sema::isObjCObjectPointerType(QualType type) const {
30   if (!type->isPointerType() && !type->isObjCQualifiedIdType())
31     return false;
32   if (type == Context.getObjCIdType() || type == Context.getObjCClassType() ||
33       type->isObjCQualifiedIdType())
34     return true;
35   
36   if (type->isPointerType()) {
37     PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr());
38     type = pointerType->getPointeeType();
39   }
40   return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType());
41 }
42
43 void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
44   TUScope = S;
45   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());
63 }
64
65 Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
66   : PP(pp), Context(ctxt), Consumer(consumer), 
67     CurFunctionDecl(0), CurMethodDecl(0) {
68   
69   // Get IdentifierInfo objects for known functions for which we
70   // do extra checking.  
71   IdentifierTable &IT = PP.getIdentifierTable();  
72
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");
83
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);
92     Context.setObjCClassType(ClassTypedef);
93     
94     // Synthesize "@class Protocol;
95     ObjCInterfaceDecl *ProtocolDecl = new ObjCInterfaceDecl(SourceLocation(), 0, 
96                                         &Context.Idents.get("Protocol"), true);
97     Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
98     
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"),
108                                              ObjT, 0);
109     Context.setObjCIdType(IdTypedef);
110     
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);
118     Context.setObjCSelType(SelTypedef);
119     
120   }
121   TUScope = 0;
122 }
123
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;
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
137 void Sema::DeleteExpr(ExprTy *E) {
138   delete static_cast<Expr*>(E);
139 }
140 void Sema::DeleteStmt(StmtTy *S) {
141   delete static_cast<Stmt*>(S);
142 }
143
144 //===----------------------------------------------------------------------===//
145 // Helper functions.
146 //===----------------------------------------------------------------------===//
147
148 bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
149   PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
150   return true;
151 }
152
153 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
154   PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  &Msg, 1);
155   return true;
156 }
157
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);
162   return true;
163 }
164
165 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
166   PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
167   return true;
168 }
169
170 bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
171                 SourceRange Range) {
172   PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
173   return true;
174 }
175
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);
180   return true;
181 }
182
183 bool 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
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);
195   return true;
196 }
197
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);
202   return true;
203 }
204
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);
210   return true;
211 }
212
213 const LangOptions &Sema::getLangOptions() const {
214   return PP.getLangOptions();
215 }