def create_structure_symbol(table): ''' Utility to create a symbol of derived type and add it to the supplied symbol table. :param table: the symbol table to which to add the new symbol. :type table: :py:class:`psyclone.psyir.symbols.SymbolTable` :returns: the new DataSymbol representing a derived type. :rtype: :py:class:`psyclone.psyir.symbols.DataSymbol` ''' region_type = symbols.StructureType.create([ ("nx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC), ("ny", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC), ("domain", symbols.TypeSymbol("dom_type", symbols.DeferredType()), symbols.Symbol.Visibility.PUBLIC) ]) region_type_sym = symbols.TypeSymbol("grid_type", region_type) region_array_type = symbols.ArrayType(region_type_sym, [2, 2]) grid_type = symbols.StructureType.create([ ("dx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC), ("area", region_type_sym, symbols.Symbol.Visibility.PUBLIC), ("levels", region_array_type, symbols.Symbol.Visibility.PUBLIC) ]) grid_type_sym = symbols.TypeSymbol("grid_type", grid_type) grid_var = symbols.DataSymbol("grid", grid_type_sym) table.add(grid_type_sym) table.add(grid_var) return grid_var
def make_component_symbol(): ''' Creates a Symbol of type "grid_type" equivalent to the Fortran: type :: grid_type integer :: nx type(region_type) :: region end type and type :: region_type integer :: startx end type :returns: symbol named "grid" of type "grid_type". :rtype: :py:class:`psyclone.psyir.symbols.DataSymbol` ''' region_type = symbols.StructureType.create([ ("startx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC) ]) region_type_symbol = symbols.TypeSymbol("region_type", region_type) grid_type = symbols.StructureType.create([ ("nx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC), ("region", region_type_symbol, symbols.Symbol.Visibility.PUBLIC) ]) grid_type_symbol = symbols.TypeSymbol("grid_type", grid_type) grid_array_type = symbols.ArrayType(grid_type_symbol, [5]) ssym = symbols.DataSymbol("grid", grid_array_type) return ssym
def test_struc_ref_create(): ''' Tests for the create method. ''' region_type = symbols.StructureType.create([ ("startx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC) ]) region_type_symbol = symbols.TypeSymbol("region_type", region_type) grid_type = symbols.StructureType.create([ ("nx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC), ("region", region_type_symbol, symbols.Symbol.Visibility.PRIVATE), ("sub_grids", symbols.ArrayType(region_type_symbol, [3]), symbols.Symbol.Visibility.PUBLIC), ("data", symbols.ArrayType(symbols.REAL_TYPE, [10, 10]), symbols.Symbol.Visibility.PUBLIC) ]) grid_type_symbol = symbols.TypeSymbol("grid_type", grid_type) ssym = symbols.DataSymbol("grid", grid_type_symbol) # Reference to scalar member of structure sref = nodes.StructureReference.create(ssym, ["nx"]) assert sref.symbol is ssym assert len(sref.children) == 1 assert sref.children[0].name == "nx" check_links(sref, sref.children) # Reference to scalar member of structure member of structure rref = nodes.StructureReference.create(ssym, ["region", "startx"]) assert rref.children[0].name == "region" assert rref.children[0].children[0].name == "startx" check_links(rref.children[0], rref.children[0].children) # Reference to an element of an array member of the structure aref = nodes.StructureReference.create(ssym, [("data", [ nodes.Literal("1", symbols.INTEGER_TYPE), nodes.Literal("5", symbols.INTEGER_TYPE) ])]) assert isinstance(aref.children[0], nodes.ArrayMember) assert aref.children[0].name == "data" assert isinstance(aref.children[0].children[1], nodes.Literal) assert aref.children[0].children[1].value == "5" check_links(aref, aref.children) check_links(aref.children[0], aref.children[0].children) # Reference to an array of structures within a structure structarray_ref = nodes.StructureReference.create( ssym, [("sub_grids", [nodes.Literal("1", symbols.INTEGER_TYPE)])]) assert isinstance(structarray_ref.children[0], nodes.ArrayMember) # Reference to a scalar member of an element of an array of structures # contained in a structure dref = nodes.StructureReference.create( ssym, [("sub_grids", [nodes.Literal("2", symbols.INTEGER_TYPE)]), "startx"]) assert isinstance(dref.children[0], nodes.ArrayOfStructuresMember) assert isinstance(dref.children[0].children[0], nodes.Member) assert isinstance(dref.children[0].children[1], nodes.Literal) check_links(dref, dref.children) check_links(dref.children[0], dref.children[0].children)
def test_struc_ref_str(): ''' Test the __str__ method of StructureReference. ''' grid_type = symbols.StructureType.create([ ("nx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC)]) grid_type_symbol = symbols.TypeSymbol("grid_type", grid_type) ssym = symbols.DataSymbol("grid", grid_type_symbol) # Reference to scalar member of structure sref = nodes.StructureReference.create(ssym, ["nx"]) assert (str(sref) == "StructureReference[name:'grid']\n" "Member[name:'nx']")
def test_reference_accesses(): ''' Test that the reference_accesses method does nothing. This will be addressed by #1028. ''' var_access_info = VariablesAccessInfo() dref = nodes.StructureReference.create( symbols.DataSymbol( "grid", symbols.TypeSymbol("grid_type", symbols.DeferredType())), ["data"]) dref.reference_accesses(var_access_info) assert var_access_info.all_signatures == []
def test_ast_str(): ''' Test that the __str__ method of the StructureReference class works OK when we have an ArrayOfStructuresReference. ''' grid_type = symbols.StructureType.create([ ("nx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC)]) grid_type_symbol = symbols.TypeSymbol("grid_type", grid_type) grid_array_type = symbols.ArrayType(grid_type_symbol, [5]) ssym = symbols.DataSymbol("grid", grid_array_type) asref = nodes.ArrayOfStructuresReference.create( ssym, [nodes.Literal("2", symbols.INTEGER_TYPE)], ["nx"]) assert (str(asref) == "ArrayOfStructuresReference[name:'grid']\n" "Member[name:'nx']\n" "Literal[value:'2', Scalar<INTEGER, UNDEFINED>]")
def test_struc_ref_semantic_nav(): ''' Test the 'member' property of the StructureReference. ''' grid_type = symbols.StructureType.create([ ("nx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC)]) grid_type_symbol = symbols.TypeSymbol("grid_type", grid_type) ssym = symbols.DataSymbol("grid", grid_type_symbol) # Reference to scalar member of structure sref = nodes.StructureReference.create(ssym, ["nx"]) assert sref.member is sref.children[0] # Break the first child to check that we get the expected error sref._children = ["broken"] with pytest.raises(InternalError) as err: _ = sref.member assert ("StructureReference malformed or incomplete. It must have a " "single child that must be a (sub-class of) Member, but " "found: ['broken']" in str(err.value))
def test_struc_ref_validate_child(): ''' Tests for the _validate_child method. ''' grid_type = symbols.StructureType.create([ ("nx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC)]) grid_type_symbol = symbols.TypeSymbol("grid_type", grid_type) ssym = symbols.DataSymbol("grid", grid_type_symbol) # Reference to scalar member of structure sref = nodes.StructureReference.create(ssym, ["nx"]) # a StructureReference is only allowed (at most) one child. with pytest.raises(GenerationError) as err: sref.addchild("wrong") assert ("Item 'str' can't be child 1 of 'StructureReference'" in str(err.value)) # If present, the first child has to be a MemberReference with pytest.raises(GenerationError) as err: sref.children[0] = "wrong" assert ("Item 'str' can't be child 0 of 'StructureReference'" in str(err.value))
def test_struc_ref_create_errors(): ''' Tests for the validation checks in the create method. ''' with pytest.raises(TypeError) as err: _ = nodes.StructureReference.create(None, []) assert ("'symbol' argument to StructureReference.create() should be a " "DataSymbol but found 'NoneType'" in str(err.value)) with pytest.raises(TypeError) as err: _ = nodes.StructureReference.create( symbols.DataSymbol("fake", symbols.INTEGER_TYPE), []) assert ("symbol that is (or could be) a structure, however symbol " "'fake' has type 'Scalar" in str(err.value)) with pytest.raises(TypeError) as err: _ = nodes.StructureReference.create( symbols.DataSymbol("grid", symbols.DeferredType()), 1) assert ("'members' argument to StructureReference._create() must be a " "list but found 'int'" in str(err.value)) with pytest.raises(ValueError) as err: _ = nodes.StructureReference.create( symbols.DataSymbol("grid", symbols.DeferredType()), []) assert ("one or more structure 'members' that are being accessed but " "got an empty list for symbol 'grid'" in str(err.value)) grid_type = symbols.StructureType.create([ ("nx", symbols.INTEGER_TYPE, symbols.Symbol.Visibility.PUBLIC) ]) tsymbol_known = symbols.TypeSymbol("grid_type", grid_type) with pytest.raises(TypeError) as err: _ = nodes.StructureReference.create( symbols.DataSymbol("grid", tsymbol_known), [1]) assert ("'members' passed to StructureType._create() must consist of " "either 'str' or 2-tuple entries but found 'int' in the last " "entry while attempting to create reference to symbol 'grid'" in str(err.value)) with pytest.raises(TypeError) as err: _ = nodes.StructureReference.create( symbols.DataSymbol("grid", tsymbol_known), [1, "hello"]) assert ("'members' passed to StructureType._create() must consist of " "either 'str' or 2-tuple entries but found 'int' while " "attempting to create reference to symbol 'grid'" in str(err.value))