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