Sema.cpp revision b034e281af9d2cb52c2606764de229c1fb825307
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  PushDeclContext(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),PreDeclaratorDC(0),
85    CurBlock(0), 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  Ident_StdNs = &IT.get("std");
117  Ident_TypeInfo = 0;
118  StdNamespace = 0;
119
120  TUScope = 0;
121  if (getLangOptions().CPlusPlus)
122    FieldCollector.reset(new CXXFieldCollector());
123}
124
125/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
126/// If there is already an implicit cast, merge into the existing one.
127  /// If isLvalue, the result of the cast is an lvalue.
128void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
129  QualType ExprTy = Context.getCanonicalType(Expr->getType());
130  QualType TypeTy = Context.getCanonicalType(Ty);
131
132  if (ExprTy == TypeTy)
133    return;
134
135  if (Expr->getType().getTypePtr()->isPointerType() &&
136      Ty.getTypePtr()->isPointerType()) {
137    QualType ExprBaseType =
138      cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
139    QualType BaseType =
140      cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
141    if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
142      Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast,
143           Expr->getSourceRange());
144    }
145  }
146
147  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
148    ImpCast->setType(Ty);
149    ImpCast->setLvalueCast(isLvalue);
150  } else
151    Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
152}
153
154void Sema::DeleteExpr(ExprTy *E) {
155  delete static_cast<Expr*>(E);
156}
157void Sema::DeleteStmt(StmtTy *S) {
158  delete static_cast<Stmt*>(S);
159}
160
161/// ActOnEndOfTranslationUnit - This is called at the very end of the
162/// translation unit when EOF is reached and all but the top-level scope is
163/// popped.
164void Sema::ActOnEndOfTranslationUnit() {
165
166}
167
168
169//===----------------------------------------------------------------------===//
170// Helper functions.
171//===----------------------------------------------------------------------===//
172
173bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
174  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
175  return true;
176}
177
178bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
179  const std::string *Strs[] = { &Msg };
180  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, Strs, 1);
181  return true;
182}
183
184bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
185                const std::string &Msg2) {
186  const std::string *MsgArr[] = { &Msg1, &Msg2 };
187  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  MsgArr, 2);
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  const std::string *Strs[] = { &Msg };
199  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, Strs, 1, &Range,1);
200  return true;
201}
202
203bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
204                const std::string &Msg2, const SourceRange& Range) {
205  const std::string *MsgArr[] = { &Msg1, &Msg2 };
206  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
207  return true;
208}
209
210bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
211                const std::string &Msg2, const std::string &Msg3,
212                const SourceRange &R1) {
213  const std::string *MsgArr[] = { &Msg1, &Msg2, &Msg3 };
214  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
215  return true;
216}
217
218bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
219                const SourceRange& R1, const SourceRange& R2) {
220  SourceRange RangeArr[] = { R1, R2 };
221  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
222  return true;
223}
224
225bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
226                const SourceRange& R1, const SourceRange& R2) {
227  SourceRange RangeArr[] = { R1, R2 };
228  const std::string *Strs[] = { &Msg };
229  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, Strs, 1, RangeArr, 2);
230  return true;
231}
232
233bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
234                const std::string &Msg2, const SourceRange& R1,
235                const SourceRange& R2) {
236  const std::string *MsgArr[] = { &Msg1, &Msg2 };
237  SourceRange RangeArr[] = { R1, R2 };
238  PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
239  return true;
240}
241
242const LangOptions &Sema::getLangOptions() const {
243  return PP.getLangOptions();
244}
245
246ObjCMethodDecl *Sema::getCurMethodDecl() {
247  DeclContext *DC = CurContext;
248  while (isa<BlockDecl>(DC))
249    DC = DC->getParent();
250  return dyn_cast<ObjCMethodDecl>(DC);
251}
252