Skip to content

Commit 610da51

Browse files
committed
fix strings issue in ruby wrappers
1 parent d5621d5 commit 610da51

File tree

6 files changed

+163
-82
lines changed

6 files changed

+163
-82
lines changed

lib/conversions/src/RLCToRuby.cpp

Lines changed: 102 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,15 @@ namespace mlir::rlc
204204
mlir::Type type, llvm::StringRef name, StreamWriter& writer)
205205
{
206206
bool isUserDefined = false;
207-
if (type and (mlir::isa<mlir::rlc::ClassType>(type) or
208-
mlir::isa<mlir::rlc::AlternativeType>(type)))
207+
if (type and (mlir::isa<mlir::rlc::ClassType>(decayCtxFrmType(type)) or
208+
mlir::isa<mlir::rlc::AlternativeType>(decayCtxFrmType(type))))
209209
isUserDefined = true;
210210

211211
bool isStringArg =
212212
mlir::isa_and_nonnull<mlir::rlc::StringLiteralType>(type);
213213
if (type != nullptr and builtinCType(type))
214214
{
215-
writer.write("rlc_convert_to_");
215+
writer.write("RLC::rlc_convert_to_");
216216
writer.writeType(type, 0);
217217
writer.write("(");
218218
}
@@ -226,7 +226,7 @@ namespace mlir::rlc
226226
writer.write(")");
227227
}
228228
if (isUserDefined)
229-
writer.write("._data");
229+
writer.write(".__rlc_data");
230230
writer.write(" ");
231231
}
232232

@@ -247,7 +247,8 @@ namespace mlir::rlc
247247
for (size_t i = 0; i != typeRange.size(); i++)
248248
{
249249
printArg(typeRange[i], infoRange[i], writer);
250-
writer.write(", ");
250+
if (i != typeRange.size() - 1)
251+
writer.write(", ");
251252
}
252253
writer.write(")");
253254
}
@@ -320,7 +321,7 @@ namespace mlir::rlc
320321
w.write("__to_return = ");
321322
w.writeType(resultType);
322323
w.writenl(".new");
323-
w.writenl("__result = __to_return._data.to_ptr");
324+
w.writenl("__result = __to_return.__rlc_data.to_ptr");
324325
}
325326
else
326327
{
@@ -662,11 +663,23 @@ namespace mlir::rlc
662663
w.endLine();
663664
{
664665
auto _ = w.indent();
665-
w.writenl(
666-
"return _",
666+
w.write(
667+
"return RLC::_",
667668
mangledName(name, isMethod, overload),
668-
isMethod ? " self, " : "",
669-
" *args");
669+
isMethod ? " self " : " ");
670+
if (isMethod and overload.getInputs().size() != 1)
671+
w.write(", ");
672+
673+
size_t i = 0;
674+
for (auto type : llvm::drop_begin(overload.getInputs(), isMethod))
675+
{
676+
w.write("args[", i, "]");
677+
if (i++ + 1 + isMethod != overload.getNumInputs())
678+
{
679+
w.write(", ");
680+
}
681+
}
682+
w.endLine();
670683
}
671684
w.writenl("end");
672685
}
@@ -700,7 +713,7 @@ namespace mlir::rlc
700713
mlir::rlc::StreamWriter& w,
701714
MemberFunctionsTable& table)
702715
{
703-
w.writenl("def _data");
716+
w.writenl("def __rlc_data");
704717
{
705718
auto _ = w.indent();
706719
w.write("return @content");
@@ -861,6 +874,8 @@ namespace mlir::rlc
861874
printCFunDecls(writer, op, builder);
862875
}
863876
writer.writenl("end");
877+
writer.writenl("module RLC");
878+
writer.writenl("module_function");
864879
writer.writenl("actions = Hash.new { |hash, key| hash[key] = [] }");
865880
writer.writenl("wrappers = Hash.new { |hash, key| hash[key] = [] }");
866881
writer.writenl("signatures = {}");
@@ -1073,7 +1088,7 @@ namespace mlir::rlc
10731088
OS << "Integer";
10741089
});
10751090
matcher.add([](mlir::rlc::BoolType type, llvm::raw_string_ostream& OS) {
1076-
OS << ":Boolean";
1091+
OS << "Boolean";
10771092
});
10781093
matcher.add([](mlir::rlc::FloatType type, llvm::raw_string_ostream& OS) {
10791094
OS << "Float";
@@ -1194,9 +1209,12 @@ namespace mlir::rlc
11941209
returnVoid ? "" : "__result, ",
11951210
"@content.to_ptr, ");
11961211

1212+
size_t i = 1;
11971213
for (auto [info, type] : llvm::zip(argsInfo, argTypes))
11981214
{
11991215
printArg(type, info.getName(), OS);
1216+
if (i++ != argsInfo.size())
1217+
OS.writenl(", ");
12001218
}
12011219
OS.writenl(")");
12021220

@@ -1319,82 +1337,87 @@ namespace mlir::rlc
13191337
table,
13201338
builder);
13211339

