internal Args_Cast(ANewExp obj)
 {
     this.obj = obj;
 }
 public override void OutANewExp(ANewExp node)
 {
     if (node.Parent() is APointerLvalue || node.Parent() is AStructLvalue)
     {
         //Move node out
         MoveOut(node, node.GetType());
     }
 }
 public virtual void InANewExp(ANewExp node)
 {
     DefaultIn(node);
 }
 public virtual void OutANewExp(ANewExp node)
 {
     DefaultOut(node);
 }
 public virtual void CaseANewExp(ANewExp node)
 {
     DefaultCase(node);
 }
 public override void CaseANewExp(ANewExp node)
 {
     InANewExp(node);
     {
         Object[] temp = new Object[node.GetArgs().Count];
         node.GetArgs().CopyTo(temp, 0);
         for (int i = temp.Length - 1; i >= 0; i--)
         {
             ((PExp)temp[i]).Apply(this);
         }
     }
     if (node.GetType() != null)
     {
         node.GetType().Apply(this);
     }
     if (node.GetToken() != null)
     {
         node.GetToken().Apply(this);
     }
     OutANewExp(node);
 }
        public override void OutANewExp(ANewExp node)
        {
            /*if (node.GetType() is AArrayTempType)
                data.ExpTypes[node] = node.GetType();
            else*/
                data.ExpTypes[node] = new APointerType(new TStar("*"), Util.MakeClone(node.GetType(), data));

            List<AEnrichmentDecl> enrichments = new List<AEnrichmentDecl>();
            List<IList> visibleDecls = Util.GetVisibleDecls(node, true);
            foreach (IList declList in visibleDecls)
            {
                foreach (PDecl decl in declList)
                {
                    if (decl is AEnrichmentDecl)
                    {
                        AEnrichmentDecl enrichment = (AEnrichmentDecl) decl;
                        if (!Util.TypesEqual(node.GetType(), enrichment.GetType(), data))
                            continue;
                        enrichments.Add(enrichment);
                    }
                }
            }
            if (enrichments.Count > 0 || node.GetType() is ANamedType)
            {
                List<ErrorCollection.Error> subErrors = new List<ErrorCollection.Error>();
                ANamedType type = (ANamedType) node.GetType();
                if (enrichments.Count > 0 || data.StructTypeLinks.ContainsKey(type))
                {
                    //Find matching constructor
                    //Token token;
                    List<AConstructorDecl> candidates = new List<AConstructorDecl>();
                    if (enrichments.Count == 0)
                    {
                        AStructDecl str = data.StructTypeLinks[type];
                        if (data.Enums.ContainsKey(str))
                        {
                            errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                                 LocRM.GetString("ErrorText138"), false,
                                                                 new ErrorCollection.Error(str.GetName(),
                                                                                           LocRM.GetString("ErrorText139"))));
                        }
                        //token = str.GetName();
                        subErrors.Add(new ErrorCollection.Error(str.GetName(), LocRM.GetString("ErrorText48") + Util.GetTypeName(str)));
                        foreach (AConstructorDecl constructorDecl in data.StructConstructors[str])
                        {
                            //Visiblity
                            if (constructorDecl.GetVisibilityModifier() is APrivateVisibilityModifier &&
                                Util.GetAncestor<AStructDecl>(constructorDecl) != Util.GetAncestor<AStructDecl>(node))
                                continue;
                            if (constructorDecl.GetVisibilityModifier() is AProtectedVisibilityModifier &&
                                (!Util.HasAncestor<AStructDecl>(node) ||
                                !Util.Extends(Util.GetAncestor<AStructDecl>(constructorDecl), Util.GetAncestor<AStructDecl>(node), data)))
                                continue;

                            if (constructorDecl.GetFormals().Count == node.GetArgs().Count)
                            {
                                //Check that parameters are assignable
                                bool add = true;
                                for (int i = 0; i < node.GetArgs().Count; i++)
                                {
                                    PType argType = data.ExpTypes[(PExp)node.GetArgs()[i]];
                                    AALocalDecl formal = (AALocalDecl)constructorDecl.GetFormals()[i];
                                    PType formalType = formal.GetType();
                                    if (formal.GetOut() != null && !Assignable(formalType, argType)
                                        || formal.GetRef() != null && !(Assignable(argType, formalType) && Assignable(formalType, argType))
                                        || formal.GetOut() == null && formal.GetRef() == null && !Assignable(argType, formalType))
                                    {
                                        add = false;
                                        break;
                                    }
                                }
                                if (add)
                                    candidates.Add(constructorDecl);
                            }
                        }
                    }
                    else
                    {
                        //token = null;
                        foreach (AEnrichmentDecl enrich in enrichments)
                        {
                            //token = enrich.GetToken();
                            subErrors.Add(new ErrorCollection.Error(enrich.GetToken(), LocRM.GetString("ErrorText77")));
                            foreach (AConstructorDecl constructorDecl in enrich.GetDecl().OfType<AConstructorDecl>())
                            {
                                //Visiblity
                                if (constructorDecl.GetVisibilityModifier() is APrivateVisibilityModifier &&
                                    Util.GetAncestor<AEnrichmentDecl>(constructorDecl) !=
                                    Util.GetAncestor<AEnrichmentDecl>(node))
                                    continue;

                                if (constructorDecl.GetFormals().Count == node.GetArgs().Count)
                                {
                                    //Check that parameters are assignable
                                    bool add = true;
                                    for (int i = 0; i < node.GetArgs().Count; i++)
                                    {
                                        PType argType = data.ExpTypes[(PExp) node.GetArgs()[i]];
                                        AALocalDecl formal = (AALocalDecl) constructorDecl.GetFormals()[i];
                                        PType formalType = formal.GetType();
                                        if (formal.GetOut() != null && !Assignable(formalType, argType)
                                            ||
                                            formal.GetRef() != null &&
                                            !(Assignable(argType, formalType) && Assignable(formalType, argType))
                                            ||
                                            formal.GetOut() == null && formal.GetRef() == null &&
                                            !Assignable(argType, formalType))
                                        {
                                            add = false;
                                            break;
                                        }
                                    }
                                    if (add)
                                        candidates.Add(constructorDecl);
                                }
                            }
                        }
                    }

                    if (candidates.Count == 0)
                    {
                        if (node.GetArgs().Count == 0 && enrichments.Count > 0)
                            return;

                        string msg = LocRM.GetString("ErrorText140");
                        foreach (PExp arg in node.GetArgs())
                        {
                            if (msg.EndsWith("("))
                                msg += Util.TypeToString(data.ExpTypes[arg]);
                            else
                                msg += ", " + Util.TypeToString(data.ExpTypes[arg]);
                        }
                        msg += ")";
                        errors.Add(new ErrorCollection.Error(node.GetToken(), currentSourceFile, msg, false, subErrors.ToArray()), true);
                        return;
                    }
                    if (candidates.Count > 1)
                    {
                        subErrors = new List<ErrorCollection.Error>();
                        int i = 0;
                        foreach (AConstructorDecl candidate in candidates)
                        {
                            i++;
                            subErrors.Add(new ErrorCollection.Error(candidate.GetName(), LocRM.GetString("ErrorText141") + i));
                        }
                        errors.Add(
                            new ErrorCollection.Error(node.GetToken(), currentSourceFile,
                                                      LocRM.GetString("ErrorText142"), false,
                                                      subErrors.ToArray()), true);
                        return;
                    }
                    PStm pStm = Util.GetAncestor<PStm>(node);
                    if (pStm == null)
                    {
                        if (Util.HasAncestor<AFieldDecl>(node))
                        {
                            data.FieldsToInitInMapInit.Add(Util.GetAncestor<AFieldDecl>(node));
                        }
                        else if (Util.HasAncestor<AStructDecl>(node))
                        {
                            //Ignore - will be fixed
                        }
                        else
                            errors.Add(new ErrorCollection.Error(node.GetToken(), currentSourceFile,
                                                                 LocRM.GetString("ErrorText143")));
                    }
                    data.ConstructorLinks.Add(node, candidates[0]);

                    //For each formal marked as ref or out, the argument must be a variable
                    for (int i = 0; i < node.GetArgs().Count; i++)
                    {
                        AALocalDecl formal = (AALocalDecl)candidates[0].GetFormals()[i];
                        if (formal.GetRef() != null || formal.GetOut() != null)
                        {
                            PExp exp = (PExp)node.GetArgs()[i];
                            while (true)
                            {
                                PLvalue lvalue;
                                if (exp is ALvalueExp)
                                {
                                    lvalue = ((ALvalueExp)exp).GetLvalue();
                                }
                                else if (exp is AAssignmentExp)
                                {
                                    lvalue = ((AAssignmentExp)exp).GetLvalue();
                                }
                                else
                                {
                                    errors.Add(new ErrorCollection.Error(node.GetToken(), currentSourceFile, LocRM.GetString("ErrorText129") + (i + 1) + LocRM.GetString("ErrorText130")));
                                    break;
                                }
                                if (lvalue is ALocalLvalue ||
                                    lvalue is AFieldLvalue ||
                                    lvalue is AStructFieldLvalue)
                                    break;
                                if (lvalue is AStructLvalue)
                                {
                                    exp = ((AStructLvalue)lvalue).GetReceiver();
                                    continue;
                                }
                                if (lvalue is AArrayLvalue)
                                {
                                    exp = ((AArrayLvalue)lvalue).GetBase();
                                    continue;
                                }
                                throw new Exception("Unexpected lvalue");
                            }
                        }
                    }
                    return;
                }
            }
            //else
            if (node.GetArgs().Count > 0)
            {
                errors.Add(new ErrorCollection.Error(node.GetToken(), currentSourceFile, LocRM.GetString("ErrorText144")));
            }
        }
 public override void CaseANewExp(ANewExp node)
 {
     IsConst = false;
 }
 ArrayList New371()
 {
     ArrayList nodeList = new ArrayList();
     ArrayList nodeArrayList5 = (ArrayList) Pop();
     ArrayList nodeArrayList4 = (ArrayList) Pop();
     ArrayList nodeArrayList3 = (ArrayList) Pop();
     ArrayList nodeArrayList2 = (ArrayList) Pop();
     ArrayList nodeArrayList1 = (ArrayList) Pop();
     TypedList listNode5 = new TypedList();
     TNew tnewNode2 = (TNew)nodeArrayList1[0];
     PType ptypeNode3 = (PType)nodeArrayList2[0];
     TypedList listNode4 = (TypedList)nodeArrayList4[0];
     if ( listNode4 != null )
     {
     listNode5.AddAll(listNode4);
     }
     ANewExp pexpNode1 = new ANewExp (
       tnewNode2,
       ptypeNode3,
       listNode5
     );
     nodeList.Add(pexpNode1);
     return nodeList;
 }
        /*Moved to it's own class
        public override void OutANamedType(ANamedType node)
        {
            //Link named type to their definition (structs)
            //Lookup the named type unless it is a primitive
            if (!GalaxyKeywords.Primitives.words.Any(primitive => node.GetName().Text == primitive))
            {
                if (node.Parent() is AEnrichmentDecl) //Enriching a struct type. Not allowed
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile, "You can not enrich this type."));
                    return;
                }

                //Look for structs
                foreach (SharedData.DeclItem<AStructDecl> declItem in data.Structs)
                {
                    if (node.GetNamespace() != null)
                    {
                        AASourceFile targetFile = Util.GetAncestor<AASourceFile>(declItem.Decl);
                        if (targetFile == null || targetFile.GetNamespace() == null || targetFile.GetNamespace().Text != node.GetNamespace().Text)
                            continue;
                    }
                    else if (!Util.IsVisible(node, declItem.Decl))
                        continue;

                    AStructDecl decl = declItem.Decl;
                    if (decl.GetName().Text == node.GetName().Text)
                    {
                        if (decl.GetClassToken() != null && !(node.Parent() is APointerType || node.Parent() is ANewExp))
                        {
                            errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile,
                                                                 "You can only make dynamic instansiations of classes."));
                        }

                        data.StructTypeLinks.Add(node, decl);
                        goto end;
                    }
                }
                //Look for delegates
                foreach (SharedData.DeclItem<AMethodDecl> declItem in data.Delegates)
                {
                    if (node.GetNamespace() != null)
                    {
                        AASourceFile targetFile = Util.GetAncestor<AASourceFile>(declItem.Decl);
                        if (targetFile == null || targetFile.GetNamespace() == null || targetFile.GetNamespace().Text != node.GetNamespace().Text)
                            continue;
                    }
                    else if (!Util.IsVisible(node, declItem.Decl))
                        continue;

                    AMethodDecl decl = declItem.Decl;
                    if (decl.GetName().Text == node.GetName().Text)
                    {
                        data.DelegateTypeLinks.Add(node, decl);
                        goto end;
                    }
                }

                errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile, "No type found named " + node.GetName().Text, false), true);
            }
            else if (node.GetNamespace() != null)
            {
                errors.Add(new ErrorCollection.Error(node.GetNamespace(), currentSourceFile, "You cannot put a namespace infront of a primitive type "), true);
            }

        end:
            base.OutANamedType(node);
        }
        */
        public override void CaseANewExp(ANewExp node)
        {
            bool wasInNew = isInANewExp;
            isInANewExp = true;
            base.CaseANewExp(node);
            isInANewExp = wasInNew;
        }
 public override void CaseANewExp(ANewExp node)
 {
     HasNew = true;
 }
 public override void OutANewExp(ANewExp node)
 {
     if (node.GetType() is ANamedType &&
         data.StructTypeLinks.ContainsKey((ANamedType) node.GetType()) &&
         data.StructTypeLinks[(ANamedType) node.GetType()] == str &&
         node.GetArgs().Count == 0)
     {
         data.ConstructorLinks[node] = constructor;
     }
     base.OutANewExp(node);
 }
            private AALocalDecl TurnDynamic(PExp exp, AALocalDecl targetDecl, PType type, bool assignBefore = true, bool assignAfter = false, bool makeDelete = true)
            {
                exp.Apply(mover);
                //insert
                //<basetype>[]* bulkCopyVar = new <baseType>[<dim>]();
                //bulkCopyVar[0] = arg[0];
                //bulkCopyVar[1] = arg[1];
                //bulkCopyVar[2] = arg[2];
                //...
                //Invoke(..., bulkCopyVar, ...);

                //If we need to clean up after, move the exp to it's own statement.

                AALocalDecl newLocal;
                if (type is AArrayTempType)
                {
                    AArrayTempType aType = (AArrayTempType) type;
                    ANewExp newExp = new ANewExp(new TNew("new"), new AArrayTempType(
                                                                       new TLBracket("["),
                                                                       Util.MakeClone(aType.GetType(),
                                                                                      data),
                                                                       Util.MakeClone(aType.GetDimention(), data),
                                                                       (TIntegerLiteral)
                                                                       aType.GetIntDim().Clone()),
                                                 new ArrayList());
                    newLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                               new APointerType(new TStar("*"),
                                                                new ADynamicArrayType(
                                                                    new TLBracket("["),
                                                                    Util.MakeClone(aType.GetType(),
                                                                                   data))),
                                               new TIdentifier("bulkCopyVar"),
                                               newExp
                        );
                }
                else
                {
                    ANewExp newExp = new ANewExp(new TNew("new"), Util.MakeClone(type, data),
                                                 new ArrayList());
                    newLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                               new APointerType(new TStar("*"),
                                                                Util.MakeClone(type, data)),
                                               new TIdentifier("bulkCopyVar"),
                                               newExp
                        );
                }
                PStm pStm = Util.GetAncestor<PStm>(exp);
                AABlock pBlock = (AABlock) pStm.Parent();
                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm),
                                                new ALocalDeclStm(new TSemicolon(";"), newLocal));
                PLvalue bulkCopyRef = new ALocalLvalue(new TIdentifier("bulkCopyVar"));
                data.LocalLinks[(ALocalLvalue) bulkCopyRef] = newLocal;
                data.LvalueTypes[bulkCopyRef] = newLocal.GetType();

                ALvalueExp bulkCopyRefExp;
                //if (!(type is AArrayTempType))
                {
                    bulkCopyRefExp = new ALvalueExp(bulkCopyRef);
                    bulkCopyRef = new APointerLvalue(new TStar("*"), bulkCopyRefExp);
                    data.ExpTypes[bulkCopyRefExp] = newLocal.GetType();
                    data.LvalueTypes[bulkCopyRef] = type;
                }

                if (assignBefore)
                {
                    List<PStm> stms = MakeAssignments(bulkCopyRef, exp, new List<AALocalDecl>() {targetDecl});
                    foreach (PStm stm in stms)
                    {
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), stm);
                    }
                }
                int addStms = 1;
                if (assignAfter)
                {
                    bulkCopyRefExp = new ALvalueExp(bulkCopyRef);
                    data.ExpTypes[bulkCopyRefExp] = data.LvalueTypes[bulkCopyRef];
                    List<PStm> stms = MakeAssignments(((ALvalueExp)exp).GetLvalue(), bulkCopyRefExp, new List<AALocalDecl>() {targetDecl});
                    addStms += stms.Count;
                    foreach (PStm stm in stms)
                    {
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, stm);
                    }
                }

                bulkCopyRef = new ALocalLvalue(new TIdentifier("bulkCopyVar"));
                bulkCopyRefExp = new ALvalueExp(bulkCopyRef);
                data.LocalLinks[(ALocalLvalue) bulkCopyRef] = newLocal;
                data.LvalueTypes[bulkCopyRef] = data.ExpTypes[bulkCopyRefExp] = newLocal.GetType();
                exp.ReplaceBy(bulkCopyRefExp);

                /*if (formal.GetRef() != null || formal.GetOut() != null)
                {
                    //Get args back
                }*/
                //Delete object

                if (makeDelete)
                {
                    bulkCopyRef = new ALocalLvalue(new TIdentifier("bulkCopyVar"));
                    bulkCopyRefExp = new ALvalueExp(bulkCopyRef);
                    data.LocalLinks[(ALocalLvalue) bulkCopyRef] = newLocal;
                    data.LvalueTypes[bulkCopyRef] = data.ExpTypes[bulkCopyRefExp] = newLocal.GetType();
                    pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + addStms,
                                                  new ADeleteStm(new TDelete("delete"), bulkCopyRefExp));
                }
                return newLocal;
            }
            public override void CaseANewExp(ANewExp node)
            {
                //Call new object or new array
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("invoke"), new ArrayList());
                ANamedType pointerType = new ANamedType(new TIdentifier("string"), null);
                if (node.GetType() is AArrayTempType)
                {
                    if (newArrayMethod == null) CreateNewArrayMethod(node, data);

                    node.GetType().Apply(this);
                    AArrayTempType type = (AArrayTempType) node.GetType();
                    invoke.GetArgs().Add(Util.MakeClone(type.GetDimention(), data));
                    data.SimpleMethodLinks[invoke] = newArrayMethod;
                }
                else if (Util.IsIntPointer(node, node.GetType(), data))
                {
                    if (node.GetType() is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType) node.GetType()))
                        data.SimpleMethodLinks[invoke] = CreateNewObjectMethod(node, data.StructTypeLinks[(ANamedType) node.GetType()],
                                                                               data);
                    else
                    {
                        data.SimpleMethodLinks[invoke] = CreateNewObjectMethod(node,
                                                                                data.EnrichmentTypeLinks[node.GetType()],
                                                                                data);
                    }
                    pointerType = new ANamedType(new TIdentifier("int"), null);
                }
                else
                {
                    if (newObjectMethod == null) CreateNewObjectMethod(node, data);

                    data.SimpleMethodLinks[invoke] = newObjectMethod;
                }
                node.ReplaceBy(invoke);
                data.ExpTypes[invoke] = pointerType;

                //Call initializer
                if (data.ConstructorLinks.ContainsKey(node))
                {
                    PStm pStm = Util.GetAncestor<PStm>(invoke);
                    AABlock pblock = (AABlock) pStm.Parent();

                    PLvalue lvalue;
                    ALvalueExp lvalueExp;
                    PStm stm;
                    AAssignmentExp assignment = null;
                    AALocalDecl localDecl = null;
                    if (invoke.Parent() is AAssignmentExp)
                    {
                        AAssignmentExp parent = (AAssignmentExp) invoke.Parent();
                        assignment = parent;
                        /*lvalue = parent.GetLvalue();
                        lvalue.Apply(new MoveMethodDeclsOut("pointerVar", data));
                        lvalue = Util.MakeClone(lvalue, data);*/
                    }
                    else if (invoke.Parent() is AALocalDecl)
                    {
                        AALocalDecl parent = (AALocalDecl) invoke.Parent();
                        localDecl = parent;
                        /*lvalue = new ALocalLvalue(new TIdentifier(parent.GetName().Text));

                        data.LocalLinks[(ALocalLvalue) lvalue] = parent;
                        data.LvalueTypes[lvalue] = parent.GetType();*/
                    }
                    else
                    {
                        //Move the new invocation out into a local decl, and use that
                        localDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                Util.MakeClone(pointerType, data),
                                                                new TIdentifier("newVar"), null);
                        ALocalLvalue localLvalue = new ALocalLvalue(new TIdentifier("newVar"));
                        lvalueExp = new ALvalueExp(localLvalue);
                        invoke.ReplaceBy(lvalueExp);
                        localDecl.SetInit(invoke);
                        stm = new ALocalDeclStm(new TSemicolon(";"), localDecl);
                        pblock.GetStatements().Insert(pblock.GetStatements().IndexOf(pStm), stm);
                        pStm = stm;
                        //lvalue = new ALocalLvalue(new TIdentifier("newVar"));

                        //data.LocalLinks[(ALocalLvalue) lvalue] =
                            data.LocalLinks[localLvalue] = localDecl;
                        //data.LvalueTypes[lvalue] =
                            data.LvalueTypes[localLvalue] =
                            data.ExpTypes[lvalueExp] = localDecl.GetType();
                    }

                    ASimpleInvokeExp oldInvoke = invoke;
                    invoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList());
                    while (node.GetArgs().Count > 0)
                    {
                        invoke.GetArgs().Add(node.GetArgs()[0]);
                    }
                    if (assignment != null)
                    {
                        assignment.SetExp(invoke);
                        invoke.GetArgs().Add(oldInvoke);
                    }
                    else
                    {
                        localDecl.SetInit(invoke);
                        invoke.GetArgs().Add(oldInvoke);
                    }
                    //lvalueExp = new ALvalueExp(lvalue);
                    //invoke.GetArgs().Add(lvalueExp);
                    //stm = new AExpStm(new TSemicolon(";"), invoke);
                    //pblock.GetStatements().Insert(pblock.GetStatements().IndexOf(pStm) + 1, stm);

                    //data.ExpTypes[lvalueExp] = data.LvalueTypes[lvalue];
                    data.SimpleMethodLinks[invoke] = data.ConstructorMap[data.ConstructorLinks[node]];
                    data.ExpTypes[invoke] = data.ConstructorMap[data.ConstructorLinks[node]].GetReturnType();
                    invoke.Apply(this);
                }
            }
 public override void CaseANewExp(ANewExp node)
 {
     isSet = true;
     isExposed = false;
     currentPointer.Clear();
 }