コード例 #1
0
        public MethodDescription(AConstructorDecl method, string type)
        {
            Parser parser = new Parser(method);

            Start = parser.Start;
            End = parser.End;
            ReturnType = type;
            Name = parser.Name;
            Formals = parser.Formals;
            Locals = parser.Locals;
            if (method.Parent() != null)
                method.Parent().RemoveChild(method);
            Decl = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new ANamedType(new TIdentifier(type), null),
                                   new TIdentifier(""), new ArrayList(), method.GetBlock());
            while (method.GetFormals().Count > 0)
                Decl.GetFormals().Add(method.GetFormals()[0]);
            Visibility = method.GetVisibilityModifier();
            Position = TextPoint.FromCompilerCoords(method.GetName());
        }
コード例 #2
0
 internal Formals_Cast(AConstructorDecl obj)
 {
     this.obj = obj;
 }
コード例 #3
0
 internal BaseArgs_Cast(AConstructorDecl obj)
 {
     this.obj = obj;
 }
コード例 #4
0
 public virtual void OutAConstructorDecl(AConstructorDecl node)
 {
     DefaultOut(node);
 }
コード例 #5
0
            public override void CaseAConstructorDecl(AConstructorDecl node)
            {
                End = Start = TextPoint.FromCompilerCoords(node.GetName().Line, node.GetName().Pos);
                Name = node.GetName().Text;

                base.CaseAConstructorDecl(node);
            }
コード例 #6
0
 public virtual void CaseAConstructorDecl(AConstructorDecl node)
 {
     DefaultCase(node);
 }
コード例 #7
0
 public virtual void InAConstructorDecl(AConstructorDecl node)
 {
     DefaultIn(node);
 }
コード例 #8
0
        public override void OutAConstructorDecl(AConstructorDecl node)
        {
            //If void return is missing, insert it.
            AABlock block = (AABlock)node.GetBlock();
            bool insertReturn = false;
            while (true)
            {
                if (block.GetStatements().Count == 0)
                {
                    insertReturn = true;
                    break;
                }
                PStm lastStm = (PStm)block.GetStatements()[block.GetStatements().Count - 1];
                if (lastStm is AVoidReturnStm)
                    break;
                if (lastStm is ABlockStm)
                {
                    block = (AABlock)((ABlockStm)block.GetStatements()[block.GetStatements().Count - 1]).GetBlock();
                    continue;
                }
                insertReturn = true;
                break;
            }
            if (insertReturn)
            {
                block.GetStatements().Add(new AVoidReturnStm(new TReturn("return", block.GetToken().Line, block.GetToken().Pos)));
            }

            base.OutAConstructorDecl(node);
        }
コード例 #9
0
 public override void CaseAConstructorDecl(AConstructorDecl node)
 {
     InAConstructorDecl(node);
     if (node.GetBlock() != null)
     {
         node.GetBlock().Apply(this);
     }
     {
         Object[] temp = new Object[node.GetBaseArgs().Count];
         node.GetBaseArgs().CopyTo(temp, 0);
         for (int i = temp.Length - 1; i >= 0; i--)
         {
             ((PExp)temp[i]).Apply(this);
         }
     }
     {
         Object[] temp = new Object[node.GetFormals().Count];
         node.GetFormals().CopyTo(temp, 0);
         for (int i = temp.Length - 1; i >= 0; i--)
         {
             ((PLocalDecl)temp[i]).Apply(this);
         }
     }
     if (node.GetName() != null)
     {
         node.GetName().Apply(this);
     }
     if (node.GetVisibilityModifier() != null)
     {
         node.GetVisibilityModifier().Apply(this);
     }
     OutAConstructorDecl(node);
 }
コード例 #10
0
 public override void OutAConstructorDecl(AConstructorDecl node)
 {
     AStructDecl parentStruct = Util.GetAncestor<AStructDecl>(node);
     if (parentStruct != null)
     {
         data.StructConstructors[parentStruct].Add(node);
         if (parentStruct.GetName().Text != node.GetName().Text)
         {
             errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile,
                                                  LocRM.GetString("ErrorText12")));
         }
     }
     base.OutAConstructorDecl(node);
 }
