2009-09-05 00:18:45 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
from util import *
|
|
|
|
from irpf90_t import *
|
|
|
|
from variables import variables
|
|
|
|
from preprocessed_text import preprocessed_text
|
|
|
|
from subroutines import subroutines
|
|
|
|
import regexps
|
2009-09-05 15:37:23 +02:00
|
|
|
import error
|
2009-09-05 00:18:45 +02:00
|
|
|
|
|
|
|
def find_variables_in_line(line):
|
|
|
|
assert isinstance(line,Line)
|
|
|
|
result = []
|
|
|
|
buffer = regexps.re_string.sub('',line.text)
|
|
|
|
for v in variables.keys():
|
|
|
|
var = variables[v]
|
2009-09-09 16:59:43 +02:00
|
|
|
if var.name in buffer.lower():
|
|
|
|
if var.regexp.search(buffer) is not None:
|
|
|
|
result.append(var.same_as)
|
2009-09-05 00:18:45 +02:00
|
|
|
return result
|
|
|
|
|
2009-09-05 15:37:23 +02:00
|
|
|
def find_subroutine_in_line(line):
|
|
|
|
assert isinstance(line,Call)
|
2009-09-05 16:05:00 +02:00
|
|
|
buffer = line.text.split('(')[0]
|
|
|
|
buffer = buffer.split()[1]
|
|
|
|
return buffer
|
2009-09-05 15:37:23 +02:00
|
|
|
|
2009-09-05 16:05:00 +02:00
|
|
|
def check_touch(line,vars,main_vars):
|
2009-09-05 15:37:23 +02:00
|
|
|
def fun(main_var):
|
|
|
|
if main_var not in variables:
|
|
|
|
error.fail(line,"Variable %s unknown"%(main_var,))
|
|
|
|
x = variables[main_var]
|
|
|
|
return [main_var]+x.others
|
2009-09-05 16:05:00 +02:00
|
|
|
all_others = make_single(flatten( map(fun,main_vars) ))
|
2009-09-05 15:37:23 +02:00
|
|
|
all_others.sort()
|
|
|
|
if len(all_others) == len(vars):
|
2009-09-05 16:05:00 +02:00
|
|
|
vars.sort()
|
2009-09-05 15:37:23 +02:00
|
|
|
for x,y in zip(vars,all_others):
|
|
|
|
if x != y:
|
|
|
|
message = "The following entities should be touched:\n"
|
|
|
|
message = "\n".join([message]+map(lambda x: "- %s"%(x,),all_others))
|
|
|
|
error.fail(line,message)
|
2009-09-05 00:18:45 +02:00
|
|
|
|
|
|
|
def get_parsed_text():
|
2009-09-05 16:05:00 +02:00
|
|
|
main_result = []
|
2009-09-07 16:35:34 +02:00
|
|
|
varlist = []
|
2009-09-05 00:18:45 +02:00
|
|
|
for filename, text in preprocessed_text:
|
2009-09-05 16:05:00 +02:00
|
|
|
result = []
|
2009-09-05 00:18:45 +02:00
|
|
|
for line in filter(
|
|
|
|
lambda x: type(x) not in [ Doc, Begin_doc, End_doc ],
|
|
|
|
text):
|
|
|
|
if type(line) in [ \
|
|
|
|
Empty_line,
|
|
|
|
Continue,
|
|
|
|
Return,
|
|
|
|
Begin_shell,
|
|
|
|
End_shell,
|
|
|
|
Openmp,
|
|
|
|
Use,
|
|
|
|
Enddo,
|
|
|
|
End_select,
|
|
|
|
Endif,
|
|
|
|
Implicit,
|
|
|
|
Program,
|
|
|
|
Subroutine,
|
|
|
|
Function,
|
|
|
|
End,
|
|
|
|
]:
|
2009-09-05 16:05:00 +02:00
|
|
|
result.append( ([],line) )
|
2009-09-07 16:35:34 +02:00
|
|
|
elif isinstance(line,End_provider):
|
|
|
|
varlist = []
|
|
|
|
result.append( ([],line) )
|
2009-09-05 00:18:45 +02:00
|
|
|
elif isinstance(line,Provide):
|
2009-09-07 16:35:34 +02:00
|
|
|
l = line.text.lower().split()[1:]
|
|
|
|
l = filter(lambda x: x not in varlist, l)
|
2009-09-09 16:59:43 +02:00
|
|
|
for v in l:
|
|
|
|
if v not in variables.keys():
|
|
|
|
error.fail(line,"Variable %s is unknown"%(v))
|
2009-09-05 16:05:00 +02:00
|
|
|
result.append( (l,Simple_line(line.i,"!%s"%(line.text),line.filename)) )
|
2009-09-05 15:37:23 +02:00
|
|
|
elif isinstance(line,Call):
|
|
|
|
sub = find_subroutine_in_line(line)
|
2009-09-05 16:05:00 +02:00
|
|
|
if sub not in subroutines:
|
2009-09-05 15:37:23 +02:00
|
|
|
t = Simple_line
|
|
|
|
else:
|
2009-09-05 16:05:00 +02:00
|
|
|
if subroutines[sub].touches == []:
|
|
|
|
t = Simple_line
|
|
|
|
else:
|
|
|
|
t = Provide_all
|
2009-09-09 16:59:43 +02:00
|
|
|
l = find_variables_in_line(line)
|
|
|
|
l = filter(lambda x: x not in varlist, l)
|
|
|
|
result.append( (l,t(line.i,line.text,line.filename)) )
|
2009-09-05 15:37:23 +02:00
|
|
|
elif isinstance(line,Free):
|
|
|
|
vars = line.text.split()
|
|
|
|
if len(vars) < 2:
|
|
|
|
error.fail(line,"Syntax error")
|
2009-09-08 18:20:20 +02:00
|
|
|
vars = map(lower,vars[1:])
|
|
|
|
for v in vars:
|
|
|
|
variables[v].is_freed = True
|
2009-09-05 15:37:23 +02:00
|
|
|
result.append( ([],Simple_line(line.i,"!%s"%(line.text),line.filename)) )
|
2009-09-09 16:59:43 +02:00
|
|
|
use = map(lambda x: " use %s"%(variables[x].fmodule),vars)
|
2009-09-05 15:37:23 +02:00
|
|
|
for var in vars:
|
2009-09-09 16:59:43 +02:00
|
|
|
result += map(lambda x: ([],Use(line.i,x,line.filename)),
|
|
|
|
make_single(use))
|
|
|
|
result += map(lambda x: ([],Simple_line(line.i,x,line.filename)),
|
|
|
|
variables[var].free)
|
2009-09-08 18:20:20 +02:00
|
|
|
elif isinstance(line,Irp_read):
|
|
|
|
variables[line.filename].is_read = True
|
|
|
|
result.append( ([],Simple_line(line.i,"!%s"%(line.text),line.filename)) )
|
|
|
|
elif isinstance(line,Irp_write):
|
|
|
|
variables[line.filename].is_written = True
|
|
|
|
result.append( ([],Simple_line(line.i,"!%s"%(line.text),line.filename)) )
|
2009-09-05 15:37:23 +02:00
|
|
|
elif isinstance(line,Touch):
|
|
|
|
vars = line.text.split()
|
|
|
|
if len(vars) < 2:
|
|
|
|
error.fail(line,"Syntax error")
|
2009-09-05 16:05:00 +02:00
|
|
|
vars = map(lower,vars[1:])
|
2009-09-08 18:20:20 +02:00
|
|
|
for v in vars:
|
2009-09-09 16:59:43 +02:00
|
|
|
variables[v]._is_touched = True
|
2009-09-05 15:37:23 +02:00
|
|
|
def fun(x):
|
|
|
|
if x not in variables:
|
|
|
|
error.fail(line,"Variable %s unknown"%(x,))
|
|
|
|
main = variables[x].same_as
|
|
|
|
return main
|
|
|
|
main_vars = make_single( map(fun, vars) )
|
2009-09-05 16:05:00 +02:00
|
|
|
check_touch(line,vars,main_vars)
|
2009-09-05 15:37:23 +02:00
|
|
|
txt = " ".join(vars)
|
2009-09-05 16:05:00 +02:00
|
|
|
result += [ ([],Simple_line(line.i,"!",line.filename)),
|
2009-09-07 16:35:34 +02:00
|
|
|
([],Simple_line(line.i,"! >>> TOUCH %s"%(txt,),line.filename)) ]
|
2009-09-05 15:37:23 +02:00
|
|
|
def fun(x):
|
|
|
|
if x not in variables:
|
|
|
|
error.fail(line,"Variable %s unknown"%(x,))
|
|
|
|
return [ ([],Simple_line(line.i," call touch_%s"%(x,),line.filename)),
|
2009-09-09 16:59:43 +02:00
|
|
|
([],Simple_line(line.i," %s_is_built = .True."%(x,),line.filename)),
|
|
|
|
([],Use(line.i," use %s"%(variables[x].fmodule), line.filename)) ]
|
2009-09-05 16:05:00 +02:00
|
|
|
result += flatten(map( fun, main_vars ))
|
2009-09-05 15:37:23 +02:00
|
|
|
def fun(x):
|
|
|
|
if x not in variables:
|
|
|
|
error.fail(line,"Variable %s unknown"%(x,))
|
2009-09-05 16:05:00 +02:00
|
|
|
return ([],Simple_line(line.i," %s_is_built = .True."%(x,),line.filename))
|
|
|
|
result += map( fun, main_vars[:-1] )
|
2009-09-05 15:37:23 +02:00
|
|
|
result += [ ([],Provide_all(line.i,"! <<< END TOUCH",line.filename)) ]
|
2009-09-06 00:47:20 +02:00
|
|
|
elif type(line) in [ Begin_provider, Cont_provider ]:
|
2009-09-07 16:35:34 +02:00
|
|
|
if isinstance(line,Begin_provider):
|
|
|
|
varlist = []
|
2009-09-06 00:47:20 +02:00
|
|
|
buffer = map(strip,line.text.replace(']','').split(','))
|
|
|
|
assert len(buffer) > 1
|
|
|
|
v = buffer[1].lower()
|
2009-09-07 16:35:34 +02:00
|
|
|
varlist.append(v)
|
2009-09-06 00:47:20 +02:00
|
|
|
variable_list = find_variables_in_line(line)
|
2009-09-09 16:59:43 +02:00
|
|
|
variable_list.remove(variables[v].same_as)
|
2009-09-06 00:47:20 +02:00
|
|
|
result.append( (variable_list,line) )
|
2009-09-05 00:18:45 +02:00
|
|
|
else:
|
|
|
|
l = find_variables_in_line(line)
|
2009-09-07 16:35:34 +02:00
|
|
|
l = filter(lambda x: x not in varlist, l)
|
2009-09-05 16:05:00 +02:00
|
|
|
result.append( (l,line) )
|
|
|
|
main_result.append( (filename, result) )
|
|
|
|
return main_result
|
2009-09-05 00:18:45 +02:00
|
|
|
|
|
|
|
parsed_text = get_parsed_text()
|
2009-09-07 16:35:34 +02:00
|
|
|
######################################################################
|
|
|
|
def move_variables():
|
|
|
|
|
|
|
|
main_result = []
|
|
|
|
for filename, text in parsed_text:
|
|
|
|
result = []
|
|
|
|
# 1st pass
|
2009-09-07 16:55:11 +02:00
|
|
|
varlist = []
|
|
|
|
ifvars = []
|
|
|
|
elsevars = []
|
|
|
|
old_varlist = []
|
|
|
|
old_ifvars = []
|
|
|
|
old_elsevars = []
|
2009-09-07 16:35:34 +02:00
|
|
|
revtext = list(text)
|
|
|
|
revtext.reverse()
|
|
|
|
for vars,line in revtext:
|
|
|
|
if type(line) in [ End_provider,End ]:
|
|
|
|
varlist = []
|
|
|
|
result.append( ([],line) )
|
|
|
|
elif type(line) in [ Endif, End_select ]:
|
2009-09-07 16:55:11 +02:00
|
|
|
old_ifvars.append(ifvars)
|
|
|
|
old_elsevars.append(elsevars)
|
2009-09-07 16:35:34 +02:00
|
|
|
old_varlist.append(varlist)
|
|
|
|
varlist = []
|
|
|
|
result.append( ([],line) )
|
2009-09-07 16:55:11 +02:00
|
|
|
elif type(line) == Else:
|
|
|
|
result.append( (varlist,line) )
|
|
|
|
elsevars = list(varlist)
|
|
|
|
if vars != []:
|
|
|
|
varlist = old_varlist.pop()
|
|
|
|
varlist += vars
|
|
|
|
old_varlist.append(varlist)
|
|
|
|
varlist = []
|
|
|
|
elif type(line) in [ Elseif, Case ]:
|
|
|
|
ifvars += varlist
|
2009-09-07 16:35:34 +02:00
|
|
|
result.append( (varlist,line) )
|
|
|
|
if vars != []:
|
|
|
|
varlist = old_varlist.pop()
|
|
|
|
varlist += vars
|
|
|
|
old_varlist.append(varlist)
|
|
|
|
varlist = []
|
|
|
|
elif type(line) in [ If, Select ]:
|
2009-09-07 16:55:11 +02:00
|
|
|
ifvars += varlist
|
2009-09-07 16:35:34 +02:00
|
|
|
result.append( (varlist,line) )
|
2009-09-07 16:55:11 +02:00
|
|
|
vars += filter(lambda x: x in elsevars, ifvars)
|
|
|
|
ifvars = old_ifvars.pop()
|
|
|
|
elsevars = old_elsevars.pop()
|
2009-09-07 16:35:34 +02:00
|
|
|
varlist = old_varlist.pop()
|
|
|
|
varlist += vars
|
|
|
|
elif type(line) in [ Begin_provider, Subroutine, Function ]:
|
|
|
|
varlist += vars
|
|
|
|
result.append( (varlist,line) )
|
|
|
|
assert old_varlist == []
|
2009-09-07 16:55:11 +02:00
|
|
|
assert old_ifvars == []
|
|
|
|
assert old_elsevars == []
|
2009-09-07 16:35:34 +02:00
|
|
|
varlist = []
|
|
|
|
else:
|
|
|
|
varlist += vars
|
|
|
|
result.append( ([],line) )
|
|
|
|
result.reverse()
|
|
|
|
# 2nd pass
|
|
|
|
text = result
|
|
|
|
result = []
|
|
|
|
old_varlist = []
|
|
|
|
varlist = []
|
|
|
|
for vars,line in text:
|
|
|
|
if vars != []:
|
|
|
|
vars = make_single(vars)
|
|
|
|
if type(line) in [ Begin_provider, Subroutine, Function ]:
|
|
|
|
varlist = list(vars)
|
|
|
|
elif type(line) in [ If, Select ]:
|
|
|
|
old_varlist.append(varlist)
|
|
|
|
vars = filter(lambda x: x not in varlist,vars)
|
|
|
|
varlist = make_single(varlist + vars)
|
|
|
|
assert old_varlist is not varlist
|
|
|
|
elif type(line) in [ Elseif, Else, Case ]:
|
|
|
|
varlist = old_varlist.pop()
|
|
|
|
old_varlist.append(varlist)
|
|
|
|
vars = filter(lambda x: x not in varlist,vars)
|
|
|
|
varlist = make_single(varlist + vars)
|
|
|
|
assert old_varlist is not varlist
|
|
|
|
elif type(line) in [ Endif, End_select ]:
|
|
|
|
varlist = old_varlist.pop()
|
|
|
|
elif type(line) == Provide_all:
|
|
|
|
vars = varlist
|
|
|
|
elif type(line) in [ End_provider, End ]:
|
|
|
|
assert old_varlist == []
|
|
|
|
varlist = []
|
|
|
|
result.append( (vars,line) )
|
|
|
|
|
|
|
|
main_result.append( (filename, result) )
|
|
|
|
return main_result
|
|
|
|
|
|
|
|
parsed_text = move_variables()
|
|
|
|
|
2009-09-09 16:59:43 +02:00
|
|
|
######################################################################
|
|
|
|
def move_to_top(text,t):
|
|
|
|
assert isinstance(text,list)
|
|
|
|
assert t in [ Declaration, Implicit, Use, Cont_provider ]
|
|
|
|
|
|
|
|
inside = False
|
|
|
|
for i in range(len(text)):
|
|
|
|
vars, line = text[i]
|
|
|
|
if type(line) in [ Begin_provider, Subroutine, Function ]:
|
|
|
|
begin = i
|
|
|
|
inside = True
|
|
|
|
elif type(line) in [ End_provider, End ]:
|
|
|
|
inside = False
|
|
|
|
elif isinstance(line,t):
|
|
|
|
if inside:
|
|
|
|
text.pop(i)
|
|
|
|
begin += 1
|
|
|
|
text.insert(begin,(vars,line))
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
2009-09-07 16:35:34 +02:00
|
|
|
######################################################################
|
|
|
|
def build_needs():
|
2009-09-07 18:02:06 +02:00
|
|
|
# Needs
|
|
|
|
for filename, text in parsed_text:
|
2009-09-09 16:59:43 +02:00
|
|
|
var = None
|
2009-09-07 18:02:06 +02:00
|
|
|
for vars,line in text:
|
|
|
|
if isinstance(line,Begin_provider):
|
|
|
|
buffer = map(strip,line.text.replace(']',',').split(','))
|
|
|
|
var = variables[buffer[1].lower()]
|
|
|
|
var.needs = []
|
|
|
|
var.to_provide = vars
|
|
|
|
elif isinstance(line,End_provider):
|
|
|
|
var.needs = make_single(var.needs)
|
|
|
|
var.to_provide = make_single(var.to_provide)
|
|
|
|
var = None
|
|
|
|
if var is not None:
|
|
|
|
var.needs += vars
|
|
|
|
for v in variables.keys():
|
|
|
|
main = variables[v].same_as
|
|
|
|
if main != v:
|
|
|
|
variables[v].needs = variables[main].needs
|
|
|
|
variables[v].to_provide = variables[main].to_provide
|
2009-09-05 00:18:45 +02:00
|
|
|
|
2009-09-07 18:02:06 +02:00
|
|
|
# Needed_by
|
|
|
|
for v in variables.keys():
|
|
|
|
variables[v].needed_by = []
|
|
|
|
for v in variables.keys():
|
|
|
|
main = variables[v].same_as
|
|
|
|
if main != v:
|
|
|
|
variables[v].needed_by = variables[main].needed_by
|
|
|
|
for v in variables.keys():
|
|
|
|
var = variables[v]
|
2009-09-09 16:59:43 +02:00
|
|
|
if var.is_main:
|
|
|
|
for x in var.needs:
|
|
|
|
variables[x].needed_by.append(var.same_as)
|
2009-09-07 18:02:06 +02:00
|
|
|
for v in variables.keys():
|
|
|
|
var = variables[v]
|
|
|
|
var.needed_by = make_single(var.needed_by)
|
|
|
|
|
|
|
|
|
|
|
|
build_needs()
|
|
|
|
|
2009-09-09 16:59:43 +02:00
|
|
|
result = []
|
|
|
|
for filename,text in parsed_text:
|
|
|
|
text = move_to_top(text,Declaration)
|
|
|
|
text = move_to_top(text,Implicit)
|
|
|
|
text = move_to_top(text,Use)
|
|
|
|
text = move_to_top(text,Cont_provider)
|
|
|
|
result.append ( (filename,text) )
|
|
|
|
parsed_text = result
|
|
|
|
|
2009-09-07 18:02:06 +02:00
|
|
|
######################################################################
|
2009-09-05 00:18:45 +02:00
|
|
|
if __name__ == '__main__':
|
2009-09-07 16:35:34 +02:00
|
|
|
for i in range(len(parsed_text)):
|
2009-09-09 16:59:43 +02:00
|
|
|
if parsed_text[i][0] == 'mpi.irp.f':
|
2009-09-07 16:35:34 +02:00
|
|
|
print '!-------- %s -----------'%(parsed_text[i][0])
|
|
|
|
for line in parsed_text[i][1]:
|
|
|
|
print line[1]
|
2009-09-09 16:59:43 +02:00
|
|
|
print line[0], line[1].filename
|
2009-09-07 18:02:06 +02:00
|
|
|
|