Esempio n. 1
0
def concurrent_stmts_iter(n):
    """Iterator on concurrent statements in n."""
    k = iirs.Get_Kind(n)
    if k == iirs.Iir_Kind.Design_File:
        for n1 in chain_iter(iirs.Get_First_Design_Unit(n)):
            for n2 in concurrent_stmts_iter(n1):
                yield n2
    elif k == iirs.Iir_Kind.Design_Unit:
        for n1 in concurrent_stmts_iter(iirs.Get_Library_Unit(n)):
            yield n1
    elif k == iirs.Iir_Kind.Entity_Declaration \
         or k == iirs.Iir_Kind.Architecture_Body \
         or k == iirs.Iir_Kind.Block_Statement:
        for n1 in chain_iter(iirs.Get_Concurrent_Statement_Chain(n)):
            yield n1
            for n2 in concurrent_stmts_iter(n1):
                yield n2
    elif k == iirs.Iir_Kind.For_Generate_Statement:
        for n1 in concurrent_stmts_iter(iirs.Get_Generate_Statement_Body(n)):
            yield n1
    elif k == iirs.Iir_Kind.If_Generate_Statement:
        while n != Null_Iir:
            for n1 in concurrent_stmts_iter(
                    iirs.Get_Generate_Statement_Body(n)):
                yield n1
            n = iirs.Get_Generate_Else_Clause(n)
    elif k == iirs.Iir_Kind.Case_Generate_Statement:
        alt = iirs.Get_Case_Statement_Alternative_Chain(n)
        for n1 in chain_iter(alt):
            blk = iirs.Get_Associated_Block(n1)
            if blk != Null_Iir:
                for n2 in concurrent_stmts_iter(
                        iirs.Get_Generate_Statement_Body(n)):
                    yield n2
Esempio n. 2
0
 def check(self, input, ast):
     assert iirs.Get_Kind(ast) == iirs.Iir_Kind.Design_File
     units = thinutils.chain_to_list(iirs.Get_First_Design_Unit(ast))
     pattern = ''
     letter = {iirs.Iir_Kind.Entity_Declaration: 'E',
               iirs.Iir_Kind.Architecture_Body: 'A',
               iirs.Iir_Kind.Configuration_Declaration: 'C',
               iirs.Iir_Kind.Package_Declaration: 'P',
               iirs.Iir_Kind.Package_Body: 'B'}
     pattern = ''.join([letter[iirs.Get_Kind(iirs.Get_Library_Unit(u))]
                        for u in units])
     if pattern not in self.patterns:
         self.error(Location(input.filename),
                    "sequence of units not allowed")
     if len(units) <= 1:
         # Always ok to have one unit. Zero unit is not allowed by vhdl.
         return
     first = iirs.Get_Library_Unit(units[0])
     if iirs.Get_Kind(first) == iirs.Iir_Kind.Entity_Declaration:
         self.check_entity(units)
     elif iirs.Get_Kind(first) == iirs.Iir_Kind.Package_Declaration:
         self.check_package(units)
     else:
         self.error(Location.from_node(first),
                    "first unit must be either an entity or a package")
Esempio n. 3
0
 def check(self, input, file):
     if self._allowed_id is None:
         self._allowed_id = [
             thin.Get_Identifier(n) for n in self._allowed_name
         ]
     for unit in thinutils.chain_iter(iirs.Get_First_Design_Unit(file)):
         for n in thinutils.chain_iter(iirs.Get_Context_Items(unit)):
             if iirs.Get_Kind(n) != iirs.Iir_Kind.Use_Clause:
                 continue
             cl = n
             while cl != thin.Null_Iir:
                 self.check_clause(cl)
                 cl = iirs.Get_Use_Clause_Chain(cl)
 def check(self, input, file):
     for unit in thinutils.chain_iter(iirs.Get_First_Design_Unit(file)):
         # Extract the list of clauses
         clauses = []
         for cl in thinutils.chain_iter(iirs.Get_Context_Items(unit)):
             k = iirs.Get_Kind(cl)
             if k == iirs.Iir_Kind.Library_Clause:
                 clauses.extend(self.extract_library(cl))
             elif k == iirs.Iir_Kind.Use_Clause:
                 clauses.extend(self.extract_use(cl))
             else:
                 assert False, "unknown context clause"
         if clauses:
             self.check_group(clauses)