コード例 #11
0
        public override void OutAConstructorDecl(AConstructorDecl node)
        {
            //Link to base constructor
            AStructDecl str = Util.GetAncestor<AStructDecl>(node);
            if (str != null && str.GetBase() != null)
            {
                AStructDecl baseStruct = data.StructTypeLinks[(ANamedType)str.GetBase()];
                List<AConstructorDecl> candidates = new List<AConstructorDecl>();
                foreach (AConstructorDecl baseConstructor in data.StructConstructors[baseStruct])
                {
                    //Visibility
                    if (baseConstructor.GetVisibilityModifier() is APrivateVisibilityModifier)
                        continue;

                    if (baseConstructor.GetFormals().Count == node.GetBaseArgs().Count)
                    {
                        //Check that parameters are assignable
                        bool add = true;
                        for (int i = 0; i < node.GetBaseArgs().Count; i++)
                        {
                            PType argType = data.ExpTypes[(PExp)node.GetBaseArgs()[i]];
                            AALocalDecl formal = (AALocalDecl)baseConstructor.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(baseConstructor);
                    }
                }
                if (candidates.Count == 0)
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText163"), false,
                                                         new ErrorCollection.Error(baseStruct.GetName(), LocRM.GetString("ErrorText164"))));
                    return;
                }
                if (candidates.Count > 1)
                {
                    List<ErrorCollection.Error> subErrors = new List<ErrorCollection.Error>();
                    foreach (AConstructorDecl candidate in candidates)
                    {
                        subErrors.Add(new ErrorCollection.Error(candidate.GetName(), LocRM.GetString("ErrorText63")));
                    }
                    errors.Add(new ErrorCollection.Error(node.GetName(),
                                                         LocRM.GetString("ErrorText165"), false,
                                                         subErrors.ToArray()));
                    return;
                }
                data.ConstructorBaseLinks[node] = candidates[0];
            }
            else if (node.GetBaseArgs().Count > 0)
            {
                errors.Add(new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText163")));
            }
            base.OutAConstructorDecl(node);
        }
コード例 #12
0
 ArrayList New224()
 {
     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();
     TypedList listNode7 = new TypedList();
     PVisibilityModifier pvisibilitymodifierNode2 = (PVisibilityModifier)nodeArrayList1[0];
     TIdentifier tidentifierNode3 = (TIdentifier)nodeArrayList2[0];
     TypedList listNode4 = (TypedList)nodeArrayList3[0];
     if ( listNode4 != null )
     {
     listNode5.AddAll(listNode4);
     }
     TypedList listNode6 = (TypedList)nodeArrayList4[0];
     if ( listNode6 != null )
     {
     listNode7.AddAll(listNode6);
     }
     PBlock pblockNode8 = (PBlock)nodeArrayList5[0];
     AConstructorDecl pdeclNode1 = new AConstructorDecl (
       pvisibilitymodifierNode2,
       tidentifierNode3,
       listNode5,
       listNode7,
       pblockNode8
     );
     nodeList.Add(pdeclNode1);
     return nodeList;
 }
コード例 #13
0
 public override void CaseAConstructorDecl(AConstructorDecl node)
 {
     MethodDescription method = new MethodDescription(node, Name + "*");
     Constructors.Add(method);
 }
コード例 #14
0
 public override void CaseAConstructorDecl(AConstructorDecl node)
 {
     MethodDescription method = new MethodDescription(node, Util.TypeToString(type) + "*");
     Constructors.Add(method);
 }
