def spec(self, variable, name): assertType(name, 'name', str, True) assertType(variable, 'variable', Variable) alias = variable.getAlias(name) if self.__intent is not None: alias.setIntent(self.__intent) if self.__allocatable is not None: if self.__allocatable: if (alias.getDimension() > 0 or alias.hasClassType()) and not alias.isPointer(): alias.setAllocatable(True) else: alias.setAllocatable(False) if self.__pointer is not None: if self.__pointer: alias.setPointer(True) alias.setAllocatable(False) else: alias.setPointer(False) if self.__optional is not None and alias.isArgument(): alias.setOptional(self.__optional) if self.__charLengthZero and alias.hasBuiltInType( ) and alias.getTypeName().startswith('CHARACTER'): alias.setTypeName('CHARACTER(len=0)') alias.setTarget(False) return str(alias)
def containerNeedsAllocation(self, variable): assertType(variable, 'variable', UsedVariable) for level in variable.levels(True): container = variable.container(level) if self.needsAllocation(container): return True return False
def __init__(self, subroutine): assertType(subroutine, 'subroutine', Subroutine) self.name = subroutine.getSimpleName().lower() self.export = '' if self.name not in subroutine.getModule().getPublicElements(): self.export = 'PUBLIC :: ' + self.name
def __init__(self, reference, dim, *indices): assertType(reference, 'reference', VariableReference) assertType(dim, 'dim', int) assertTypeAll(indices, 'indices', str) super(FilledVariable, self).__init__(reference) self.__dim = dim self.__indices = indices
def needsAllocationFilled(self, variable, dim, *indices): assertType(variable, 'variable', UsedVariable) assertType(dim, 'dim', int) assertTypeAll(indices, 'indices', str) filled = self.fillIndices(variable, dim, *indices) return (variable.allocatableOrPointer() or variable.hasClassType()) and not self.alreadyAllocated(filled)
def containerNeedsRegistration(self, variable): assertType(variable, 'variable', UsedVariable) for level in variable.levels(True): if not self.alreadyRegistered(variable.container(level)): return True return False
def ubound(self, variable, dim, *placeholder): '''DEPRECATED: work with fillIndices instead''' assertType(variable, 'variable', UsedVariable, True) assertType(dim, 'dim', int) assertTypeAll(placeholder, 'placeholder', str) bound = self.__bound(variable, dim, placeholder) if bound != '': return 'U' + bound return ''
def _setTypesToSubroutineVariables(self, subroutine, types): assertType(subroutine, 'subroutine', Subroutine) assertType(types, 'types', TypeCollection) for variable in subroutine.getVariables(): if variable.hasDerivedType() and not variable.isTypeAvailable(): typE = types.getType(variable.getDerivedTypeName(), subroutine.getModule()) if typE is not None: variable.setType(typE) else: printWarning("Type " + variable.getDerivedTypeName() + " of variable " + variable.getName() + " not found.", location = "CodeGenerator")
def __init__(self, subroutine, typeArgumentReferences): assertType(subroutine, 'subroutine', Subroutine) assertTypeAll(typeArgumentReferences, 'typeArgumentReferences', VariableReference) self.input = ArgumentsSubNameSpace(subroutine.getInArguments(), typeArgumentReferences) self.output = ArgumentsSubNameSpace(subroutine.getOutArguments(), typeArgumentReferences) self.all = ArgumentsSubNameSpace(subroutine.getArguments(), typeArgumentReferences)
def generate(self, subroutineFullName): assertType(subroutineFullName, 'subroutineFullName', SubroutineFullName) if self._settings.ignorePrefix != '': self._settings.ignoreSubroutinesRegex = re.compile('^' + self._settings.ignorePrefix + '.*$') else: self._settings.ignoreSubroutinesRegex = None subroutine = self._findSubroutine(subroutineFullName) if subroutine is None: raise LookupError("Subroutine not found: " + str(subroutineFullName)) if self._settings.measureTime: self.__initTime() printLine('Build Call Graph', indent = 1) callgraph = self.__graphBuilder.buildCallGraph(subroutineFullName, self._settings.clearCache) if self._settings.measureTime: self.__time(2) printLine('Find Interfaces and Types', indent = 1) useTraversal = UseTraversal(self._sourceFiles, self._settings.excludeModules, self._settings.abstractTypes) useTraversal.parseModules(callgraph.getRoot()) interfaces = useTraversal.getInterfaces() types = useTraversal.getTypes() if self._settings.measureTime: self.__time(2) printLine('Analyse Source Code', indent = 1) argumentTracker = VariableTracker(self._sourceFiles, self._settings, interfaces, types, callGraphBuilder = self.__graphBuilder) printLine('Find References to Type Argument Members', indent = 2) typeArgumentReferences = argumentTracker.trackDerivedTypeArguments(callgraph) subroutine = self._findSubroutine(subroutineFullName) if subroutine.isFunction() and subroutine.getResultVariable().hasDerivedType(): printLine('Find References to Type Result', indent = 2) typeResultReferences = argumentTracker.trackDerivedTypeResult(callgraph) else: typeResultReferences = [] printLine('Find References to Global Variables', indent = 2) globalsTracker = GlobalVariableTracker(self._sourceFiles, self._settings, interfaces, types, callGraphBuilder = self.__graphBuilder) globalsReferences = globalsTracker.trackGlobalVariables(callgraph) sourceFile = subroutine.getSourceFile() sourceFilePath = sourceFile.getPath() if not os.path.isfile(sourceFile.getPath()): raise IOError("File not found: " + sourceFilePath); if self._settings.measureTime: self.__time(2) self.addCode(subroutineFullName, typeArgumentReferences, typeResultReferences, globalsReferences, callgraph, types) if self._settings.measureTime: self.__time(2, True, 1)
def __init__(self, sourceDirs, backupSuffix): assertType(backupSuffix, 'backupSuffix', str) if isinstance(sourceDirs, str): sourceDirs = [sourceDirs] assertTypeAll(sourceDirs, 'sourceDirs', str) for baseDir in sourceDirs: if not os.path.isdir(baseDir): raise IOError("Not a directory: " + baseDir) self.__sourceDirs = sourceDirs self.__backupSuffix = '.' + backupSuffix.lstrip('.')
def extendSpecialModuleFiles(self, specialModuleFiles): assertType(specialModuleFiles, 'specialModuleFiles', dict) backupFiles = self.find() for module, filE in specialModuleFiles.items(): backupFile = self.__getBackupFile(filE) if backupFile in backupFiles: specialModuleFiles[module] = backupFile backupFiles.remove(backupFile) for backupFile in backupFiles: specialModuleFiles[self.__getModuleName(backupFile)] = backupFile[( backupFile.rfind('/') + 1):] return specialModuleFiles
def __init__(self, currentModule, typeArgumentReferences, typeResultReferences, globalsReferences, subroutine, callgraph, postProcessor): assertType(currentModule, 'currentModule', Module) assertTypeAll(typeArgumentReferences, 'typeArgumentReferences', VariableReference) assertTypeAll(typeResultReferences, 'typeResultReferences', VariableReference) assertTypeAll(globalsReferences, 'globalsReferences', VariableReference) assertType(subroutine, 'subroutine', Subroutine) assertType(callgraph, 'callgraph', CallGraph) assertType(postProcessor, 'postProcessor', CodePostProcessor) self._postProcessor = postProcessor self.module = ModuleNameSpace(currentModule.getName(), callgraph) self.globals = ExportGlobalsNameSpace(currentModule, globalsReferences) self.types = ExportTypesNameSpace(currentModule, typeArgumentReferences, typeResultReferences, globalsReferences, subroutine) self.subroutine = SubroutineNameSpace(subroutine, None, None, callgraph) self.clearLine = CodePostProcessor.CLEAR_LINE
def generate(self, subroutineFullName): assertType(subroutineFullName, 'subroutineFullName', SubroutineFullName) if self.__ignorePrefix != '': ignoreRegex = re.compile('^' + self.__ignorePrefix + subroutineFullName.getSimpleName() + '_.*$') else: ignoreRegex = None print " Build Call Graph" callGraph = self.__graphBuilder.buildCallGraph(subroutineFullName) print " Find Interfaces" useTraversal = UseTraversal(self._sourceFiles, self.__excludeModules) useTraversal.parseModules(callGraph.getRoot()) interfaces = useTraversal.getInterfaces() types = useTraversal.getTypes() print " Analyse Source Code" print " Find References to Type Argument Members" argumentTracker = VariableTracker(self._sourceFiles, self.__excludeModules, self.__ignoredTypes, interfaces, types) argumentTracker.setIgnoreRegex(ignoreRegex) typeArgumentReferences = argumentTracker.trackDerivedTypeArguments( callGraph) print " Find References to Global Variables" globalsTracker = GlobalVariableTracker(self._sourceFiles, self.__excludeModules, self.__ignoredModulesForGlobals, self.__ignoredTypes, interfaces, types) globalsTracker.setIgnoreRegex(ignoreRegex) globalsReferences = globalsTracker.trackGlobalVariables(callGraph) subroutine = self._findSubroutine(subroutineFullName) if subroutine is None: raise LookupError("Subroutine not found: " + str(subroutineFullName)) sourceFile = subroutine.getSourceFile() sourceFilePath = sourceFile.getPath() if not os.path.isfile(sourceFile.getPath()): raise IOError("File not found: " + sourceFilePath) self.addCode(subroutine, typeArgumentReferences, globalsReferences)
def __init__(self, subroutine, typeArgumentReferences, typeResultReferences, globalsReferences, includeTestModule): assertType(subroutine, 'subroutine', Subroutine) assertTypeAll(typeArgumentReferences, 'typeArgumentReferences', VariableReference) assertTypeAll(typeResultReferences, 'typeResultReferences', VariableReference) assertTypeAll(globalsReferences, 'globalsReferences', VariableReference) assertType(includeTestModule, 'includeTestModule', bool) types = set() for variable in subroutine.getDerivedTypeArguments(): if variable.hasDerivedType() and variable.isTypeAvailable(): types.add(variable.getType()) refTypes = set(types) for reference in typeArgumentReferences + typeResultReferences + globalsReferences: for level in reference.getLevels(): variable = reference.getVariable(level) if variable is not None: if variable.hasDerivedType() and variable.isTypeAvailable( ): refTypes.add(variable.getType()) for typE in refTypes: if typE.isAbstract() and typE.hasAssignedImplementation(): types.add(typE.getAssignedImplementation()) types = sorted(types) testModule = subroutine.getModule() modules = dict() for typE in types: module = typE.getModule() if module is not None and module != testModule or includeTestModule: moduleName = module.getName() if moduleName not in modules: modules[moduleName] = [] modules[moduleName].append(typE.getName()) self.imports = '' for module, typeNames in modules.items(): self.imports += ' USE ' + module + ', ONLY: ' for typeName in typeNames: self.imports += typeName + ', ' self.imports = self.imports.strip(', ') self.imports += "\n" self.imports = self.imports.strip("\n")
def __init__(self, variable, references): assertType(variable, 'variable', Variable) assert variable.isFunctionResult() assertTypeAll(references, 'references', VariableReference) self.__var = variable self.__used = [] for ref in references: if ref.getLevel0Variable() == self.__var: self.__used.append(UsedVariable(ref)) if not self.__used and variable.hasBuiltInType(): self.__used.append( UsedVariable( VariableReference(variable.getName(), variable.getDeclaredIn().getName(), 0, variable)))
def allocatedOrAssociated(self, variable, minLevel=0): assertType(variable, 'variable', UsedVariable, True) assertType(minLevel, 'minLevel', int) assert minLevel >= 0 if variable is None: return '' if variable.allocatable(): return 'ALLOCATED(' + variable.expression() + ')' elif variable.pointer(): return 'ASSOCIATED(' + variable.expression() + ')' elif variable.level() > minLevel: return self.allocatedOrAssociated(variable.container()) else: return ''
def __bound(self, variable, dim, placeholder): assertType(variable, 'variable', UsedVariable, True) assertType(dim, 'dim', int) assertTypeAll(placeholder, 'placeholder', str) if variable is None: return '' noDim = False if dim <= 0: noDim = True ref = variable.getReference() if ref.isOneVariableArray(): if noDim: dim = ref.getTotalDimensions() elif dim > ref.getTotalDimensions(): return '' top = 0 perc = '' bound = 'BOUND(' for level in ref.getLevels(): var = ref.getVariable(level) if var is None: return '' bound += perc + var.getName() perc = '%' bot = top top += var.getDimension() if top < dim: if top > bot: bound += '(' sep = '' for i in range(bot, top): bound += sep + placeholder[i] sep = ', ' bound += ')' else: break if not noDim: bound += ', ' + str(dim - bot) bound += ')' return bound return ''
def __init__(self, intent=None, allocatable=None, pointer=None, optional=None, charLengthZero=False): assertType(intent, 'intent', str, True) assertType(allocatable, 'allocatable', bool, True) assertType(pointer, 'pointer', bool, True) assertType(optional, 'optional', bool, True) assert not (allocatable and pointer) assertType(charLengthZero, 'charLengthZero', bool) self.__intent = intent self.__allocatable = allocatable self.__pointer = pointer self.__optional = optional self.__charLengthZero = charLengthZero
def __init__(self, currentModule, typeArgumentReferences, typeResultReferences, globalsReferences, subroutine): assertType(currentModule, 'currentModule', Module) assertType(subroutine, 'subroutine', Subroutine) assertTypeAll(typeArgumentReferences, 'typeArgumentReferences', VariableReference) assertTypeAll(typeResultReferences, 'typeResultReferences', VariableReference) assertTypeAll(globalsReferences, 'globalsReferences', VariableReference) types = set() for variable in subroutine.getDerivedTypeArguments(): if variable.hasDerivedType() and variable.isTypeAvailable(): types.add(variable.getType()) refTypes = set(types) for reference in typeArgumentReferences + typeResultReferences + globalsReferences: for level in reference.getLevels(): variable = reference.getVariable(level) if variable is not None: if variable.hasDerivedType() and variable.isTypeAvailable( ): refTypes.add(variable.getType()) for typE in refTypes: if typE.isAbstract() and typE.hasAssignedImplementation(): implType = typE.getAssignedImplementation() if implType.getName() not in types: types.add(implType) types = sorted(types) publicElements = currentModule.getPublicElements() self.exports = 'PUBLIC :: ' for typE in types: module = typE.getModule() typeName = typE.getName() if module is not None and module == currentModule and not typE.isPublic( ) and typeName not in publicElements: self.exports += typeName + ", " self.exports = self.exports.strip(', ') if self.exports == 'PUBLIC ::': self.exports = ''
def __init__(self, currentModule, globalsReferences): assertType(currentModule, 'currentModule', Module) assertType(globalsReferences, 'globalsReferences', list) publicElements = currentModule.getPublicElements() self.exports = 'PUBLIC :: ' variables = set() for ref in globalsReferences: variable = ref.getLevel0Variable() refModule = variable.getModule() if refModule == currentModule: variableName = variable.getOriginalName().lower() if variableName not in variables and not variable.isPublic( ) and variableName not in publicElements: self.exports += variableName + ", " variables.add(variableName) self.exports = self.exports.strip(', ') if self.exports == 'PUBLIC ::': self.exports = ''
def __init__(self, sourceFiles, templateDir, testSourceDir, testDataDir, graphBuilder, backupSuffix, excludeModules=[], ignoredModulesForGlobals=[], ignoredTypes=[], ignoreRegex=''): assertType(sourceFiles, 'sourceFiles', SourceFiles) assertType(templateDir, 'templateDir', str) if not os.path.isdir(templateDir): raise IOError("Not a directory: " + templateDir) assertType(testDataDir, 'testDataDir', str) if not os.path.isdir(testDataDir): raise IOError("Not a directory: " + testDataDir) super(ReplayCodeGenerator, self).__init__(sourceFiles, graphBuilder, backupSuffix, excludeModules, ignoredModulesForGlobals, ignoredTypes, ignoreRegex) self.__testTemplate = os.path.join(templateDir, self.TEST_TEMPLATE) self.__testSourceDir = testSourceDir self.__testDataDir = testDataDir
def __init__(self, subroutine, sourceFile, globalsReferences, includeTestModule): assertType(subroutine, 'subroutine', Subroutine) assertType(sourceFile, 'sourceFile', SourceFile) assertTypeAll(globalsReferences, 'globalsReferences', VariableReference) assertType(includeTestModule, 'includeTestModule', bool) self.usedVariables = [] variables = set() for ref in globalsReferences: self.usedVariables.append(UsedVariable(ref)) variable = ref.getLevel0Variable() variables.add(variable) variables = sorted(variables) self.usedVariables = sorted(self.usedVariables) testModule = subroutine.getModule() modules = dict() for variable in variables: moduleName = variable.getDeclaredInName() if moduleName != testModule.getName() or includeTestModule: if moduleName not in modules: modules[moduleName] = [] varName = variable.getName() if varName != variable.getOriginalName(): varName += ' => ' + variable.getOriginalName() modules[moduleName].append(varName) self.imports = [] for module, elements in sorted(modules.items()): self.imports.append('USE ' + module + ', ONLY: ' + ', '.join(elements)) self.imports = "\n".join(self.imports)
def addCode(self, subroutineFullName, typeArgumentReferences, typeResultReferences, globalsReferences, callgraph, types): assertType(subroutineFullName, 'subroutineFullName', SubroutineFullName) assertTypeAll(typeArgumentReferences, 'typeArgumentReferences', VariableReference) assertTypeAll(typeResultReferences, 'typeResultReferences', VariableReference) assertTypeAll(globalsReferences, 'globalsReferences', VariableReference) assertType(callgraph, 'callgraph', CallGraph) assertType(types, 'types', TypeCollection) printLine('Create code in new test source file', indent=1) tempTestFile = os.path.join(self.__testSourceDir, self.TEMP_TEST_FILE) printLine('Create file ' + tempTestFile, indent=2) self._writeFile(tempTestFile, []) subroutine = self._findSubroutine(subroutineFullName) self._setTypesToSubroutineVariables(subroutine, types) templateNameSpace = ReplayTemplatesNameSpace( subroutine, typeArgumentReferences, typeResultReferences, globalsReferences, self.__testDataDir, callgraph, self._postProcessor) self._processTemplate(tempTestFile, 0, self.TEST_TEMPLATE_PART, templateNameSpace) testModuleName = self.__findModuleNameInTestFile(tempTestFile) if testModuleName is not None: testFilePath = os.path.join(self.__testSourceDir, testModuleName + '.f90') printLine('Rename file to ' + testFilePath, indent=2) os.rename(tempTestFile, testFilePath)
def __init__(self, subroutine, typeArgumentReferences, globalsReferences, testDataDir): assertType(subroutine, 'subroutine', Subroutine) assertType(typeArgumentReferences, 'typeArgumentReferences', list) assertType(globalsReferences, 'globalsReferences', list) self.__subroutine = subroutine self._typeArgumentReferences = typeArgumentReferences self._globalsReferences = [] for reference in globalsReferences: reference = reference.cleanCopy() variableName = reference.getVariableName() if reference.getDeclaredInName() != subroutine.getModuleName(): variable = reference.getLevel0Variable() moduleName = variable.getDeclaredInName() if variable is not None and moduleName is not None: newName = moduleName + '__' + variableName alias = variable.getAlias(newName) reference.setLevel0Variable(alias) self._globalsReferences.append(reference) self.subroutine = SubroutineNameSpace(subroutine) self.module = ModuleNameSpace(subroutine.getModuleName()) self.arguments = ArgumentsNameSpace(subroutine, typeArgumentReferences) self.globals = GlobalsNameSpace(subroutine, subroutine.getSourceFile(), self._globalsReferences, False) self.dataDir = testDataDir.rstrip('/')
def __init__(self, moduleName, sourceFile, globalsReferences): assertType(moduleName, 'moduleName', str) assertType(sourceFile, 'sourceFile', SourceFile) assertType(globalsReferences, 'globalsReferences', list) publicElements = sourceFile.getPublicElements() self.exports = 'PUBLIC :: ' variables = set() types = set() for ref in globalsReferences: variable = ref.getLevel0Variable() refModule = variable.getDeclaredInName() if refModule == moduleName: variableName = variable.getOriginalName().lower() if variableName not in variables and not variable.isPublic( ) and variableName not in publicElements: self.exports += variableName + ", " variables.add(variableName) if variable.hasDerivedType() and variable.isTypeAvailable(): typE = variable.getType() refModule = typE.getDeclaredInName() if refModule == moduleName: typeName = typE.getName().lower() if typeName not in types and typeName not in publicElements: self.exports += typeName + ", " types.add(typeName) self.exports = self.exports.strip(', ') if self.exports == 'PUBLIC ::': self.exports = ''
def __init__(self, subroutine, typeArgumentReferences, globalsReferences, includeTestModule): assertType(subroutine, 'subroutine', Subroutine) assertTypeAll(typeArgumentReferences, 'typeArgumentReferences', VariableReference) assertTypeAll(globalsReferences, 'globalsReferences', VariableReference) assertType(includeTestModule, 'includeTestModule', bool) variables = set(subroutine.getDerivedTypeArguments()) for reference in globalsReferences: variables.add(reference.getLevel0Variable()) self.__types = dict() for variable in variables: if variable.hasDerivedType() and variable.isTypeAvailable(): typE = variable.getType() if typE.getName() not in self.__types: self.__types[typE.getName()] = typE #self.__addMemberTypesToTypSet(typE) testModule = subroutine.getName().getModuleName() modules = dict() for typE in self.__types.values(): moduleName = typE.getDeclaredInName() if isinstance(moduleName, str) and (moduleName != testModule or includeTestModule): if moduleName not in modules: modules[moduleName] = [] modules[moduleName].append(typE.getName()) self.imports = '' for module, typeNames in modules.iteritems(): self.imports += ' USE ' + module + ', ONLY: ' for typeName in typeNames: self.imports += typeName + ', ' self.imports = self.imports.strip(', ') self.imports += "\n" self.imports = self.imports.strip("\n")
def writeVarNameWithFilledIndicesToString(self, variable, destination, dim, *indices): assertType(variable, 'variable', UsedVariable, True) if variable is None: return '' parts = [] for index in indices: parts.append("', " + index + ", '") filled = self.fillIndices(variable, dim, *parts) if not filled: return '' if filled.expression() == variable.expression(): return destination + ' = "' + variable.expression() + '"' write = "WRITE (" + destination + ",'(" write += 'A,I0,' * min(dim, len(indices), variable.totalDim()) write += "A)') '" + filled.expression() + "'" return write
def __init__(self, moduleName, sourceFile, globalsReferences): assertType(moduleName, 'moduleName', str) assertType(sourceFile, 'sourceFile', SourceFile) assertType(globalsReferences, 'globalsReferences', list) self.module = ModuleNameSpace(moduleName) self.globals = ExportGlobalsNameSpace(moduleName, sourceFile, globalsReferences)
def alloc(self, variable, *dimSizes): assertType(variable, 'variable', UsedVariable, True) assertTypeAll(dimSizes, 'dimSizes', str) if variable is None: return '' dim = variable.dim() alloc = 'ALLOCATE(' if variable.polymorph(): alloc += variable.dynamicType() + '::' alloc += variable.expression() if dim > 0: alloc += '(' sep = '' for d in range(min(dim, len(dimSizes))): alloc += sep + dimSizes[d] sep = ', ' alloc += ')' alloc += ')' self.setAllocated(variable) return alloc