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