Sema.cpp revision df042e6c2bf06b2d9ed53c52469599ac1bd93a3f
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/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
19#include "clang/Lex/Preprocessor.h"
20#include "clang/Basic/Diagnostic.h"
21
22using namespace clang;
23
24bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
25  const char *typeName = TD->getIdentifier()->getName();
26  return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
27         strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
28}
29
30static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name)
31{
32  if (C.getLangOptions().CPlusPlus)
33    return CXXRecordDecl::Create(C, TagDecl::TK_struct,
34                                 C.getTranslationUnitDecl(),
35                                 SourceLocation(), &C.Idents.get(Name));
36  else
37    return RecordDecl::Create(C, TagDecl::TK_struct,
38                              C.getTranslationUnitDecl(),
39                              SourceLocation(), &C.Idents.get(Name));
40}
41
42void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
43  TUScope = S;
44  CurContext = Context.getTranslationUnitDecl();
45  if (!PP.getLangOptions().ObjC1) return;
46
47  // Synthesize "typedef struct objc_selector *SEL;"
48  RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
49  PushOnScopeChains(SelTag, TUScope);
50
51  QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
52  TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
53                                                SourceLocation(),
54                                                &Context.Idents.get("SEL"),
55                                                SelT, 0);
56  PushOnScopeChains(SelTypedef, TUScope);
57  Context.setObjCSelType(SelTypedef);
58
59  // FIXME: Make sure these don't leak!
60  RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
61  QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
62  TypedefDecl *ClassTypedef =
63    TypedefDecl::Create(Context, CurContext, SourceLocation(),
64                        &Context.Idents.get("Class"), ClassT, 0);
65  PushOnScopeChains(ClassTag, TUScope);
66  PushOnScopeChains(ClassTypedef, TUScope);
67  Context.setObjCClassType(ClassTypedef);
68  // Synthesize "@class Protocol;
69  ObjCInterfaceDecl *ProtocolDecl =
70    ObjCInterfaceDecl::Create(Context, SourceLocation(),
71                              &Context.Idents.get("Protocol"),
72                              SourceLocation(), true);
73  Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
74  PushOnScopeChains(ProtocolDecl, TUScope);
75
76  // Synthesize "typedef struct objc_object { Class isa; } *id;"
77  RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
78
79  QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
80  PushOnScopeChains(ObjectTag, TUScope);
81  TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
82                                               SourceLocation(),
83                                               &Context.Idents.get("id"),
84                                               ObjT, 0);
85  PushOnScopeChains(IdTypedef, TUScope);
86  Context.setObjCIdType(IdTypedef);
87}
88
89Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
90  : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0), CurBlock(0) {
91
92  // Get IdentifierInfo objects for known functions for which we
93  // do extra checking.
94  IdentifierTable &IT = PP.getIdentifierTable();
95
96  KnownFunctionIDs[id_printf]    = &IT.get("printf");
97  KnownFunctionIDs[id_fprintf]   = &IT.get("fprintf");
98  KnownFunctionIDs[id_sprintf]   = &IT.get("sprintf");
99  KnownFunctionIDs[id_snprintf]  = &IT.get("snprintf");
100  KnownFunctionIDs[id_asprintf]  = &IT.get("asprintf");
101  KnownFunctionIDs[id_NSLog]     = &IT.get("NSLog");
102  KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
103  KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
104  KnownFunctionIDs[id_vfprintf]  = &IT.get("vfprintf");
105  KnownFunctionIDs[id_vsprintf]  = &IT.get("vsprintf");
106  KnownFunctionIDs[id_vprintf]   = &IT.get("vprintf");
107
108  SuperID = &IT.get("super");
109
110  TUScope = 0;
111  if (getLangOptions().CPlusPlus)
112    FieldCollector.reset(new CXXFieldCollector());
113}
114
115/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
116/// If there is already an implicit cast, merge into the existing one.
117void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty) {
118  QualType ExprTy = Context.getCanonicalType(Expr->getType());
119  QualType TypeTy = Context.getCanonicalType(Ty);
120
121  if (ExprTy == TypeTy)
122    return;
123
124  if (Expr->getType().getTypePtr()->isPointerType() &&
125      Ty.getTypePtr()->isPointerType()) {
126    QualType ExprBaseType =
127      cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
128    QualType BaseType =
129      cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
130    if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
131      Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast,
132           Expr->getSourceRange());
133    }
134  }
135
136  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
137    ImpCast->setType(Ty);
138  else
139    Expr = new ImplicitCastExpr(Ty, Expr);
140}
141
142void Sema::DeleteExpr(ExprTy *E) {
143  delete static_cast<Expr*>(E);
144}
145void Sema::DeleteStmt(StmtTy *S) {
146  delete static_cast<Stmt*>(S);
147}
148
149/// ActOnEndOfTranslationUnit - This is called at the very end of the
150/// translation unit when EOF is reached and all but the top-level scope is
151/// popped.
152void Sema::ActOnEndOfTranslationUnit() {
153
154}
155
156
157//===----------------------------------------------------------------------===//
158// Helper functions.
159//===----------------------------------------------------------------------===//
160
161bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
162  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
163  return true;
164}
165
166bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
167  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  &Msg, 1);
168  return true;
169}
170
171bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
172                const std::string &Msg2) {
173  std::string MsgArr[] = { Msg1, Msg2 };
174  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  MsgArr, 2);
175  return true;
176}
177
178bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange& Range) {
179  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
180  return true;
181}
182
183bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
184                const SourceRange& Range) {
185  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
186  return true;
187}
188
189bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
190                const std::string &Msg2, const SourceRange& Range) {
191  std::string MsgArr[] = { Msg1, Msg2 };
192  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
193  return true;
194}
195
196bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
197                const std::string &Msg2, const std::string &Msg3,
198                const SourceRange& R1) {
199  std::string MsgArr[] = { Msg1, Msg2, Msg3 };
200  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
201  return true;
202}
203
204bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
205                const SourceRange& R1, const SourceRange& R2) {
206  SourceRange RangeArr[] = { R1, R2 };
207  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
208  return true;
209}
210
211bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
212                const SourceRange& R1, const SourceRange& R2) {
213  SourceRange RangeArr[] = { R1, R2 };
214  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  &Msg, 1, RangeArr, 2);
215  return true;
216}
217
218bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
219                const std::string &Msg2, const SourceRange& R1,
220                const 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
231ObjCMethodDecl *Sema::getCurMethodDecl() {
232  return dyn_cast<ObjCMethodDecl>(CurContext);
233}
234