Exemplo n.º 1
0
 def l(qn, *args, **kargs):
     if not isinstance(qn, QualifiedName):
         try:
             qn = qn._qname
         except:
             internal_error("trying to get name of " + str(qn))
     return f(qn, *args, **kargs)
Exemplo n.º 2
0
def load_pervasives(pervasives_dir=None, **_):
    if pervasives_dir is None:
        internal_error("can't call without pervasives_dir")
    pervasives_dir = Path(pervasives_dir)
    infos.pervasives_dir = pervasives_dir
    radl_file = pervasives_dir / 'radl.radl'
    radl_obj = pervasives_dir / 'radl.radlo'
    radl_mod = pervasives_dir / 'radlast_4_radl'
    if (radl_mod.is_dir()
        and radl_obj.is_file()
        and 0 == internal_call(obj_compatibility,
                               object_file=str(radl_obj), **dict(_)).error_code):
        # The radl_mod dir exists, the rald_obj too and the object is compatible
        objectfile.load(radl_obj)
    else: # Something isn't right let's compile pervasives
        log1("Compiling pervasives")
        args = dict(_) # Make a copy of the arguments as baseline
        args.update({  # Change arguments to compile pervasives
            'ws_dir'          : pervasives_dir,
            'no_pervasives'   : True,
            'pervasives_dir'  : str(pervasives_dir),
            'radl_file'       : str(radl_file),
            'object_files'    : None,
            'object_dest'     : str(radl_obj),
            'ROS'             : True,
            'relative_links'  : True
            })
        e = internal_call(compile_source, **args)
        if e.error_code != 0:
            raise e
    return 0
Exemplo n.º 3
0
def _check_kind(node, expected):
    """ Verify that the kind of node is as expected (a subtype of one of them).
    Returns the first kind from expected fitting node.
    """
    loc = node._location
    actual = node._kind
    #No approximation first
    if actual in expected:
        return actual
    for k in expected:
        if is_sub(actual, k):
            return k
    #Actual is not correct
    if len(expected) == 0:
        internal_error("No expected type.")
    if isinstance(node, Alias):
        msg1 = "This value, resolved to {},".format(node._is_alias_of._qname)
    elif isinstance(node, Ident):
        msg1 = "This value, resolved to {},".format(node._qname)
    else:
        msg1 = "This value"
    if len(expected) == 1:
        error(msg1+" is of type {},\nwhen a value of type {} is expected."
              "".format(actual, expected[0]), loc)
    else:
        error(msg1+" is of type {},\n"
              "when a value of type among the following set is expected\n"
              "{{{}}}".format(actual, ', '.join(expected)), loc)
Exemplo n.º 4
0
def chan_kind(src, dest, topic, incoming, plantinfo):
    """
    @param incoming may be True or False for outgoing channels
    It is expected that we are the destination (dest).
    """
    src_sys = plantinfo[src]['system']
    src_mac = plantinfo[src_sys]['machine']
    dest_sys = plantinfo[dest]['system']
    dest_mac = plantinfo[dest_sys]['machine']

    if src_sys._kind != 'linux' or dest_sys._kind != 'linux':
        internal_error("for now only linux to linux channels.")

    if src_sys == dest_sys:
        if src_mac != dest_mac:
            internal_error("same system on two machines")
        return kestrel_linux_ipc()
    elif (src_mac._kind == 'lynxsecure_vm'
          and dest_mac._kind == 'lynxsecure_vm'):
        src_lynx = plantinfo[src_mac]['hypervisor']
        dest_lynx = plantinfo[dest_mac]['hypervisor']
        if src_lynx == dest_lynx:
            return kestrel_lynxsecureVM_ivc(dest_mac, src_mac, plantinfo)
        else:
            return kestrel_linux_ip(dest_mac, src_mac)
    elif (src_mac._kind == 'certikos_vm' and dest_mac._kind == 'certikos_vm'):
        src_cos = plantinfo[src_mac]['hypervisor']
        dest_cos = plantinfo[dest_mac]['hypervisor']
        if src_cos == dest_cos:
            return kestrel_certikosVM_ivc(dest_mac, src_mac, plantinfo)
        else:
            return kestrel_linux_ip(dest_mac, src_mac)
    else:
        return kestrel_linux_ip(dest_mac, src_mac)
