Пример #1
0
 public override bool Equals(object obj)
 {
     if (obj is ReturnTypeNode)
     {
         var o = obj as ReturnTypeNode;
         return(DataType.Equals(o.DataType));
     }
     return(false);
 }
Пример #2
0
        public static bool IsVoid(TypeNode type)
        {
            TypeModifier mtype = type as TypeModifier;

            if (mtype != null)
            {
                return(IsVoid(mtype.ModifiedType));
            }

            if (type != null && type.Equals(Cci.SystemTypes.Void))
            {
                return(true);
            }
            return(false);
        }
Пример #3
0
        public void validateRelationBetween(ref TypeNode f, ref TypeNode typeAssignmentNode)
        {
            var ogiginalType = f;
            var tdn          = (typeAssignmentNode is ArrayTypeNode || typeAssignmentNode is AbstractTypeNode)?getTypeForIdentifier(Utils.getNameForType(typeAssignmentNode)):typeAssignmentNode;
            var t            = (f is AbstractTypeNode || f is ArrayTypeNode)?getTypeForIdentifier(Utils.getNameForType(f)):f;

            t = (t is VarTypeNode)?tdn:t;
            string rule  = f.ToString() + "," + typeAssignmentNode.ToString();
            string rule2 = (t is AbstractTypeNode)?"":t.getComparativeType() + "," + typeAssignmentNode.ToString();
            string rule3 = (t is AbstractTypeNode)?"":t.getComparativeType() + "," + tdn.getComparativeType();

            if (!assignmentRules.Contains(rule) &&
                !assignmentRules.Contains(rule2) &&
                !assignmentRules.Contains(rule3) &&
                f.ToString() != typeAssignmentNode.ToString() &&
                !f.Equals(typeAssignmentNode))
            {
                f = (f is AbstractTypeNode || f is ArrayTypeNode)?getTypeForIdentifier(Utils.getNameForType(f)):f;
                f = (f is VarTypeNode)?tdn:f;
                if (f.ToString() == "Person" && tdn.ToString() == "Student")
                {
                    Console.Write("");
                }

                if (f.getComparativeType() == Utils.Class && tdn.getComparativeType() == Utils.Class)
                {
                    if (!checkRelationBetween(f, tdn))
                    {
                        Utils.ThrowError("1Not a valid assignment. Trying to assign " + tdn.ToString() + " to field with type " + f.ToString() + " " + f.token.getLine());
                    }
                }
                else if ((!(f.getComparativeType() == Utils.Class || f.getComparativeType() == Utils.String) && tdn is NullTypeNode))
                {
                    Utils.ThrowError("2Not a valid assignment. Trying to assign " + tdn.ToString() + " to field with type " + f.ToString() + " " + f.token.getLine());
                }
                else
                {
                    Utils.ThrowError("3Not a valid assignment. Trying to assign " + tdn.ToString() + " to field with type " + f.ToString() + " " + f.token.getLine());
                }
                if (ogiginalType is ArrayTypeNode)
                {
                    f = ogiginalType;
                }
            }
        }
Пример #4
0
        public override TypeNode EvaluateType(API api, TypeNode type, bool isStatic)
        {
            TypeNode t1 = leftExpression.EvaluateType(api, null, true);
            TypeNode t2 = rightExpression.EvaluateType(api, null, true);

            t1 = (t1 is AbstractTypeNode || t1 is ArrayTypeNode)?api.getTypeForIdentifier(Utils.getNameForType(t1)):t1;
            t2 = (t2 is AbstractTypeNode || t2 is ArrayTypeNode)?api.getTypeForIdentifier(Utils.getNameForType(t2)):t2;
            string rule  = t1.ToString() + "," + t2.ToString();
            string rule2 = t1.getComparativeType() + "," + t2.ToString();
            string rule3 = t1.getComparativeType() + "," + t2.getComparativeType();

            if (!rule.Contains(rule) &&
                !rule.Contains(rule2) &&
                !rule.Contains(rule3) &&
                !t1.Equals(t2))
            {
                if (t1.getComparativeType() == Utils.Class && t2.getComparativeType() == Utils.Class)
                {
                    if (!api.checkRelationBetween(t1, t2))
                    {
                        Utils.ThrowError("Not a valid assignment. Trying to assign " + t2.ToString() + " to field with type " + t1.ToString());
                    }
                }
                else if ((!(t1.getComparativeType() == Utils.Class || t1.getComparativeType() == Utils.String) && t2 is NullTypeNode))
                {
                    Utils.ThrowError("Not a valid assignment. Trying to assign " + t2.ToString() + " to field with type " + t1.ToString());
                }
                else if (t1.getComparativeType() == Utils.Var)
                {
                    t1 = t2;
                }
                else if (t1.getComparativeType() == Utils.Array && t2.getComparativeType() == Utils.Array)
                {
                    var token = new Token();
                    token.row    = -1;
                    token.column = -1;
                    api.checkArrays(t1, t2, token);
                }
                else
                {
                    throw new SemanticException("Not a valid assignment. Trying to assign " + t2.ToString() + " to field with type " + t1.ToString());
                }
            }
            return(t1);
        }
