Sema.cpp revision 225a14c2ff9acbecaae369367432c1685a4073dc
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
21using namespace clang;
22
23bool 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
29bool 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
43void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
44  TUScope = S;
45  CurContext = Context.getTranslationUnitDecl();
46  if (!PP.getLangOptions().ObjC1) return;
47
48  TypedefType *t;
49
50  // Add the built-in ObjC types.
51  t = cast<TypedefType>(Context.getObjCIdType().getTypePtr());
52  PushOnScopeChains(t->getDecl(), TUScope);
53  t = cast<TypedefType>(Context.getObjCClassType().getTypePtr());
54  PushOnScopeChains(t->getDecl(), TUScope);
55  ObjCInterfaceType *it = cast<ObjCInterfaceType>(Context.getObjCProtoType());
56  ObjCInterfaceDecl *IDecl = it->getDecl();
57  PushOnScopeChains(IDecl, TUScope);
58
59  // Synthesize "typedef struct objc_selector *SEL;"
60  RecordDecl *SelTag = RecordDecl::Create(Context, TagDecl::TK_struct, CurContext,
61                                          SourceLocation(),
62                                          &Context.Idents.get("objc_selector"),
63                                          0);
64  PushOnScopeChains(SelTag, TUScope);
65
66  QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
67  TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
68                                                SourceLocation(),
69                                                &Context.Idents.get("SEL"),
70                                                SelT, 0);
71  PushOnScopeChains(SelTypedef, TUScope);
72  Context.setObjCSelType(SelTypedef);
73}
74
75Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
76  : PP(pp), Context(ctxt), Consumer(consumer),
77    CurFunctionDecl(0), CurMethodDecl(0), CurContext(0) {
78
79  // Get IdentifierInfo objects for known functions for which we
80  // do extra checking.
81  IdentifierTable &IT = PP.getIdentifierTable();
82
83  KnownFunctionIDs[id_printf]    = &IT.get("printf");
84  KnownFunctionIDs[id_fprintf]   = &IT.get("fprintf");
85  KnownFunctionIDs[id_sprintf]   = &IT.get("sprintf");
86  KnownFunctionIDs[id_snprintf]  = &IT.get("snprintf");
87  KnownFunctionIDs[id_asprintf]  = &IT.get("asprintf");
88  KnownFunctionIDs[id_NSLog]     = &IT.get("NSLog");
89  KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
90  KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
91  KnownFunctionIDs[id_vfprintf]  = &IT.get("vfprintf");
92  KnownFunctionIDs[id_vsprintf]  = &IT.get("vsprintf");
93  KnownFunctionIDs[id_vprintf]   = &IT.get("vprintf");
94
95  // FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope()
96  // and make sure the decls get inserted into TUScope!
97  // FIXME: And make sure they don't leak!
98  if (PP.getLangOptions().ObjC1) {
99    TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
100
101    // Synthesize "typedef struct objc_class *Class;"
102    RecordDecl *ClassTag = RecordDecl::Create(Context, TagDecl::TK_struct,
103                                              TUDecl,
104                                              SourceLocation(),
105                                              &IT.get("objc_class"), 0);
106    QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
107    TypedefDecl *ClassTypedef =
108      TypedefDecl::Create(Context, TUDecl, SourceLocation(),
109                          &Context.Idents.get("Class"), ClassT, 0);
110    Context.setObjCClassType(ClassTypedef);
111
112    // Synthesize "@class Protocol;
113    ObjCInterfaceDecl *ProtocolDecl =
114      ObjCInterfaceDecl::Create(Context, SourceLocation(), 0,
115                                &Context.Idents.get("Protocol"),
116                                SourceLocation(), true);
117    Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
118
119    // Synthesize "typedef struct objc_object { Class isa; } *id;"
120    RecordDecl *ObjectTag =
121      RecordDecl::Create(Context, TagDecl::TK_struct, TUDecl,
122                         SourceLocation(),
123                         &IT.get("objc_object"), 0);
124    FieldDecl *IsaDecl = FieldDecl::Create(Context, SourceLocation(),
125                                           &Context.Idents.get("isa"),
126                                           Context.getObjCClassType());
127    ObjectTag->defineBody(&IsaDecl, 1);
128    QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
129    TypedefDecl *IdTypedef = TypedefDecl::Create(Context, TUDecl,
130                                                 SourceLocation(),
131                                                 &Context.Idents.get("id"),
132                                                 ObjT, 0);
133    Context.setObjCIdType(IdTypedef);
134  }
135  TUScope = 0;
136}
137
138/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
139/// If there is already an implicit cast, merge into the existing one.
140void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
141  if (Expr->getType().getCanonicalType() == Type.getCanonicalType()) return;
142
143  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
144    ImpCast->setType(Type);
145  else
146    Expr = new ImplicitCastExpr(Type, Expr);
147}
148
149
150
151void Sema::DeleteExpr(ExprTy *E) {
152  delete static_cast<Expr*>(E);
153}
154void Sema::DeleteStmt(StmtTy *S) {
155  delete static_cast<Stmt*>(S);
156}
157
158//===----------------------------------------------------------------------===//
159// Helper functions.
160//===----------------------------------------------------------------------===//
161
162bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
163  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
164  return true;
165}
166
167bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
168  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  &Msg, 1);
169  return true;
170}
171
172bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
173                const std::string &Msg2) {
174  std::string MsgArr[] = { Msg1, Msg2 };
175  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  MsgArr, 2);
176  return true;
177}
178
179bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
180  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
181  return true;
182}
183
184bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
185                SourceRange Range) {
186  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
187  return true;
188}
189
190bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
191                const std::string &Msg2, SourceRange Range) {
192  std::string MsgArr[] = { Msg1, Msg2 };
193  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
194  return true;
195}
196
197bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
198                const std::string &Msg2, const std::string &Msg3,
199                SourceRange R1) {
200  std::string MsgArr[] = { Msg1, Msg2, Msg3 };
201  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
202  return true;
203}
204
205bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
206                SourceRange R1, SourceRange R2) {
207  SourceRange RangeArr[] = { R1, R2 };
208  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
209  return true;
210}
211
212bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
213                SourceRange R1, SourceRange R2) {
214  SourceRange RangeArr[] = { R1, R2 };
215  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  &Msg, 1, RangeArr, 2);
216  return true;
217}
218
219bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
220                const std::string &Msg2, SourceRange R1, SourceRange R2) {
221  std::string MsgArr[] = { Msg1, Msg2 };
222  SourceRange RangeArr[] = { R1, R2 };
223  PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
224  return true;
225}
226
227const LangOptions &Sema::getLangOptions() const {
228  return PP.getLangOptions();
229}
230