forked from CyanogenMod/android_frameworks_compile_slang
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathslang_rs_pragma_handler.cpp
299 lines (248 loc) · 9.11 KB
/
slang_rs_pragma_handler.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright 2010, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "slang_rs_pragma_handler.h"
#include <sstream>
#include <string>
#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/LiteralSupport.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Token.h"
#include "slang_assert.h"
#include "slang_rs_context.h"
namespace slang {
namespace { // Anonymous namespace
class RSExportTypePragmaHandler : public RSPragmaHandler {
private:
void handleItem(const std::string &Item) {
mContext->addPragma(this->getName(), Item);
mContext->addExportType(Item);
}
public:
RSExportTypePragmaHandler(llvm::StringRef Name, RSContext *Context)
: RSPragmaHandler(Name, Context) { return; }
void HandlePragma(clang::Preprocessor &PP,
clang::PragmaIntroducerKind Introducer,
clang::Token &FirstToken) {
this->handleItemListPragma(PP, FirstToken);
}
};
class RSJavaPackageNamePragmaHandler : public RSPragmaHandler {
public:
RSJavaPackageNamePragmaHandler(llvm::StringRef Name, RSContext *Context)
: RSPragmaHandler(Name, Context) { return; }
void HandlePragma(clang::Preprocessor &PP,
clang::PragmaIntroducerKind Introducer,
clang::Token &FirstToken) {
// FIXME: Need to validate the extracted package name from pragma.
// Currently "all chars" specified in pragma will be treated as package
// name.
//
// 18.1 The Grammar of the Java Programming Language
// (http://java.sun.com/docs/books/jls/third_edition/html/syntax.html#18.1)
//
// CompilationUnit:
// [[Annotations] package QualifiedIdentifier ; ] {ImportDeclaration}
// {TypeDeclaration}
//
// QualifiedIdentifier:
// Identifier { . Identifier }
//
// Identifier:
// IDENTIFIER
//
// 3.8 Identifiers
// (http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.8)
//
//
clang::Token &PragmaToken = FirstToken;
std::string PackageName;
// Skip first token, "java_package_name"
PP.LexUnexpandedToken(PragmaToken);
// Now, the current token must be clang::tok::lpara
if (PragmaToken.isNot(clang::tok::l_paren))
return;
while (PragmaToken.isNot(clang::tok::eod)) {
// Lex package name
PP.LexUnexpandedToken(PragmaToken);
bool Invalid;
std::string Spelling = PP.getSpelling(PragmaToken, &Invalid);
if (!Invalid)
PackageName.append(Spelling);
// Pre-mature end (syntax error will be triggered by preprocessor later)
if (PragmaToken.is(clang::tok::eod) || PragmaToken.is(clang::tok::eof)) {
break;
} else {
// Next token is ')' (end of pragma)
const clang::Token &NextTok = PP.LookAhead(0);
if (NextTok.is(clang::tok::r_paren)) {
mContext->addPragma(this->getName(), PackageName);
mContext->setReflectJavaPackageName(PackageName);
// Lex until meets clang::tok::eod
do {
PP.LexUnexpandedToken(PragmaToken);
} while (PragmaToken.isNot(clang::tok::eod));
break;
}
}
}
return;
}
};
class RSReflectLicensePragmaHandler : public RSPragmaHandler {
private:
void handleItem(const std::string &Item) {
mContext->addPragma(this->getName(), Item);
mContext->setLicenseNote(Item);
}
public:
RSReflectLicensePragmaHandler(llvm::StringRef Name, RSContext *Context)
: RSPragmaHandler(Name, Context) { return; }
void HandlePragma(clang::Preprocessor &PP,
clang::PragmaIntroducerKind Introducer,
clang::Token &FirstToken) {
this->handleOptionalStringLiteralParamPragma(PP, FirstToken);
}
};
class RSVersionPragmaHandler : public RSPragmaHandler {
private:
void handleInt(const int v) {
std::stringstream ss;
ss << v;
mContext->addPragma(this->getName(), ss.str());
mContext->setVersion(v);
}
public:
RSVersionPragmaHandler(llvm::StringRef Name, RSContext *Context)
: RSPragmaHandler(Name, Context) { return; }
void HandlePragma(clang::Preprocessor &PP,
clang::PragmaIntroducerKind Introducer,
clang::Token &FirstToken) {
this->handleIntegerParamPragma(PP, FirstToken);
}
};
} // namespace
RSPragmaHandler *
RSPragmaHandler::CreatePragmaExportTypeHandler(RSContext *Context) {
return new RSExportTypePragmaHandler("export_type", Context);
}
RSPragmaHandler *
RSPragmaHandler::CreatePragmaJavaPackageNameHandler(RSContext *Context) {
return new RSJavaPackageNamePragmaHandler("java_package_name", Context);
}
RSPragmaHandler *
RSPragmaHandler::CreatePragmaReflectLicenseHandler(RSContext *Context) {
return new RSReflectLicensePragmaHandler("set_reflect_license", Context);
}
RSPragmaHandler *
RSPragmaHandler::CreatePragmaVersionHandler(RSContext *Context) {
return new RSVersionPragmaHandler("version", Context);
}
void RSPragmaHandler::handleItemListPragma(clang::Preprocessor &PP,
clang::Token &FirstToken) {
clang::Token &PragmaToken = FirstToken;
// Skip first token, like "export_var"
PP.LexUnexpandedToken(PragmaToken);
// Now, the current token must be clang::tok::lpara
if (PragmaToken.isNot(clang::tok::l_paren))
return;
while (PragmaToken.isNot(clang::tok::eod)) {
// Lex variable name
PP.LexUnexpandedToken(PragmaToken);
if (PragmaToken.is(clang::tok::identifier))
this->handleItem(PP.getSpelling(PragmaToken));
else
break;
slangAssert(PragmaToken.isNot(clang::tok::eod));
PP.LexUnexpandedToken(PragmaToken);
if (PragmaToken.isNot(clang::tok::comma))
break;
}
return;
}
void RSPragmaHandler::handleNonParamPragma(clang::Preprocessor &PP,
clang::Token &FirstToken) {
clang::Token &PragmaToken = FirstToken;
// Skip first token, like "export_var_all"
PP.LexUnexpandedToken(PragmaToken);
// Should be end immediately
if (PragmaToken.isNot(clang::tok::eod))
fprintf(stderr, "RSPragmaHandler::handleNonParamPragma: "
"expected a clang::tok::eod\n");
return;
}
void RSPragmaHandler::handleOptionalStringLiteralParamPragma(
clang::Preprocessor &PP, clang::Token &FirstToken) {
clang::Token &PragmaToken = FirstToken;
// Skip first token, like "set_reflect_license"
PP.LexUnexpandedToken(PragmaToken);
// Now, the current token must be clang::tok::lpara
if (PragmaToken.isNot(clang::tok::l_paren))
return;
// If not ')', eat the following string literal as the license
PP.LexUnexpandedToken(PragmaToken);
if (PragmaToken.isNot(clang::tok::r_paren)) {
// Eat the whole string literal
clang::StringLiteralParser StringLiteral(&PragmaToken, 1, PP);
if (StringLiteral.hadError)
fprintf(stderr, "RSPragmaHandler::handleOptionalStringLiteralParamPragma"
": illegal string literal\n");
else
this->handleItem(std::string(StringLiteral.GetString()));
// The current token should be clang::tok::r_para
PP.LexUnexpandedToken(PragmaToken);
if (PragmaToken.isNot(clang::tok::r_paren))
fprintf(stderr, "RSPragmaHandler::handleOptionalStringLiteralParamPragma"
": expected a ')'\n");
} else {
// If no argument, remove the license
this->handleItem("");
}
}
void RSPragmaHandler::handleIntegerParamPragma(
clang::Preprocessor &PP, clang::Token &FirstToken) {
clang::Token &PragmaToken = FirstToken;
// Skip first token, like "version"
PP.LexUnexpandedToken(PragmaToken);
// Now, the current token must be clang::tok::lpara
if (PragmaToken.isNot(clang::tok::l_paren))
return;
PP.LexUnexpandedToken(PragmaToken);
if (PragmaToken.is(clang::tok::numeric_constant)) {
clang::NumericLiteralParser NumericLiteral(PragmaToken.getLiteralData(),
PragmaToken.getLiteralData() + PragmaToken.getLength(),
PragmaToken.getLocation(), PP);
if (NumericLiteral.hadError) {
fprintf(stderr, "RSPragmaHandler::handleIntegerParamPragma"
": illegal numeric literal\n");
} else {
llvm::APInt Val(32, 0);
NumericLiteral.GetIntegerValue(Val);
this->handleInt(static_cast<int>(Val.getSExtValue()));
}
PP.LexUnexpandedToken(PragmaToken);
} else {
// If no argument, set the version to 0
this->handleInt(0);
}
if (PragmaToken.isNot(clang::tok::r_paren)) {
fprintf(stderr, "RSPragmaHandler::handleIntegerParamPragma"
": expected a ')'\n");
}
do {
PP.LexUnexpandedToken(PragmaToken);
} while (PragmaToken.isNot(clang::tok::eod));
}
} // namespace slang