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