Commits

Craig Rasmussen committed 732785a

Lots of changes to allow mpigen.py to work with entire mpi-f08-interfaces.F90 file

  • Participants
  • Parent commits 9530977

Comments (0)

Files changed (1)

File ompi/mpi/fortran/scripts/pyOFP/tests/mpigen.py

 import sys
 sys.path = ['/usr/local/packages/blaze-core-master',] + sys.path
 
-from blaze.aterm import parse, match, build
+from blaze.aterm import parse, match, build, terms
 
 def err_match(pattern, subject):
    (matches,capture) = match(pattern, subject)
    return capture
 # end err_match
 
+def matches_none(aterm):
+   return match('None', str(aterm))[0]
+# end matches_none
+
+def match_some(aterm):
+   err_match('Some', str(aterm.spine))
+   return aterm.args[0]
+# end match_some
+
+def match_some_tuple(aterm):
+   # return contents of first item in tuple
+   return match_some(aterm).args[0]
+# end match_some_tuple
+
+def match_tuple(aterm):
+   if isinstance(aterm, terms.ATuple) == False:
+      print "Failed to match tuple"
+      print "  ATERM:", aterm
+      quit(1)
+   return aterm.args
+# end match_tuple
+
+def match_list(aterm):
+   if isinstance(aterm, terms.AList) == False:
+      print "Failed to match list"
+      print "  ATERM:", aterm
+      quit(1)
+   return aterm.args
+# end match_tuple
+
 def match_some_or_none(aterm):
    if match('None', str(aterm))[0]:
       return None
 def match_interface_body(aterm):
    err_match('InterfaceBody(<term>,<term>,<term>)', str(aterm))
 
-   ## TODO - match subroutine-stmt or function-stmt
-   err_match('SubroutineStmt(<term>,<term>,<term>,<term>,<str>)', str(aterm.args[0]))
-   err_match('SpecificationPart(<list>,<list>,<term>,<list>)', str(aterm.args[1]))
-   err_match('EndSubroutineStmt(<term>,<term>,<str>)', str(aterm.args[2]))
+   proc_stmt = aterm.args[0]
+   spec_part = aterm.args[1]
+   end_stmt  = aterm.args[2]
 
-   return aterm.args
+   if match('SubroutineStmt(<term>,<term>,<term>,<term>,<str>)', str(proc_stmt))[0]:
+      err_match('SpecificationPart(<list>,<list>,<term>,<list>)', str(spec_part))
+      err_match('EndSubroutineStmt(<term>,<term>,<str>)', str(aterm.args[2]))
+   elif match('FunctionStmt(<term>,<term>,<term>,<list>,<term>,<str>)', str(proc_stmt))[0]:
+      err_match('SpecificationPart(<list>,<list>,<term>,<list>)', str(spec_part))
+      err_match('EndFunctionStmt(<term>,<term>,<str>)', str(end_stmt))
+
+   return (proc_stmt, spec_part, end_stmt)
 
 # end match_interface_body
 
 
 # end match_specification_part(aterm)
 
+def match_kind_selector(aterm):
+   err_match('KindSelector_KIND(None,<term>)', str(aterm))
+   matches,capture = match('KindSelector_KIND(None,<term>)', str(aterm))
+   return match_expr(str(capture[0]))
+# end match_kind_selector
+
+def match_type_param_value(aterm):
+   if match('TypeParamValue_STAR', str(aterm))[0]:
+      return "LEN=*"
+   err_match('TypeParamValue_Expr(<term>)', str(aterm))
+   return match_expr(aterm.args[0])
+# end match_type_param_value
+
+def match_char_selector(aterm):
+   err_match('CharSelector_LS(LengthSelector_LEN(<term>))', str(aterm))
+   (matches,capture) = match('CharSelector_LS(LengthSelector_LEN(<term>))', str(aterm))
+   return match_type_param_value(capture[0])
+# end match_kind_selector
+
 def match_intrinsic_type_spec(aterm):
