private PType LvalueToType(PLvalue lvalue) { if (lvalue is AStructLvalue) { /*AStructLvalue structLvalue = (AStructLvalue) lvalue; * TIdentifier typeName = structLvalue.GetName(); * if (structLvalue.GetReceiver() is ALvalueExp && ((ALvalueExp)structLvalue.GetReceiver()).GetLvalue() is AAmbiguousNameLvalue) * { * AAmbiguousNameLvalue ambiguousNameLvalue = (AAmbiguousNameLvalue) ((ALvalueExp) structLvalue.GetReceiver()).GetLvalue(); * return new ANamedType(typeName, ((ASimpleName)ambiguousNameLvalue.GetAmbiguous()).GetIdentifier()); * } * else*/ { //errors.Add(new ErrorCollection.Error(((AStructLvalue)lvalue).GetName(), currentSourceFile, "Invalid type name. Must be \"<namespace>.<struct name>\".")); throw new ParserException(null, null); } } if (lvalue is AArrayLvalue) { AArrayLvalue aLvalue = (AArrayLvalue)lvalue; if (!(aLvalue.GetBase() is ALvalueExp)) { //errors.Add(new ErrorCollection.Error(GetToken(lvalue), currentSourceFile, "Whatever that is, it's not allowed in a type")); throw new ParserException(null, null); } PLvalue newLvalue = ((ALvalueExp)aLvalue.GetBase()).GetLvalue(); return(new AArrayTempType(aLvalue.GetToken(), LvalueToType(newLvalue), aLvalue.GetIndex(), null)); } //it must be an AAmbiguousNameLvalue then return(new ANamedType(((AAmbiguousNameLvalue)lvalue).GetAmbiguous())); }
/* * Apply after assignement fixup * Assume no i++ * * Convert usages to method invocations. */ public static void Parse(FinalTransformations finalTrans) { SharedData data = finalTrans.data; foreach (KeyValuePair <APropertyLvalue, APropertyDecl> pair in data.PropertyLinks) { APropertyLvalue lvalue = pair.Key; APropertyDecl property = pair.Value; if (Util.GetAncestor <AAProgram>(lvalue) == null) { continue; } if (lvalue.Parent() is AAssignmentExp) { AAssignmentExp assignment = (AAssignmentExp)lvalue.Parent(); ASimpleInvokeExp invoke = new ASimpleInvokeExp( new TIdentifier("Set" + property.GetName().Text, lvalue.GetName().Line, lvalue.GetName().Pos), new ArrayList() { assignment.GetExp() }); assignment.ReplaceBy(invoke); data.SimpleMethodLinks[invoke] = data.Setters[property]; data.ExpTypes[invoke] = new AVoidType(new TVoid("void")); } else { ALvalueExp exp = (ALvalueExp)lvalue.Parent(); ASimpleInvokeExp invoke = new ASimpleInvokeExp( new TIdentifier("Get" + property.GetName().Text, lvalue.GetName().Line, lvalue.GetName().Pos), new ArrayList() { }); exp.ReplaceBy(invoke); data.SimpleMethodLinks[invoke] = data.Getters[property]; data.ExpTypes[invoke] = property.GetType(); } } foreach (KeyValuePair <AStructLvalue, APropertyDecl> pair in data.StructPropertyLinks) { AStructLvalue lvalue = pair.Key; APropertyDecl property = pair.Value; AEnrichmentDecl enrichmentDecl = null; AStructDecl structDecl = null; if (data.EnrichmentTypeLinks.ContainsKey(data.ExpTypes[lvalue.GetReceiver()])) { enrichmentDecl = data.EnrichmentTypeLinks[data.ExpTypes[lvalue.GetReceiver()]]; } if (enrichmentDecl == null) { structDecl = data.StructTypeLinks[(ANamedType)data.ExpTypes[lvalue.GetReceiver()]]; } if (Util.GetAncestor <AAProgram>(lvalue) == null) { continue; } PExp structArg; if (structDecl == null || structDecl.GetClassToken() == null) { structArg = lvalue.GetReceiver(); } else { //Send pointer ALvalueExp lvalueExp = (ALvalueExp)lvalue.GetReceiver(); APointerLvalue pointerValue = (APointerLvalue)lvalueExp.GetLvalue(); structArg = pointerValue.GetBase(); } if (lvalue.Parent() is AAssignmentExp) { AAssignmentExp assignment = (AAssignmentExp)lvalue.Parent(); ASimpleInvokeExp invoke = new ASimpleInvokeExp( new TIdentifier("Set" + property.GetName().Text, lvalue.GetName().Line, lvalue.GetName().Pos), new ArrayList() { assignment.GetExp(), structArg }); assignment.ReplaceBy(invoke); data.SimpleMethodLinks[invoke] = data.Setters[property]; data.ExpTypes[invoke] = new AVoidType(new TVoid("void")); } else { ALvalueExp exp = (ALvalueExp)lvalue.Parent(); ASimpleInvokeExp invoke = new ASimpleInvokeExp( new TIdentifier("Get" + property.GetName().Text, lvalue.GetName().Line, lvalue.GetName().Pos), new ArrayList() { structArg }); exp.ReplaceBy(invoke); data.SimpleMethodLinks[invoke] = data.Getters[property]; data.ExpTypes[invoke] = property.GetType(); } } foreach (KeyValuePair <AArrayLvalue, Util.Pair <APropertyDecl, bool> > pair in data.ArrayPropertyLinks) { AArrayLvalue lvalue = pair.Key; APropertyDecl property = pair.Value.First; bool implicitMatch = pair.Value.Second; AEnrichmentDecl enrichmentDecl = null; AStructDecl structDecl = null; if (OldEnrichmentParents.ContainsKey(property)) { enrichmentDecl = OldEnrichmentParents[property]; } else { structDecl = OldStructParents[property]; } if (Util.GetAncestor <AAProgram>(lvalue) == null) { continue; } PExp structArg; if (structDecl == null || structDecl.GetClassToken() == null) { structArg = lvalue.GetBase(); } else { //Send pointer if (implicitMatch) { structArg = lvalue.GetBase(); } else { ALvalueExp lvalueExp = (ALvalueExp)lvalue.GetBase(); APointerLvalue pointerValue = (APointerLvalue)lvalueExp.GetLvalue(); structArg = pointerValue.GetBase(); } } /* if (!(structArg is ALvalueExp && * (((ALvalueExp)structArg).GetLvalue() is ALocalLvalue || ((ALvalueExp)structArg).GetLvalue() is AFieldLvalue || * ((ALvalueExp)structArg).GetLvalue() is AStructLvalue || ((ALvalueExp)structArg).GetLvalue() is AStructFieldLvalue)) * { * //Make new local * AALocalDecl decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, * Util.MakeClone(data.ExpTypes[structArg], data), * new TIdentifier("propertyVar"), structArg); * ALocalLvalue declRef = new ALocalLvalue(new TIdentifier("propertyVar")); * structArg = new ALvalueExp(declRef); * PStm stm = Util.GetAncestor<PStm>(lvalue); * }*/ if (lvalue.Parent() is AAssignmentExp) { AAssignmentExp assignment = (AAssignmentExp)lvalue.Parent(); ASimpleInvokeExp invoke = new ASimpleInvokeExp( new TIdentifier("SetThis", lvalue.GetToken().Line, lvalue.GetToken().Pos), new ArrayList() { lvalue.GetIndex(), assignment.GetExp(), structArg }); assignment.ReplaceBy(invoke); data.SimpleMethodLinks[invoke] = data.Setters[property]; data.ExpTypes[invoke] = new AVoidType(new TVoid("void")); } else { ALvalueExp exp = (ALvalueExp)lvalue.Parent(); ASimpleInvokeExp invoke = new ASimpleInvokeExp( new TIdentifier("GetThis", lvalue.GetToken().Line, lvalue.GetToken().Pos), new ArrayList() { lvalue.GetIndex(), structArg }); exp.ReplaceBy(invoke); data.SimpleMethodLinks[invoke] = data.Getters[property]; data.ExpTypes[invoke] = property.GetType(); } } }