def search_Type_Declaration_Stmt(stmt, node, gentype=None): """ Identifying a name in Type_Declaration_Stmt node""" from kgen_utils import pack_innamepath, match_namepath # collect excluded names if Config.exclude.has_key('namepath'): for pattern, actions in Config.exclude['namepath'].iteritems(): decls = [] if isinstance(node.items[2], Fortran2003.Entity_Decl): decls.append(node.items[2].items[0].string.lower()) elif isinstance(node.items[2], Fortran2003.Entity_Decl_List): for item in node.items[2].items: decls.append(item.items[0].string.lower()) for decl in decls: namepath = pack_innamepath(stmt, decl) if match_namepath(pattern, namepath): if not hasattr(stmt, 'exclude_names'): stmt.exclude_names = OrderedDict() if stmt.exclude_names.has_key(decl): stmt.exclude_names[decl].extend(actions) else: stmt.exclude_names[decl] = actions defer_items(stmt, node)
def locate_callsite(cs_tree): from statements import Comment from block_statements import executable_construct import re def get_next_non_comment(stmt): if not stmt: return if not hasattr(stmt, 'parent'): return started = False for s in stmt.parent.content: if s == stmt: if not isinstance(s, Comment): return s started = True elif started: if not isinstance(s, Comment): return s def get_names(node, bag, depth): from Fortran2003 import Name if isinstance(node, Name) and not node.string in bag: bag.append(node.string) # collect directives directs = [] for stmt, depth in walk(cs_tree): if isinstance(stmt, Comment): line = stmt.item.comment.strip() match = re.match(r'^[c!*]\$kgen\s+(.+)$', line, re.IGNORECASE) if match: dsplit = match.group(1).split(' ', 1) dname = dsplit[0].strip() if len(dsplit) > 1: clause = dsplit[1].strip() else: clause = None if dname.startswith('begin_'): sname = dname[6:] directs.append(sname) State.kernel['name'] = clause elif dname.startswith('end_'): ename = dname[4:] if directs[-1] == ename: directs.pop() if ename == 'callsite': pass else: raise UserException( 'WARNING: Not supported KGEN directive: %s' % ename) else: raise UserException('Directive name mismatch: %s, %s' % (dname_stack[-1], ename)) elif dname == 'callsite': next_fort_stmt = get_next_non_comment(stmt) if next_fort_stmt: State.kernel['name'] = clause State.callsite['stmts'].append(next_fort_stmt) else: raise UserException('WARNING: callsite is not found') else: if Config.callsite[ 'namepath'] and stmt.__class__ in executable_construct: names = [] traverse(stmt.f2003, get_names, names) for name in names: if match_namepath(Config.callsite['namepath'], pack_exnamepath(stmt, name), internal=False): State.kernel['name'] = name for _s, _d in walk(stmt): State.callsite['stmts'].append(_s) return elif len(directs) > 0 and directs[-1] == 'callsite': State.callsite['stmts'].append(stmt) if len(State.callsite['stmts']) == 0: raise UserException('Can not find callsite')
def get_name_or_defer(stmt, node, resolvers, defer=True, gentype=None): """Select a name to be searched, or defer to lower level of nodes in AST. Parameters ---------- stmt : F2PY parser statement object Specify a statement object to be searched node : F2PY Fortran2003 parser object Specify an expression object to be searched resolvers : A list of statement classes for resolver Limits the classes of resolver defer : bool check if to search lower level of nodes in AST. gentype : Type of state data (IN or OUT) Specify the type of state data Returns ------- None See also -------- f2003_search_unknowns get_name defer defer_names """ from kgen_utils import KGName, pack_innamepath, get_innamepath, match_namepath from kgen_state import ResState if node is None: return # uncomment below line for debug #print node.__class__, str(node) if isinstance(node, Fortran2003.Name): # # skip if intrinsic # if node.string.lower() in Intrinsic_Procedures: # if Config.search['skip_intrinsic'] and not is_except(node, stmt): # if hasattr(node, 'parent') and not isinstance(node.parent, Fortran2003.Part_Ref) and \ # not (isinstance(node.parent, Fortran2003.Function_Reference) and node.string.lower()=='null') and \ # not (isinstance(node.parent, Fortran2003.Specific_Binding) and node.string.lower()=='null'): # Logger.info('Intrinsic procedure name of "%s" is used for name resolution'% \ # (node.string.lower()), stdout=True) # Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) # Logger.info('\tin %s'% stmt.reader.id, stdout=True) # else: # #if node.string.lower()!='null': # # Logger.info('Intrinsic procedure name of "%s" is skipped from name resolution'% \ # # (node.string.lower()), stdout=True) # #Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) # #Logger.info('\tin %s'% stmt.reader.id, stdout=True) # return # # elif not Config.search['skip_intrinsic'] and is_except(node, stmt): # if hasattr(node, 'parent') and not isinstance(node.parent, Fortran2003.Part_Ref) and \ # not (isinstance(node.parent, Fortran2003.Function_Reference) and node.string.lower()=='null') and \ # not (isinstance(node.parent, Fortran2003.Specific_Binding) and node.string.lower()=='null'): # #Logger.info('Intrinsic procedure name of "%s" is NOT skipped from name resolution'% \ # # (node.string.lower()), stdout=True) # #Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) # #Logger.info('\tin %s'% stmt.reader.id, stdout=True) # pass # else: # if node.string.lower()!='null': # Logger.info('Intrinsic procedure name of "%s" is skipped from name resolution'% \ # (node.string.lower()), stdout=True) # Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) # Logger.info('\tin %s'% stmt.reader.id, stdout=True) # return # skip if excluded #if Config.exclude.has_key('namepath') and stmt.__class__ in execution_part: if Config.exclude.has_key('namepath'): for pattern, actions in Config.exclude['namepath'].iteritems(): name = node.string.lower() namepath = pack_innamepath(stmt, name) #Logger.info('%s and %s are being checked for exclusion'%(pattern, namepath)) if match_namepath(pattern, namepath): #Logger.info('%s and %s are mathched for exclusion'%(pattern, namepath)) if not hasattr(stmt, 'exclude_names'): stmt.exclude_names = OrderedDict() if stmt.exclude_names.has_key(name): stmt.exclude_names[name].extend(actions) else: stmt.exclude_names[name] = actions node.skip_search = True if hasattr(node, 'parent'): node.parent.skip_search = True return ukey = KGName(pack_innamepath(stmt, node.string.lower()), node=node, stmt=stmt) if gentype is None: gentype = KGGenType.STATE_IN if resolvers is None: stmt.unknowns[ukey] = ResState(gentype, ukey, stmt, res_default) else: stmt.unknowns[ukey] = ResState(gentype, ukey, stmt, resolvers) Logger.info('%s is saved as unknown' % node.string.lower(), name=ukey, stmt=stmt) elif defer: f2003_search_unknowns(stmt, node, resolvers, gentype=gentype)
def get_name_or_defer(stmt, node, resolvers, defer=True, gentype=None): from kgen_utils import KGName, pack_innamepath, get_innamepath, match_namepath from kgen_state import ResState if node is None: return # uncomment below line for debug #print node.__class__, str(node) if isinstance(node, Fortran2003.Name): # skip if intrinsic if node.string.lower() in Intrinsic_Procedures: if Config.search['skip_intrinsic'] and not is_except(node, stmt): if hasattr(node, 'parent') and not isinstance(node.parent, Fortran2003.Part_Ref) and \ not (isinstance(node.parent, Fortran2003.Function_Reference) and node.string.lower()=='null') and \ not (isinstance(node.parent, Fortran2003.Specific_Binding) and node.string.lower()=='null'): Logger.info('Intrinsic procedure name of "%s" is used for name resolution'% \ (node.string.lower()), stdout=True) Logger.info('\tnear "%s"' % stmt.item.line, stdout=True) Logger.info('\tin %s' % stmt.reader.id, stdout=True) else: #if node.string.lower()!='null': # Logger.info('Intrinsic procedure name of "%s" is skipped from name resolution'% \ # (node.string.lower()), stdout=True) #Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) #Logger.info('\tin %s'% stmt.reader.id, stdout=True) return elif not Config.search['skip_intrinsic'] and is_except(node, stmt): if hasattr(node, 'parent') and not isinstance(node.parent, Fortran2003.Part_Ref) and \ not (isinstance(node.parent, Fortran2003.Function_Reference) and node.string.lower()=='null') and \ not (isinstance(node.parent, Fortran2003.Specific_Binding) and node.string.lower()=='null'): #Logger.info('Intrinsic procedure name of "%s" is NOT skipped from name resolution'% \ # (node.string.lower()), stdout=True) #Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) #Logger.info('\tin %s'% stmt.reader.id, stdout=True) pass else: if node.string.lower() != 'null': Logger.info('Intrinsic procedure name of "%s" is skipped from name resolution'% \ (node.string.lower()), stdout=True) Logger.info('\tnear "%s"' % stmt.item.line, stdout=True) Logger.info('\tin %s' % stmt.reader.id, stdout=True) return # skip if excluded #if Config.exclude.has_key('namepath') and stmt.__class__ in execution_part: if Config.exclude.has_key('namepath'): for pattern, actions in Config.exclude['namepath'].iteritems(): name = node.string.lower() namepath = pack_innamepath(stmt, name) if match_namepath(pattern, namepath): if not hasattr(stmt, 'exclude_names'): stmt.exclude_names = OrderedDict() if stmt.exclude_names.has_key(name): stmt.exclude_names[name].extend(actions) else: stmt.exclude_names[name] = actions node.skip_search = True if hasattr(node, 'parent'): node.parent.skip_search = True return ukey = KGName(pack_innamepath(stmt, node.string.lower()), node=node, stmt=stmt) if gentype is None: gentype = KGGenType.STATE_IN if resolvers is None: stmt.unknowns[ukey] = ResState(gentype, ukey, stmt, res_default) else: stmt.unknowns[ukey] = ResState(gentype, ukey, stmt, resolvers) Logger.info('%s is saved as unknown' % node.string.lower(), name=ukey, stmt=stmt) elif defer: f2003_search_unknowns(stmt, node, resolvers, gentype=gentype)
def locate_callsite(cs_tree): from statements import Comment from block_statements import executable_construct import re def get_next_non_comment(stmt): if not stmt: return if not hasattr(stmt, 'parent'): return started = False for s in stmt.parent.content: if s==stmt: if not isinstance(s, Comment): return s started = True elif started: if not isinstance(s, Comment): return s def get_names(node, bag, depth): from Fortran2003 import Name if isinstance(node, Name) and not node.string in bag: bag.append(node.string) # collect directives directs = [] for stmt, depth in walk(cs_tree): if isinstance(stmt, Comment): line = stmt.item.comment.strip() match = re.match(r'^[c!*]\$kgen\s+(.+)$', line, re.IGNORECASE) if match: dsplit = match.group(1).split(' ', 1) dname = dsplit[0].strip() if len(dsplit)>1: clause = dsplit[1].strip() else: clause = None if dname.startswith('begin_'): sname = dname[6:] directs.append(sname) State.kernel['name'] = clause elif dname.startswith('end_'): ename = dname[4:] if directs[-1]==ename: directs.pop() if ename=='callsite': pass else: raise UserException('WARNING: Not supported KGEN directive: %s'%ename) else: raise UserException('Directive name mismatch: %s, %s'%(dname_stack[-1], ename)) elif dname=='callsite': next_fort_stmt = get_next_non_comment(stmt) if next_fort_stmt: State.kernel['name'] = clause State.callsite['stmts'].append(next_fort_stmt) else: raise UserException('WARNING: callsite is not found') elif 'callsite' in directs: State.callsite['stmts'].append(stmt) elif 'callsite' in directs: State.callsite['stmts'].append(stmt) else: if Config.callsite['namepath'] and stmt.__class__ in executable_construct: names = [] traverse(stmt.f2003, get_names, names) for name in names: if match_namepath(Config.callsite['namepath'], pack_exnamepath(stmt, name), internal=False): State.kernel['name'] = name for _s, _d in walk(stmt): State.callsite['stmts'].append(_s) return elif len(directs)>0 and directs[-1]=='callsite': State.callsite['stmts'].append(stmt) if len(State.callsite['stmts'])==0: raise UserException('Can not find callsite')
def get_name_or_defer(stmt, node, resolvers, defer=True, gentype=None): """Select a name to be searched, or defer to lower level of nodes in AST. Parameters ---------- stmt : F2PY parser statement object Specify a statement object to be searched node : F2PY Fortran2003 parser object Specify an expression object to be searched resolvers : A list of statement classes for resolver Limits the classes of resolver defer : bool check if to search lower level of nodes in AST. gentype : Type of state data (IN or OUT) Specify the type of state data Returns ------- None See also -------- f2003_search_unknowns get_name defer defer_names """ from kgen_utils import KGName, pack_innamepath, get_innamepath, match_namepath from kgen_state import ResState if node is None: return # uncomment below line for debug #print node.__class__, str(node) if isinstance(node, Fortran2003.Name): # skip if intrinsic if node.string.lower() in Intrinsic_Procedures: if Config.search['skip_intrinsic'] and not is_except(node, stmt): if hasattr(node, 'parent') and not isinstance(node.parent, Fortran2003.Part_Ref) and \ not (isinstance(node.parent, Fortran2003.Function_Reference) and node.string.lower()=='null') and \ not (isinstance(node.parent, Fortran2003.Specific_Binding) and node.string.lower()=='null'): Logger.info('Intrinsic procedure name of "%s" is used for name resolution'% \ (node.string.lower()), stdout=True) Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) Logger.info('\tin %s'% stmt.reader.id, stdout=True) else: #if node.string.lower()!='null': # Logger.info('Intrinsic procedure name of "%s" is skipped from name resolution'% \ # (node.string.lower()), stdout=True) #Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) #Logger.info('\tin %s'% stmt.reader.id, stdout=True) return elif not Config.search['skip_intrinsic'] and is_except(node, stmt): if hasattr(node, 'parent') and not isinstance(node.parent, Fortran2003.Part_Ref) and \ not (isinstance(node.parent, Fortran2003.Function_Reference) and node.string.lower()=='null') and \ not (isinstance(node.parent, Fortran2003.Specific_Binding) and node.string.lower()=='null'): #Logger.info('Intrinsic procedure name of "%s" is NOT skipped from name resolution'% \ # (node.string.lower()), stdout=True) #Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) #Logger.info('\tin %s'% stmt.reader.id, stdout=True) pass else: if node.string.lower()!='null': Logger.info('Intrinsic procedure name of "%s" is skipped from name resolution'% \ (node.string.lower()), stdout=True) Logger.info('\tnear "%s"'% stmt.item.line, stdout=True) Logger.info('\tin %s'% stmt.reader.id, stdout=True) return # skip if excluded #if Config.exclude.has_key('namepath') and stmt.__class__ in execution_part: if Config.exclude.has_key('namepath'): for pattern, actions in Config.exclude['namepath'].iteritems(): name = node.string.lower() namepath = pack_innamepath(stmt, name) #Logger.info('%s and %s are being checked for exclusion'%(pattern, namepath)) if match_namepath(pattern, namepath): #Logger.info('%s and %s are mathched for exclusion'%(pattern, namepath)) if not hasattr(stmt, 'exclude_names'): stmt.exclude_names = OrderedDict() if stmt.exclude_names.has_key(name): stmt.exclude_names[name].extend(actions) else: stmt.exclude_names[name] = actions node.skip_search = True if hasattr(node, 'parent'): node.parent.skip_search = True return ukey = KGName(pack_innamepath(stmt, node.string.lower()), node=node, stmt=stmt) if gentype is None: gentype = KGGenType.STATE_IN if resolvers is None: stmt.unknowns[ukey] = ResState(gentype, ukey, stmt, res_default) else: stmt.unknowns[ukey] = ResState(gentype, ukey, stmt, resolvers) Logger.info('%s is saved as unknown' % node.string.lower(), name=ukey, stmt=stmt) elif defer: f2003_search_unknowns(stmt, node, resolvers, gentype=gentype)