-   if match('IntrinsicTypeSpec_INTEGER(None)', str(aterm))[0]:
-      return "ftypes.Integer"
-   return "None"
+   matches,capture = match('IntrinsicTypeSpec_INTEGER(<term>)', str(aterm))
+   if matches:
+      type_spec = "ftypes.Integer"
+      if not matches_none(capture[0]):
+         some = match_some(capture[0])
+         kind = match_kind_selector(some)
+         type_spec += '(' + kind + ')'
+      return type_spec
+
+   matches,capture = match('IntrinsicTypeSpec_DOUBLE_PRECISION', str(aterm))
+   if matches:
+      type_spec = "ftypes.Real(kind(0.0D0))"
+      return type_spec
+
+   matches,capture = match('IntrinsicTypeSpec_LOGICAL(<term>)', str(aterm))
+   if matches:
+      type_spec = "ftypes.Logical"
+      if not matches_none(capture[0]):
+         some = match_some(capture[0])
+         kind = match_kind_selector(some)
+         type_spec += '(' + kind + ')'
+      return type_spec
+
+   matches,capture = match('IntrinsicTypeSpec_CHARACTER(<term>)', str(aterm))
+   if matches:
+      type_spec = "ftypes.Character"
+      if not matches_none(capture[0]):
+         some = match_some(capture[0])
+         kind = match_char_selector(some)
+         type_spec += '(' + kind + ')'
+      return type_spec
+
+   print ">>>>>> UNKNOWN_TYPE_SPEC >>>>>>", aterm
+   return "UNKNOWN"
 # end match_intrinsic_type_spec
 
 def match_derived_type_spec(aterm):
    if match('DerivedTypeSpec(<term>,<term>)', str(aterm))[0]:
       return 'ftypes.Derived("{0}")'.format(aterm.args[0].args[0].val)
+
+   print ">>>>>> UNKNOWN_TYPE_SPEC >>>>>>", aterm
    return "None"
 # end match_derived_type_spec
 
    if match('DeclarationTypeSpec_TYPE', str(aterm))[0]:
       return "ftypes.Assumed()"
 
+   print ">>>>>> UNKNOWN_TYPE_SPEC >>>>>>", aterm
    return "None"
 # end match_declaration_type_spec
 
       return 'INTENT(OUT)'
    if match('AttrSpec_INTENT(IntentSpec_INOUT)', str(aterm))[0]:
       return 'INTENT(INOUT)'
+   if match('AttrSpec_ASYNCHRONOUS', str(aterm))[0]:
+      return 'ASYNCHRONOUS'
    if match('AttrSpec_OPTIONAL', str(aterm))[0]:
       return 'OPTIONAL'
    if match('AttrSpec_DIMENSION(ArraySpec(AssumedRankSpec))', str(aterm))[0]:
       return 'DIMENSION(..)'
 
-   print aterm
-   return None
+   print ">>>>>> UNKNOWN_ATTR_SPEC >>>>>>", aterm
+   return 'UNKNOWN_ATTR_SPEC'
 # end match_attr_spec
 
 def match_attr_spec_list(aterm):
-   err_match('Some', str(aterm.spine))
-   err_match('Some', str(aterm.args[0].args[0].spine))
-   err_match('<list>', str(aterm.args[0].args[0].args[0].args[0].args))
-   attributes = aterm.args[0].args[0].args[0].args[0].args
+   if matches_none(aterm): return []
+   some = match_some_tuple(aterm)
+   if matches_none(some): return []
+   some = match_some_tuple(some)
+
+   err_match('<list>', str(some))
+   attributes = some.args
 
    attr_spec_list = []
    for attr in attributes:
    return attr_spec_list
 # end match_attr_spec_list
 
-def match_lower_bounds(aterm):
-   if match('None', str(aterm))[0]:
-      return None
-
-   #err_match('LowerBounds(SpecificationExpr(IntExpr(<term>)', str(aterm))
-   print match('LowerBounds(<term>)', str(aterm))
-   print type(aterm.args), aterm
-
-   return ""
-# end match_array_bounds
-
 def match_data_ref(aterm):
    ##TODO - complete part-ref
    capture = err_match('DataRef(PartRef(<term>,None,None),<list>)', str(aterm))
    return match_data_ref(capture[0])
 # end match_designator
 
+def match_constant(aterm):
+   err_match('Constant_AMB(<term>)', str(aterm))
+   capture = err_match('LiteralConstant(IntLiteralConstant(<term>,<term>))', str(aterm.args[0]))
+   capture = err_match('DigitString(<str>)', str(capture[0]))
+   return capture[0].val
+# end match_constant
+
 def match_primary(aterm):
