Ejemplo n.º 1
0
            protected Type TypeFromDeclarator(Declarator decl, Type baseType)
            {
                var source = decl.Source;
                var ty     = baseType;

                while (decl is not SimpleDeclarator)
                {
                    switch (decl)
                    {
                    case ArrayDeclarator when ty is RefType:
                        Diagnostics.AddError($"Array of references is not valid", source);
                        return(baseType);

                    case ArrayDeclarator d:
                        ty   = new UnresolvedArrayType(ty, d.Length);
                        decl = d.Inner;
                        break;

                    case RefDeclarator when ty is RefType:
                        Diagnostics.AddError($"Reference to reference is not valid", source);
                        return(baseType);

                    case RefDeclarator d:
                        ty   = new RefType(ty);
                        decl = d.Inner;
                        break;

                    default: throw new NotImplementedException();
                    }
                    ;
                }

                return(ty);
            }
Ejemplo n.º 2
0
 public FuncDef(
     DeclSpecs specifiers,
     Declarator declarator,
     IEnumerable <Declaration> declarations,
     CompoundStmt statement
     )
 {
     this.specifiers   = specifiers;
     this.declarator   = declarator;
     this.declarations = declarations;
     this.body         = statement;
 }
Ejemplo n.º 3
0
 bool HasStronglyBoundPointer(Declarator d)
 {
     if (d == null)
     {
         return(false);
     }
     else if (d is PointerDeclarator && ((PointerDeclarator)d).StrongBinding)
     {
         return(true);
     }
     else
     {
         return(HasStronglyBoundPointer(d.InnerDeclarator));
     }
 }
Ejemplo n.º 4
0
 FunctionDeclarator GetFunctionDeclarator(Declarator d)
 {
     if (d == null)
     {
         return(null);
     }
     else if (d is FunctionDeclarator)
     {
         return((FunctionDeclarator)d);
     }
     else
     {
         return(GetFunctionDeclarator(d.InnerDeclarator));
     }
 }
