Ejemplo n.º 1
0
        public VariableDescription(AALocalDecl localDecl, VariableTypes type)
        {
            Name = localDecl.GetName().Text;
            Type = Util.TypeToString(localDecl.GetType());
            switch (type)
            {
            case VariableTypes.LocalVariable:
                PlacementPrefix = "Local";
                break;

            case VariableTypes.Parameter:
                PlacementPrefix = "Parameter";
                break;

            case VariableTypes.StructVariable:
                PlacementPrefix = "Struct field";
                break;

            default:
                PlacementPrefix = "";
                break;
            }
            VariableType = type;
            Const        = localDecl.GetConst() != null;
            IsStatic     = localDecl.GetStatic() != null;
            Visibility   = localDecl.GetVisibilityModifier();
            realType     = (PType)localDecl.GetType().Clone();
            init         = localDecl.GetInit();
            Line         = localDecl.GetName().Line;
            Position     = TextPoint.FromCompilerCoords(localDecl.GetName());
        }
        private int FoldInt(PLvalue lvalue, ref bool valid)
        {
            if (!valid)
            {
                return(-1);
            }

            if (lvalue is ALocalLvalue)
            {
                ALocalLvalue aLvalue = (ALocalLvalue)lvalue;
                AALocalDecl  decl    = data.LocalLinks[aLvalue];
                if (decl.GetConst() == null)
                {
                    valid = false;
                    return(-1);
                }
                return(FoldInt(decl.GetInit(), ref valid));
            }
            if (lvalue is AFieldLvalue)
            {
                AFieldLvalue aLvalue = (AFieldLvalue)lvalue;
                AFieldDecl   decl    = data.FieldLinks[aLvalue];
                if (decl.GetConst() == null)
                {
                    valid = false;
                    return(-1);
                }
                return(FoldInt(decl.GetInit(), ref valid));
            }
            if (lvalue is AStructFieldLvalue)
            {
                AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue;
                AALocalDecl        decl    = data.StructMethodFieldLinks[aLvalue];
                if (decl.GetConst() == null)
                {
                    valid = false;
                    return(-1);
                }
                return(FoldInt(decl.GetInit(), ref valid));
            }
            if (lvalue is AStructLvalue)
            {
                AStructLvalue aLvalue = (AStructLvalue)lvalue;
                AALocalDecl   decl    = data.StructFieldLinks[aLvalue];
                if (decl.GetConst() == null)
                {
                    valid = false;
                    return(-1);
                }
                return(FoldInt(decl.GetInit(), ref valid));
            }

            valid = false;
            return(-1);
        }
Ejemplo n.º 3
0
        public override void CaseAALocalDecl(AALocalDecl node)
        {
            if (node.GetConst() == null)
            {
                return;
            }

            initialLocalDecl = node;

            if (IsConstant(node.GetInit()))
            {
                {
                    List <ALocalLvalue> lvalues = new List <ALocalLvalue>();
                    lvalues.AddRange(data.LocalLinks.Where(link => link.Value == node).Select(link => link.Key));
                    foreach (ALocalLvalue lvalue in lvalues)
                    {
                        PExp parent = (PExp)lvalue.Parent();
                        parent.ReplaceBy(Util.MakeClone(node.GetInit(), data));
                    }
                }
                {
                    List <AStructLvalue> lvalues = new List <AStructLvalue>();
                    lvalues.AddRange(data.StructFieldLinks.Where(link => link.Value == node).Select(link => link.Key));
                    foreach (AStructLvalue lvalue in lvalues)
                    {
                        PExp parent = (PExp)lvalue.Parent();
                        parent.ReplaceBy(Util.MakeClone(node.GetInit(), data));
                    }
                }
                {
                    List <AStructFieldLvalue> lvalues = new List <AStructFieldLvalue>();
                    lvalues.AddRange(
                        data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key));
                    foreach (AStructFieldLvalue lvalue in lvalues)
                    {
                        PExp parent = (PExp)lvalue.Parent();
                        parent.ReplaceBy(Util.MakeClone(node.GetInit(), data));
                    }
                }
                if (node.Parent() is ALocalDeclStm)
                {
                    node.Parent().Parent().RemoveChild(node.Parent());
                }
                else
                {
                    node.Parent().RemoveChild(node);
                }
            }


            initialLocalDecl = null;
        }