-   capture = err_match('Primary_AMB(<term>)', str(aterm))
-   return match_designator(capture[0])
+   err_match('Primary_AMB(<term>)', str(aterm))
+
+   if match('Designator_AMB(<term>)', str(aterm.args[0]))[0]:
+      return match_designator(aterm.args[0])
+
+   return match_constant(aterm.args[0])
 # end match_primary
 
 def match_expr(aterm):
 # end match_upper_bounds
 
 def match_array_spec(aterm):
-   err_match('ArraySpec(<list>)', str(aterm))
+   assumed_size = False
+   (matches,capture) = match('ArraySpec(AssumedSizeSpec(<list>,<term>))', str(aterm))
+   if matches:
+      assumed_size = True
+   else:
+      capture = err_match('ArraySpec(<list>)', str(aterm))
+
    spec_list = '('
-   for aspec in aterm.args[0].args:
-      err_match('ExplicitShapeSpec(<term>,<term>)', str(aspec))
-      lb = match_lower_bounds(aspec.args[0])
-      ub = match_upper_bounds(aspec.args[1])
-      spec_list += ub
+   for aspec in capture[0].args:
+      if match('ExplicitShapeSpec(<term>,<term>)', str(aspec.args[0]))[0]:
+         #lb = match_lower_bounds(aspec.args[0])
+         ub = match_upper_bounds(aspec.args[0].args[1])
+         spec_list += ub + ','
+      elif match('ImpliedShapeSpec(<term>)', str(aspec))[0]:
+         #lb = Some argument, TODO
+         spec_list += '*' + ','
+
+   if assumed_size:
+      spec_list += '*'
+   
+   spec_list = spec_list.rstrip(',')
    return spec_list + ')'
 # end match_array_spec_list
 
-
 def match_entity_decl_list(aterm):
    err_match('<list>', str(aterm))
    entities = aterm.args
       entity_decl = match_name(capture[0])
 
       # grab the array-spec
-      array_spec = match_some_or_none(capture[1])
-      if array_spec:
-         entity_decl += match_array_spec(array_spec)
+      #array_spec = match_some_or_none(capture[1])
+      #if array_spec:
+      #   entity_decl += match_array_spec(array_spec)
+
+      if not matches_none(capture[1]):
+         entity_decl += match_array_spec(match_some_tuple(capture[1]))
 
       entity_decl_list += [entity_decl,]
 
 # end match_only_list
 
 def match_use_stmt(aterm):
-   err_match('UseStmt(<term>,<term>,<term>,<list>,<str>)', str(aterm))
+   (matches,capture) = match('UseStmt_Rename(<term>,<term>,<term>,<term>,<str>)', str(aterm))
+   if not matches:
+     err_match('UseStmt_Only(<term>,<term>,<term>,<list>,<str>)', str(aterm))
+
    label = match_label(aterm.args[0])
-   nature = None # FIXME
+
+   ## grab the module-nature
+   #
+   nature = None
+   some = match_some_tuple(aterm.args[1])
+   if not matches_none(some):
+      some = match_some_tuple(some)
+      err_match('ModuleNature_INTRINSIC', str(some))
+      nature = '"' + 'intrinsic' + '"'
    name = match_name(aterm.args[2])
-   only_list = match_only_list(aterm.args[3])
+   only_list = []
+   if not matches_none(aterm.args[3]):
+      only_list = match_only_list(aterm.args[3])
    return (label, nature, name, only_list)
 # end match_use_stmt
 
    return (label, type_spec, attr_spec_list, entity_decl_list)
 # end match_type_declaration_stmt
 
+def match_procedure_declaration_stmt(aterm):
+   err_match('ProcedureDeclarationStmt(<term>,<term>,<term>,<list>,<str>)', str(aterm))
+   label = match_label(aterm.args[0])
+   interface = match_some(aterm.args[1])
+   interface_name = err_match('ProcInterface(<str>)', str(interface))[0].val
+   err_match('Some((None))', str(aterm.args[2]))
+   proc_decl = err_match('<list>', str(aterm.args[3]))[0].args[0]
+   #TODO - add entire list (and make it a real proc_decl
+   proc_decl_name = err_match('ProcDecl(<str>,None)', str(proc_decl))[0].val
+
+   return (label, '"'+interface_name+'"', [proc_decl_name])
+# end match_type_declaration_stmt
+
 def match_declaration_construct(aterm):
