private IEnumerable <StructField_v1> ExpandStructFields(IEnumerable <StructDecl> decls)
        {
            int offset = 0;

            foreach (var decl in decls)
            {
                var ntde = new NamedDataTypeExtractor(platform, decl.SpecQualifierList, symbolTable);
                foreach (var declarator in decl.FieldDeclarators)
                {
                    var nt      = ntde.GetNameAndType(declarator);
                    var rawSize = (nt.DataType != null)
                        ? nt.DataType.Accept(symbolTable.Sizer)
                        : 0;
                    offset = Align(offset, rawSize, 8);     //$BUG: disregards temp. alignment changes. (__declspec(align))
                    yield return(new StructField_v1
                    {
                        Offset = offset,
                        Name = nt.Name,
                        Type = nt.DataType,
                    });

                    offset += rawSize;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Given a C declaration, adds it to the symbol table
        /// as a function or a type declaration.
        /// </summary>
        /// <param name="decl"></param>
        public List <SerializedType> AddDeclaration(Decl decl)
        {
            var types = new List <SerializedType>();

            if (decl is FunctionDecl fndec)
            {
                return(types);
            }

            IEnumerable <DeclSpec> declspecs = decl.decl_specs;
            var isTypedef = false;

            if (decl.decl_specs[0] is StorageClassSpec scspec &&
                scspec.Type == CTokenType.Typedef)
            {
                declspecs = decl.decl_specs.Skip(1);
                isTypedef = true;
            }

            var ntde = new NamedDataTypeExtractor(platform, declspecs, this, pointerSize);

            foreach (var declarator in decl.init_declarator_list)
            {
                var nt      = ntde.GetNameAndType(declarator.Declarator);
                var serType = nt.DataType !;

                if (nt.DataType is SerializedSignature sSig)
                {
                    sSig.Convention ??= GetCallingConventionFromAttributes(decl.attribute_list);
                    if (sSig.ReturnValue != null)
                    {
                        sSig.ReturnValue.Kind = ntde.GetArgumentKindFromAttributes(
                            "returns", decl.attribute_list);
                    }
                    var sProc = MakeProcedure(nt.Name !, sSig, decl.attribute_list);
                    Procedures.Add(sProc);
                    types.Add(sSig);
                }
                else if (!isTypedef)
                {
                    var variable = new GlobalDataItem_v2
                    {
                        Name     = nt.Name,
                        DataType = serType,
                    };
                    Variables.Add(variable);
                    types.Add(serType);
                }
                if (isTypedef)
                {
                    //$REVIEW: should make sure that if the typedef already exists,
                    // then the types match but a real compiler would have validated that.
                    var typedef = new SerializedTypedef
                    {
                        Name     = nt.Name,
                        DataType = serType
                    };
                    Types.Add(typedef);
                    //$REVIEW: do we really need to check for consistence?
                    NamedTypes[typedef.Name !] = serType;
Example #3
0
        public List <SerializedType> AddDeclaration(Decl decl)
        {
            var types = new List <SerializedType>();
            var fndec = decl as FunctionDecl;

            if (fndec != null)
            {
                return(types);
            }

            IEnumerable <DeclSpec> declspecs = decl.decl_specs;
            var isTypedef = false;
            var scspec    = decl.decl_specs[0] as StorageClassSpec;

            if (scspec != null && scspec.Type == CTokenType.Typedef)
            {
                declspecs = decl.decl_specs.Skip(1);
                isTypedef = true;
            }

            var ntde = new NamedDataTypeExtractor(declspecs, this);

            foreach (var declarator in decl.init_declarator_list)
            {
                var nt      = ntde.GetNameAndType(declarator.Declarator);
                var serType = nt.DataType;

                var sSig = nt.DataType as SerializedSignature;
                if (sSig != null)
                {
                    if (sSig.ReturnValue != null)
                    {
                        sSig.ReturnValue.Kind = ntde.GetArgumentKindFromAttributes(decl.attribute_list);
                    }
                    Procedures.Add(new Procedure_v1
                    {
                        Name      = nt.Name,
                        Signature = sSig,
                    });
                    types.Add(sSig);
                }
                if (isTypedef)
                {
                    //$REVIEW: should make sure that if the typedef already exists,
                    // then the types match but a real compiler would have validated that.
                    var typedef = new SerializedTypedef
                    {
                        Name     = nt.Name,
                        DataType = serType
                    };
                    Types.Add(typedef);
                    //$REVIEW: do we really need to check for consistence?
                    NamedTypes[typedef.Name] = serType;
                    types.Add(serType);
                }
            }
            return(types);
        }
 private IEnumerable <UnionAlternative_v1> ExpandUnionFields(IEnumerable <StructDecl> decls)
 {
     foreach (var decl in decls)
     {
         var ndte = new NamedDataTypeExtractor(platform, decl.SpecQualifierList, symbolTable);
         foreach (var declarator in decl.FieldDeclarators)
         {
             var nt = ndte.GetNameAndType(declarator);
             yield return(new UnionAlternative_v1
             {
                 Name = nt.Name,
                 Type = nt.DataType
             });
         }
     }
 }
 private Argument_v1 ConvertParameter(ParamDecl decl)
 {
     if (decl.IsEllipsis)
     {
         return(new Argument_v1
         {
             Name = "...",
         });
     }
     else
     {
         var ntde = new NamedDataTypeExtractor(platform, decl.DeclSpecs, symbolTable);
         var nt   = ConvertArrayToPointer(ntde.GetNameAndType(decl.Declarator));
         var kind = GetArgumentKindFromAttributes("arg", decl.Attributes);
         return(new Argument_v1
         {
             Kind = kind,
             Name = nt.Name,
             Type = nt.DataType,
         });
     }
 }