Commit 974fc2ec authored by Thanassis Tsiodras's avatar Thanassis Tsiodras

Abolish global state that was marking types to skip over.

parent 7fbc9b49
......@@ -37,7 +37,7 @@ def Version():
# the second param is not asnFile, it is asnFiles
def OnStartup(unused_modelingLanguage, asnFiles, outputDir):
def OnStartup(unused_modelingLanguage, asnFiles, outputDir, unused_badTypes):
#print "Use ASN1SCC to generate the structures for '%s'" % asnFile
asn1SccPath = spawn.find_executable('asn1.exe')
if not asn1SccPath:
......@@ -77,5 +77,5 @@ def OnChoice(nodeTypename, node, leafTypeDict):
pass # pragma: no cover
def OnShutdown():
def OnShutdown(unused_badTypes):
pass # pragma: no cover
......@@ -36,7 +36,7 @@ def Version():
# Especially for the C mapper, since we need to pass the complete ASN.1 files list to ASN1SCC,
# the second param is not asnFile, it is asnFiles
def OnStartup(unused_modelingLanguage, asnFiles, outputDir):
def OnStartup(unused_modelingLanguage, asnFiles, outputDir, unused_badTypes):
#print "Use ASN1SCC to generate the structures for '%s'" % asnFile
asn1SccPath = spawn.find_executable('asn1.exe')
if not asn1SccPath:
......@@ -81,5 +81,5 @@ def OnChoice(nodeTypename, node, leafTypeDict):
pass # pragma: no cover
def OnShutdown():
def OnShutdown(unused_badTypes):
pass # pragma: no cover
......@@ -187,7 +187,7 @@ def main():
verify.VerifyRanges(node, commonPy.asnParser.g_names)
# If some AST nodes must be skipped (for any reason), go learn about them
commonPy.cleanupNodes.DiscoverBadTypes()
badTypes = commonPy.cleanupNodes.DiscoverBadTypes()
C_HeaderFile = open(commonPy.configMT.outputDir + os.sep + "PrintTypes.h", "w")
C_HeaderFile.write('#ifndef __PRINTTYPES_H__\n')
......@@ -217,7 +217,7 @@ def main():
for nodeTypename in names:
# Check if this type must be skipped
if commonPy.cleanupNodes.IsBadType(nodeTypename):
if nodeTypename in badTypes:
continue
node = names[nodeTypename]
if node._isArtificial:
......
......@@ -206,7 +206,7 @@ def main():
verify.VerifyRanges(node, commonPy.asnParser.g_names)
# If some AST nodes must be skipped (for any reason), go learn about them
commonPy.cleanupNodes.DiscoverBadTypes()
badTypes = commonPy.cleanupNodes.DiscoverBadTypes()
C_HeaderFile = open(commonPy.configMT.outputDir + os.sep + "PrintTypesAsASN1.h", "w")
C_HeaderFile.write('#ifndef __PRINTTYPESASASN1_H__\n')
......@@ -236,7 +236,7 @@ def main():
for nodeTypename in names:
# Check if this type must be skipped
if commonPy.cleanupNodes.IsBadType(nodeTypename):
if nodeTypename in badTypes:
continue
node = names[nodeTypename]
if node._isArtificial:
......
......@@ -31,7 +31,7 @@ def Version():
print("Code generator: " + "$Id: og_A_mapper.py 2382 2012-06-22 08:35:33Z ttsiodras $") # pragma: no cover
def OnStartup(unused_modelingLanguage, asnFile, outputDir):
def OnStartup(unused_modelingLanguage, asnFile, outputDir, unused_badTypes):
global g_asnFile
g_asnFile = asnFile
global g_outputDir
......@@ -82,7 +82,7 @@ def OnChoice(nodeTypename, node, leafTypeDict):
# return outputText
def OnShutdown():
def OnShutdown(unused_badTypes):
# text = open(g_asnFile, 'r').read()
# text = re.sub(r'^.*BEGIN', 'Datamodel DEFINITIONS ::= BEGIN', text)
# text = re.sub(r'--.*', '', text)
......
......@@ -48,7 +48,7 @@ def CleanNameAsPythonWants(name):
return re.sub(r'[^a-zA-Z0-9_]', '_', name)
def OnStartup(unused_modelingLanguage, asnFile, outputDir):
def OnStartup(unused_modelingLanguage, asnFile, outputDir, badTypes):
os.system("bash -c '[ ! -f \"" + outputDir + "/" + asnFile + "\" ] && cp \"" + asnFile + "\" \"" + outputDir + "\"'")
this_path = os.path.dirname(__file__)
stubs = this_path + os.sep + 'Stubs.py'
......@@ -172,7 +172,7 @@ clean:
%(tab)srm -f $(BDIR)/$(GRAMMAR)_asn.py
''' % {'tab': '\t', 'base': base, 'origGrammarBase': origGrammarBase, 'mono': mono_exe})
Makefile.close()
CreateDeclarationsForAllTypes(commonPy.asnParser.g_names, commonPy.asnParser.g_leafTypeDict)
CreateDeclarationsForAllTypes(commonPy.asnParser.g_names, commonPy.asnParser.g_leafTypeDict, badTypes)
g_outputGetSetH.write('\n/* Helper functions for NATIVE encodings */\n\n')
g_outputGetSetC.write('\n/* Helper functions for NATIVE encodings */\n\n')
......@@ -249,7 +249,7 @@ def OnChoice(unused_nodeTypename, unused_node, unused_leafTypeDict):
pass
def OnShutdown():
def OnShutdown(unused_badTypes):
pass
......@@ -497,7 +497,7 @@ def CreateDeclarationForType(nodeTypename, names, leafTypeDict):
panic("Unexpected ASN.1 type... Send this grammar to Semantix") # pragma: no cover
def CreateDeclarationsForAllTypes(names, leafTypeDict):
def CreateDeclarationsForAllTypes(names, leafTypeDict, badTypes):
for nodeTypename in names:
if not names[nodeTypename]._isArtificial and not commonPy.cleanupNodes.IsBadType(nodeTypename):
if not names[nodeTypename]._isArtificial and nodeTypename not in badTypes):
CreateDeclarationForType(nodeTypename, names, leafTypeDict)
......@@ -57,7 +57,7 @@ def CleanNameAsSimulinkWants(name):
# Especially for the C mapper, since we need to pass the complete ASN.1 files list to ASN1SCC,
# the second param is not asnFile, it is asnFiles
def OnStartup(unused_modelingLanguage, asnFiles, outputDir):
def OnStartup(unused_modelingLanguage, asnFiles, outputDir, unused_badTypes):
#print "Use ASN1SCC to generate the structures for '%s'" % asnFile
asn1SccPath = spawn.find_executable('asn1.exe')
if not asn1SccPath:
......@@ -117,7 +117,7 @@ def OnChoice(nodeTypename, node, leafTypeDict):
pass # pragma: nocover
def OnShutdown():
def OnShutdown(unused_badTypes):
pass
......
......@@ -47,7 +47,7 @@ def CleanNameAsSimulinkWants(name):
return re.sub(r'[^a-zA-Z0-9_]', '_', name)
def OnStartup(unused_modelingLanguage, unused_asnFile, outputDir):
def OnStartup(unused_modelingLanguage, unused_asnFile, outputDir, unused_badTypes):
global g_bHasStartupRunOnce
if g_bHasStartupRunOnce:
# Don't rerun, it has already done all the work
......@@ -96,7 +96,7 @@ def OnChoice(nodeTypename, node, leafTypeDict):
pass # pragma: no cover
def OnShutdown():
def OnShutdown(unused_badTypes):
pass
......
......@@ -31,7 +31,7 @@ def Version():
print("Code generator: " + "$Id: og_A_mapper.py 1879 2010-05-17 10:13:12Z ttsiodras $") # pragma: no cover
def OnStartup(unused_modelingLanguage, asnFile, outputDir):
def OnStartup(unused_modelingLanguage, asnFile, outputDir, unused_badTypes):
global g_asnFile
g_asnFile = asnFile
global g_outputDir
......@@ -82,7 +82,7 @@ def OnChoice(nodeTypename, node, leafTypeDict):
# outputText += c
# return outputText
def OnShutdown():
def OnShutdown(unused_badTypes):
# text = open(g_asnFile, 'r').read()
# text = re.sub(r'^.*BEGIN', 'Datamodel DEFINITIONS ::= BEGIN', text)
# text = re.sub(r'--.*', '', text)
......
......@@ -96,7 +96,7 @@ def FixupNestedStringsAndEnumerated():
node._containedType = newName # pragma: no cover
def OnStartup(unused_modelingLanguage, asnFile, outputDir):
def OnStartup(unused_modelingLanguage, asnFile, outputDir, unused_badTypes):
outputFilename = CleanNameAsScadeWants(os.path.basename(os.path.splitext(asnFile)[0])) + ".xscade"
FixupNestedStringsAndEnumerated()
......@@ -338,5 +338,5 @@ def OnChoice(nodeTypename, node, leafTypeDict):
OnSequence(nodeTypename, node, leafTypeDict, isChoice=True)
def OnShutdown():
def OnShutdown(unused_badTypes):
g_outputFile.write(g_doc.toprettyxml(indent=" ", encoding="UTF-8"))
......@@ -43,7 +43,7 @@ def CleanNameAsSimulinkWants(name):
return re.sub(r'[^a-zA-Z0-9_]', '_', name)
def OnStartup(unused_modelingLanguage, unused_asnFile, outputDir):
def OnStartup(unused_modelingLanguage, unused_asnFile, outputDir, unused_badTypes):
global g_bHasStartupRunOnce
if g_bHasStartupRunOnce:
# Don't rerun, it has already done all the work
......@@ -92,7 +92,7 @@ def OnChoice(nodeTypename, node, leafTypeDict):
pass
def OnShutdown():
def OnShutdown(unused_badTypes):
pass
......
......@@ -28,7 +28,6 @@ import random
from commonPy.asnAST import AsnMetaMember, AsnChoice, AsnSet, AsnSequence, AsnSequenceOf, AsnSetOf
from commonPy.asnParser import g_names, g_leafTypeDict, CleanNameForAST
from commonPy.utility import panic, warn
from commonPy.cleanupNodes import IsBadType
g_catalogueXML = None
g_innerTypes = {}
......@@ -107,7 +106,7 @@ def FixupAstForSMP2():
g_bStartupRun = False
def OnStartup(unused_modelingLanguage, asnFiles, outputDir):
def OnStartup(unused_modelingLanguage, asnFiles, outputDir, unused_badTypes):
'''
Smp2 cannot represent constraint changes in unnamed inner types
e.g. this...
......@@ -291,7 +290,7 @@ def CreateChoice(nodeTypename, node, _):
g_bShutdownRun = False
def OnShutdown():
def OnShutdown(badTypes):
global g_bShutdownRun
if g_bShutdownRun:
return # pragma: no cover
......@@ -310,7 +309,7 @@ def OnShutdown():
(g_asnFiles if isinstance(g_asnFiles, str) else '","'.join(g_asnFiles)))
typenameList = []
for nodeTypename in sorted(list(g_innerTypes.keys()) + list(g_names.keys())):
if IsBadType(nodeTypename):
if nodeTypename in badTypes:
continue
if nodeTypename not in typenameList:
typenameList.append(nodeTypename)
......
......@@ -28,7 +28,6 @@ from commonPy.asnAST import (
AsnMetaMember, AsnChoice, AsnSet, AsnSequence, AsnSequenceOf, AsnSetOf)
from commonPy.asnParser import g_names, g_leafTypeDict, CleanNameForAST
from commonPy.utility import panic, warn
from commonPy.cleanupNodes import IsBadType
g_sqlOutput = None
g_innerTypes = {}
......@@ -136,7 +135,7 @@ def FixupAstForSQL():
g_bStartupRun = False
def OnStartup(unused_modelingLanguage, asnFiles, outputDir):
def OnStartup(unused_modelingLanguage, asnFiles, outputDir, unused_badTypes):
'''
SQL cannot represent unnamed inner types
e.g. this...
......@@ -278,7 +277,7 @@ def CreateChoice(nodeTypename, node, _):
g_bShutdownRun = False
def OnShutdown():
def OnShutdown(badTypes):
global g_bShutdownRun
if g_bShutdownRun:
return # pragma: no cover
......@@ -291,7 +290,7 @@ def OnShutdown():
g_sqlOutput.write('-- SQL statements for types used in "%s"\n' % d)
typenameList = []
for nodeTypename in sorted(list(g_innerTypes.keys()) + list(g_names.keys())):
if IsBadType(nodeTypename):
if nodeTypename in badTypes:
continue
if nodeTypename not in typenameList:
typenameList.append(nodeTypename)
......
......@@ -29,7 +29,6 @@ from commonPy.asnAST import (
AsnSetOf, isSequenceVariable)
from commonPy.asnParser import g_names, g_leafTypeDict, CleanNameForAST
from commonPy.utility import panic, warn
from commonPy.cleanupNodes import IsBadType
g_sqlalchemyOutput = None
g_innerTypes = {}
......@@ -137,7 +136,7 @@ def FixupAstForSQLAlchemy():
g_bStartupRun = False
def OnStartup(unused_modelingLanguage, asnFiles, outputDir):
def OnStartup(unused_modelingLanguage, asnFiles, outputDir, unused_badTypes):
'''
SQL cannot represent unnamed inner types
e.g. this...
......@@ -516,7 +515,7 @@ def CreateChoice(nodeTypename, node, _):
g_bShutdownRun = False
def OnShutdown():
def OnShutdown(badTypes):
global g_bShutdownRun
if g_bShutdownRun:
return # pragma: no cover
......@@ -528,7 +527,7 @@ def OnShutdown():
d = g_asnFiles if isinstance(g_asnFiles, str) else '","'.join(g_asnFiles)
typenameList = []
for nodeTypename in sorted(list(g_innerTypes.keys()) + list(g_names.keys())):
if IsBadType(nodeTypename):
if nodeTypename in badTypes:
continue
if nodeTypename not in typenameList:
typenameList.append(nodeTypename)
......
......@@ -346,7 +346,7 @@ def main():
node._asnSize = param._signal._asnSize
# If some AST nodes must be skipped (for any reason), go learn about them
commonPy.cleanupNodes.DiscoverBadTypes()
badTypes = commonPy.cleanupNodes.DiscoverBadTypes()
if {"ada", "qgenada"} & {y[2].lower() for y in SystemsAndImplementations}:
SpecialCodes(SystemsAndImplementations, uniqueDataFiles, uniqueASNfiles, useOSS)
......@@ -417,7 +417,7 @@ def main():
inform("This param uses definitions from %s", asnFile)
for nodeTypename in names:
# Check if this type must be skipped
if commonPy.cleanupNodes.IsBadType(nodeTypename):
if nodeTypename in badTypes:
continue
# Async backends need to collect all types and create Encode/Decode functions for them.
......
......@@ -146,7 +146,7 @@ def main():
loadedBackends = {}
# If some AST nodes must be skipped (for any reason), go learn about them
commonPy.cleanupNodes.DiscoverBadTypes()
badTypes = commonPy.cleanupNodes.DiscoverBadTypes()
# For each ASN.1 grammar file referenced in the system level description
for arg, modelingLanguage in argsToTools.items():
......@@ -166,14 +166,14 @@ def main():
# instead of working per type:
if modelingLanguage.lower() in ["c", "ada", "smp2", "qgenc", "qgenada"]:
if 'OnStartup' in dir(backend):
backend.OnStartup(modelingLanguage, list(uniqueASNfiles.keys()), commonPy.configMT.outputDir)
backend.OnStartup(modelingLanguage, list(uniqueASNfiles.keys()), commonPy.configMT.outputDir, badTypes)
if 'OnShutdown' in dir(backend):
backend.OnShutdown()
else:
# Work on each ASN.1 file's types
for asnFile in uniqueASNfiles:
if 'OnStartup' in dir(backend):
backend.OnStartup(modelingLanguage, asnFile, commonPy.configMT.outputDir)
backend.OnStartup(modelingLanguage, asnFile, commonPy.configMT.outputDir, badTypes)
leafTypeDict = uniqueASNfiles[asnFile][2]
......@@ -181,7 +181,7 @@ def main():
names = uniqueASNfiles[asnFile][0]
for nodeTypename in names:
# Check if this type must be skipped
if commonPy.cleanupNodes.IsBadType(nodeTypename):
if nodeTypename in badTypes:
continue
node = names[nodeTypename]
inform("Processing %s (%s)...", nodeTypename, modelingLanguage)
......@@ -216,7 +216,7 @@ def main():
panic("Unexpected type of element: %s" % leafTypeDict[nodeTypename]) # pragma: no cover
if 'OnShutdown' in dir(backend):
backend.OnShutdown()
backend.OnShutdown(badTypes)
if __name__ == "__main__":
if "-pdb" in sys.argv:
......
......@@ -49,42 +49,48 @@ from commonPy.asnAST import (
AsnMetaMember, AsnSetOf, AsnNode
)
badTypes = {} # type: Dict[str, bool]
cache = {} # type: Dict[AsnNode, bool]
def DiscoverBadTypes() -> Dict[str, bool]:
'''
This returns a dictionary that tells us which types to skip
pver during type mappings. For now, it includes IA5Strings
and types whose descendants end up having such a field.
'''
badTypes = {} # type: Dict[str, bool]
cache = {} # type: Dict[AsnNode, bool]
def CheckNodeForIA5(node_or_str: Union[AsnNode,str]) -> bool:
names = commonPy.asnParser.g_names
if isinstance(node_or_str, str):
node = names[node_or_str] # type: AsnNode
else:
node = node_or_str
if node in cache:
return cache[node]
if isinstance(node, AsnAsciiString):
cache[node] = True
return True
elif isinstance(node, (AsnChoice, AsnSequence, AsnSet)):
for child in node._members:
if isinstance(child[1], AsnAsciiString):
cache[node] = True
return cache[node]
cache[node] = any(
CheckNodeForIA5(names[child[1]._containedType])
for child in node._members
if isinstance(child[1], AsnMetaMember))
return cache[node]
elif isinstance(node, (AsnSequenceOf, AsnSetOf)):
if isinstance(node._containedType, AsnAsciiString):
def CheckNodeForIA5(node_or_str: Union[AsnNode, str]) -> bool:
names = commonPy.asnParser.g_names
if isinstance(node_or_str, str):
node = names[node_or_str] # type: AsnNode
else:
node = node_or_str
if node in cache:
return cache[node]
if isinstance(node, AsnAsciiString):
cache[node] = True
return True
cache[node] = isinstance(node._containedType, str) and CheckNodeForIA5(names[node._containedType])
elif isinstance(node, (AsnChoice, AsnSequence, AsnSet)):
for child in node._members:
if isinstance(child[1], AsnAsciiString):
cache[node] = True
return cache[node]
cache[node] = any(
CheckNodeForIA5(names[child[1]._containedType])
for child in node._members
if isinstance(child[1], AsnMetaMember))
return cache[node]
elif isinstance(node, (AsnSequenceOf, AsnSetOf)):
if isinstance(node._containedType, AsnAsciiString):
cache[node] = True
return True
cache[node] = \
isinstance(node._containedType, str) \
and \
CheckNodeForIA5(names[node._containedType])
return cache[node]
cache[node] = False
return cache[node]
cache[node] = False
return cache[node]
def DiscoverBadTypes():
# Hack for IA5Strings (IA5s are used in TASTE's runtime configuration spec)
names = commonPy.asnParser.g_names
while True:
......@@ -96,7 +102,4 @@ def DiscoverBadTypes():
foundOne = True
if not foundOne:
break
def IsBadType(nodeTypename: str) -> bool:
return nodeTypename in badTypes
return badTypes
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