C++ emitter: improvements in the readability of generated code (4)

This commit is contained in:
Jonathan Protzenko 2015-11-04 15:51:05 -08:00
Родитель 8ceba04407
Коммит 6cc818486a
2 изменённых файлов: 20 добавлений и 2 удалений

Просмотреть файл

@ -88,7 +88,10 @@ module TDev {
env.indent + this.visit(env, expr.tree) + ";");
}
public visitExprHolder(env: H.Env, locals: J.JLocalDef[], expr: J.JExprHolder) {
public visitExprHolder(env: H.Env, locals: J.JLocalDef[], expr: J.JExpr) {
if (H.isInitialRecordAssignment(locals, expr))
return this.visit(env, locals[0])+"(new "+H.mkType(env, this.libraryMap, locals[0].type)+"_)";
var decls = locals.map(d => {
// Side-effect: marks [d] as promoted, if needed.
var decl = this.visit(env, d);
@ -122,7 +125,7 @@ module TDev {
var l = H.resolveLocal(env, name, id);
if (H.shouldPromoteToRef(env, type, isByRef)) {
H.markPromoted(env, id);
return "ManagedType<"+ t + "> " + l + "(new "+t+")";
return "Ref<"+ t + "> " + l;
} else {
return t + " " + l;
}

Просмотреть файл

@ -479,6 +479,21 @@ module TDev {
return x.nodeType == "stringLiteral" && (<J.JStringLiteral> x).value;
}
export function isInitialRecordAssignment(locals: J.JLocalDef[], expr: J.JExpr) {
return (
locals.length == 1 &&
expr.nodeType == "call" &&
(<J.JCall> expr).name == ":=" &&
(<J.JCall> expr).args.length == 2 &&
(<J.JCall> expr).args[1].nodeType == "call" &&
isRecordConstructor(
(<J.JCall> (<J.JCall> expr).args[1]).name,
(<J.JCall> (<J.JCall> expr).args[1]).args) &&
(<J.JCall> expr).args[0].nodeType == "localRef" &&
(<J.JLocalRef> (<J.JCall> expr).args[0]).localId == <any> locals[0].id
);
}
export function willCompile (f: J.JAction) {
return isShimBody(f.body) == null;
}