Exemplo n.º 5
0
def copy_to(var, node, shallow=True, init=False):
    """ Used to copy the value of node in var.
    Require a node with actual access to its values (._val).
    The var is expected to be allocated and of the correct type.
    @param deep: if True the copy will be deep (recursively). If shallow (False)
    it requires cvarname(node) to already be defined so it can be referenced.
    @param init: if True, it assumes that the value of cvarname(node) isn't
    initialized. The init flag is turned off for any internal copy, thus if
    shallow, any internal variable used by node is assumed to be initialized.
    """
    t = types.of(node)
    if shallow and not init:
        yield var + ' = &' + qn.c_varname(node) + ';'
    else:  #Deep copy of this level
        if isinstance(t, str):  #Basic types, always do deep copy.
            v = language.value_mapping[t]['C'](node._val)
            yield var + ' = ' + v + ';'
        elif isinstance(t, StructType):
            for (f_name, _) in t.elems_t:
                v = getattr(node, f_name)
                yield from copy_to(var + '.' + f_name, v, shallow
                                   and t.shallow, False)
        elif isinstance(t, ArrayType):
            vs = node['VALUES']
            for i, v in enumerate(vs):
                yield from copy_to(var + '[' + str(i) + ']', v, False, False)
        elif isinstance(t, EmptyType):
            yield var + ' = 0;'
        else:
            internal_error("Type unsupported.")
Exemplo n.º 6
0
def _bool_convert_C(value):
    if value == 'false':
        return '0'
    elif value == 'true':
        return '1'
    else:
        internal_error("incorrect boolean value")
Exemplo n.º 7
0
 def refresh(self, qname, node):
     """ Reassociating name with a new node. """
     assert (qname.modname() == self.qname)
     try:
         self[qname.name()]  #verify it already exists
     except KeyError:
         internal_error("Refreshing an unknown ident.")
     self.associate(qname, node)
Exemplo n.º 8
0
def of(node):
    """Return the type of a node, assumes typecheck has been done.
    """
    if not _is_typed(node):
        if node is None:
            internal_error("asking for type of None")
        internal_error("Typing hasn't been done on the node {}."
                       "".format(node._qname), node._location)
    return node._type
Exemplo n.º 9
0
def ros_typename(t):
    if isinstance(t, str):
        return t
    elif isinstance(t, ArrayType):
        return '{}[{}]'.format(ros_typename(t.elem_t), t.size)
    elif isinstance(t, StructType):
        return qn_msgtype(infos.ros_type_of_struct[t])
    else:
        internal_error("Unexpected type.")
Exemplo n.º 10
0
def ros_typename(t):
    if isinstance(t, str):
        return t
    elif isinstance(t, ArrayType):
        return '{}[{}]'.format(ros_typename(t.elem_t), t.size)
    elif isinstance(t, StructType):
        return qn_msgtype(infos.ros_type_of_struct[t])
    else:
        internal_error("Unexpected type.")
Exemplo n.º 11
0
def ros_typename(t):
    if isinstance(t, str):
        tname = language.type_mapping[t]['ROS']
        return tname
    elif isinstance(t, ArrayType):
        return '{}[{}]'.format(ros_typename(t.elem_t), t.size)
    elif isinstance(t, StructType):
        return msg_ros_qname(*infos.ros_type_of_struct[t])
    else:
        internal_error("Unexpected type.")
Exemplo n.º 12
0
 def onleaf(visitor, leaf, namespace):
     if isinstance(leaf, parsimonious.nodes.Node):
         if leaf.expr_name != '_qname':
             internal_error("The Ast have parsing node "
                            "{} left over".format(leaf.expr_name))
         try:
             node = namespace.resolve(leaf.text)
         except NonExistingIdent:
             msg = "Undefined identifier {}".format(leaf.text)
             error(msg, loc_of_parsimonious(leaf))
         return Ident(node, loc_of_parsimonious(leaf)), namespace
     else:
         return leaf, namespace
