Beispiel #1
0
        public override ISemantReturn <ABT.ExprType> DecorateType(ABT.Env env, ABT.ExprType targetType)
        {
            var isConst    = this.TypeQuals.Contains(TypeQual.CONST);
            var isVolatile = this.TypeQuals.Contains(TypeQual.VOLATILE);

            return(SemantReturn.Create(env, new ABT.PointerType(targetType, isConst, isVolatile)));
        }
Beispiel #2
0
        public ISemantReturn <ABT.ExprType> DecorateType(ABT.Env env, ABT.ExprType baseType)
        {
            var type = this.TypeModifiers
                       .Reverse()  // The first Type modifier is nearest to the symbol name, which indicates the outmost Type.
                       .Aggregate( // Wrap up the Type based on the Type modifiers.
                baseType, (currentType, typeModifier) => Semant(typeModifier.DecorateType, currentType, ref env)
                );

            return(SemantReturn.Create(env, type));
        }
Beispiel #3
0
        public override ISemantReturn <ABT.ExprType> DecorateType(ABT.Env env, ABT.ExprType returnType)
        {
            if (this.ParamTypeList.IsNone)
            {
                return(SemantReturn.Create(env, ABT.FunctionType.Create(returnType)));
            }

            var paramTypeList = this.ParamTypeList.Value;

            var namesAndTypes = Semant(paramTypeList.GetNamesAndTypes, ref env);
            var hasVarArgs    = paramTypeList.HasVarArgs;

            return(SemantReturn.Create(env, ABT.FunctionType.Create(returnType, namesAndTypes, hasVarArgs)));
        }
Beispiel #4
0
        public override ABT.Expr GetExpr(ABT.Env env)
        {
            ABT.Expr expr = this.Expr.GetExpr(env);
            String   name = this.Member;

            if (expr.Type.Kind != ABT.ExprTypeKind.STRUCT_OR_UNION)
            {
                throw new InvalidOperationException("Must get the attribute from a struct or union.");
            }

            ABT.Utils.StoreEntry entry = (expr.Type as ABT.StructOrUnionType).Attribs.First(_ => _.name == name);
            ABT.ExprType         type  = entry.type;

            return(new ABT.Attribute(expr, name, type));
        }
Beispiel #5
0
        public override ABT.Expr GetExpr(ABT.Env env)
        {
            ABT.Expr expr = this.Expr.GetExpr(env);

            if (expr.Type.Kind != ABT.ExprTypeKind.POINTER)
            {
                throw new InvalidOperationException("Expected a pointer.");
            }

            ABT.ExprType type = ((ABT.PointerType)expr.Type).RefType;
            if (type.Kind == ABT.ExprTypeKind.STRUCT_OR_UNION && !((ABT.StructOrUnionType)type).IsComplete)
            {
                throw new InvalidOperationException("Cannot dereference incomplete Type.");
            }

            return(new ABT.Dereference(expr, type));
        }
Beispiel #6
0
        public override ISemantReturn <ABT.ExprType> DecorateType(ABT.Env env, ABT.ExprType elemType)
        {
            if (this.NumElems.IsNone)
            {
                return(SemantReturn.Create(env, new ABT.IncompleteArrayType(elemType)));
            }

            // Get number of elements.
            // Be careful: the environment might change.
            var numElems = SemantExpr(this.NumElems.Value, ref env);

            // Try to cast number of elements to a integer.
            // TODO: allow float???
            numElems = ABT.TypeCast.MakeCast(numElems, new ABT.LongType(true, false));

            if (!numElems.IsConstExpr)
            {
                throw new InvalidOperationException("Number of elements of an array must be constant.");
            }

            return(SemantReturn.Create(env, new ABT.ArrayType(elemType, ((ABT.ConstLong)numElems).Value)));
        }
Beispiel #7
0
 public override ABT.Expr GetExpr(ABT.Env env)
 {
     ABT.ExprType type = Semant(this.TypeName.GetExprType, ref env);
     ABT.Expr     expr = this.Expr.GetExpr(env);
     return(ABT.TypeCast.MakeCast(expr, type));
 }
Beispiel #8
0
        public ISemantReturn <Tuple <ABT.ExprType, Option <ABT.Initr> > > GetDecoratedTypeAndInitr(ABT.Env env, ABT.ExprType baseType)
        {
            // Get the Type based on the declarator. Note that this might be an incomplete array.
            var type = Semant(this.Declr.DecorateType, baseType, ref env);

            Option <ABT.Initr> initrOption;

            if (this.Initr.IsNone)
            {
                initrOption = Option <ABT.Initr> .None;
            }
            else
            {
                // If an initializer is present:

                var initr = Semant(this.Initr.Value.GetInitr, ref env);

                // Check that the initializer conforms to the Type.
                initr = initr.ConformType(type);

                // If the object is an incomplete array, we must determine the length based on the initializer.
                if (type.Kind == ABT.ExprTypeKind.INCOMPLETE_ARRAY)
                {
                    // Now we need to determine the length.
                    // Find the last element in the Init list.
                    var lastOffset = -1;
                    initr.Iterate(type, (offset, _) => { lastOffset = offset; });

                    if (lastOffset == -1)
                    {
                        throw new InvalidOperationException("Cannot determine the length of the array based on an empty initializer list.");
                    }

                    var elemType = ((ABT.IncompleteArrayType)type).ElemType;

                    var numElems = 1 + lastOffset / ((ABT.IncompleteArrayType)type).ElemType.SizeOf;

                    type = new ABT.ArrayType(elemType, numElems);
                }

                initrOption = Option.Some(initr);
            }

            // Now everything is created. Check that the Type is complete.
            if (!type.IsComplete)
            {
                throw new InvalidOperationException("Cannot create an object with an incomplete Type.");
            }

            return(SemantReturn.Create(env, Tuple.Create(type, initrOption)));
        }
Beispiel #9
0
 public abstract ISemantReturn <ABT.ExprType> DecorateType(ABT.Env env, ABT.ExprType baseType);
Beispiel #10
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
 new ABT.BitwiseOr(left, right);
Beispiel #11
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
 new ABT.LogicalOr(left, right);
Beispiel #12
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
 new ABT.NotEqual(left, right);
Beispiel #13
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
 new ABT.Greater(left, right);
Beispiel #14
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
 new ABT.RShift(left, right);
Beispiel #15
0
 public abstract ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type);
Beispiel #16
0
 public override ABT.Expr ConstructExpr(ABT.Expr left, ABT.Expr right, ABT.ExprType type) =>
 new ABT.Multiply(left, right);