Ejemplo n.º 5
0
 Declarator FixPointerAndArrayPrecedence(Declarator d)
 {
     if (d is PointerDeclarator && d.InnerDeclarator != null && d.InnerDeclarator is ArrayDeclarator)
     {
         var a = d.InnerDeclarator;
         var p = d;
         var i = a.InnerDeclarator;
         a.InnerDeclarator = p;
         p.InnerDeclarator = i;
         return(a);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 6
0
        Declarator MakeArrayDeclarator(Declarator left, TypeQualifiers tq, Expression len, bool isStatic)
        {
            var a = new ArrayDeclarator();

            a.LengthExpression = len;

            if (left.StrongBinding)
            {
                var i = left.InnerDeclarator;
                a.InnerDeclarator    = i;
                left.InnerDeclarator = a;
                return(left);
            }
            else
            {
                a.InnerDeclarator = left;
                return(a);
            }
        }
Ejemplo n.º 7
0
        private CType MakeCFunctionType(CType type, Declarator decl)
        {
            var fdecl = (FunctionDeclarator)decl;

            var name       = decl.DeclaredIdentifier;
            var returnType = type;
            var ftype      = new CFunctionType(returnType);

            foreach (var pdecl in fdecl.Parameters)
            {
                var pt = MakeCType(pdecl.DeclarationSpecifiers, pdecl.Declarator);
                if (!pt.IsVoid)
                {
                    ftype.Parameters.Add(new CFunctionType.Parameter(pdecl.Name, pt));
                }
            }
            type = ftype;

            type = MakeCType(type, fdecl.InnerDeclarator);

            return(type);
        }
Ejemplo n.º 8
0
 public Declarator ArrayDeclarator(Declarator decl, CExpression expr)
 {
     return new ArrayDeclarator { Declarator = decl, Size = expr };
 }
Ejemplo n.º 9
0
 public InitDeclarator InitDeclarator(Declarator decl, Initializer init)
 {
     return new InitDeclarator { Declarator = decl, Init = init };
 }
Ejemplo n.º 10
0
 public ParamDecl ParamDecl(List<DeclSpec> dsl, Declarator decl)
 {
     return new ParamDecl
     {
         DeclSpecs = dsl,
         Declarator = decl,
     };
 }
Ejemplo n.º 11
0
 public FieldDeclarator FieldDeclarator(Declarator decl, CExpression bitField)
 {
     return new FieldDeclarator { Declarator = decl, FieldSize = bitField };
 }
Ejemplo n.º 12
0
 public Declaration(DeclarationSpecifiers specs, Declarator decl, Initializer init)
 {
     Specifiers  = specs;
     Declarator  = decl;
     Initializer = init;
 }
Ejemplo n.º 13
0
 internal Decl FunctionDefinition(List<DeclSpec> decl_spec_list, Declarator declarator, List<Stat> statements)
 {
     return new FunctionDecl
     {
         decl_specs = decl_spec_list,        //$REVIEW: dupe?
         Signature = Decl(decl_spec_list, declarator),
         Body = statements
     };
         
 }
Ejemplo n.º 14
0
 public Declarator CallConventionDeclarator(CTokenType conv, Declarator decl)
 {
     return new CallConventionDeclarator
     {
         Convention = conv,
         Declarator = decl,
     };
 }
Ejemplo n.º 15
0
 public ArrayDeclarator(Declarator inner, Expression length, SourceRange source) : base(source)
     => (Inner, Length) = (inner, length);
Ejemplo n.º 16
0
 public ParameterDecl(DeclarationSpecifiers specs, Declarator dec)
 {
     DeclarationSpecifiers = specs;
     Name       = dec.DeclaredIdentifier;
     Declarator = dec;
 }
Ejemplo n.º 17
0
        CType MakeCType(CType type, Declarator decl)
        {
            if (decl is IdentifierDeclarator)
            {
                // This is the name
            }
            else if (decl is PointerDeclarator)
            {
                var pdecl           = (PointerDeclarator)decl;
                var isPointerToFunc = false;

                if (pdecl.StrongBinding)
                {
                    type            = MakeCType(type, pdecl.InnerDeclarator);
                    isPointerToFunc = type is CFunctionType;
                }

                var p = pdecl.Pointer;
                while (p != null)
                {
                    type = new CPointerType(type);
                    type.TypeQualifiers = p.TypeQualifiers;
                    p = p.NextPointer;
                }

                if (!pdecl.StrongBinding)
                {
                    type = MakeCType(type, pdecl.InnerDeclarator);
                }

                //
                // Remove 1 level of pointer indirection if this is
                // a pointer to a function since functions are themselves
                // pointers
                //
                if (isPointerToFunc)
                {
                    type = ((CPointerType)type).InnerType;
                }
            }
            else if (decl is ArrayDeclarator)
            {
                var adecl = (ArrayDeclarator)decl;

                while (adecl != null)
                {
                    type  = new CArrayType(type, adecl.LengthExpression);
                    adecl = adecl.InnerDeclarator as ArrayDeclarator;
                    if (adecl != null && adecl.InnerDeclarator != null)
                    {
                        if (adecl.InnerDeclarator is IdentifierDeclarator)
                        {
                        }
                        else if (!(adecl.InnerDeclarator is ArrayDeclarator))
                        {
                            type = MakeCType(type, adecl.InnerDeclarator);
                        }
                        else
                        {
                            //throw new NotSupportedException("Unrecognized array syntax");
                        }
                    }
                }
            }
            else if (decl is FunctionDeclarator)
            {
                type = MakeCFunctionType(type, decl);
            }

            return(type);
        }
Ejemplo n.º 18
0
        CType MakeCType(DeclarationSpecifiers specs, Declarator decl)
        {
            var type = MakeCType(specs);

            return(MakeCType(type, decl));
        }
Ejemplo n.º 19
0
 public Declaration(string type, Declarator declarator, Expression?initializer, SourceRange source) : base(source)
     => (Type, Declarator, Initializer) = (type, declarator, initializer);
Ejemplo n.º 20
0
 public Declarator FunctionDeclarator(Declarator decl, List<ParamDecl> parameters)
 {
     return new FunctionDeclarator { Declarator = decl, Parameters = parameters };
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Get hash code
 /// </summary>
 public override int GetHashCode()
 {
     return(Declarator.GetHashCode() ^
            Initializer.GetHashCode());
 }
Ejemplo n.º 22
0
 public Declarator PointerDeclarator(Declarator decl, List<TypeQualifier> tqs)
 {
     return new PointerDeclarator { TypeQualifierList = tqs, Pointee = decl};
 }
Ejemplo n.º 23
0
		public void AddDeclarator (Declarator decl)
		{
			if (declarators == null)
				declarators = new List<Declarator> ();

			declarators.Add (decl);
		}
Ejemplo n.º 24
0
 internal Decl Decl(List<DeclSpec> list, Declarator decl)
 {
     return new Decl
     {
         decl_specs = list,
         init_declarator_list = new List<InitDeclarator> 
         {
             new InitDeclarator {
                 Declarator = decl,
                 Init= null
             }
         }
     };
 }
Ejemplo n.º 25
0
 internal Decl Decl(List<CAttribute> attrs, List<DeclSpec> list, Declarator decl)
 {
     return new Decl
     {
         attribute_list = attrs,
         decl_specs = list,
         init_declarator_list = new List<InitDeclarator> 
         {
             new InitDeclarator {
                 Declarator = decl,
                 Init= null
             }
         }
     };
 }
Ejemplo n.º 26
0
        public static void SetDeclarationRules()
        {
            // declaration
            //   : declaration-specifiers [Init-declarator-list]? ';'
            Declaration.Is(
                (DeclarationSpecifiers)
                .Then(InitDeclaratorList.Optional(ImmutableList <InitDeclr> .Empty))
                .Then(Semicolon)
                .Then(Decln.Create)
                .TransformResult(
                    _ => {
                var result = _.Result;
                var env    = _.Environment;
                env        = result.InitDeclrs.Aggregate(env, (currentEnv, initDeclr) =>
                                                         currentEnv.AddSymbol(
                                                             initDeclr.Declr.Name,
                                                             result.DeclnSpecs.StorageClsSpecs.DefaultIfEmpty(StorageClsSpec.AUTO).First()
                                                             )
                                                         );
                return(ParserSucceeded.Create(result, env, _.Source));
            }
                    )
                );

            // declaration-specifiers
            //   : [ storage-class-specifier | Type-specifier | Type-qualifier ]+
            DeclarationSpecifiers.Is(
                Parser.Seed(DeclnSpecs.Empty)
                .Then(
                    Either(
                        Given <DeclnSpecs>()
                        .Then(StorageClassSpecifier)
                        .Then(DeclnSpecs.Add)
                        ).Or(
                        Given <DeclnSpecs>()
                        .Then(TypeSpecifier)
                        .Then(DeclnSpecs.Add)
                        ).Or(
                        Given <DeclnSpecs>()
                        .Then(TypeQualifier)
                        .Then(DeclnSpecs.Add)
                        ).OneOrMore()
                    )
                );

            // Init-declarator-list
            //   : Init-declarator [ ',' Init-declarator ]*
            InitDeclaratorList.Is(
                InitDeclarator.OneOrMore(Comma)
                );

            // Init-declarator
            //   : declarator [ '=' initializer ]?
            InitDeclarator.Is(
                (Declarator)
                .Then(
                    (Assign).Then(Initializer).Optional()
                    ).Then(InitDeclr.Create)
                );

            // storage-class-specifier
            //   : auto | register | static | extern | typedef
            StorageClassSpecifier.Is(
                Either(Auto)
                .Or(Register)
                .Or(Static)
                .Or(Extern)
                .Or(Typedef)
                );

            // Type-specifier
            //   : void
            //   | char
            //   | short
            //   | int
            //   | long
            //   | float
            //   | double
            //   | signed
            //   | unsigned
            //   | struct-or-union-specifier
            //   | enum-specifier
            //   | typedef-name
            TypeSpecifier.Is(
                Either(
                    Either(Void)
                    .Or(Char)
                    .Or(Short)
                    .Or(Int)
                    .Or(Long)
                    .Or(Float)
                    .Or(Double)
                    .Or(Signed)
                    .Or(Unsigned)
                    .Then(kind => new BasicTypeSpec(kind) as TypeSpec)
                    )
                .Or(StructOrUnionSpecifier)
                .Or(EnumSpecifier)
                .Or(TypeDefName.Then(TypedefName.Create))
                );

            // type_qualifier
            //   : const
            //   | volatile
            TypeQualifier.Is(
                Either(Const).Or(Volatile)
                );

            // declarator
            //   : [pointer]? direct-declarator
            Declarator.Is(
                (Pointer.Optional())
                .Then(DirectDeclarator)
                .Then(Declr.Create)
                );

            // pointer
            //   : [ '*' [Type-qualifier-list]? ]+
            Pointer.Is(
                (
                    Mult.
                    Then(TypeQualifierList.Optional(ImmutableList <TypeQual> .Empty))
                    .Then(PointerModifier.Create)
                ).OneOrMore()
                .Then(pointerModifiers => pointerModifiers.Reverse())
                );

            // parameter-Type-list
            //   : parameter-list [ ',' '...' ]?
            ParameterTypeList.Is(
                ParameterList
                .Then(
                    (Comma)
                    .Then(Period).Then(Period).Then(Period)
                    .Optional()
                    ).Then(ParamTypeList.Create)
                );

            // parameter-list
            //   : parameter-declaration [ ',' parameter-declaration ]*
            ParameterList.Is(
                ParameterDeclaration.OneOrMore(Comma)
                );

            // Type-qualifier-list
            //   : [Type-qualifier]+
            TypeQualifierList.Is(
                TypeQualifier.OneOrMore()
                );

            // direct-declarator
            //   : [
            //         identifier | '(' declarator ')'
            //     ] [
            //         '[' [constant-expression]? ']'
            //       | '(' [parameter-Type-list]? ')'
            //     ]*
            DirectDeclarator.Is(
                (
                    Either(
                        (Identifier).Then(Declr.Create)
                        ).Or(
                        (LeftParen).Then(Declarator).Then(RightParen)
                        )
                ).Then(
                    Either(
                        Given <Declr>()
                        .Then(LeftBracket)
                        .Then(
                            ConstantExpression.Optional().Then(ArrayModifier.Create)
                            ).Then(RightBracket)
                        .Then(Declr.Add)
                        ).Or(
                        Given <Declr>()
                        .Then(LeftParen)
                        .Then(
                            ParameterTypeList
                            .Optional()
                            .Then(FunctionModifier.Create)
                            ).Then(RightParen)
                        .Then(Declr.Add)
                        )
                    .ZeroOrMore()
                    )
                );

            // enum-specifier
            //   : enum [identifier]? '{' enumerator-list '}'
            //   | enum identifier
            EnumSpecifier.Is(
                (Enum)
                .Then(
                    Either(
                        Identifier.Optional()
                        .Then(LeftCurlyBrace)
                        .Then(EnumeratorList)
                        .Then(RightCurlyBrace)
                        .Then(EnumSpec.Create)
                        ).Or(
                        (Identifier)
                        .Then(EnumSpec.Create)
                        )
                    )
                );

            // enumerator-list
            //   : enumerator [ ',' enumerator ]*
            EnumeratorList.Is(
                Enumerator.OneOrMore(Comma)
                );

            // enumerator
            //   : enumeration-constant [ '=' constant-expression ]?
            Enumerator.Is(
                EnumerationConstant
                .Then(
                    (Assign)
                    .Then(ConstantExpression)
                    .Optional()
                    ).Then(Enumr.Create)
                );

            // enumeration-constant
            //   : identifier
            EnumerationConstant.Is(
                Identifier
                );

            // struct-or-union-specifier
            //   : struct-or-union [identifier]? { struct-declaration-list }
            //   | struct-or-union identifier
            StructOrUnionSpecifier.Is(
                (StructOrUnion)
                .Then(
                    Either(
                        Given <StructOrUnion>()
                        .Then(Identifier.Optional())
                        .Then(LeftCurlyBrace)
                        .Then(StructDeclarationList)
                        .Then(RightCurlyBrace)
                        .Then(StructOrUnionSpec.Create)
                        ).Or(
                        Given <StructOrUnion>()
                        .Then(Identifier)
                        .Then(StructOrUnionSpec.Create)
                        )
                    )
                );

            // struct-or-union
            //   : struct | union
            StructOrUnion.Is(
                Either(Struct).Or(Union)
                );

            // struct-declaration-list
            //   : [struct-declaration]+
            StructDeclarationList.Is(
                StructDeclaration.OneOrMore()
                );

            // struct-declaration
            //   : specifier-qualifier-list struct-declarator-list ';'
            StructDeclaration.Is(
                (SpecifierQualifierList)
                .Then(StructDeclaratorList)
                .Then(Semicolon)
                .Then(StructDecln.Create)
                );

            // specifier-qualifier-list
            //   : [ Type-specifier | Type-qualifier ]+
            SpecifierQualifierList.Is(
                Parser.Seed(SpecQualList.Empty)
                .Then(
                    Either(
                        Given <SpecQualList>()
                        .Then(TypeSpecifier)
                        .Then(SpecQualList.Add)
                        ).Or(
                        Given <SpecQualList>()
                        .Then(TypeQualifier)
                        .Then(SpecQualList.Add)
                        )
                    .OneOrMore()
                    )
                );

            // struct-declarator-list
            //   : struct-declarator [ ',' struct-declarator ]*
            StructDeclaratorList.Is(
                StructDeclarator.OneOrMore(Comma)
                );

            // struct-declarator
            //   : [declarator]? ':' constant-expression
            //   | declarator
            StructDeclarator.Is(
                Either(
                    (Declarator.Optional())
                    .Then(Colon)
                    .Then(ConstantExpression)
                    .Then(StructDeclr.Create)
                    ).Or(
                    (Declarator)
                    .Then(StructDeclr.Create)
                    )
                );

            // parameter-declaration
            //   : declaration-specifiers [ declarator | abstract-declarator ]?
            ParameterDeclaration.Is(
                (DeclarationSpecifiers)
                .Then(
                    Either(
                        (Declarator).Then(ParamDeclr.Create)
                        ).Or(
                        (AbstractDeclarator).Then(ParamDeclr.Create)
                        ).Optional()
                    ).Then(ParamDecln.Create)
                );

            // abstract-declarator
            //   : [pointer]? direct-abstract-declarator
            //   | pointer
            AbstractDeclarator.Is(
                Either(
                    (Pointer.Optional(ImmutableList <PointerModifier> .Empty))
                    .Then(DirectAbstractDeclarator)
                    .Then(AbstractDeclr.Add)
                    ).Or(
                    (Pointer)
                    .Then(AbstractDeclr.Create)
                    )
                );

            // direct-abstract-declarator
            //   : [
            //         '(' abstract-declarator ')'
            //       | '[' [constant-expression]? ']'  // array modifier
            //       | '(' [parameter-type_list]? ')'  // function modifier
            //     ] [
            //         '[' [constant-expression]? ']'  // array modifier
            //       | '(' [parameter-Type-list]? ')'  // function modifier
            //     ]*
            DirectAbstractDeclarator.Is(
                (
                    Either(
                        (LeftParen)
                        .Then(AbstractDeclarator)
                        .Then(RightParen)
                        ).Or(
                        (LeftBracket)
                        .Then(ConstantExpression.Optional())
                        .Then(RightBracket)
                        .Then(ArrayModifier.Create)
                        .Then <ArrayModifier, ImmutableList <ArrayModifier> >(ImmutableList.Create)
                        .Then(AbstractDeclr.Create)
                        ).Or(
                        (LeftParen)
                        .Then(ParameterTypeList.Optional())
                        .Then(RightParen)
                        .Then(FunctionModifier.Create)
                        .Then <FunctionModifier, ImmutableList <FunctionModifier> >(ImmutableList.Create)
                        .Then(AbstractDeclr.Create)
                        )
                ).Then(
                    Either(
                        Given <AbstractDeclr>()
                        .Then(
                            LeftBracket
                            .Then(ConstantExpression.Optional())
                            .Then(RightBracket)
                            .Then(ArrayModifier.Create)
                            ).Then(
                            AbstractDeclr.Add
                            )
                        ).Or(
                        Given <AbstractDeclr>()
                        .Then(
                            (LeftParen)
                            .Then(ParameterTypeList.Optional())
                            .Then(RightParen)
                            .Then(FunctionModifier.Create)
                            ).Then(
                            AbstractDeclr.Add
                            )
                        )
                    .ZeroOrMore()
                    )
                );

            // initializer
            //   : assignment-expression
            //   | '{' initializer-list '}'
            //   | '{' initializer-list ',' '}'
            Initializer.Is(
                Either <Initr>(
                    (AssignmentExpression)
                    .Then(InitExpr.Create)
                    ).Or(
                    (LeftCurlyBrace)
                    .Then(InitializerList)
                    .Then(RightCurlyBrace)
                    ).Or(
                    (LeftCurlyBrace)
                    .Then(InitializerList)
                    .Then(Comma)
                    .Then(RightCurlyBrace)
                    )
                );

            // initializer-list
            //   : initializer [ ',' initializer ]*
            InitializerList.Is(
                Initializer.OneOrMore(Comma)
                .Then(InitList.Create)
                );

            // Type-name
            //   : specifier-qualifier-list [abstract-declarator]?
            TypeName.Is(
                (SpecifierQualifierList)
                .Then(AbstractDeclarator.Optional())
                .Then(AST.TypeName.Create)
                );

            // typedef-name
            //   : identifier
            TypeDefName.Is(
                (Identifier)
                .Check(
                    result => result.Environment.IsTypedefName(result.Result)
                    )
                );
        }
Ejemplo n.º 27
0
 public PointerDeclarator(Pointer pointer, Declarator decl)
 {
     Pointer         = pointer;
     InnerDeclarator = decl;
 }
Ejemplo n.º 28
0
 private void Run(DeclSpec[] declSpecs, Declarator decl)
 {
     var ndte = new NamedDataTypeExtractor(declSpecs, parserState);
     this.nt = ndte.GetNameAndType(decl);
 }
Ejemplo n.º 29
0
 private void Run(DeclSpec[] declSpecs, Declarator decl)
 {
     var ndte = new NamedDataTypeExtractor(platform, declSpecs, symbolTable);
     this.nt = ndte.GetNameAndType(decl);
 }
Ejemplo n.º 30
0
			public Declarator (Declarator clone, Expression initializer)
			{
				this.li = clone.li;
				this.initializer = initializer;
			}
Ejemplo n.º 31
0
        private void Run(DeclSpec[] declSpecs, Declarator decl)
        {
            var ndte = new NamedDataTypeExtractor(declSpecs, parserState);

            this.nt = ndte.GetNameAndType(decl);
        }
Ejemplo n.º 32
0
        private void Run(DeclSpec[] declSpecs, Declarator decl)
        {
            var ndte = new NamedDataTypeExtractor(platform, declSpecs, symbolTable);

            this.nt = ndte.GetNameAndType(decl);
        }
Ejemplo n.º 33
0
 public ParamDecl ParamDecl(List<CAttribute> attrs, List<DeclSpec> dsl, Declarator decl)
 {
     return new ParamDecl
     {
         Attributes = attrs,
         DeclSpecs = dsl,
         Declarator = decl,
     };
 }