コード例 #15
0
        public override void CaseAConstructorDecl(AConstructorDecl node)
        {
            AStructDecl str = Util.GetAncestor<AStructDecl>(node);
            AEnrichmentDecl enrichment = Util.GetAncestor<AEnrichmentDecl>(node);
            AMethodDecl replacer = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")),
                                                   node.GetName(), new ArrayList(), node.GetBlock());
            replacer.GetName().Text += "_Constructor";
            while (node.GetFormals().Count > 0)
            {
                replacer.GetFormals().Add(node.GetFormals()[0]);
            }

            //Move the method outside the struct
            AASourceFile file = Util.GetAncestor<AASourceFile>(node);
            if (str != null)
                str.RemoveChild(node.Parent());
            else
                enrichment.RemoveChild(node);
            int i = file.GetDecl().IndexOf(str ?? (PDecl)enrichment);
            file.GetDecl().Insert(i/* + 1*/, replacer);
            //Add the struct as a parameter
            PType type;
            if (str != null)
            {
                ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                finalTrans.data.StructTypeLinks[structType] = str;
                type = structType;
            }
            else
            {
                type = Util.MakeClone(enrichment.GetType(), finalTrans.data);
            }
            finalTrans.data.ConstructorMap[node] = replacer;
            structFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new APointerType(new TStar("*"), type), new TIdentifier("currentStruct", replacer.GetName().Line, replacer.GetName().Pos), null);
            replacer.GetFormals().Add(structFormal);
            finalTrans.data.Methods.Add(new SharedData.DeclItem<AMethodDecl>(file, replacer));

            //Add return stm
            replacer.SetReturnType(new APointerType(new TStar("*"), Util.MakeClone(type, data)));
            replacer.Apply(new TransformConstructorReturns(structFormal, data));

            //Insert call to base constructor););
            if (finalTrans.data.ConstructorBaseLinks.ContainsKey(node))
            {
                AMethodDecl baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]];
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier(baseConstructor.GetName().Text), new ArrayList());
                while (node.GetBaseArgs().Count > 0)
                {
                    invoke.GetArgs().Add(node.GetBaseArgs()[0]);
                }
                AThisLvalue thisLvalue1 = new AThisLvalue(new TThis("this"));
                ALvalueExp thisExp1 = new ALvalueExp(thisLvalue1);
                invoke.GetArgs().Add(thisExp1);

                AThisLvalue thisLvalue2 = new AThisLvalue(new TThis("this"));

                AAssignmentExp assignExp = new AAssignmentExp(new TAssign("="), thisLvalue2, invoke);

                ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                finalTrans.data.StructTypeLinks[structType] = str;

                finalTrans.data.LvalueTypes[thisLvalue1] =
                    finalTrans.data.LvalueTypes[thisLvalue2] =
                    finalTrans.data.ExpTypes[thisExp1] =
                    finalTrans.data.ExpTypes[assignExp] =
                    finalTrans.data.ExpTypes[invoke] = new APointerType(new TStar("*"), structType);

                //finalTrans.data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                finalTrans.data.SimpleMethodLinks[invoke] = baseConstructor;

                ((AABlock)replacer.GetBlock()).GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), assignExp));

                //Inline if base and current are two different kinds of pointer types (int/string)
                AStructDecl baseStruct = null;
                AConstructorDecl baseC = finalTrans.data.ConstructorBaseLinks[node];
                foreach (KeyValuePair<AStructDecl, List<AConstructorDecl>> pair in finalTrans.data.StructConstructors)
                {
                    bool found = false;
                    foreach (AConstructorDecl decl in pair.Value)
                    {
                        if (baseC == decl)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        baseStruct = pair.Key;
                        break;
                    }
                }
                if ((str.GetIntDim() == null) != (baseStruct.GetIntDim() == null))
                {
                    //For the inilining, change the type to the type of the caller
                    AALocalDecl lastFormal = baseConstructor.GetFormals().OfType<AALocalDecl>().Last();
                    lastFormal.SetRef(new TRef("ref"));
                    APointerType oldType = (APointerType) lastFormal.GetType();

                    structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                    finalTrans.data.StructTypeLinks[structType] = str;

                    APointerType newType = new APointerType(new TStar("*"), structType);
                    lastFormal.SetType(newType);

                    foreach (
                        ALocalLvalue lvalue in
                            data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key))
                    {
                        data.LvalueTypes[lvalue] = newType;
                        if (lvalue.Parent() is ALvalueExp)
                        {
                            data.ExpTypes[(PExp) lvalue.Parent()] = newType;
                            if (lvalue.Parent().Parent() is APointerLvalue)
                                data.LvalueTypes[(PLvalue) lvalue.Parent().Parent()] = newType.GetType();
                        }
                    }

                    FixInlineMethods.Inline(invoke, finalTrans);
                    lastFormal.SetRef(null);
                    foreach (
                        ALocalLvalue lvalue in
                            data.LocalLinks.Where(pair => pair.Value == lastFormal).Select(pair => pair.Key))
                    {
                        data.LvalueTypes[lvalue] = oldType;
                        if (lvalue.Parent() is ALvalueExp)
                        {
                            data.ExpTypes[(PExp) lvalue.Parent()] = oldType;
                            if (lvalue.Parent().Parent() is APointerLvalue)
                                data.LvalueTypes[(PLvalue) lvalue.Parent().Parent()] = oldType.GetType();
                        }
                    }

                    lastFormal.SetType(oldType);
                }

                //Inline it instead, Since the pointer implementations might not be the same (int vs string)

                /*AMethodDecl baseConstructor = finalTrans.data.ConstructorMap[finalTrans.data.ConstructorBaseLinks[node]];

                AABlock localsBlock = new AABlock(new ArrayList(), new TRBrace("}"));
                ABlockStm cloneBlock = new ABlockStm(new TLBrace("{"), (PBlock) baseConstructor.GetBlock().Clone());
                Dictionary<AALocalDecl, PLvalue> localMap = new Dictionary<AALocalDecl, PLvalue>();
                for (int argNr = 0; argNr < baseConstructor.GetFormals().Count; argNr++)
                {
                    AALocalDecl formal = (AALocalDecl) baseConstructor.GetFormals()[i];
                    PExp arg;
                    if (i < baseConstructor.GetFormals().Count - 1)
                        arg = (PExp)node.GetBaseArgs()[i];
                    else
                    {
                        AThisLvalue thisLvalue = new AThisLvalue(new TThis("this"));
                        ALvalueExp thisExp = new ALvalueExp(thisLvalue);

                        ANamedType structType = new ANamedType(new TIdentifier(str.GetName().Text), null);
                        finalTrans.data.StructTypeLinks[structType] = str;

                        finalTrans.data.LvalueTypes[thisLvalue] =
                            finalTrans.data.ExpTypes[thisExp] = new APointerType(new TStar("*"), structType);

                        arg = thisExp;
                    }

                    if (formal.GetRef() != null || formal.GetOut() != null)
                    {
                        //Use same variable
                        localMap[formal] = ((ALvalueExp) arg).GetLvalue();
                    }
                    else
                    {
                        //Make a new variable
                        AALocalDecl newLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                               Util.MakeClone(formal.GetType(), finalTrans.data),
                                                               new TIdentifier(formal.GetName().Text),
                                                               Util.MakeClone(arg, data));

                        ALocalLvalue newLocalRef = new ALocalLvalue(new TIdentifier(newLocal.GetName().Text));

                        localMap[formal] = newLocalRef;
                        data.LvalueTypes[newLocalRef] = newLocal.GetType();
                        data.LocalLinks[newLocalRef] = newLocal;

                        localsBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), newLocal));
                    }

                }

                CloneMethod cloner = new CloneMethod(finalTrans.data, localMap, cloneBlock);
                baseConstructor.GetBlock().Apply(cloner);

                ((AABlock)cloneBlock.GetBlock()).GetStatements().Insert(0, new ABlockStm(new TLBrace("{"), localsBlock));
                ((AABlock)node.GetBlock()).GetStatements().Insert(0, cloneBlock);*/
            }

            //Fix refferences to other struct stuff);
            base.CaseAMethodDecl(replacer);

            //Add functionality to refference the current struct in a constructor
            //Want to do it as a pointer type, since the constructer can only be called for pointer types
        }
コード例 #16
0
 public NewConstructorFixup(AConstructorDecl constructor, AStructDecl str, SharedData data)
 {
     this.constructor = constructor;
     this.str = str;
     this.data = data;
 }