1322-
//// discover all enums
1323-
llvm::StringMap<mlir::rlc::EnumDeclarationOp> enums;
1324-
for (auto op : getOperation().getOps<mlir::rlc::EnumDeclarationOp>())
13251340
{
1326-
enums[op.getName()] = op;
1327-
}
1341+
auto _ = matcher.getWriter().indent();
13281342

1329-
// emit declarations of types
1330-
for (auto t : ::rlc::postOrderTypes(getOperation()))
1331-
{
1332-
if (auto casted = mlir::dyn_cast<mlir::rlc::ClassType>(t))
1343+
//// discover all enums
1344+
llvm::StringMap<mlir::rlc::EnumDeclarationOp> enums;
1345+
for (auto op : getOperation().getOps<mlir::rlc::EnumDeclarationOp>())
1346+
{
1347+
enums[op.getName()] = op;
1348+
}
1349+
1350+
// emit declarations of types
1351+
for (auto t : ::rlc::postOrderTypes(getOperation()))
13331352
{
1334-
emitDeclaration(
1335-
casted,
1336-
matcher.getWriter(),
1337-
table,
1338-
builder,
1339-
enums.count(casted.getName()) ? enums[casted.getName()]
1340-
: nullptr);
1341-
if (builder.isClassOfAction(casted))
1353+
if (auto casted = mlir::dyn_cast<mlir::rlc::ClassType>(t))
13421354
{
1343-
auto action = mlir::cast<mlir::rlc::ActionFunction>(
1344-
builder.getActionOf(casted).getDefiningOp());
1345-
emitActionFunctions(action, builder, matcher.getWriter());
1355+
emitDeclaration(
1356+
casted,
1357+
matcher.getWriter(),
1358+
table,
1359+
builder,
1360+
enums.count(casted.getName()) ? enums[casted.getName()]
1361+
: nullptr);
1362+
if (builder.isClassOfAction(casted))
1363+
{
1364+
auto action = mlir::cast<mlir::rlc::ActionFunction>(
1365+
builder.getActionOf(casted).getDefiningOp());
1366+
emitActionFunctions(action, builder, matcher.getWriter());
1367+
}
1368+
1369+
matcher.getWriter().writenl("end");
13461370
}
1347-
1348-
matcher.getWriter().writenl("end");
1371+
if (auto casted = mlir::dyn_cast<mlir::rlc::AlternativeType>(t))
1372+
emitDeclaration(casted, matcher.getWriter(), table, builder);
13491373
}
1350-
if (auto casted = mlir::dyn_cast<mlir::rlc::AlternativeType>(t))
1351-
emitDeclaration(casted, matcher.getWriter(), table, builder);
1352-
}
13531374

1354-
//// emit declarations of free functions
1355-
matcher.apply(getOperation());
1375+
//// emit declarations of free functions
1376+
matcher.apply(getOperation());
13561377

1357-
// emit dispatcher of free functions
1358-
llvm::StringMap<llvm::SmallVector<mlir::FunctionType>> sortedOverloads;
1359-
for (auto op : getOperation().getOps<mlir::rlc::FunctionOp>())
1360-
{
1361-
if (op.getIsMemberFunction())
1362-
continue;
1363-
sortedOverloads[op.getUnmangledName()].push_back(op.getType());
1364-
if (not op.getPrecondition().empty())
1365-
sortedOverloads["can_" + op.getUnmangledName().str()].push_back(
1366-
mlir::FunctionType::get(
1367-
op.getContext(),
1368-
op.getType().getInputs(),
1369-
{ mlir::rlc::BoolType::get(op.getContext()) }));
1370-
}
1371-
for (auto op : getOperation().getOps<mlir::rlc::ActionFunction>())
1372-
{
1373-
if (op.getIsMemberFunction())
1374-
continue;
1375-
sortedOverloads[op.getUnmangledName()].push_back(
1376-
op.getMainActionType());
1377-
if (not op.getPrecondition().empty())
1378-
sortedOverloads["can_" + op.getUnmangledName().str()].push_back(
1379-
mlir::FunctionType::get(
1380-
op.getContext(),
1381-
op.getMainActionType().getInputs(),
1382-
{ mlir::rlc::BoolType::get(op.getContext()) }));
1383-
1384-
auto types = builder.getConverter().getTypes().get(
1385-
("Any" + op.getClassType().getName() + "Action").str());
1386-
if (not types.empty())
1387-
matcher.getWriter().writenl(
1388-
"actionToAnyFunctionType[\"",
1389-
op.getUnmangledName(),
1390-
"\"] = Any",
1391-
op.getClassType().getName(),
1392-
"Action");
1393-
}
1378+
// emit dispatcher of free functions
1379+
llvm::StringMap<llvm::SmallVector<mlir::FunctionType>> sortedOverloads;
1380+
for (auto op : getOperation().getOps<mlir::rlc::FunctionOp>())
1381+
{
1382+
if (op.getIsMemberFunction())
1383+
continue;
1384+
sortedOverloads[op.getUnmangledName()].push_back(op.getType());
1385+
if (not op.getPrecondition().empty())
1386+
sortedOverloads["can_" + op.getUnmangledName().str()].push_back(
1387+
mlir::FunctionType::get(
1388+
op.getContext(),
1389+
op.getType().getInputs(),
1390+
{ mlir::rlc::BoolType::get(op.getContext()) }));
1391+
}
1392+
for (auto op : getOperation().getOps<mlir::rlc::ActionFunction>())
1393+
{
1394+
if (op.getIsMemberFunction())
1395+
continue;
1396+
sortedOverloads[op.getUnmangledName()].push_back(
1397+
op.getMainActionType());
1398+
if (not op.getPrecondition().empty())
1399+
sortedOverloads["can_" + op.getUnmangledName().str()].push_back(
1400+
mlir::FunctionType::get(
1401+
op.getContext(),
1402+
op.getMainActionType().getInputs(),
1403+
{ mlir::rlc::BoolType::get(op.getContext()) }));
1404+
1405+
auto types = builder.getConverter().getTypes().get(
1406+
("Any" + op.getClassType().getName() + "Action").str());
1407+
if (not types.empty())
1408+
matcher.getWriter().writenl(
1409+
"actionToAnyFunctionType[\"",
1410+
op.getUnmangledName(),
1411+
"\"] = Any",
1412+
op.getClassType().getName(),
1413+
"Action");
1414+
}
13941415