Exemplo n.º 13
0
def user_node_class(node, state_var, in_var, in_f_var, out_var, out_f_var):
    """ The node is searched for a C or CXX class defining the step machine.
    This returns the instructions to initialize the state_var, the
    expression call to do a step and the instructions to finalize the state_var.
    """
    source_type = node_sources_type(node)
    source = node[source_type]
    if source_type == 'C':
        state = source['TYPENAME']
        state = state._val if state else 'RADL_STATE'
        init_fun = source['INIT_FUNCTION']
        init_fun = init_fun._val if init_fun else 'RADL_INIT_FUN'
        step_fun = source['STEP_FUNCTION']
        step_fun = step_fun._val if step_fun else 'RADL_STEP_FUN'
        finish_fun = source['FINISH_FUNCTION']
        finish_fun = finish_fun._val if finish_fun else 'RADL_FINISH_FUN'

        init = "  {t} {v};\n  {i}(&{v});\n".format(t=state,
                                                   v=state_var,
                                                   i=init_fun)
        step = "    {s}(&{v}, {i}, {i_f}, {o}, {o_f});\n".format(s=step_fun,
                                                                 v=state_var,
                                                                 i=in_var,
                                                                 i_f=in_f_var,
                                                                 o=out_var,
                                                                 o_f=out_f_var)
        finish = "  {f}(&{v});\n".format(v=state_var, f=finish_fun)
    elif source_type == 'CXX':
        step_method = source['STEP_METHOD']
        step_method = step_method._val if step_method else 'step'

        init = "  {t} {v};\n".format(t=source['CLASS']._val, v=state_var)
        step = "    {v}.{s}({i}, {i_f}, {o}, {o_f});\n".format(s=step_method,
                                                               v=state_var,
                                                               i=in_var,
                                                               i_f=in_f_var,
                                                               o=out_var,
                                                               o_f=out_f_var)
        finish = ""
    else:
        internal_error("Node with unknown source type.\n" +
                       str(node._location))

    if infos.instrument_step_timings:
        step = (instrumentation.step_timings.print_entering(node._qname) +
                step + instrumentation.step_timings.print_exiting(node._qname))
    step = '    // Calling the step function\n' + step
    return init, step, finish
Exemplo n.º 14
0
 def optarg(var, mapacc_ver, mapred_ver, red_ver, bf_ver):
     if var: return var
     elif kind == 'mapacc': return mapacc_ver
     elif kind == 'mapred': return mapred_ver
     elif kind == 'red': return red_ver
     elif kind == 'bf': return bf_ver
     else: raise internal_error('visitor with wrong kind')
Exemplo n.º 15
0
def _from_cxx(visitor, cxx, d):
    _, d = visitor.node_mapred(cxx, d)
    #give the correct root to relative paths
    pwd = rosutils.user_file_relativepath / cxx._pwd
    d['dirs'].add(pwd)
    for c in cxx['FILENAME']:
        d['sources'].add(pwd / c._val)
    for l in cxx['LIB']:
        if l._kind == 'cmake_library':
            d['libname'] = l['CMAKE_MODULE']._val
        elif l._kind == 'static_library':
            d['libname'] = l._qname.name()
        else:
            internal_error("unknown library type")
        for t in lib_templates: app(d, t)
    return (), d
Exemplo n.º 16
0
def _from_cxx(visitor, cxx, d):
    _, d = visitor.node_mapred(cxx, d)
    #give the correct root to relative paths
    pwd = rosutils.user_file_relativepath / cxx._pwd
    d['dirs'].add(pwd)
    for c in cxx['FILENAME']:
        d['sources'].add(pwd / c._val)
    for l in cxx['LIB']:
        if l._kind == 'cmake_library':
            d['libname'] = l['CMAKE_MODULE']._val
        elif l._kind == 'static_library':
            d['libname'] = l._qname.name()
        else:
            internal_error("unknown library type")
        for t in lib_templates:
            app(d, t)
    return (), d
Exemplo n.º 17
0
def float_fits(value, size):
    d = decimal.Decimal(value)
    if size == 32:
        return d.normalize() == d.normalize(context=context_IEEE_754_float32)
    if size == 64:
        return d.normalize() == d.normalize(context=context_IEEE_754_float64)
    if size == 128:
        return d.normalize() == d.normalize(context=context_IEEE_754_float128)
    raise internal_error("Trying to fit a float of size {}".format(size))
Exemplo n.º 18
0
def float_fits(value, size):
    d = decimal.Decimal(value)
    if size == 32:
        return d.normalize() == d.normalize(context=context_IEEE_754_float32)
    if size == 64:
        return d.normalize() == d.normalize(context=context_IEEE_754_float64)
    if size == 128:
        return d.normalize() == d.normalize(context=context_IEEE_754_float128)
    raise internal_error("Trying to fit a float of size {}".format(size))
