Sema.cpp revision 2bac0f6b3724734d7bb7bf8231bd8511cb49570f
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"
21using namespace clang;
22
23static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
24  if (C.getLangOptions().CPlusPlus)
25    return CXXRecordDecl::Create(C, TagDecl::TK_struct,
26                                 C.getTranslationUnitDecl(),
27                                 SourceLocation(), &C.Idents.get(Name));
28
29  return RecordDecl::Create(C, TagDecl::TK_struct,
30                            C.getTranslationUnitDecl(),
31                            SourceLocation(), &C.Idents.get(Name));
32}
33
34void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
35  TUScope = S;
36  PushDeclContext(Context.getTranslationUnitDecl());
37  if (!PP.getLangOptions().ObjC1) return;
38
39  // Synthesize "typedef struct objc_selector *SEL;"
40  RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
41  PushOnScopeChains(SelTag, TUScope);
42
43  QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
44  TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
45                                                SourceLocation(),
46                                                &Context.Idents.get("SEL"),
47                                                SelT, 0);
48  PushOnScopeChains(SelTypedef, TUScope);
49  Context.setObjCSelType(SelTypedef);
50
51  // FIXME: Make sure these don't leak!
52  RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
53  QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
54  TypedefDecl *ClassTypedef =
55    TypedefDecl::Create(Context, CurContext, SourceLocation(),
56                        &Context.Idents.get("Class"), ClassT, 0);
57  PushOnScopeChains(ClassTag, TUScope);
58  PushOnScopeChains(ClassTypedef, TUScope);
59  Context.setObjCClassType(ClassTypedef);
60  // Synthesize "@class Protocol;
61  ObjCInterfaceDecl *ProtocolDecl =
62    ObjCInterfaceDecl::Create(Context, SourceLocation(),
63                              &Context.Idents.get("Protocol"),
64                              SourceLocation(), true);
65  Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
66  PushOnScopeChains(ProtocolDecl, TUScope);
67
68  // Synthesize "typedef struct objc_object { Class isa; } *id;"
69  RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
70
71  QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
72  PushOnScopeChains(ObjectTag, TUScope);
73  TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
74                                               SourceLocation(),
75                                               &Context.Idents.get("id"),
76                                               ObjT, 0);
77  PushOnScopeChains(IdTypedef, TUScope);
78  Context.setObjCIdType(IdTypedef);
79}
80
81Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
82  : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0),PreDeclaratorDC(0),
83    CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()) {
84
85  // Get IdentifierInfo objects for known functions for which we
86  // do extra checking.
87  IdentifierTable &IT = PP.getIdentifierTable();
88
89  KnownFunctionIDs[id_printf]        = &IT.get("printf");
90  KnownFunctionIDs[id_fprintf]       = &IT.get("fprintf");
91  KnownFunctionIDs[id_sprintf]       = &IT.get("sprintf");
92  KnownFunctionIDs[id_sprintf_chk]   = &IT.get("__builtin___sprintf_chk");
93  KnownFunctionIDs[id_snprintf]      = &IT.get("snprintf");
94  KnownFunctionIDs[id_snprintf_chk]  = &IT.get("__builtin___snprintf_chk");
95  KnownFunctionIDs[id_asprintf]      = &IT.get("asprintf");
96  KnownFunctionIDs[id_NSLog]         = &IT.get("NSLog");
97  KnownFunctionIDs[id_vsnprintf]     = &IT.get("vsnprintf");
98  KnownFunctionIDs[id_vasprintf]     = &IT.get("vasprintf");
99  KnownFunctionIDs[id_vfprintf]      = &IT.get("vfprintf");
100  KnownFunctionIDs[id_vsprintf]      = &IT.get("vsprintf");
101  KnownFunctionIDs[id_vsprintf_chk]  = &IT.get("__builtin___vsprintf_chk");
102  KnownFunctionIDs[id_vsnprintf]     = &IT.get("vsnprintf");
103  KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
104  KnownFunctionIDs[id_vprintf]       = &IT.get("vprintf");
105
106  Ident_StdNs = &IT.get("std");
107  Ident_TypeInfo = 0;
108  StdNamespace = 0;
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.
117  /// If isLvalue, the result of the cast is an lvalue.
118void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
119  QualType ExprTy = Context.getCanonicalType(Expr->getType());
120  QualType TypeTy = Context.getCanonicalType(Ty);
121
122  if (ExprTy == TypeTy)
123    return;
124
125  if (Expr->getType().getTypePtr()->isPointerType() &&
126      Ty.getTypePtr()->isPointerType()) {
127    QualType ExprBaseType =
128      cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
129    QualType BaseType =
130      cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
131    if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
132      Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
133        << Expr->getSourceRange();
134    }
135  }
136
137  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
138    ImpCast->setType(Ty);
139    ImpCast->setLvalueCast(isLvalue);
140  } else
141    Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
142}
143
144void Sema::DeleteExpr(ExprTy *E) {
145  delete static_cast<Expr*>(E);
146}
147void Sema::DeleteStmt(StmtTy *S) {
148  delete static_cast<Stmt*>(S);
149}
150
151/// ActOnEndOfTranslationUnit - This is called at the very end of the
152/// translation unit when EOF is reached and all but the top-level scope is
153/// popped.
154void Sema::ActOnEndOfTranslationUnit() {
155
156}
157
158
159//===----------------------------------------------------------------------===//
160// Helper functions.
161//===----------------------------------------------------------------------===//
162
163DiagnosticInfo Sema::Diag(SourceLocation Loc, unsigned DiagID) {
164  return PP.getDiagnostics().Report(FullSourceLoc(Loc, PP.getSourceManager()),
165                                    DiagID);
166}
167
168bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
169  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Msg;
170  return true;
171}
172
173bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
174                const std::string &Msg2) {
175  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Msg1 << Msg2;
176  return true;
177}
178
179bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
180                const SourceRange& Range) {
181  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Msg << Range;
182  return true;
183}
184
185const LangOptions &Sema::getLangOptions() const {
186  return PP.getLangOptions();
187}
188
189ObjCMethodDecl *Sema::getCurMethodDecl() {
190  DeclContext *DC = CurContext;
191  while (isa<BlockDecl>(DC))
192    DC = DC->getParent();
193  return dyn_cast<ObjCMethodDecl>(DC);
194}
195