Esempio n. 5
0
 def check(self, input, ast):
     for du in thinutils.chain_iter(iirs.Get_First_Design_Unit(ast)):
         ent = iirs.Get_Library_Unit(du)
         if iirs.Get_Kind(ent) != iirs.Iir_Kind.Entity_Declaration:
             continue
         gen = iirs.Get_Generic_Chain(ent)
         if gen != thin.Null_Iir:
             self.check_declarations(gen)
         ports = iirs.Get_Port_Chain(ent)
         if ports != thin.Null_Iir:
             self.check_declarations(ports)
             port = ports
             while port != thin.Null_Iir:
                 if not iirs.Get_Has_Mode(port):
                     self.error(Location.from_node(port),
                                "in/out/inout required for port")
                 port = iirs.Get_Chain(port)
Esempio n. 6
0
def constructs_iter(n):
    """Iterator on library unit, concurrent statements and declarations
       that appear directly within a declarative part."""
    if n == thin.Null_Iir:
        return
    k = iirs.Get_Kind(n)
    if k == iirs.Iir_Kind.Design_File:
        for n1 in chain_iter(iirs.Get_First_Design_Unit(n)):
            for n2 in constructs_iter(n1):
                yield n2
    elif k == iirs.Iir_Kind.Design_Unit:
        n1 = iirs.Get_Library_Unit(n)
        yield n1
        for n2 in constructs_iter(n1):
            yield n2
    elif k in [
            iirs.Iir_Kind.Entity_Declaration, iirs.Iir_Kind.Architecture_Body,
            iirs.Iir_Kind.Block_Statement,
            iirs.Iir_Kind.Generate_Statement_Body
    ]:
        for n1 in chain_iter(iirs.Get_Declaration_Chain(n)):
            yield n1
            for n2 in constructs_iter(n1):
                yield n2
        for n1 in chain_iter(iirs.Get_Concurrent_Statement_Chain(n)):
            yield n1
            for n2 in constructs_iter(n1):
                yield n2
    elif k in [
            iirs.Iir_Kind.Configuration_Declaration,
            iirs.Iir_Kind.Package_Declaration, iirs.Iir_Kind.Package_Body,
            iirs.Iir_Kind.Function_Body, iirs.Iir_Kind.Procedure_Body,
            iirs.Iir_Kind.Protected_Type_Declaration,
            iirs.Iir_Kind.Protected_Type_Body, iirs.Iir_Kind.Process_Statement,
            iirs.Iir_Kind.Sensitized_Process_Statement
    ]:
        for n1 in chain_iter(iirs.Get_Declaration_Chain(n)):
            yield n1
            for n2 in constructs_iter(n1):
                yield n2
    elif k == iirs.Iir_Kind.For_Generate_Statement:
        n1 = iirs.Get_Generate_Statement_Body(n)
        yield n1
        for n2 in constructs_iter(n1):
            yield n2
    elif k == iirs.Iir_Kind.If_Generate_Statement:
        while n != Null_Iir:
            n1 = iirs.Get_Generate_Statement_Body(n)
            yield n1
            for n2 in constructs_iter(n1):
                yield n2
            n = iirs.Get_Generate_Else_Clause(n)
    elif k == iirs.Iir_Kind.Case_Generate_Statement:
        alt = iirs.Get_Case_Statement_Alternative_Chain(n)
        for n1 in chain_iter(alt):
            blk = iirs.Get_Associated_Block(n1)
            if blk != Null_Iir:
                n2 = iirs.Get_Generate_Statement_Body(blk)
                yield n2
                for n3 in constructs_iter(n2):
                    yield n3
Esempio n. 7
0
 def check(self, input, ast):
     assert iirs.Get_Kind(ast) == iirs.Iir_Kind.Design_File
     for u in thinutils.chain_iter(iirs.Get_First_Design_Unit(ast)):
         self.chk_context_clauses(u, 1)
         lib_unit = iirs.Get_Library_Unit(u)
         self.chk_library_unit(lib_unit, 1)
Esempio n. 8
0
 def check(self, input, ast):
     assert iirs.Get_Kind(ast) == iirs.Iir_Kind.Design_File
     for u in thinutils.chain_iter(iirs.Get_First_Design_Unit(ast)):
         lu = iirs.Get_Library_Unit(u)
         if iirs.Get_Kind(lu) == iirs.Iir_Kind.Entity_Declaration:
             self.check_entity(lu)