-   err_match('DeclarationConstruct(<term>)', str(aterm))
-   return match_type_declaration_stmt(aterm.args[0])
+   construct = err_match('DeclarationConstruct(<term>)', str(aterm))[0]
+   return construct
 # end match_declaration_construct
 
 
 
 for declaration_construct in declaration_constructs.args:
 
-  #declaration_construct  = declaration_constructs.args[1]
-
   (matches,payload) = match('DeclarationConstruct(<term>)', str(declaration_construct))
   interface_block = declaration_construct.args[0]
 
-  # interface-block : InterfaceStmt * List(InterfaceSpecification) * EndInterfaceStmt -> InterfaceBlock
   (matches,payload) = match('InterfaceBlock(<term>,<list>,<term>)', str(interface_block))
   interface_stmt = interface_block.args[0]
   interface_specifications = interface_block.args[1]
   (matches,payload) = match('InterfaceStmt(<term>,<term>,<str>)', str(interface_stmt))
   (matches,payload) = match('Some(<term>)', str(interface_stmt.args[1]))
   generic_spec = interface_stmt.args[1].args[0]
-  interface_name = generic_spec.args[0]
+  interface_name = '"' + match_name(generic_spec.args[0]) + '"'
 
   if len(interface_specifications.args) != 1:
       print "ERROR: expected [interface-specification] length of 1"
   (matches,payload) = match('InterfaceSpecification(<term>)', str(interface_specification))
   interface_body = interface_specification.args[0]
 
-  (subroutine_stmt, spec_part, end_subroutine_stmt) = match_interface_body(interface_body)
+  (proc_stmt, spec_part, end_proc_stmt) = match_interface_body(interface_body)
 
-  proc_name    = subroutine_stmt.args[2].args[0]
-  args_binding = subroutine_stmt.args[3]
+  (matches,capture) = match('FunctionStmt(<term>,<term>,<term>,<list>,<term>,<str>)', str(proc_stmt))
+  if not matches:
+    (matches,capture) = match('SubroutineStmt(<term>,<term>,<term>,<term>,<str>)', str(proc_stmt))
+  
 
-  (matches,payload) = match('None', str(args_binding))
-  if matches:
-      print "ERROR: No argument list found for subroutine"
-  args_binding = args_binding.args[0]
+  proc_name = '"' + match_name(capture[2]) + '"'
+  proc_args = []
 
-  (matches,payload) = match('(<list>,<term>)', str(args_binding))
+  if len(capture[3].args) > 0:
+     proc_args = match_tuple(capture[3].args[0])
+     proc_args = match_list(proc_args[0])
+
   dummy_arg_list = []
-  for arg in payload[0].args:
-      dummy_arg_list += [arg.args[0].args[0],]
+  for arg in proc_args:
+      dummy_arg_list += [match_name(arg.args[0]),]
 
   (uses, imports, implicit_part, decl_constructs) = match_specification_part(spec_part)
   decl_construct_list = decl_constructs.args
     print use_str, label, ',', nature, ',', name, ',', only_list, ')'
 
   for decl in decl_construct_list:
-     (label,type_spec,attr_spec_list,entity_decl_list) = match_declaration_construct(decl)
-     decl_str = indent + 'ast.VarDecl(fdecl,'
-     print decl_str, type_spec, ',', attr_spec_list, ',', entity_decl_list, ')'
+     construct = match_declaration_construct(decl)
+     if match('TypeDeclarationStmt', str(construct.spine))[0]:
+        (label,type_spec,attr_spec_list,entity_decl_list) = match_type_declaration_stmt(construct)
+        decl_str = indent + 'ast.VarDecl(fdecl,'
+        print decl_str, type_spec, ',', attr_spec_list, ',', entity_decl_list, ')'
+     elif match('ProcedureDeclarationStmt', str(construct.spine))[0]:
+        (label, interface_name, proc_decl_list) = match_procedure_declaration_stmt(construct)
+        decl_str = indent + 'ast.ProcDecl(fdecl,'
+        print decl_str, interface_name, ',', proc_decl_list, ')'
 
   print "\n"