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