1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the actions class which performs semantic analysis and
// builds an AST out of a parse stream.
//
//===----------------------------------------------------------------------===//
#include "Sema.h"
#include "clang/AST/ASTContext.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/Diagnostic.h"
using namespace clang;
void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
TUScope = S;
}
/// GetObjcIdType - The following method assumes that "id" is imported
/// via <objc/objc.h>. This is the way GCC worked for almost 20 years.
/// In GCC 4.0, "id" is now a built-in type. Unfortunately, typedefs *cannot* be
/// redefined (even if they are identical). To allow a built-in types to coexist
/// with <objc/objc.h>, GCC has a special hack on decls (DECL_IN_SYSTEM_HEADER).
/// For now, we will *not* install id as a built-in. FIXME: reconsider this.
QualType Sema::GetObjcIdType(SourceLocation Loc) {
assert(TUScope && "GetObjcIdType(): Top-level scope is null");
if (!Context.getObjcIdType().isNull())
return Context.getObjcIdType();
IdentifierInfo *IdIdent = &Context.Idents.get("id");
ScopedDecl *IdDecl = LookupScopedDecl(IdIdent, Decl::IDNS_Ordinary,
SourceLocation(), TUScope);
TypedefDecl *ObjcIdTypedef = dyn_cast_or_null<TypedefDecl>(IdDecl);
if (!ObjcIdTypedef) {
Diag(Loc, diag::err_missing_id_definition);
return QualType();
}
Context.setObjcIdType(ObjcIdTypedef);
return Context.getObjcIdType();
}
/// GetObjcSelType - See comments for Sema::GetObjcIdType above; replace "id"
/// with "SEL".
QualType Sema::GetObjcSelType(SourceLocation Loc) {
assert(TUScope && "GetObjcSelType(): Top-level scope is null");
if (Context.getObjcSelType().isNull()) {
IdentifierInfo *SelIdent = &Context.Idents.get("SEL");
ScopedDecl *SelDecl = LookupScopedDecl(SelIdent, Decl::IDNS_Ordinary,
SourceLocation(), TUScope);
TypedefDecl *ObjcSelTypedef = dyn_cast_or_null<TypedefDecl>(SelDecl);
if (!ObjcSelTypedef) {
Diag(Loc, diag::err_missing_sel_definition);
return QualType();
}
Context.setObjcSelType(ObjcSelTypedef);
}
return Context.getObjcSelType();
}
/// GetObjcProtoType - See comments for Sema::GetObjcIdType above; replace "id"
/// with "Protocol".
QualType Sema::GetObjcProtoType(SourceLocation Loc) {
assert(TUScope && "GetObjcProtoType(): Top-level scope is null");
if (Context.getObjcProtoType().isNull()) {
IdentifierInfo *ProtoIdent = &Context.Idents.get("Protocol");
ScopedDecl *ProtoDecl = LookupScopedDecl(ProtoIdent, Decl::IDNS_Ordinary,
SourceLocation(), TUScope);
TypedefDecl *ObjcProtoTypedef = dyn_cast_or_null<TypedefDecl>(ProtoDecl);
if (!ObjcProtoTypedef) {
Diag(Loc, diag::err_missing_proto_definition);
return QualType();
}
Context.setObjcProtoType(ObjcProtoTypedef);
}
return Context.getObjcProtoType();
}
/// GetObjcClassType - See comments for Sema::GetObjcIdType above; replace "id"
/// with "Protocol".
QualType Sema::GetObjcClassType(SourceLocation Loc) {
assert(TUScope && "GetObjcClassType(): Top-level scope is null");
if (!Context.getObjcClassType().isNull())
return Context.getObjcClassType();
IdentifierInfo *ClassIdent = &Context.Idents.get("Class");
ScopedDecl *ClassDecl = LookupScopedDecl(ClassIdent, Decl::IDNS_Ordinary,
SourceLocation(), TUScope);
TypedefDecl *ObjcClassTypedef = dyn_cast_or_null<TypedefDecl>(ClassDecl);
if (!ObjcClassTypedef) {
Diag(Loc, diag::err_missing_class_definition);
return QualType();
}
Context.setObjcClassType(ObjcClassTypedef);
return Context.getObjcClassType();
}
Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
: PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
// Get IdentifierInfo objects for known functions for which we
// do extra checking.
IdentifierTable& IT = PP.getIdentifierTable();
KnownFunctionIDs[ id_printf ] = &IT.get("printf");
KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
TUScope = 0;
}
void Sema::DeleteExpr(ExprTy *E) {
delete static_cast<Expr*>(E);
}
void Sema::DeleteStmt(StmtTy *S) {
delete static_cast<Stmt*>(S);
}
//===----------------------------------------------------------------------===//
// Helper functions.
//===----------------------------------------------------------------------===//
bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
PP.getDiagnostics().Report(Loc, DiagID);
return true;
}
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
return true;
}
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
const std::string &Msg2) {
std::string MsgArr[] = { Msg1, Msg2 };
PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
return true;
}
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
return true;
}
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
SourceRange Range) {
PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
return true;
}
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
const std::string &Msg2, SourceRange Range) {
std::string MsgArr[] = { Msg1, Msg2 };
PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
return true;
}
bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
SourceRange R1, SourceRange R2) {
SourceRange RangeArr[] = { R1, R2 };
PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
return true;
}
bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
SourceRange R1, SourceRange R2) {
SourceRange RangeArr[] = { R1, R2 };
PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
return true;
}
bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
const std::string &Msg2, SourceRange R1, SourceRange R2) {
std::string MsgArr[] = { Msg1, Msg2 };
SourceRange RangeArr[] = { R1, R2 };
PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
return true;
}
const LangOptions &Sema::getLangOptions() const {
return PP.getLangOptions();
}
|