Commit 71e0e3b1 authored by Maxime Perrotin's avatar Maxime Perrotin

Start supporting choice determinant as local type

parent 1c512a25
......@@ -2175,7 +2175,9 @@ def _enumerated_value(primary, **kwargs):
for each in basic.EnumValues:
if each.lower() == enumerant:
break
prefix = type_name(basic)
# no "asn1Scc" prefix if the enumerated is a choice selector
use_prefix = getattr(basic.EnumValues[each], "IsStandardEnum", True)
prefix = type_name(basic, use_prefix=use_prefix)
ada_string = (prefix + basic.EnumValues[each].EnumID)
return [], unicode(ada_string), []
......@@ -2965,7 +2967,7 @@ def type_name(a_type, use_prefix=True):
elif a_type.kind == 'StateEnumeratedType':
return u''
elif a_type.kind == 'EnumeratedType':
return u'asn1Scc'
return u'asn1Scc' if use_prefix else ''
else:
raise NotImplementedError('Type name for {}'.format(a_type.kind))
......
......@@ -4,7 +4,7 @@
"""
Python API for the ASN1Scc compiler
Copyright (c) 2013-2018 European Space Agency
Copyright (c) 2013-2019 European Space Agency
Designed and implemented by Maxime Perrotin
......@@ -45,7 +45,7 @@ except ImportError:
raise ImportError('Enum module not found. Run pip install --user enum34')
__all__ = ['ASN1', 'parse_asn1']
__all__ = ['ASN1', 'parse_asn1', 'create_choice_determinant_types']
class ASN1(Enum):
......@@ -178,6 +178,41 @@ def parse_asn1(*files, **options):
ast.html = html_filepath
return ast
def create_choice_determinant_types(ast):
''' Postprocess the AST to add extra types corresponding to the CHOICE
determinants. This allows the user to declare variables of these types,
for local storage and comparison purposes.
input: ast is the module generate by asn1scc. ast.types are the types
returns the newly created types (does not modify input AST)
'''
new_sorts = {}
for each in (sort for sort in ast.types.viewvalues()
if sort.type.kind == 'ChoiceType'):
new_sort = each.__name__ + '-selection'
if new_sort in ast.types:
continue
choices = {key : type (key, (object,), {
"IntValue": 0,
"Line": each.Line,
"CharPositionInLine": each.CharPositionInLine,
"EnumID": each.type.Children[key].EnumID,
"IsStandardEnum" : False
}) for key in each.type.Children.keys()
}
new_sorts[new_sort] = \
type(new_sort, (object,), {
"Line": each.Line,
"CharPositionInLine": each.CharPositionInLine,
"type": type(new_sort + "_type", (object,), {
"Line" : each.Line,
"CharPositionInLine": each.CharPositionInLine,
"kind": "EnumeratedType",
"Extensible": "False",
"ValuesAutoCalculated": "False",
"EnumValues": choices
})
})
return new_sorts
def asn2dataModel(*files):
''' Call asn1DataModel, including the Makefile.python and return
......
......@@ -43,7 +43,7 @@ from sdl92Parser import sdl92Parser
import samnmax
import ogAST
from Asn1scc import parse_asn1, ASN1
from Asn1scc import parse_asn1, ASN1, create_choice_determinant_types
LOG = logging.getLogger(__name__)
......@@ -168,6 +168,9 @@ def set_global_DV(asn1_filenames):
rename_policy=rename_policy,
flags=[ASN1.AstOnly],
pretty_print=True)
# Create new types corresponding to CHOICE determinants as enum
choice_selectors = create_choice_determinant_types (DV)
DV.types.update(choice_selectors)
except (ImportError, NameError) as err:
# Can happen if DataView.py is not there
LOG.error('Error loading ASN.1 model')
......@@ -2771,7 +2774,7 @@ def syntype(root, ta_ast, context):
"kind": reftype + "Type"
})
DV.types[str(newtypename)] = newtype
types[str(newtypename)] = newtype
LOG.debug("Found new SYNTYPE " + newtypename)
return errors, warnings
......@@ -2791,19 +2794,19 @@ def newtype(root, ta_ast, context):
if len(root.children) < 2:
warnings.append('Use newtype definitions for arrays and records only')
newtype.kind = "BooleanType"
DV.types[str(newtypename)] = newtype
types[str(newtypename)] = newtype
LOG.debug("Boolean newtype " + newtypename)
elif (root.getChild(1).type == lexer.ARRAY):
newtype.kind = "SequenceOfType"
newtype.type = get_array_type(root.getChild(1))
newtype.Min = "Min"
newtype.Max = "Max"
DV.types[str(newtypename)] = newtype
types[str(newtypename)] = newtype
LOG.debug("Found new ARRAY type " + newtypename)
elif (root.getChild(1).type == lexer.STRUCT):
newtype.kind = "SequenceType"
newtype.Children = get_struct_children(root.getChild(1))
DV.types[str(newtypename)] = newtype
types[str(newtypename)] = newtype
LOG.debug("Found new STRUCT type " + newtypename)
else:
warnings.append(
......
......@@ -17,7 +17,7 @@ clean:
examiner bin *.wrn *.gpr *.ll *.s dataview-uniq.c dataview-uniq.h \
real.c xer.c ber.c acn.c asn1crt.c asn1crt.h test_ada test_llvm \
*.autosave *_simu.sh *_interface.aadl *.lst *.gcno *.gcda *.gcov \
check
check obj src
%.o: %.pr FORCE
$(OPENGEODE) $< system_structure.pr --llvm -O$(O)
......
......@@ -26,7 +26,7 @@ test-qgen-gt-c:
$(TESTQGEN_GT_C) $(ROOT_MODEL)
test-ada:
mkdir -p src && cd src && ../$(OPENGEODE) ../toto.pr --toAda && \
mkdir -p src && cd src && ../$(OPENGEODE) ../toto.pr -g --toAda && \
mono $(ASN1SCC) -Ada -typePrefix asn1Scc -equal ../dataview.asn
mkdir -p obj && cd obj && gnat make -O$(O) ../src/toto.adb
......
......@@ -19,6 +19,8 @@ system toto;
-- and choice determinants
dcl val MyChoice, det MyEnum;
dcl choice_selectors MyChoice_selection := foo;
/* CIF ENDTEXT */
/* CIF START (240, 89), (70, 35) */
START;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment