示例#1
0
 def offsetof(self, struct_name, name):
     return c_ast.FuncCall(
         c_ast.ID('offsetof'),
         c_ast.ExprList([
             c_ast.Typename(
                 None, [],
                 c_ast.TypeDecl(None, [], c_ast.Struct(struct_name, None))),
             c_ast.ID(name)
         ]))
 def test_generate_struct_union_enum_exception(self):
     generator = c_generator.CGenerator()
     self.assertRaises(
         AssertionError,
         generator._generate_struct_union_enum,
         n=c_ast.Struct(
             name='TestStruct',
             decls=[],
         ),
         name='',
     )
示例#3
0
def global_log(ast_tree):
    global_log = c_ast.Decl(
        'global_log', [], [], [],
        c_ast.TypeDecl('global_log', [], c_ast.Struct('Global_Log', None)),
        None, None)
    temp_ls1 = []
    temp_ls1.append(global_log)
    temp_ls2 = []
    temp_ls2 = copy.deepcopy(ast_tree.ext)
    global_log = c_ast.FileAST(temp_ls1 + temp_ls2)
    return global_log
示例#4
0
    def inline_struct_annotated(self, a, decl):
        """
        Import all the decls from decl's type, replacing decl in the current AnnotatedStruct
        """
        name = decl.type.type.name
        try:
            struct = next((e.type for e in self.ext
                           if hasattr(e, 'type') and hasattr(e.type, 'name')
                           and e.type.name == name))
        except Exception as ex:
            raise ExpansionError(a, decl, ex.message)

        struct = c_ast.Struct(self.struct.name, struct.decls, struct.coord)
        return AnnotatedStruct(self.annotations, struct, self.json_annotations,
                               self.ext)
示例#5
0
def generate_sai_api_tbl_h(apis):
    def createDecl(api):
        apitype = 'sai_' + api + '_api_t'
        apivar = 'sai_' + api + '_api_tbl'
        return c_ast.Decl(
            apivar, list(), list(), list(),
            c_ast.PtrDecl(
                list(),
                c_ast.TypeDecl(apivar, list(), c_ast.IdentifierType([
                    apitype,
                ]))), None, None)

    tdecls = [createDecl(api) for api in apis]
    tstruct = c_ast.Struct('_sai_api_tbl_t', tdecls)
    tdec = c_ast.TypeDecl('sai_api_tbl_t', list(), tstruct)
    tdef = c_ast.Typedef('sai_api_tbl_t', list(), ['typedef'], tdec)
    externdec = c_ast.Decl(
        'sai_api_tbl', list(), ['extern'], list(),
        c_ast.TypeDecl('sai_api_tbl', list(),
                       c_ast.IdentifierType(['sai_api_tbl_t'])), None, None)
    api_t = c_ast.FileAST([tdef, externdec])
    generator = c_generator.CGenerator()
    sai_api_tbl_h_str = r'''#include "sai.h"
#ifndef SAI_API_TBL
#define SAI_API_TBL

'''
    sai_api_tbl_h_str += generator.visit(api_t)
    sai_api_tbl_h_str += r'''
extern sai_status_t sai_api_tbl_init();

#endif
'''
    # print(api_t)
    print(sai_api_tbl_h_str)
    with open('adaptor/gen-inc/sai_api_tbl.h', 'w') as f:
        f.write(sai_api_tbl_h_str)
示例#6
0
    def expand(self, ast, filename):
        """
        Expand a pycparser ast with extra structures/data etc
        """
        idx = 0

        self._extract_ast_info(ast)

        struct_object_property_decl = c_ast.Struct('jstruct_object_property',
                                                   None)

        def ppdirective(a, n, ext):
            id = ' '.join(
                (a.directive, a.content)) if a.content else a.directive
            ext.insert(c_ast.ID('#{0}'.format(id)))
            return False

        def annotate_struct(a, n, ext):

            if not isinstance(n.type, c_ast.Struct):
                raise ExpansionError(
                    a, n, 'Cannot expand annotation @{0} on {1}'.format(
                        a.name, n.__class__.__name__))

            json_annotations = {}
            if a.name == 'json' and a.content != '':
                json_annotations = json.loads(a.content)
                json_annotations['@root'] = a
                if not isinstance(json_annotations, dict):
                    raise ExpansionError(
                        a, n,
                        'Expected @json content to be empty or a JSON object.\nGot {0}'
                        .format(a.content))
            name = n.type.name
            struct = n.type
            annotated_struct = AnnotatedStruct(self, struct, json_annotations,
                                               ext)
            properties = c_ast.Decl(
                self.PROPERTIES_NAME(name),
                [],  #quals
                [],  #storage
                [],  #funcspec
                # type
                c_ast.ArrayDecl(
                    c_ast.TypeDecl(self.PROPERTIES_NAME(name), [],
                                   struct_object_property_decl),
                    None,  # dim
                    []  # dim_quals
                ),
                annotated_struct.init_list,  # init
                None  # bitsize
            )

            struct.decls = annotated_struct.decls
            if not ext.seek(n):
                raise ExpansionError(a, n, "Couldn't seek to node")
            ext.insert(properties)
            return True

        process = {'#': ppdirective, 'json': annotate_struct}
        ext = NodeList(ast.ext)
        for n in ast.ext:
            done = False
            if n.coord.file != filename:
                continue

            annotations = self.get(n)
            for a in annotations:
                if not done and a.name in process:
                    done = process[a.name](a, n, ext)
                    if done:
                        try:
                            a_name = a.name
                            a = next(annotations)
                            raise ExpansionError(
                                a, n, 'Unexpected annotation after ' + a_name)
                        except Exception as e:
                            pass
                else:
                    raise ExpansionError(a, n, 'Unexpected annotation')

        ast.ext = ext