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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
//===------ SemaWasm.cpp ---- WebAssembly target-specific routines --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements semantic analysis functions specific to WebAssembly.
//
//===----------------------------------------------------------------------===//
#include "clang/Sema/SemaWasm.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Type.h"
#include "clang/Basic/AddressSpaces.h"
#include "clang/Basic/DiagnosticSema.h"
#include "clang/Basic/TargetBuiltins.h"
#include "clang/Sema/Attr.h"
#include "clang/Sema/Sema.h"
namespace clang {
SemaWasm::SemaWasm(Sema &S) : SemaBase(S) {}
/// Checks the argument at the given index is a WebAssembly table and if it
/// is, sets ElTy to the element type.
static bool CheckWasmBuiltinArgIsTable(Sema &S, CallExpr *E, unsigned ArgIndex,
QualType &ElTy) {
Expr *ArgExpr = E->getArg(ArgIndex);
const auto *ATy = dyn_cast<ArrayType>(ArgExpr->getType());
if (!ATy || !ATy->getElementType().isWebAssemblyReferenceType()) {
return S.Diag(ArgExpr->getBeginLoc(),
diag::err_wasm_builtin_arg_must_be_table_type)
<< ArgIndex + 1 << ArgExpr->getSourceRange();
}
ElTy = ATy->getElementType();
return false;
}
/// Checks the argument at the given index is an integer.
static bool CheckWasmBuiltinArgIsInteger(Sema &S, CallExpr *E,
unsigned ArgIndex) {
Expr *ArgExpr = E->getArg(ArgIndex);
if (!ArgExpr->getType()->isIntegerType()) {
return S.Diag(ArgExpr->getBeginLoc(),
diag::err_wasm_builtin_arg_must_be_integer_type)
<< ArgIndex + 1 << ArgExpr->getSourceRange();
}
return false;
}
bool SemaWasm::BuiltinWasmRefNullExtern(CallExpr *TheCall) {
if (SemaRef.checkArgCount(TheCall, /*DesiredArgCount=*/0))
return true;
TheCall->setType(getASTContext().getWebAssemblyExternrefType());
return false;
}
bool SemaWasm::BuiltinWasmRefIsNullExtern(CallExpr *TheCall) {
if (SemaRef.checkArgCount(TheCall, 1)) {
return true;
}
Expr *ArgExpr = TheCall->getArg(0);
if (!ArgExpr->getType().isWebAssemblyExternrefType()) {
SemaRef.Diag(ArgExpr->getBeginLoc(),
diag::err_wasm_builtin_arg_must_be_externref_type)
<< 1 << ArgExpr->getSourceRange();
return true;
}
return false;
}
bool SemaWasm::BuiltinWasmRefNullFunc(CallExpr *TheCall) {
ASTContext &Context = getASTContext();
if (SemaRef.checkArgCount(TheCall, /*DesiredArgCount=*/0))
return true;
// This custom type checking code ensures that the nodes are as expected
// in order to later on generate the necessary builtin.
QualType Pointee = Context.getFunctionType(Context.VoidTy, {}, {});
QualType Type = Context.getPointerType(Pointee);
Pointee = Context.getAddrSpaceQualType(Pointee, LangAS::wasm_funcref);
Type = Context.getAttributedType(attr::WebAssemblyFuncref, Type,
Context.getPointerType(Pointee));
TheCall->setType(Type);
return false;
}
/// Check that the first argument is a WebAssembly table, and the second
/// is an index to use as index into the table.
bool SemaWasm::BuiltinWasmTableGet(CallExpr *TheCall) {
if (SemaRef.checkArgCount(TheCall, 2))
return true;
QualType ElTy;
if (CheckWasmBuiltinArgIsTable(SemaRef, TheCall, 0, ElTy))
return true;
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 1))
return true;
// If all is well, we set the type of TheCall to be the type of the
// element of the table.
// i.e. a table.get on an externref table has type externref,
// or whatever the type of the table element is.
TheCall->setType(ElTy);
return false;
}
/// Check that the first argumnet is a WebAssembly table, the second is
/// an index to use as index into the table and the third is the reference
/// type to set into the table.
bool SemaWasm::BuiltinWasmTableSet(CallExpr *TheCall) {
if (SemaRef.checkArgCount(TheCall, 3))
return true;
QualType ElTy;
if (CheckWasmBuiltinArgIsTable(SemaRef, TheCall, 0, ElTy))
return true;
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 1))
return true;
if (!getASTContext().hasSameType(ElTy, TheCall->getArg(2)->getType()))
return true;
return false;
}
/// Check that the argument is a WebAssembly table.
bool SemaWasm::BuiltinWasmTableSize(CallExpr *TheCall) {
if (SemaRef.checkArgCount(TheCall, 1))
return true;
QualType ElTy;
if (CheckWasmBuiltinArgIsTable(SemaRef, TheCall, 0, ElTy))
return true;
return false;
}
/// Check that the first argument is a WebAssembly table, the second is the
/// value to use for new elements (of a type matching the table type), the
/// third value is an integer.
bool SemaWasm::BuiltinWasmTableGrow(CallExpr *TheCall) {
if (SemaRef.checkArgCount(TheCall, 3))
return true;
QualType ElTy;
if (CheckWasmBuiltinArgIsTable(SemaRef, TheCall, 0, ElTy))
return true;
Expr *NewElemArg = TheCall->getArg(1);
if (!getASTContext().hasSameType(ElTy, NewElemArg->getType())) {
return Diag(NewElemArg->getBeginLoc(),
diag::err_wasm_builtin_arg_must_match_table_element_type)
<< 2 << 1 << NewElemArg->getSourceRange();
}
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 2))
return true;
return false;
}
/// Check that the first argument is a WebAssembly table, the second is an
/// integer, the third is the value to use to fill the table (of a type
/// matching the table type), and the fourth is an integer.
bool SemaWasm::BuiltinWasmTableFill(CallExpr *TheCall) {
if (SemaRef.checkArgCount(TheCall, 4))
return true;
QualType ElTy;
if (CheckWasmBuiltinArgIsTable(SemaRef, TheCall, 0, ElTy))
return true;
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 1))
return true;
Expr *NewElemArg = TheCall->getArg(2);
if (!getASTContext().hasSameType(ElTy, NewElemArg->getType())) {
return Diag(NewElemArg->getBeginLoc(),
diag::err_wasm_builtin_arg_must_match_table_element_type)
<< 3 << 1 << NewElemArg->getSourceRange();
}
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 3))
return true;
return false;
}
/// Check that the first argument is a WebAssembly table, the second is also a
/// WebAssembly table (of the same element type), and the third to fifth
/// arguments are integers.
bool SemaWasm::BuiltinWasmTableCopy(CallExpr *TheCall) {
if (SemaRef.checkArgCount(TheCall, 5))
return true;
QualType XElTy;
if (CheckWasmBuiltinArgIsTable(SemaRef, TheCall, 0, XElTy))
return true;
QualType YElTy;
if (CheckWasmBuiltinArgIsTable(SemaRef, TheCall, 1, YElTy))
return true;
Expr *TableYArg = TheCall->getArg(1);
if (!getASTContext().hasSameType(XElTy, YElTy)) {
return Diag(TableYArg->getBeginLoc(),
diag::err_wasm_builtin_arg_must_match_table_element_type)
<< 2 << 1 << TableYArg->getSourceRange();
}
for (int I = 2; I <= 4; I++) {
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, I))
return true;
}
return false;
}
bool SemaWasm::CheckWebAssemblyBuiltinFunctionCall(const TargetInfo &TI,
unsigned BuiltinID,
CallExpr *TheCall) {
switch (BuiltinID) {
case WebAssembly::BI__builtin_wasm_ref_null_extern:
return BuiltinWasmRefNullExtern(TheCall);
case WebAssembly::BI__builtin_wasm_ref_null_func:
return BuiltinWasmRefNullFunc(TheCall);
case WebAssembly::BI__builtin_wasm_ref_is_null_extern:
return BuiltinWasmRefIsNullExtern(TheCall);
case WebAssembly::BI__builtin_wasm_table_get:
return BuiltinWasmTableGet(TheCall);
case WebAssembly::BI__builtin_wasm_table_set:
return BuiltinWasmTableSet(TheCall);
case WebAssembly::BI__builtin_wasm_table_size:
return BuiltinWasmTableSize(TheCall);
case WebAssembly::BI__builtin_wasm_table_grow:
return BuiltinWasmTableGrow(TheCall);
case WebAssembly::BI__builtin_wasm_table_fill:
return BuiltinWasmTableFill(TheCall);
case WebAssembly::BI__builtin_wasm_table_copy:
return BuiltinWasmTableCopy(TheCall);
}
return false;
}
WebAssemblyImportModuleAttr *
SemaWasm::mergeImportModuleAttr(Decl *D,
const WebAssemblyImportModuleAttr &AL) {
auto *FD = cast<FunctionDecl>(D);
if (const auto *ExistingAttr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
if (ExistingAttr->getImportModule() == AL.getImportModule())
return nullptr;
Diag(ExistingAttr->getLocation(), diag::warn_mismatched_import)
<< 0 << ExistingAttr->getImportModule() << AL.getImportModule();
Diag(AL.getLoc(), diag::note_previous_attribute);
return nullptr;
}
if (FD->hasBody()) {
Diag(AL.getLoc(), diag::warn_import_on_definition) << 0;
return nullptr;
}
return ::new (getASTContext())
WebAssemblyImportModuleAttr(getASTContext(), AL, AL.getImportModule());
}
WebAssemblyImportNameAttr *
SemaWasm::mergeImportNameAttr(Decl *D, const WebAssemblyImportNameAttr &AL) {
auto *FD = cast<FunctionDecl>(D);
if (const auto *ExistingAttr = FD->getAttr<WebAssemblyImportNameAttr>()) {
if (ExistingAttr->getImportName() == AL.getImportName())
return nullptr;
Diag(ExistingAttr->getLocation(), diag::warn_mismatched_import)
<< 1 << ExistingAttr->getImportName() << AL.getImportName();
Diag(AL.getLoc(), diag::note_previous_attribute);
return nullptr;
}
if (FD->hasBody()) {
Diag(AL.getLoc(), diag::warn_import_on_definition) << 1;
return nullptr;
}
return ::new (getASTContext())
WebAssemblyImportNameAttr(getASTContext(), AL, AL.getImportName());
}
void SemaWasm::handleWebAssemblyImportModuleAttr(Decl *D,
const ParsedAttr &AL) {
auto *FD = cast<FunctionDecl>(D);
StringRef Str;
SourceLocation ArgLoc;
if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
return;
if (FD->hasBody()) {
Diag(AL.getLoc(), diag::warn_import_on_definition) << 0;
return;
}
FD->addAttr(::new (getASTContext())
WebAssemblyImportModuleAttr(getASTContext(), AL, Str));
}
void SemaWasm::handleWebAssemblyImportNameAttr(Decl *D, const ParsedAttr &AL) {
auto *FD = cast<FunctionDecl>(D);
StringRef Str;
SourceLocation ArgLoc;
if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
return;
if (FD->hasBody()) {
Diag(AL.getLoc(), diag::warn_import_on_definition) << 1;
return;
}
FD->addAttr(::new (getASTContext())
WebAssemblyImportNameAttr(getASTContext(), AL, Str));
}
void SemaWasm::handleWebAssemblyExportNameAttr(Decl *D, const ParsedAttr &AL) {
ASTContext &Context = getASTContext();
if (!isFuncOrMethodForAttrSubject(D)) {
Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
<< AL << AL.isRegularKeywordAttribute() << ExpectedFunction;
return;
}
auto *FD = cast<FunctionDecl>(D);
if (FD->isThisDeclarationADefinition()) {
Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
return;
}
StringRef Str;
SourceLocation ArgLoc;
if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
return;
D->addAttr(::new (Context) WebAssemblyExportNameAttr(Context, AL, Str));
D->addAttr(UsedAttr::CreateImplicit(Context));
}
} // namespace clang
|