-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidAttributeChecker.hpp
More file actions
262 lines (229 loc) · 8.13 KB
/
Copy pathValidAttributeChecker.hpp
File metadata and controls
262 lines (229 loc) · 8.13 KB
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
#ifndef GLU_SEMA_SEMANTICPASS_VALIDATTRIBUTECHECKER_HPP
#define GLU_SEMA_SEMANTICPASS_VALIDATTRIBUTECHECKER_HPP
#include "AST/ASTContext.hpp"
#include "AST/ASTNode.hpp"
#include "AST/ASTWalker.hpp"
#include "AST/Decls.hpp"
#include "Basic/Diagnostic.hpp"
namespace glu::sema {
/// @brief Walks a module. Checks the attributes on each declaration,
/// and emits diagnostics for invalid attributes.
class ValidAttributeChecker
: public ast::ASTWalker<ValidAttributeChecker, void> {
DiagnosticManager &_diagManager;
private:
/// @brief Validates the @alignment attribute parameter
void validateAlignmentAttribute(ast::Attribute *attr)
{
if (!attr->getParameter())
return;
auto *literal = llvm::dyn_cast<ast::LiteralExpr>(attr->getParameter());
if (!literal
|| !std::holds_alternative<llvm::APInt>(literal->getValue()))
return;
uint64_t alignment
= std::get<llvm::APInt>(literal->getValue()).getZExtValue();
// Check if alignment is a power of 2 and non-zero
if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
_diagManager.error(
attr->getLocation(),
llvm::Twine("Alignment must be a power of 2, got ")
+ llvm::Twine(alignment)
);
return;
}
// Check if alignment is reasonable (LLVM max is 2^29)
if (alignment > (1ULL << 29)) {
_diagManager.error(
attr->getLocation(),
llvm::Twine("Alignment ") + llvm::Twine(alignment)
+ " is too large (maximum is " + llvm::Twine(1ULL << 29)
+ ")"
);
}
}
/// @brief Validates the @linkage_name attribute parameter
void validateLinkageNameAttribute(ast::Attribute *attr)
{
if (!attr->getParameter())
return;
auto *literal = llvm::dyn_cast<ast::LiteralExpr>(attr->getParameter());
if (!literal) {
_diagManager.error(
attr->getLocation(),
"Attribute '@linkage_name' expects a string literal parameter"
);
return;
}
if (!std::holds_alternative<llvm::StringRef>(literal->getValue())) {
_diagManager.error(
attr->getLocation(),
"Attribute '@linkage_name' expects a string literal, not a "
"numeric or other literal type"
);
return;
}
llvm::StringRef linkageName
= std::get<llvm::StringRef>(literal->getValue());
// Check if linkage name is non-empty
if (linkageName.empty()) {
_diagManager.error(
attr->getLocation(), "Linkage name cannot be empty"
);
}
}
/// @brief Validates attribute-specific constraints
void validateAttributeValue(ast::Attribute *attr)
{
// TODO: made a visitor
switch (attr->getAttributeKind()) {
case ast::AttributeKind::AlignmentKind:
validateAlignmentAttribute(attr);
break;
case ast::AttributeKind::LinkageNameKind:
validateLinkageNameAttribute(attr);
break;
default: break;
}
}
/// @brief Checks parameter validity (presence and type)
void checkParameterValidity(ast::Attribute *attr)
{
if (attr->expectsParameter() && !attr->getParameter()) {
_diagManager.error(
attr->getLocation(),
llvm::Twine("Attribute '@") + attr->getAttributeKindSpelling()
+ "' expects a parameter of type "
+ attr->getExpectedParameterTypeName()
);
} else if (!attr->expectsParameter() && attr->getParameter()) {
_diagManager.error(
attr->getLocation(),
llvm::Twine("Attribute '@") + attr->getAttributeKindSpelling()
+ "' does not accept a parameter"
);
} else if (attr->getParameter()
&& !attr->isValidParameterType(attr->getParameter())) {
_diagManager.error(
attr->getLocation(),
llvm::Twine("Attribute '@") + attr->getAttributeKindSpelling()
+ "' expects a parameter of type "
+ attr->getExpectedParameterTypeName()
+ ", but got an incompatible expression"
);
}
}
public:
explicit ValidAttributeChecker(DiagnosticManager &diagManager)
: _diagManager(diagManager)
{
}
void check(
ast::DeclBase *decl, ast::AttributeAttachment attachment,
llvm::Twine description
)
{
if (!decl->getAttributes())
return;
for (auto *attr : decl->getAttributes()->getAttributes()) {
// Check if attribute is valid on this declaration type
if (!attr->isValidOn(attachment)) {
_diagManager.error(
attr->getLocation(),
llvm::Twine("Attribute '@")
+ attr->getAttributeKindSpelling()
+ "' is not valid on " + description
);
continue;
}
// Check parameter validity (presence and type)
checkParameterValidity(attr);
// Validate attribute-specific constraints
validateAttributeValue(attr);
}
}
void preVisitFunctionDecl(ast::FunctionDecl *node)
{
if (node->getBody()) {
check(
node, ast::AttributeAttachment::FunctionDefinitionAttachment,
"function definitions"
);
} else {
check(
node, ast::AttributeAttachment::FunctionPrototypeAttachment,
"function prototypes"
);
}
// Check for mutual exclusivity between @linkage_name and @no_mangling
if (node->getAttributes()) {
bool hasLinkageName
= node->hasAttribute(ast::AttributeKind::LinkageNameKind);
bool hasNoMangling
= node->hasAttribute(ast::AttributeKind::NoManglingKind);
if (hasLinkageName && hasNoMangling) {
_diagManager.error(
node->getLocation(),
"Attributes '@linkage_name' and '@no_mangling' are "
"mutually exclusive"
);
}
}
}
void preVisitImportDecl(ast::ImportDecl *node)
{
check(node, ast::AttributeAttachment::ImportAttachment, "imports");
}
void preVisitStructDecl(ast::StructDecl *node)
{
check(node, ast::AttributeAttachment::StructAttachment, "structs");
}
void preVisitEnumDecl(ast::EnumDecl *node)
{
check(node, ast::AttributeAttachment::EnumAttachment, "enums");
}
void preVisitTypeAliasDecl(ast::TypeAliasDecl *node)
{
check(
node, ast::AttributeAttachment::TypeAliasAttachment, "type aliases"
);
}
void preVisitVarDecl(ast::VarDecl *node)
{
if (node->isGlobal()) {
check(
node, ast::AttributeAttachment::GlobalVarAttachment,
"global variables"
);
} else {
check(
node, ast::AttributeAttachment::LocalVarAttachment,
"local variables"
);
}
}
void preVisitLetDecl(ast::LetDecl *node)
{
if (node->isGlobal()) {
check(
node, ast::AttributeAttachment::GlobalLetAttachment,
"global constants"
);
} else {
check(
node, ast::AttributeAttachment::LocalLetAttachment,
"local constants"
);
}
}
void preVisitParamDecl(ast::ParamDecl *node)
{
check(node, ast::AttributeAttachment::ParamAttachment, "parameters");
}
void preVisitFieldDecl(ast::FieldDecl *node)
{
check(node, ast::AttributeAttachment::FieldAttachment, "fields");
}
};
} // namespace glu::sema
#endif // GLU_SEMA_SEMANTICPASS_VALIDATTRIBUTECHECKER_HPP