Esempio n. 9
0
    def execute(self, files):
        inputs = []
        props = ['synth']
        # First file
        for filename in files:
            if filename.startswith('--'):
                # Handle properties
                if filename == '--import':
                    props = ['import']
                elif filename == '--synth':
                    props = ['synth']
                elif filename == '--top':
                    props = ['synth', 'top']
                elif filename == '--tb':
                    props = ['tb']
                else:
                    fatal("unknown property '{0}'".format(filename))
                continue

            # Read the file
            fid = thin.Get_Identifier(filename.encode('utf-8'))
            fe = thin.Read_Source_File(0, fid)
            if fe == thin.No_Source_File_Entry:
                fatal('cannot open {0}'.format(filename))

            fbuf = thin.Get_File_Buffer(fe)
            flen = thin.Get_File_Length(fe)

            # Not very efficient (it copies the string), but let's use it
            # for now.
            filebuf = ctypes.string_at(fbuf, flen)

            input = RuleInput(filename, fe)
            input.filebuf = filebuf
            input.props = props
            inputs.append(input)

            if 'import' not in input.props:
                self._nbr_files += 1

                # Again, not very efficient (creates the substrings).
                flines = filebuf.splitlines(True)

                loc = Location(filename)
                for r in self._file_rules:
                    r.check(loc, flines)

        # Then tokens
        thin.Scanner.Flag_Comment.value = True
        for input in inputs:
            if 'import' not in input.props:
                thin.Scanner.Set_File(input.fe)
                filebuf = input.filebuf
                while True:
                    thin.Scanner.Scan()
                    tok = thin.Scanner.Current_Token.value
                    loc = TokLocation(input.filename,
                                      thin.Scanner.Get_Current_Line(),
                                      thin.Scanner.Get_Token_Column(),
                                      thin.Scanner.Get_Token_Position(),
                                      thin.Scanner.Get_Position())
                    if tok == tokens.Tok.Comment:
                        input.comments[loc.line] = (loc.start, loc.end)
                    for r in self._lex_rules:
                        r.check(loc, filebuf, tok)
                    if tok == tokens.Tok.Eof:
                        break
                thin.Scanner.Close_File()
        if not (self._syntax_rules or self._syntax_node_rules or
                self._sem_rules or self._sem_node_rules or self._synth_rules):
            return

        # Then syntax
        # The parser doesn't handle comments
        thin.Scanner.Flag_Comment.value = False
        # Keep extra locations
        thin.Flags.Flag_Elocations.value = True
        # Keep all parenthesis
        thin.Parse.Flag_Parse_Parenthesis.value = True
        # Be sure to initialize std and work (and only once).
        # Humm, not very elegant.
        if thin.Get_Libraries_Chain() == thin.Null_Iir:
            thin.analyze_init()
        for input in inputs:
            thin.Scanner.Set_File(input.fe)
            loc = Location(input.filename)
            input.ast = thin.Parse.Parse_Design_File()
            if 'import' not in input.props:
                for r in self._syntax_rules:
                    r.check(input, input.ast)
                if self._syntax_node_rules:
                    for n in thinutils.nodes_iter(input.ast):
                        for r in self._syntax_node_rules:
                            r.check(loc, n)
            thin.Scanner.Close_File()

        # Then semantic
        if self._sem_rules or self._sem_node_rules or self._synth_rules:
            # Reduce Canon
            thin.Canon.Flag_Concurrent_Stmts.value = False
            thin.Canon.Flag_Configurations.value = False
            thin.Canon.Flag_Associations.value = False
            # First add all units in the work library, so that they they are
            # known by the analyzer.
            for input in inputs:
                unit_ast = iirs.Get_First_Design_Unit(input.ast)
                while unit_ast != thin.Null_Iir:
                    # Detach the unit from its design file
                    next_unit_ast = iirs.Get_Chain(unit_ast)
                    iirs.Set_Chain(unit_ast, thin.Null_Iir)
                    # Add
                    thin.Add_Design_Unit_Into_Library(unit_ast, False)
                    input.units_ast.append(unit_ast)
                    unit_ast = next_unit_ast
            # Handle all unit
            for input in inputs:
                if 'import' not in input.props:
                    for unit in input.units_ast:
                        if iirs.Get_Library_Unit(unit) == thin.Null_Iir:
                            # Over-written.
                            # FIXME: remove from the list ?
                            continue
                        # Be sure the unit was analyzed. It could have been
                        # already analyzed if referenced. And a unit cannot be
                        # analyzed twice.
                        if iirs.Get_Date_State(unit) == iirs.Date_State.Parse:
                            thin.Finish_Compilation(unit, False)
                            iirs.Set_Date_State(unit, iirs.Date_State.Analyze)
                        for r in self._sem_rules:
                            r.check(input, unit)
                        for n in thinutils.nodes_iter(unit):
                            for r in self._sem_node_rules:
                                r.check(input, n)

            for input in inputs:
                if 'synth' in input.props:
                    for unit in input.units_ast:
                        if iirs.Get_Library_Unit(unit) == thin.Null_Iir:
                            # Over-written.
                            continue
                        for r in self._synth_rules:
                            r.check(input, unit)