Sema.cpp revision 52a81c04a25001409f8b0c9b296248fd7cf80e13
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), 0);
36  else
37    return RecordDecl::Create(C, TagDecl::TK_struct,
38                              C.getTranslationUnitDecl(),
39                              SourceLocation(), &C.Idents.get(Name), 0);
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 Type) {
118  if (Context.getCanonicalType(Expr->getType()) ==
119        Context.getCanonicalType(Type)) return;
120
121  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
122    ImpCast->setType(Type);
123  else
124    Expr = new ImplicitCastExpr(Type, Expr);
125}
126
127void Sema::DeleteExpr(ExprTy *E) {
128  delete static_cast<Expr*>(E);
129}
130void Sema::DeleteStmt(StmtTy *S) {
131  delete static_cast<Stmt*>(S);
132}
133
134/// ActOnEndOfTranslationUnit - This is called at the very end of the
135/// translation unit when EOF is reached and all but the top-level scope is
136/// popped.
137void Sema::ActOnEndOfTranslationUnit() {
138
139}
140
141
142//===----------------------------------------------------------------------===//
143// Helper functions.
144//===----------------------------------------------------------------------===//
145
146bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
147  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
148  return true;
149}
150
151bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
152  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  &Msg, 1);
153  return true;
154}
155
156bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
157                const std::string &Msg2) {
158  std::string MsgArr[] = { Msg1, Msg2 };
159  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  MsgArr, 2);
160  return true;
161}
162
163bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange& Range) {
164  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
165  return true;
166}
167
168bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
169                const SourceRange& Range) {
170  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
171  return true;
172}
173
174bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
175                const std::string &Msg2, const SourceRange& Range) {
176  std::string MsgArr[] = { Msg1, Msg2 };
177  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
178  return true;
179}
180
181bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
182                const std::string &Msg2, const std::string &Msg3,
183                const SourceRange& R1) {
184  std::string MsgArr[] = { Msg1, Msg2, Msg3 };
185  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
186  return true;
187}
188
189bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
190                const SourceRange& R1, const SourceRange& R2) {
191  SourceRange RangeArr[] = { R1, R2 };
192  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
193  return true;
194}
195
196bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
197                const SourceRange& R1, const SourceRange& R2) {
198  SourceRange RangeArr[] = { R1, R2 };
199  PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID,  &Msg, 1, RangeArr, 2);
200  return true;
201}
202
203bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
204                const std::string &Msg2, const SourceRange& R1,
205                const SourceRange& R2) {
206  std::string MsgArr[] = { Msg1, Msg2 };
207  SourceRange RangeArr[] = { R1, R2 };
208  PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
209  return true;
210}
211
212const LangOptions &Sema::getLangOptions() const {
213  return PP.getLangOptions();
214}
215
216ObjCMethodDecl *Sema::getCurMethodDecl() {
217  return dyn_cast<ObjCMethodDecl>(CurContext);
218}
219