Ejemplo n.º 4
0
 public override void CaseAALocalDecl(AALocalDecl node)
 {
     if (node.GetConst() != null)
     {
         Write("const ");
     }
     node.GetType().Apply(this);
     Write(" " + node.GetName().Text);
     if (node.GetInit() != null)
     {
         Write(" = ");
         node.GetInit().Apply(this);
     }
 }
Ejemplo n.º 5
0
        public override void OutAStructFieldLvalue(AStructFieldLvalue node)
        {
            if (folding)
            {
                if (data.StructMethodPropertyLinks.ContainsKey(node))
                {
                    errors.Add(
                        new ErrorCollection.Error(node.GetName(),
                                                  LocRM.GetString("ErrorText61"),
                                                  false,
                                                  new ErrorCollection.Error(
                                                      data.StructMethodPropertyLinks[node].GetName(),
                                                      LocRM.GetString("ErrorText62"))), true);
                    throw new ParserException(node.GetName(), "TypeLinking.OutAStructFieldLvalue");
                }
                AALocalDecl field = data.StructMethodFieldLinks[node];
                if (!(field.GetType() is ANamedType && ((ANamedType)field.GetType()).IsPrimitive("int")))
                {
                    errors.Add(
                        new ErrorCollection.Error(node.GetName(),
                                                  LocRM.GetString("ErrorText59"),
                                                  false, new ErrorCollection.Error(field.GetName(), LocRM.GetString("ErrorText60"))), true);
                    throw new ParserException(node.GetName(), "TypeLinking.OutAStructFieldLvalue");
                }

                if (field.GetConst() == null)
                {
                    if (!isANewExp)
                    {
                        errors.Add(
                            new ErrorCollection.Error(node.GetName(),
                                                      LocRM.GetString("ErrorText62"),
                                                      false, new ErrorCollection.Error(field.GetName(), LocRM.GetString("ErrorText60"))), true);
                        throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue");
                    }
                }
                if (field.GetInit() == null)//An error will be given earlier - constant fields must have an initializer
                {
                    throw new ParserException(node.GetName(), "TypeLinking.OutAFieldLvalue");
                }
                field.GetInit().Apply(this);
            }
        }
Ejemplo n.º 6
0
 public override void OutALocalLvalue(ALocalLvalue node)
 {
     if (folding)
     {
         AALocalDecl local = data.LocalLinks[node];
         if (local.GetConst() == null)
         {
             if (!isANewExp)
             {
                 errors.Add(
                     new ErrorCollection.Error(node.GetName(),
                                               LocRM.GetString("ErrorText61"),
                                               false), true);
                 throw new ParserException(null, null);
             }
         }
         if (local.GetInit() == null)//An error will be given earlier
         {
             throw new ParserException(null, null);
         }
         local.GetInit().Apply(this);
     }
 }
Ejemplo n.º 7
0
 public override void OutALocalLvalue(ALocalLvalue node)
 {
     if (folding)
     {
         AALocalDecl local = data.LocalLinks[node];
         if (local.GetConst() == null)
         {
             if (!isANewExp)
             {
                 errors.Add(
                     new ErrorCollection.Error(node.GetName(),
                                               "Dimensions of array types must be constant expressions.",
                                               false), true);
                 throw new ParserException(null, null);
             }
         }
         if (local.GetInit() == null)//An error will be given earlier
         {
             throw new ParserException(null, null);
         }
         local.GetInit().Apply(this);
     }
 }
