/// <summary> /// Copy constructor. /// </summary> /// <param name="that">The assignment target to be copied.</param> protected AssignmentTarget(AssignmentTarget that) : base(that) { AssignmentTargetType = that.AssignmentTargetType; }
/// <summary> /// pre-run for emitting the needed entities before emitting the real code /// - emits sequence variable declarations (only once for every variable, declaration only possible at assignment targets) /// </summary> void EmitNeededVarEntities(AssignmentTarget tgt, SourceBuilder source) { source.AppendFront(DeclareResultVar(tgt)); switch(tgt.AssignmentTargetType) { case AssignmentTargetType.Var: { AssignmentTargetVar var = (AssignmentTargetVar)tgt; EmitVarIfNew(var.DestVar, source); break; } case AssignmentTargetType.YieldingToVar: { AssignmentTargetYieldingVar var = (AssignmentTargetYieldingVar)tgt; EmitVarIfNew(var.DestVar, source); break; } default: break; } }
public SequenceComputationAssignment(AssignmentTarget tgt, SequenceComputation srcValueProvider) : base(SequenceComputationType.Assignment) { Target = tgt; SourceValueProvider = srcValueProvider; }
void EmitAssignment(AssignmentTarget tgt, string sourceValueComputation, SourceBuilder source) { switch(tgt.AssignmentTargetType) { case AssignmentTargetType.YieldingToVar: { AssignmentTargetYieldingVar tgtYield = (AssignmentTargetYieldingVar)tgt; source.AppendFront(SetVar(tgtYield.DestVar, sourceValueComputation)); source.AppendFront(SetResultVar(tgtYield, GetVar(tgtYield.DestVar))); break; } case AssignmentTargetType.Visited: { AssignmentTargetVisited tgtVisitedFlag = (AssignmentTargetVisited)tgt; source.AppendFront("bool visval_"+tgtVisitedFlag.Id+" = (bool)"+sourceValueComputation+";\n"); source.AppendFront("graph.SetVisited((GRGEN_LIBGR.IGraphElement)"+GetVar(tgtVisitedFlag.GraphElementVar) + ", (int)" + GetSequenceExpression(tgtVisitedFlag.VisitedFlagExpression, source) + ", visval_" + tgtVisitedFlag.Id + ");\n"); source.AppendFront(SetResultVar(tgtVisitedFlag, "visval_"+tgtVisitedFlag.Id)); break; } case AssignmentTargetType.IndexedVar: { AssignmentTargetIndexedVar tgtIndexedVar = (AssignmentTargetIndexedVar)tgt; string container = "container_" + tgtIndexedVar.Id; source.AppendFront("object " + container + " = " + GetVar(tgtIndexedVar.DestVar) + ";\n"); string key = "key_" + tgtIndexedVar.Id; source.AppendFront("object " + key + " = " + GetSequenceExpression(tgtIndexedVar.KeyExpression, source) + ";\n"); source.AppendFront(SetResultVar(tgtIndexedVar, container)); // container is a reference, so we can assign it already here before the changes if(tgtIndexedVar.DestVar.Type == "") { source.AppendFront("if(" + container + " is IList) {\n"); source.Indent(); string array = "((System.Collections.IList)" + container + ")"; if(!TypesHelper.IsSameOrSubtype(tgtIndexedVar.KeyExpression.Type(env), "int", model)) { source.AppendFront("if(true) {\n"); source.Indent(); source.AppendFront("throw new Exception(\"Can't access non-int index in array\");\n"); } else { source.AppendFront("if(" + array + ".Count > (int)" + key + ") {\n"); source.Indent(); source.AppendFront(array + "[(int)" + key + "] = " + sourceValueComputation + ";\n"); } source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("} else if(" + container + " is GRGEN_LIBGR.IDeque) {\n"); source.Indent(); string deque = "((GRGEN_LIBGR.IDeque)" + container + ")"; if(!TypesHelper.IsSameOrSubtype(tgtIndexedVar.KeyExpression.Type(env), "int", model)) { source.AppendFront("if(true) {\n"); source.Indent(); source.AppendFront("throw new Exception(\"Can't access non-int index in deque\");\n"); } else { source.AppendFront("if(" + deque + ".Count > (int)" + key + ") {\n"); source.Indent(); source.AppendFront(deque + "[(int)" + key + "] = " + sourceValueComputation + ";\n"); } source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("} else {\n"); source.Indent(); string dictionary = "((System.Collections.IDictionary)" + container + ")"; source.AppendFront("if(" + dictionary + ".Contains(" + key + ")) {\n"); source.Indent(); source.AppendFront(dictionary + "[" + key + "] = " + sourceValueComputation + ";\n"); source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("}\n"); } else if(tgtIndexedVar.DestVar.Type.StartsWith("array")) { string array = GetVar(tgtIndexedVar.DestVar); source.AppendFront("if(" + array + ".Count > (int)" + key + ") {\n"); source.Indent(); source.AppendFront(array + "[(int)" + key + "] = " + sourceValueComputation + ";\n"); source.Unindent(); source.AppendFront("}\n"); } else if(tgtIndexedVar.DestVar.Type.StartsWith("deque")) { string deque = GetVar(tgtIndexedVar.DestVar); source.AppendFront("if(" + deque + ".Count > (int)" + key + ") {\n"); source.Indent(); source.AppendFront(deque + "[(int)" + key + "] = " + sourceValueComputation + ";\n"); source.Unindent(); source.AppendFront("}\n"); } else { string dictionary = GetVar(tgtIndexedVar.DestVar); string dictSrcType = TypesHelper.XgrsTypeToCSharpType(TypesHelper.ExtractSrc(tgtIndexedVar.DestVar.Type), model); source.AppendFront("if(" + dictionary + ".ContainsKey((" + dictSrcType + ")" + key + ")) {\n"); source.Indent(); source.AppendFront(dictionary + "[(" + dictSrcType + ")" + key + "] = " + sourceValueComputation + ";\n"); source.Unindent(); source.AppendFront("}\n"); } break; } case AssignmentTargetType.Var: { AssignmentTargetVar tgtVar = (AssignmentTargetVar)tgt; source.AppendFront(SetVar(tgtVar.DestVar, sourceValueComputation)); source.AppendFront(SetResultVar(tgtVar, GetVar(tgtVar.DestVar))); break; } case AssignmentTargetType.Attribute: { AssignmentTargetAttribute tgtAttr = (AssignmentTargetAttribute)tgt; source.AppendFront("object value_" + tgtAttr.Id + " = " + sourceValueComputation + ";\n"); source.AppendFront("GRGEN_LIBGR.IGraphElement elem_" + tgtAttr.Id + " = (GRGEN_LIBGR.IGraphElement)" + GetVar(tgtAttr.DestVar) + ";\n"); source.AppendFront("GRGEN_LIBGR.AttributeType attrType_" + tgtAttr.Id + ";\n"); source.AppendFront("value_" + tgtAttr.Id + " = GRGEN_LIBGR.ContainerHelper.IfAttributeOfElementIsContainerThenCloneContainer(elem_" + tgtAttr.Id + ", \"" + tgtAttr.AttributeName + "\", value_" + tgtAttr.Id + ", out attrType_" + tgtAttr.Id + ");\n"); source.AppendFront("if(elem_" + tgtAttr.Id + " is GRGEN_LIBGR.INode)\n"); source.AppendFront("\tgraph.ChangingNodeAttribute((GRGEN_LIBGR.INode)elem_" + tgtAttr.Id + ", attrType_" + tgtAttr.Id + ", GRGEN_LIBGR.AttributeChangeType.Assign, value_" + tgtAttr.Id + ", null);\n"); source.AppendFront("else\n"); source.AppendFront("\tgraph.ChangingEdgeAttribute((GRGEN_LIBGR.IEdge)elem_" + tgtAttr.Id + ", attrType_" + tgtAttr.Id + ", GRGEN_LIBGR.AttributeChangeType.Assign, value_" + tgtAttr.Id + ", null);\n"); source.AppendFront("elem_" + tgtAttr.Id + ".SetAttribute(\"" + tgtAttr.AttributeName + "\", value_" + tgtAttr.Id + ");\n"); if(gen.FireDebugEvents) { source.AppendFront("if(elem_" + tgtAttr.Id + " is GRGEN_LIBGR.INode)\n"); source.AppendFront("\tgraph.ChangedNodeAttribute((GRGEN_LIBGR.INode)elem_" + tgtAttr.Id + ", attrType_" + tgtAttr.Id + ");\n"); source.AppendFront("else\n"); source.AppendFront("\tgraph.ChangedEdgeAttribute((GRGEN_LIBGR.IEdge)elem_" + tgtAttr.Id + ", attrType_" + tgtAttr.Id + ");\n"); } source.AppendFront(SetResultVar(tgtAttr, "value_" + tgtAttr.Id)); break; } case AssignmentTargetType.AttributeIndexed: { AssignmentTargetAttributeIndexed tgtAttrIndexedVar = (AssignmentTargetAttributeIndexed)tgt; string value = "value_" + tgtAttrIndexedVar.Id; source.AppendFront("object " + value + " = " + sourceValueComputation + ";\n"); source.AppendFront(SetResultVar(tgtAttrIndexedVar, "value_" + tgtAttrIndexedVar.Id)); source.AppendFront("GRGEN_LIBGR.IGraphElement elem_" + tgtAttrIndexedVar.Id + " = (GRGEN_LIBGR.IGraphElement)" + GetVar(tgtAttrIndexedVar.DestVar) + ";\n"); string container = "container_" + tgtAttrIndexedVar.Id; source.AppendFront("object " + container + " = elem_" + tgtAttrIndexedVar.Id + ".GetAttribute(\"" + tgtAttrIndexedVar.AttributeName + "\");\n"); string key = "key_" + tgtAttrIndexedVar.Id; source.AppendFront("object " + key + " = " + GetSequenceExpression(tgtAttrIndexedVar.KeyExpression, source) + ";\n"); source.AppendFront("GRGEN_LIBGR.AttributeType attrType_" + tgtAttrIndexedVar.Id + " = elem_" + tgtAttrIndexedVar.Id + ".Type.GetAttributeType(\"" + tgtAttrIndexedVar.AttributeName + "\");\n"); source.AppendFront("if(elem_" + tgtAttrIndexedVar.Id + " is GRGEN_LIBGR.INode)\n"); source.AppendFront("\tgraph.ChangingNodeAttribute((GRGEN_LIBGR.INode)elem_" + tgtAttrIndexedVar.Id + ", attrType_" + tgtAttrIndexedVar.Id + ", GRGEN_LIBGR.AttributeChangeType.AssignElement, " + value + ", " + key + ");\n"); source.AppendFront("else\n"); source.AppendFront("\tgraph.ChangingEdgeAttribute((GRGEN_LIBGR.IEdge)elem_" + tgtAttrIndexedVar.Id + ", attrType_" + tgtAttrIndexedVar.Id + ", GRGEN_LIBGR.AttributeChangeType.AssignElement, " + value + ", " + key + ");\n"); if(tgtAttrIndexedVar.DestVar.Type == "") { source.AppendFront("if(" + container + " is IList) {\n"); source.Indent(); string array = "((System.Collections.IList)" + container + ")"; if(!TypesHelper.IsSameOrSubtype(tgtAttrIndexedVar.KeyExpression.Type(env), "int", model)) { source.AppendFront("if(true) {\n"); source.Indent(); source.AppendFront("throw new Exception(\"Can't access non-int index in array\");\n"); } else { source.AppendFront("if(" + array + ".Count > (int)" + key + ") {\n"); source.Indent(); source.AppendFront(array + "[(int)" + key + "] = " + value + ";\n"); } source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("} else if(" + container + " is GRGEN_LIBGR.IDeque) {\n"); source.Indent(); string deque = "((GRGEN_LIBGR.IDeque)" + container + ")"; if(!TypesHelper.IsSameOrSubtype(tgtAttrIndexedVar.KeyExpression.Type(env), "int", model)) { source.AppendFront("if(true) {\n"); source.Indent(); source.AppendFront("throw new Exception(\"Can't access non-int index in deque\");\n"); } else { source.AppendFront("if(" + deque + ".Count > (int)" + key + ") {\n"); source.Indent(); source.AppendFront(deque + "[(int)" + key + "] = " + value + ";\n"); } source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("} else {\n"); source.Indent(); string dictionary = "((System.Collections.IDictionary)" + container + ")"; source.AppendFront("if(" + dictionary + ".Contains(" + key + ")) {\n"); source.Indent(); source.AppendFront(dictionary + "[" + key + "] = " + value + ";\n"); source.Unindent(); source.AppendFront("}\n"); source.Unindent(); source.AppendFront("}\n"); } else { GrGenType nodeOrEdgeType = TypesHelper.GetNodeOrEdgeType(tgtAttrIndexedVar.DestVar.Type, env.Model); AttributeType attributeType = nodeOrEdgeType.GetAttributeType(tgtAttrIndexedVar.AttributeName); string ContainerType = TypesHelper.AttributeTypeToXgrsType(attributeType); if(ContainerType.StartsWith("array")) { string array = GetVar(tgtAttrIndexedVar.DestVar); source.AppendFront("if(" + array + ".Count > (int)" + key + ") {\n"); source.Indent(); source.AppendFront(array + "[(int)" + key + "] = " + sourceValueComputation + ";\n"); source.Unindent(); source.AppendFront("}\n"); } else if(ContainerType.StartsWith("deque")) { string deque = GetVar(tgtAttrIndexedVar.DestVar); source.AppendFront("if(" + deque + ".Count > (int)" + key + ") {\n"); source.Indent(); source.AppendFront(deque + "[(int)" + key + "] = " + sourceValueComputation + ";\n"); source.Unindent(); source.AppendFront("}\n"); } else { string dictionary = GetVar(tgtAttrIndexedVar.DestVar); string dictSrcType = TypesHelper.XgrsTypeToCSharpType(TypesHelper.ExtractSrc(tgtAttrIndexedVar.DestVar.Type), model); source.AppendFront("if(" + dictionary + ".ContainsKey((" + dictSrcType + ")" + key + ")) {\n"); source.Indent(); source.AppendFront(dictionary + "[(" + dictSrcType + ")" + key + "] = " + value + ";\n"); source.Unindent(); source.AppendFront("}\n"); } } if(gen.FireDebugEvents) { source.AppendFront("if(elem_" + tgtAttrIndexedVar.Id + " is GRGEN_LIBGR.INode)\n"); source.AppendFront("\tgraph.ChangedNodeAttribute((GRGEN_LIBGR.INode)elem_" + tgtAttrIndexedVar.Id + ", attrType_" + tgtAttrIndexedVar.Id + ");\n"); source.AppendFront("else\n"); source.AppendFront("\tgraph.ChangedEdgeAttribute((GRGEN_LIBGR.IEdge)elem_" + tgtAttrIndexedVar.Id + ", attrType_" + tgtAttrIndexedVar.Id + ");\n"); } break; } default: throw new Exception("Unknown assignment target type: " + tgt.AssignmentTargetType); } }