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