Пример #5
0
        public override bool TrySetType(TypeNode expectedType, IMetaDataProvider metaDataProvider, out TypeNode resultType)
        {
            if (typeof(T) == typeof(Parameter))
            {
                var      p    = (Parameter)(object)this.Element;
                TypeNode type = metaDataProvider.ParameterType(p);
                this.isManagedPointer = metaDataProvider.IsManagedPointer(type);
                ResultType            = resultType = metaDataProvider.ManagedPointer(type);
                return(true);
            }

            if (typeof(T) == typeof(Field))
            {
                var      f    = (Field)(object)this.Element;
                TypeNode type = metaDataProvider.FieldType(f);
                this.isStatic         = metaDataProvider.IsStatic(f);
                this.isManagedPointer = metaDataProvider.IsManagedPointer(type);
                ResultType            = resultType = metaDataProvider.ManagedPointer(type);

                TypeNode declaringType = metaDataProvider.DeclaringType(f);
                if (metaDataProvider.IsManagedPointer(expectedType))
                {
                    expectedType = metaDataProvider.ElementType(expectedType);
                }
                expectedType = metaDataProvider.Unspecialized(expectedType);

                if (!metaDataProvider.IsStatic(f) && declaringType.Equals(expectedType) &&
                    (!metaDataProvider.DerivesFrom(expectedType, declaringType) ||
                     !metaDataProvider.IsProtected(f) && !metaDataProvider.IsPublic(f)))
                {
                    this.castTo = metaDataProvider.FullName(declaringType);
                }

                return(true);
            }

            if (typeof(T) == typeof(Local))
            {
                var      local = (Local)(object)this.Element;
                TypeNode type  = metaDataProvider.LocalType(local);
                this.isManagedPointer = metaDataProvider.IsManagedPointer(type);
                ResultType            = resultType = metaDataProvider.ManagedPointer(type);

                return(true);
            }

            if (typeof(T) == typeof(Method))
            {
                var method = (Method)(object)this.Element;
                ResultType = resultType = !IsAddressOf
                                                                ? metaDataProvider.ReturnType(method)
                                                                : metaDataProvider.ManagedPointer(metaDataProvider.ReturnType(method));

                if (metaDataProvider.IsManagedPointer(expectedType))
                {
                    expectedType = metaDataProvider.ElementType(expectedType);
                }
                expectedType = metaDataProvider.Unspecialized(expectedType);

                TypeNode declaringType = metaDataProvider.DeclaringType(method);
                if (!metaDataProvider.IsStatic(method) && declaringType.Equals(expectedType) &&
                    (!metaDataProvider.DerivesFrom(expectedType, declaringType) ||
                     !metaDataProvider.IsProtected(method) && !metaDataProvider.IsPublic(method)))
                {
                    this.castTo = metaDataProvider.FullName(declaringType);
                }

                return(true);
            }

            ResultType = resultType = default(TypeNode);
            return(false);
        }
Пример #6
0
 public bool IsVoid(TypeNode type)
 {
     return(type.Equals(System_Void));
 }
        public override TypeNode EvaluateType(API api, TypeNode type, bool isStatic)
        {
            TypeNode expType = expresion.EvaluateType(api, null, isStatic);
            TypeNode t       = api.getTypeForIdentifier(Utils.getNameForType(targetCastType));
            string   rule    = t.ToString() + "," + expType.ToString();
            string   rule2   = t.getComparativeType() + "," + expType.ToString();
            string   rule3   = t.getComparativeType() + "," + expType.getComparativeType();

            if (rules.Contains(rule) || rules.Contains(rule2) || rules.Contains(rule3) || t.Equals(expType))
            {
                return(t);
            }
            if (!(t is ClassTypeNode))
            {
                Utils.ThrowError("There is no relation between " + expType.ToString() + " and " + t.ToString());
            }
            if (expType is ClassTypeNode)
            {
                if (api.checkRelationBetween(expType, t))
                {
                    return(t);
                }
            }
            if (!(expType is NullTypeNode))
            {
                Utils.ThrowError("There is no relation between " + expType.ToString() + " and " + t.ToString());
            }

            return(t);
        }
        public override TypeNode EvaluateType(API api, TypeNode type, bool isStatic)
        {
            TypeNode tdn            = null;
            int      arraysOfarrays = 0;
            VariableInitializerNode previous_expr = null;

            foreach (VariableInitializerNode vi in arrayInitializers)
            {
                if (previous_expr != null)
                {
                    validateExpressions(previous_expr, vi);
                }
                previous_expr = vi;
                TypeNode tdn2 = vi.EvaluateType(api, null, isStatic);
                if (tdn != null)
                {
                    if (!tdn.Equals(tdn2))
                    {
                        if ((tdn is NullTypeNode && tdn2 is PrimitiveTypeNode) || (tdn2 is NullTypeNode && tdn is PrimitiveTypeNode))
                        {
                            throw new SemanticException("Cannot use 'null' and primitive values in an array initialization.");
                        }
                        if (!(tdn is NullTypeNode && tdn2 is NullTypeNode))
                        {
                            throw new SemanticException("Values in array initialization are not equal. '" + tdn.ToString() + "' and '" + tdn2.ToString() + "'");
                        }
                    }
                }
                tdn = tdn2;
                if ((vi is InlineExpressionNode))
                {
                    if (((InlineExpressionNode)vi).primary is ArrayInstantiationNode)
                    {
                        arraysOfarrays++;
                    }
                }
            }
            if (tdn is ArrayTypeNode)
            {
                if (arraysOfarrays > 0)
                {
                    for (int i = 0; i < arraysOfarrays - 1; i++)
                    {
                        ((ArrayTypeNode)tdn).multidimsArrays.Add(new MultidimensionArrayTypeNode(1, null));
                    }
                }
                else
                {
                    ((ArrayTypeNode)tdn).multidimsArrays[0].dimensions++;
                }
            }
            else
            {
                var i = new MultidimensionArrayTypeNode(1, null);
                var l = new List <MultidimensionArrayTypeNode>();
                l.Add(i);
                var a = new ArrayTypeNode(tdn, l, null);
                return(a);
            }
            return(tdn);
        }