From c2be4a50804030929595c7d2b63a178a06d8476e Mon Sep 17 00:00:00 2001 From: bdemsky Date: Sat, 3 Nov 2007 18:39:16 +0000 Subject: [PATCH] more string support stuff --- Robust/src/ClassLibrary/String.java | 7 ++ Robust/src/IR/AssignOperation.java | 4 + Robust/src/IR/Flat/BuildFlat.java | 104 ++++++++++++++++++++------ Robust/src/IR/Tree/SemanticCheck.java | 17 +++++ Robust/src/IR/TypeUtil.java | 3 + 5 files changed, 111 insertions(+), 24 deletions(-) diff --git a/Robust/src/ClassLibrary/String.java b/Robust/src/ClassLibrary/String.java index c93d67cf..00cf7a0f 100644 --- a/Robust/src/ClassLibrary/String.java +++ b/Robust/src/ClassLibrary/String.java @@ -71,6 +71,13 @@ public class String { return this.lastindexOf(ch, count - 1); } + public static String concat2(String s1, String s2) { + if (s1==null) + return "null".concat(s2); + else + return s1.concat(s2); + } + public String concat(String str) { String newstr=new String(); newstr.count=this.count+str.count; diff --git a/Robust/src/IR/AssignOperation.java b/Robust/src/IR/AssignOperation.java index 1a4e2a2b..3e750b9f 100644 --- a/Robust/src/IR/AssignOperation.java +++ b/Robust/src/IR/AssignOperation.java @@ -25,6 +25,10 @@ public class AssignOperation { this.operation=parseOp(op); } + public int getOp() { + return operation; + } + public Operation getBaseOp() { switch(operation) { case EQ: diff --git a/Robust/src/IR/Flat/BuildFlat.java b/Robust/src/IR/Flat/BuildFlat.java index d873cbe8..5bc766fd 100644 --- a/Robust/src/IR/Flat/BuildFlat.java +++ b/Robust/src/IR/Flat/BuildFlat.java @@ -414,14 +414,23 @@ public class BuildFlat { //If it is a preinc we need to store the initial value TempDescriptor src_tmp2=pre?TempDescriptor.tempFactory("src",an.getDest().getType()):out_temp; TempDescriptor tmp=TempDescriptor.tempFactory("srctmp3",an.getDest().getType()); - FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2); last.addNext(ffn); last=ffn; - FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); - src_tmp=tmp; - last.addNext(fon); - last=fon; + + if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) { + ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass); + MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)}); + FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp}); + src_tmp=tmp; + last.addNext(fc); + last=fc; + } else { + FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); + src_tmp=tmp; + last.addNext(fon); + last=fon; + } } FlatSetFieldNode fsfn=new FlatSetFieldNode(dst_tmp, fan.getField(), src_tmp); @@ -460,10 +469,22 @@ public class BuildFlat { FlatElementNode fen=new FlatElementNode(dst_tmp, index_tmp, src_tmp2); last.addNext(fen); last=fen; - FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); - src_tmp=tmp; - last.addNext(fon); - last=fon; + + if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) { + ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass); + MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)}); + FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp}); + src_tmp=tmp; + last.addNext(fc); + last=fc; + } else { + + + FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); + src_tmp=tmp; + last.addNext(fon); + last=fon; + } } @@ -500,10 +521,21 @@ public class BuildFlat { FlatFieldNode ffn=new FlatFieldNode(fan.getField(), dst_tmp, src_tmp2); last.addNext(ffn); last=ffn; - FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); - src_tmp=tmp; - last.addNext(fon); - last=fon; + + + if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) { + ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass); + MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)}); + FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp}); + src_tmp=tmp; + last.addNext(fc); + last=fc; + } else { + FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); + src_tmp=tmp; + last.addNext(fon); + last=fon; + } } @@ -534,10 +566,21 @@ public class BuildFlat { last.addNext(ffn); } last=ffn; - FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); - src_tmp=tmp; - last.addNext(fon); - last=fon; + + + if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) { + ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass); + MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)}); + FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp}); + src_tmp=tmp; + last.addNext(fc); + last=fc; + } else { + FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); + src_tmp=tmp; + last.addNext(fon); + last=fon; + } } FlatSetFieldNode fsfn=new FlatSetFieldNode(getTempforVar(nn.getVar()), nn.getField(), src_tmp); @@ -570,13 +613,26 @@ public class BuildFlat { last=fon; } - FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); - if (first==null) - first=fon; - else - last.addNext(fon); - src_tmp=tmp; - last=fon; + + if (base.getOp()==Operation.ADD&&an.getDest().getType().isString()) { + ClassDescriptor stringcd=typeutil.getClass(TypeUtil.StringClass); + MethodDescriptor concatmd=typeutil.getMethod(stringcd, "concat2", new TypeDescriptor[] {new TypeDescriptor(stringcd), new TypeDescriptor(stringcd)}); + FlatCall fc=new FlatCall(concatmd, tmp, null, new TempDescriptor[] {src_tmp2, src_tmp}); + if (first==null) + first=fc; + else + last.addNext(fc); + src_tmp=tmp; + last=fc; + } else { + FlatOpNode fon=new FlatOpNode(tmp, src_tmp2, src_tmp, base); + if (first==null) + first=fon; + else + last.addNext(fon); + src_tmp=tmp; + last=fon; + } } FlatOpNode fon=new FlatOpNode(getTempforVar(nn.getVar()), src_tmp, null, new Operation(Operation.ASSIGN)); diff --git a/Robust/src/IR/Tree/SemanticCheck.java b/Robust/src/IR/Tree/SemanticCheck.java index 53df0128..145f8efa 100644 --- a/Robust/src/IR/Tree/SemanticCheck.java +++ b/Robust/src/IR/Tree/SemanticCheck.java @@ -502,6 +502,7 @@ public class SemanticCheck { throw new Error("Bad lside in "+an.printNode(0)); checkExpressionNode(md, nametable, an.getDest(), null); + /* We want parameter variables to tasks to be immutable */ if (md instanceof TaskDescriptor) { if (an.getDest() instanceof NameNode) { @@ -513,6 +514,22 @@ public class SemanticCheck { } } + if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) { + //String add + ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass); + TypeDescriptor stringtd=new TypeDescriptor(stringcl); + NameDescriptor nd=new NameDescriptor("String"); + NameDescriptor valuend=new NameDescriptor(nd, "valueOf"); + + if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) { + MethodInvokeNode rightmin=new MethodInvokeNode(valuend); + rightmin.addArgument(an.getSrc()); + an.right=rightmin; + checkExpressionNode(md, nametable, an.getSrc(), null); + } + } + + if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) { throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0)); } diff --git a/Robust/src/IR/TypeUtil.java b/Robust/src/IR/TypeUtil.java index 9471925c..92e4e586 100644 --- a/Robust/src/IR/TypeUtil.java +++ b/Robust/src/IR/TypeUtil.java @@ -121,6 +121,9 @@ public class TypeUtil { /* Is this more specific than bestmd */ } } + if (bestmd==null) + throw new Error("Could find: "+name + " in "+cd); + return bestmd; } -- 2.34.1