Exemplo n.º 19
0
def decl_and_def_type(t, tname, register_type=True):
    """ This function generate the C type declaration and definition of the 
    RADL type t. The typename (typedefed) associated is tname.
    If register_type is True, the type is registered in the global type map
    (infos.c_typedecl) so that the the typename is recoverable from the type.

    It returns tdecl, tdef, decl_deps and def_deps:
    - tdecl is the type declaration, (the typedef), usually to be written in 
    the header file.
    - tdef is the type actual definition. Note that this may be empty,
    for example for array types.
    - decl_deps is an enumeration of other typenames onto which tdecl depends
    - def_deps is a enumeration of other typenames onto which tdef depends.
    """
    if isinstance(t, ArrayType):
        el_t = typedeclof(t.elem_t)
        s = t.size
        tdecl = "typedef {} {}[{}];\n".format(el_t, tname, s)
        decl_deps = (el_t, )
        tdef = ''
        def_deps = ()
    elif isinstance(t, StructType):
        tdecl = "typedef struct {} {};\n".format(tname, tname)
        decl_deps = ()
        tdef = "struct {} {{\n".format(tname)
        def_deps = []
        tdef_template = "  {}* {};\n" if t.shallow else "  {} {};\n"
        for (f_name, f_type) in t.elems_t:
            f_t = typedeclof(f_type)
            def_deps.append(f_t)
            tdef += tdef_template.format(f_t, f_name)
        tdef += '};\n'
    elif isinstance(t, EmptyType):
        tdecl = "typedef int8_t {};\n".format(tname)
        decl_deps = ()
        tdef = ''
        def_deps = ()
    else:
        internal_error("unknown type instance")
    if register_type:
        infos.c_typedecl[t] = tname
    return (tdecl, tdef, decl_deps, def_deps)
Exemplo n.º 20
0
 def m_def(visitor, node, namespace):
     ident = node.children[0][0] if node.children[0] else None
     loc = loc_of_parsimonious(node)
     if isinstance(ident, parsimonious.nodes.Node):
         try:
             qname = namespace.qualify(ident.text)
         except ExistingIdent:
             error("This name is already used.", loc)
     else:  #generate a name since none is given
         qname = namespace.generate("_" + kind)
     thisnamespace = namespace.push(qname)
     childs, _ = visitor.mapacc(node.children, thisnamespace)
     fields = BucketDict(childs[2 if annoted else 1])
     for (fname, mod) in field_specs:
         v = fields.get(fname, False)
         err = lambda m: error(m.format(fname), loc)
         if not mod:
             if v is False:
                 err("field {} is mandatory.")
             elif isinstance(v, list):
                 err("field {} requires one and only one value.")
         elif mod == '?':
             if v is False:
                 fields[fname] = None
             elif len(v) != 1:
                 err("field {} may be given at most one value.")
             else:
                 fields[fname] = v[0]
         elif mod == '*':
             if v is False:
                 fields[fname] = []
         elif mod == '+':
             if v is False:
                 err("field {} requires at least one value.")
         else:
             internal_error("unknown modifier")
     n = AstNode(kind, qname, fields, thisnamespace, loc)
     namespace.associate(qname, n)
     return n, namespace
Exemplo n.º 21
0
def collect_user_files(node_list, src_path=user_src_path):
    """ This function collect the include directories, source files, libraries
    and find library calls to be done to compile the node_list elements.
    """
    include_dirs = []
    source_files = []
    lib_deps = []
    find_libs = []
    for node in node_list:
        for f in node['FILENAME']:
            source_files.append(src_path / wd.of(node) / f._val)
        include_dirs.append(src_path / wd.of(node))
        for l in node['LIB']:
            if l._kind == 'static_library':
                lib_deps.append(qn.cmake_userlib(l))
            elif l._kind == 'cmake_library':
                #RMQ: here we have to do all this mess because add_library(INTERFACE) exists only for cmake v3 not cmake v2...
                # Otherwise, we would not differentiate the library kinds. It would simply be a matter of wrapping things nicely into a library target, see packagen.py.
                n = l['CMAKE_MODULE']._val
                p = src_path / wd.of(l)
                components = listjoin(' ',
                                      (c._val for c in l['CMAKE_COMPONENTS']))
                if l['CMAKE_VAR_LIBRARIES']:
                    lib_deps.append("${{{}}}".format(
                        l['CMAKE_VAR_LIBRARIES']._val))
                else:
                    lib_deps.append("${{{}_LIBRARIES}}".format(n))
                if l['CMAKE_VAR_INCLUDE_DIRS']:
                    include_dirs.append("${{{}}}".format(
                        l['CMAKE_VAR_INCLUDE_DIRS']._val))
                else:
                    include_dirs.append("${{{}_INCLUDE_DIRS}}".format(n))

                find_libs.append(find_lib_template.format(p, n, components))
            else:
                internal_error("unsupported library kind.")
    return source_files, include_dirs, lib_deps, find_libs
