Skip to content

Commit 730d448

Browse files
committed
finilized types ast structure
1 parent 964ed69 commit 730d448

18 files changed

+478
-285
lines changed

Diff for: lib/dialect/include/rlc/dialect/SymbolTable.h

+5
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,17 @@ namespace mlir::rlc
122122
{
123123
return converter.convertType(type);
124124
}
125+
mlir::Type shugarizedConvertType(mlir::Type type)
126+
{
127+
return shugarizedConverter.convertType(type);
128+
}
125129

126130
void setErrorLocation(mlir::Location newLoc) { loc = newLoc; }
127131

128132
private:
129133
TypeTable types;
130134
mlir::TypeConverter converter;
135+
mlir::TypeConverter shugarizedConverter;
131136
mlir::Location loc;
132137
};
133138

Diff for: lib/dialect/src/Attrs.td

+100-13
Original file line numberDiff line numberDiff line change
@@ -31,50 +31,102 @@ def RLC_SourceRangeAttr : RLC_Attr<"SourceRange", "source_range_attr"> {
3131

3232
}
3333

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+
3459
def RLC_FunctionArgumentAttr : RLC_Attr<"FunctionArgument", "function_argument"> {
3560
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.";
3661
let description = [{
3762
info about function argument
3863
}];
3964

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);
4166

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^ )? `>`";
4368

4469
let builders = [
4570
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);
4772
}]>,
4873
];
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+
}];
4982
}
5083

5184
def RLC_FunctionInfoAttr : RLC_Attr<"FunctionInfo", "function_info"> {
5285
let summary = "AST infos about a function";
5386

5487

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);
5689

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^ )? `>`";
5891

5992
let builders = [
6093
AttrBuilder<(ins "llvm::ArrayRef<llvm::StringRef>":$arg_names),[{
6194
llvm::SmallVector<mlir::rlc::FunctionArgumentAttr, 4> functionArgs;
6295
for (auto name : arg_names) {
6396
functionArgs.push_back(mlir::rlc::FunctionArgumentAttr::get($_ctxt, name));
6497
}
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);
66105
}]>,
67106
];
68107

69108
let extraClassDeclaration = [{
70109
mlir::rlc::FunctionInfoAttr addSelfArgument() {
71110
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));
73112
for (const auto& arg : getArgs())
74113
{
75114
args.push_back(arg);
76115
}
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+
78130
}
79131
}];
80132
}
@@ -85,23 +137,58 @@ def RLC_ClassFieldAttr : RLC_Attr<"ClassField", "class_field"> {
85137
Implementation agnostic integer type.
86138
}];
87139

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);
89160

90161

91-
let assemblyFormat = "`<` $name `:` $type (`location` `=` $type_location^ )? `>`";
162+
let assemblyFormat = "`<` $field (`shugarized` `=` $shugarized_type^ )? `>`";
92163

93164
let builders = [
94165
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);
96170
}]>,
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);
99177
}]>,
100178
];
101179

180+
let extraClassDeclaration = [{
181+
mlir::Type getDeshugarizedType() {
182+
return getField().getType();
183+
}
184+
llvm::StringRef getName() {
185+
return getField().getName();
186+
}
187+
}];
102188
}
103189

104190

105191
def ClassFieldsArrayAttr : TypedArrayAttrBase<RLC_ClassFieldAttr, "array of attribudes describing array attr">;
192+
def ClassDeclarationFieldsArrayAttr : TypedArrayAttrBase<RLC_ClassFieldDeclarationAttr, "array of attribudes describing array attr">;
106193

107194
#endif

Diff for: lib/dialect/src/Dialect.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class TypeAliasASMInterface: public mlir::OpAsmDialectInterface
3131

