@@ -31,50 +31,102 @@ def RLC_SourceRangeAttr : RLC_Attr<"SourceRange", "source_range_attr"> {
31
31
32
32
}
33
33
34
+ def RLC_ShugarizedTypeAttr : RLC_Attr<"ShugarizedType", "shugarized_type"> {
35
+ let summary = "AST info about a place in the source program where there was a hand written type";
36
+ let description = [{
37
+ info about a type in the source
38
+ }];
39
+
40
+ let parameters = (ins "mlir::rlc::SourceRangeAttr":$location, "mlir::Type":$type);
41
+
42
+ let assemblyFormat = "`<` $location $type`>`";
43
+
44
+ let builders = [
45
+ AttrBuilderWithInferredContext<(ins "mlir::rlc::SourceRangeAttr":$location, "mlir::Type":$type),[{
46
+ assert(location != nullptr);
47
+ assert(type != nullptr);
48
+ return mlir::rlc::ShugarizedTypeAttr::get(type.getContext(), location, type);
49
+ }]>,
50
+ ];
51
+
52
+ let extraClassDeclaration = [{
53
+ mlir::rlc::ShugarizedTypeAttr replaceType(mlir::Type type) const {
54
+ return mlir::rlc::ShugarizedTypeAttr::get(getContext(), getLocation(), type);
55
+ }
56
+ }];
57
+ }
58
+
34
59
def RLC_FunctionArgumentAttr : RLC_Attr<"FunctionArgument", "function_argument"> {
35
60
let summary = "AST infos about function arguments, such as the name of the field, the source range of the name, the shugarized type argument and the source range of the shugarized type.";
36
61
let description = [{
37
62
info about function argument
38
63
}];
39
64
40
- let parameters = (ins StringRefParameter<>:$name, OptionalParameter<"mlir::rlc::SourceRangeAttr">:$name_location, OptionalParameter<"mlir::Type">:$shugarized_type, OptionalParameter<"mlir:: rlc::SourceRangeAttr ">:$type_location );
65
+ let parameters = (ins StringRefParameter<>:$name, OptionalParameter<"mlir::rlc::SourceRangeAttr">:$name_location, OptionalParameter<"mlir::rlc::ShugarizedTypeAttr ">:$shugarized_type );
41
66
42
- let assemblyFormat = "`<` `name` `=` $name (`shugarized_type` `=` $shugarized_type^ )? (`name_location` `=` $name_location^ )? (`type_location` `=` $type_location^ )? `>`";
67
+ let assemblyFormat = "`<` `name` `=` $name (`shugarized_type` `=` $shugarized_type^ )? (`name_location` `=` $name_location^ )? `>`";
43
68
44
69
let builders = [
45
70
AttrBuilder<(ins "llvm::StringRef":$name),[{
46
- return mlir::rlc::FunctionArgumentAttr::get($_ctxt, name, nullptr, nullptr, nullptr );
71
+ return mlir::rlc::FunctionArgumentAttr::get($_ctxt, name, nullptr, nullptr);
47
72
}]>,
48
73
];
74
+
75
+ let extraClassDeclaration = [{
76
+ mlir::rlc::FunctionArgumentAttr replaceType(mlir::Type type) const {
77
+ if (getShugarizedType() == nullptr)
78
+ return *this;
79
+ return mlir::rlc::FunctionArgumentAttr::get(getContext(), getName(), getNameLocation(), getShugarizedType().replaceType(type));
80
+ }
81
+ }];
49
82
}
50
83
51
84
def RLC_FunctionInfoAttr : RLC_Attr<"FunctionInfo", "function_info"> {
52
85
let summary = "AST infos about a function";
53
86
54
87
55
- let parameters = (ins ArrayRefParameter<"mlir::rlc::FunctionArgumentAttr">:$args, OptionalParameter<"mlir::rlc::SourceRangeAttr">:$return_type_location, OptionalParameter<"mlir::Type ">:$shugarized_return_type);
88
+ let parameters = (ins ArrayRefParameter<"mlir::rlc::FunctionArgumentAttr">:$args, OptionalParameter<"mlir::rlc::ShugarizedTypeAttr ">:$shugarized_return_type);
56
89
57
- let assemblyFormat = "`<` `args` `=` $args (`return_type_location` `=` $return_type_location^ )? (` shugarized_return_type` `=` $shugarized_return_type^ )? `>`";
90
+ let assemblyFormat = "`<` `args` `=` $args (`shugarized_return_type` `=` $shugarized_return_type^ )? `>`";
58
91
59
92
let builders = [
60
93
AttrBuilder<(ins "llvm::ArrayRef<llvm::StringRef>":$arg_names),[{
61
94
llvm::SmallVector<mlir::rlc::FunctionArgumentAttr, 4> functionArgs;
62
95
for (auto name : arg_names) {
63
96
functionArgs.push_back(mlir::rlc::FunctionArgumentAttr::get($_ctxt, name));
64
97
}
65
- return mlir::rlc::FunctionInfoAttr::get($_ctxt, functionArgs, nullptr, nullptr);
98
+ return mlir::rlc::FunctionInfoAttr::get($_ctxt, functionArgs, nullptr);
99
+ }]>,
100
+ AttrBuilder<(ins "llvm::ArrayRef<mlir::rlc::FunctionArgumentAttr>":$args),[{
101
+ return mlir::rlc::FunctionInfoAttr::get($_ctxt, args, nullptr);
102
+ }]>,
103
+ AttrBuilder<(ins),[{
104
+ return mlir::rlc::FunctionInfoAttr::get($_ctxt, llvm::SmallVector<mlir::rlc::FunctionArgumentAttr, 4>(), nullptr);
66
105
}]>,
67
106
];
68
107
69
108
let extraClassDeclaration = [{
70
109
mlir::rlc::FunctionInfoAttr addSelfArgument() {
71
110
llvm::SmallVector<mlir::rlc::FunctionArgumentAttr, 4> args;
72
- args.push_back(mlir::rlc::FunctionArgumentAttr::get(getContext(), "self", nullptr, nullptr, nullptr ));
111
+ args.push_back(mlir::rlc::FunctionArgumentAttr::get(getContext(), "self", nullptr, nullptr));
73
112
for (const auto& arg : getArgs())
74
113
{
75
114
args.push_back(arg);
76
115
}
77
- return mlir::rlc::FunctionInfoAttr::get(getContext(), args, getReturnTypeLocation(), getShugarizedReturnType());
116
+ return mlir::rlc::FunctionInfoAttr::get(getContext(), args, getShugarizedReturnType());
117
+ }
118
+
119
+ mlir::rlc::FunctionInfoAttr replaceTypes(mlir::FunctionType shugarizedNewType) const {
120
+ llvm::SmallVector<mlir::rlc::FunctionArgumentAttr, 2> newArgs;
121
+ for (auto [arg, new_type] : llvm::zip(getArgs(), shugarizedNewType.getInputs()))
122
+ newArgs.push_back(arg.replaceType(new_type));
123
+
124
+ mlir::rlc::ShugarizedTypeAttr attr = nullptr;
125
+ if (getShugarizedReturnType() != nullptr and shugarizedNewType.getNumResults() != 0) {
126
+ attr = getShugarizedReturnType().replaceType(shugarizedNewType.getResults()[0]);
127
+ }
128
+ return mlir::rlc::FunctionInfoAttr::get(getContext(), newArgs, attr);
129
+
78
130
}
79
131
}];
80
132
}
@@ -85,23 +137,58 @@ def RLC_ClassFieldAttr : RLC_Attr<"ClassField", "class_field"> {
85
137
Implementation agnostic integer type.
86
138
}];
87
139
88
- let parameters = (ins StringRefParameter<>:$name, "mlir::Type":$type, OptionalParameter<"mlir::rlc::SourceRangeAttr">:$type_location);
140
+ let parameters = (ins StringRefParameter<>:$name, "mlir::Type":$type);
141
+
142
+
143
+ let assemblyFormat = "`<` $name `:` $type `>`";
144
+
145
+ let builders = [
146
+ AttrBuilderWithInferredContext<(ins "llvm::StringRef":$name, "mlir::Type":$type),[{
147
+ return mlir::rlc::ClassFieldAttr::get(type.getContext(), name, type);
148
+ }]>,
149
+ ];
150
+
151
+ }
152
+
153
+ def RLC_ClassFieldDeclarationAttr : RLC_Attr<"ClassFieldDeclaration", "class_field_declaration"> {
154
+ let summary = "class field attribute ";
155
+ let description = [{
156
+ Implementation agnostic integer type.
157
+ }];
158
+
159
+ let parameters = (ins "mlir::rlc::ClassFieldAttr":$field, OptionalParameter<"mlir::rlc::ShugarizedTypeAttr">:$shugarized_type);
89
160
90
161
91
- let assemblyFormat = "`<` $name `:` $type (`location ` `=` $type_location ^ )? `>`";
162
+ let assemblyFormat = "`<` $field (`shugarized ` `=` $shugarized_type ^ )? `>`";
92
163
93
164
let builders = [
94
165
AttrBuilderWithInferredContext<(ins "llvm::StringRef":$name, "mlir::Type":$type),[{
95
- return mlir::rlc::ClassFieldAttr::get(type.getContext(), name, type, nullptr);
166
+ return mlir::rlc::ClassFieldDeclarationAttr::get(type.getContext(), mlir::rlc::ClassFieldAttr::get(name, type), nullptr);
167
+ }]>,
168
+ AttrBuilderWithInferredContext<(ins "mlir::rlc::ClassFieldAttr":$field),[{
169
+ return mlir::rlc::ClassFieldDeclarationAttr::get(field.getContext(), field, nullptr);
96
170
}]>,
97
- AttrBuilderWithInferredContext<(ins "llvm::StringRef":$name, "mlir::Type":$type, "mlir::rlc::SourceRangeAttr":$type_location),[{
98
- return mlir::rlc::ClassFieldAttr::get(type.getContext(), name, type, type_location);
171
+ AttrBuilderWithInferredContext<(ins "mlir::rlc::ClassFieldAttr":$field, "mlir::rlc::ShugarizedTypeAttr":$shugarized_type),[{
172
+ return mlir::rlc::ClassFieldDeclarationAttr::get(field.getContext(), field, shugarized_type);
173
+ }]>,
174
+ AttrBuilderWithInferredContext<(ins "llvm::StringRef":$name, "mlir::Type":$type, "mlir::rlc::ShugarizedTypeAttr":$shugarized_type),[{
175
+ assert(shugarized_type != nullptr);
176
+ return mlir::rlc::ClassFieldDeclarationAttr::get(shugarized_type.getContext(), mlir::rlc::ClassFieldAttr::get(name, type), shugarized_type);
99
177
}]>,
100
178
];
101
179
180
+ let extraClassDeclaration = [{
181
+ mlir::Type getDeshugarizedType() {
182
+ return getField().getType();
183
+ }
184
+ llvm::StringRef getName() {
185
+ return getField().getName();
186
+ }
187
+ }];
102
188
}
103
189
104
190
105
191
def ClassFieldsArrayAttr : TypedArrayAttrBase<RLC_ClassFieldAttr, "array of attribudes describing array attr">;
192
+ def ClassDeclarationFieldsArrayAttr : TypedArrayAttrBase<RLC_ClassFieldDeclarationAttr, "array of attribudes describing array attr">;
106
193
107
194
#endif
0 commit comments