Exemplo n.º 22
0
    def __init__(self,
                 instruments_to_use,
                 nodes_to_track,
                 stats_class=StreamStats):
        """
        @argument instruments_to_use is the list of instruments to use.
        @argument nodes_to_track is expected to be a list of node qualified names.
        If the empty list is provided, nodes will be discovered on the fly.
        """
        self.newnodesonthefly = (nodes_to_track is None)
        self.stats_class = stats_class
        self.nodes = OrderedDict()
        self.il = dict()
        # Resolve instrument classes
        try:
            for i in instruments_to_use:
                self.il[i] = instruments[i]
        except AttributeError:
            internal_error("Unknown analyser.")

        # Initialize all the analyzer statically known
        if not self.newnodesonthefly:
            for n in nodes_to_track:
                self.record_node(n)
Exemplo n.º 23
0
 def _val(self):
     """ Used for nodes holding one value as their unique child."""
     if len(self._children) != 1:
         internal_error("Tried to get _val of a node with {} childs"
                        "".format(len(self._children)))
     return self._children[0]
Exemplo n.º 24
0
 def father(self):
     internal_error("A root namespace has no father.")
Exemplo n.º 25
0
 def pop(self):
     internal_error("A root namespace can't be popped.")
Exemplo n.º 26
0
 def __copy__(self):
     internal_error("Trying to copy an Ident.")
Exemplo n.º 27
0
 def __deepcopy__(self, d):
     internal_error("Trying to deepcopy an Ident.")
Exemplo n.º 28
0
 def modname(self):
     internal_error("rootname doesn't have modname")
Exemplo n.º 29
0
 def _val(self):
     """ Used for nodes holding one value as their unique child."""
     if len(self._children) != 1:
         internal_error("Tried to get _val of a node with {} childs"
                        "".format(len(self._children)))
     return self._children[0]
Exemplo n.º 30
0
def qn_file(qname, prefix='', suffix=''):
    p = list(qname.pathwalk())
    if len(p) < 2:
        internal_error("trying to use a qn with size < 2 for file gen.")
    return ws_rospath('/'.join(p[0:-1]) + '/src/' + prefix + p[-1] + suffix)
Exemplo n.º 31
0
def node_package(qname):
    p = list(qname.pathwalk())
    if len(p) != 2:
        internal_error("trying to use a qn with size != 2 for package.")
    return p[0]
Exemplo n.º 32
0
    def __deepcopy__(self, d): internal_error("Trying to deepcopy an Ident.")

class RootNamespace(Namespace):
Exemplo n.º 33
0
 def refresh(self, qname, node):
     """ Reassociating name with a new node. """
     assert(qname.modname() == self.qname)
     try: self[qname.name()] #verify it already exists
     except KeyError: internal_error("Refreshing an unknown ident.")
     self.associate(qname, node)
Exemplo n.º 34
0
 def __copy__(self):
     internal_error("Trying to copy an Ident.")
Exemplo n.º 35
0
 def tuple_mapacc(self, t, acc):
     """ mapacc self.visit on t"""
     if self.inplace:
         internal_error("Trying to modify in place a tuple.")
     return mapacc(self.visit, t, acc, False)
Exemplo n.º 36
0
 def modname(self): internal_error("rootname doesn't have modname")
 def qname(self, sep='', root=None): return root if root else ''
Exemplo n.º 37
0
 def pop(self): internal_error("A root namespace can't be popped.")
Exemplo n.º 38
0
 def father(self): internal_error("A root namespace has no father.")
 def pop(self): internal_error("A root namespace can't be popped.")
Exemplo n.º 39
0
 def __copy__(self): internal_error("Trying to copy an Ident.")
 def __deepcopy__(self, d): internal_error("Trying to deepcopy an Ident.")
Exemplo n.º 40
0
 def tuple_mapred(self, t, acc):
     """ imapred self.visit on t"""
     if self.inplace:
         internal_error("Trying to modify in place a tuple.")
     return imapred(self.visit, t, acc)