3232
AliasResult getAlias(mlir::Attribute type, llvm::raw_ostream &OS) const final
3333
{
34+
if (auto casted = type.dyn_cast<mlir::rlc::ShugarizedTypeAttr>())
35+
{
36+
OS << "shugar_type";
37+
return AliasResult::FinalAlias;
38+
}
3439
if (auto casted = type.dyn_cast<mlir::rlc::FunctionInfoAttr>())
3540
{
3641
OS << "fun_info";

Diff for: lib/dialect/src/EmitEnumEntitiesPass.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ namespace mlir::rlc
336336
mlir::FunctionType::get(
337337
rewriter.getContext(), {}, { expression.getResult().getType() }),
338338

339-
mlir::rlc::FunctionInfoAttr::get(rewriter.getContext(), {}),
339+
mlir::rlc::FunctionInfoAttr::get(rewriter.getContext()),
340340
true);
341341
rewriter.createBlock(&op.getBody());
342342
auto retStm = rewriter.create<mlir::rlc::ReturnStatement>(
@@ -440,17 +440,25 @@ namespace mlir::rlc
440440
for (auto declaration : ops)
441441
{
442442
rewriter.setInsertionPoint(declaration);
443-
llvm::SmallVector<mlir::Attribute, 1> fields{
443+
llvm::SmallVector<mlir::rlc::ClassFieldAttr, 1> fields{
444444
mlir::rlc::ClassFieldAttr::get(
445445
"value",
446446
mlir::rlc::ScalarUseType::get(getOperation().getContext(), "Int"))
447447
};
448+
assert(declaration.getTypeLocation().has_value());
449+
auto shugarType = mlir::rlc::ShugarizedTypeAttr::get(
450+
declaration.getContext(),
451+
*declaration.getTypeLocation(),
452+
fields.back().getType());
453+
llvm::SmallVector<mlir::rlc::ClassFieldDeclarationAttr, 1> fieldsDecl{
454+
mlir::rlc::ClassFieldDeclarationAttr::get(
455+
declaration.getContext(), fields.back(), shugarType)
456+
};
448457
auto op = rewriter.create<mlir::rlc::ClassDeclaration>(
449458
declaration.getLoc(),
450-
mlir::rlc::UnknownType::get(getOperation().getContext()),
451459
declaration.getNameAttr(),
452-
rewriter.getArrayAttr(fields),
453-
rewriter.getTypeArrayAttr({}),
460+
fieldsDecl,
461+
mlir::ArrayRef<mlir::Type>({}),
454462
*declaration.getTypeLocation());
455463
moveAllFunctionDeclsToNewClass(declaration, op, rewriter);
456464
if (moveAllMethodExpressionToNewClassFunctions(

Diff for: lib/dialect/src/ExtractPreconditionPass.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static mlir::rlc::FunctionMetadataOp emitPreconditionFunction(
3838
("can_" + fun.getUnmangledName()).str(),
3939
ftype,
4040
mlir::rlc::FunctionInfoAttr::get(
41-
rewriter.getContext(), fun.getInfo().getArgs(), nullptr, nullptr),
41+
rewriter.getContext(), fun.getInfo().getArgs(), nullptr),
4242
fun.getIsMemberFunction());
4343
rewriter.cloneRegionBefore(
4444
fun.getPrecondition(),

Diff for: lib/dialect/src/Interfaces.td

+1-6
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ def TypeUser : RLC_Interface<"TypeUser"> {
3636
let methods = [
3737
InterfaceMethod<
3838
"range in the source file where the type was used. Null if there was no range",
39-
"llvm::SmallVector<mlir::rlc::SourceRangeAttr, 2>", "getTypeSourceRange",
40-
(ins)
41-
>,
42-
InterfaceMethod<
43-
"type written by the user in the source code. Empty if there are none. There may be more than one in places such as function declarations, since they include the return type and the argument type too.",
44-
"llvm::SmallVector<mlir::Type, 2>", "getExplicitType",
39+
"llvm::SmallVector<mlir::rlc::ShugarizedTypeAttr, 2>", "getShugarizedTypes",
4540
(ins)
4641
>,
4742
];

0 commit comments

Comments
 (0)