Ejemplo n.º 8
0
 bool IsConstant(PLvalue lvalue)
 {
     if (lvalue is ALocalLvalue)
     {
         ALocalLvalue aLvalue = (ALocalLvalue)lvalue;
         AALocalDecl  decl    = data.LocalLinks[aLvalue];
         if (decl == initialLocalDecl)
         {
             return(false);
         }
         return(decl.GetConst() != null && IsConstant(decl.GetInit()));
     }
     if (lvalue is AFieldLvalue)
     {
         AFieldLvalue aLvalue = (AFieldLvalue)lvalue;
         AFieldDecl   decl    = data.FieldLinks[aLvalue];
         if (decl == initialFieldDecl)
         {
             return(false);
         }
         return(decl.GetConst() != null && IsConstant(decl.GetInit()));
     }
     if (lvalue is APropertyLvalue)
     {
         return(false);
     }
     if (lvalue is ANamespaceLvalue)
     {
         return(true);
     }
     if (lvalue is AStructFieldLvalue)
     {
         AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue;
         AALocalDecl        decl    = data.StructMethodFieldLinks[aLvalue];
         if (decl == initialLocalDecl)
         {
             return(false);
         }
         return(decl.GetConst() != null && IsConstant(decl.GetInit()));
     }
     if (lvalue is AStructLvalue)
     {
         AStructLvalue aLvalue = (AStructLvalue)lvalue;
         AALocalDecl   decl    = data.StructFieldLinks[aLvalue];
         if (decl == initialLocalDecl)
         {
             return(false);
         }
         return(decl.GetConst() != null && IsConstant(decl.GetInit()));
     }
     if (lvalue is AArrayLvalue)
     {
         AArrayLvalue aLvalue = (AArrayLvalue)lvalue;
         return(IsConstant(aLvalue.GetIndex()) && IsConstant(aLvalue.GetBase()));
     }
     if (lvalue is APointerLvalue)
     {
         APointerLvalue aLvalue = (APointerLvalue)lvalue;
         return(IsConstant(aLvalue.GetBase()));
     }
     if (lvalue is APArrayLengthLvalue)
     {
         APArrayLengthLvalue aLvalue = (APArrayLengthLvalue)lvalue;
         return(data.ExpTypes[aLvalue.GetBase()] is AArrayTempType);
     }
     if (lvalue is AThisLvalue)
     {
         return(false);
     }
     if (lvalue is AValueLvalue)
     {
         return(false);
     }
     throw new Exception("Unexpected lvalue. Got " + lvalue);
 }
Ejemplo n.º 9
0
        public override void CaseAALocalDecl(AALocalDecl node)
        {
            //Convert a static struct field into a global variable. All refferences to it are structFieldLvalues.
            if (node.GetStatic() == null)
            {
                return;
            }



            AStructDecl str = (AStructDecl)node.Parent();

            if (data.StructFields[str].Contains(node))
            {
                data.StructFields[str].Remove(node);
            }
            AFieldDecl replacementField;

            //Don't enhrit static fields.
            if (data.EnheritanceLocalMap.ContainsKey(node))
            {
                str.RemoveChild(node);

                AALocalDecl realVar = data.EnheritanceLocalMap[node];
                if (convertionMap.ContainsKey(realVar))
                {
                    //Already converted to a field
                    replacementField = convertionMap[realVar];
                    foreach (AStructFieldLvalue lvalue in data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key))
                    {
                        AFieldLvalue newLvalue = new AFieldLvalue(new TIdentifier(replacementField.GetName().Text));
                        data.FieldLinks[newLvalue]  = replacementField;
                        data.LvalueTypes[newLvalue] = replacementField.GetType();
                        lvalue.ReplaceBy(newLvalue);
                    }
                }
                else
                {
                    List <AStructFieldLvalue> refferences = new List <AStructFieldLvalue>();
                    refferences.AddRange(data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key));
                    foreach (AStructFieldLvalue lvalue in refferences)
                    {
                        data.StructMethodFieldLinks[lvalue] = realVar;
                    }
                }
                return;
            }

            replacementField = new AFieldDecl(new APublicVisibilityModifier(), null, node.GetConst(), node.GetType(), node.GetName(), node.GetInit());

            replacementField.GetName().Text = str.GetName().Text + "_" + replacementField.GetName().Text;

            AASourceFile file = Util.GetAncestor <AASourceFile>(node);

            file.GetDecl().Insert(file.GetDecl().IndexOf(node.Parent()), replacementField);
            data.Fields.Add(new SharedData.DeclItem <AFieldDecl>(file, replacementField));

            if (ContainsNewExp(replacementField.GetInit()))
            {
                data.FieldsToInitInMapInit.Add(replacementField);
            }

            foreach (AStructFieldLvalue lvalue in data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key))
            {
                AFieldLvalue newLvalue = new AFieldLvalue(new TIdentifier(replacementField.GetName().Text));
                data.FieldLinks[newLvalue]  = replacementField;
                data.LvalueTypes[newLvalue] = replacementField.GetType();
                lvalue.ReplaceBy(newLvalue);
            }

            convertionMap.Add(node, replacementField);
            node.Parent().RemoveChild(node);
        }