1395-
for (auto& pair : sortedOverloads)
1396-
emitOverloadDispatcher(
1397-
pair.first(), pair.second, matcher.getWriter(), false);
1416+
for (auto& pair : sortedOverloads)
1417+
emitOverloadDispatcher(
1418+
pair.first(), pair.second, matcher.getWriter(), false);
1419+
}
1420+
matcher.getWriter().writenl("end");
13981421
}
13991422
};
14001423

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# REQUIRES: has_ruby
2+
# RUN: split-file %s %t
3+
4+
# RUN: rlc %t/source.rl -o %t/lib%sharedext -i %stdlib --shared
5+
# RUN: rlc %t/source.rl -o %t/lib%libext -i %stdlib --compile
6+
7+
# RUN: rlc %t/source.rl -o %t/library.rb -i %stdlib --ruby
8+
# RUN: ruby %t/to_run.rb
9+
10+
11+
#--- source.rl
12+
act play() -> Game:
13+
frm asd = 0
14+
act pick(Int x)
15+
asd = x
16+
17+
#--- to_run.rb
18+
require_relative 'library'
19+
20+
pair = RLC.play
21+
pair.pick 3
22+
puts(pair.is_done)
23+
raise "this is wrong" unless pair.asd == 3

tool/rlc/test/wrappers/ruby_class.rl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ cls Outer:
2626
#--- to_run.rb
2727
require_relative 'library'
2828

29-
pair = Outer.new
29+
pair = RLC::Outer.new
3030
pair.inner.x = 3
3131
pair.inner.y = 2
3232

tool/rlc/test/wrappers/ruby_function.rl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ fun to_invoke() -> Int {true}:
1515
#--- to_run.rb
1616
require_relative 'library'
1717

18-
raise "this is wrong" unless to_invoke == 5
18+
raise "this is wrong" unless RLC.to_invoke == 5
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# REQUIRES: has_ruby
2+
# RUN: split-file %s %t
3+
4+
# RUN: rlc %t/source.rl -o %t/lib%sharedext -i %stdlib --shared
5+
# RUN: rlc %t/source.rl -o %t/lib%libext -i %stdlib --compile
6+
7+
# RUN: rlc %t/source.rl -o %t/library.rb -i %stdlib --ruby
8+
# RUN: ruby %t/to_run.rb
9+
10+
11+
#--- source.rl
12+
import collections.vector
13+
import serialization.print
14+
15+
cls Context:
16+
Int x
17+
Int y
18+
19+
act sequence(ctx Context context) -> Sequence:
20+
frm accumulator : Vector<Int> # drop this and Sequence will become trivially constructible (that is, no mallocs triggered)
21+
while true:
22+
act add(Int z)
23+
accumulator.append(context.x + context.y + z)
24+
print(accumulator)
25+
26+
#--- to_run.rb
27+
require_relative 'library'
28+
29+
ctx = RLC::Context.new
30+
ctx.x = 3
31+
ctx.y = 2
32+
state = RLC.sequence ctx
33+
if state.can_add(ctx, 10)
34+
state.add(ctx, 10)
35+
end

tool/rlc/test/wrappers/ruby_vectorl.rl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fun asd():
2121
require_relative 'library'
2222

2323

24-
pair = VectorTint64_tT.new
24+
pair = RLC::VectorTint64_tT.new
2525
x = 2;
2626
pair.append(x);
2727
pair.append(x);

0 commit comments

Comments
 (0)