| 1 |
! { dg-do compile } |
| 2 |
! { dg-options "-fdump-tree-original" } |
| 3 |
! |
| 4 |
! Test the fix for PR117763, which was a regression caused by the patch for |
| 5 |
! PR109345. |
| 6 |
! |
| 7 |
! Contributed by Juergen Reuter <[email protected]> |
| 8 |
! |
| 9 |
module iso_varying_string |
| 10 |
implicit none |
| 11 |
integer, parameter, private :: GET_BUFFER_LEN = 1 |
| 12 |
|
| 13 |
type, public :: varying_string |
| 14 |
private |
| 15 |
character(LEN=1), dimension(:), allocatable :: chars |
| 16 |
end type varying_string |
| 17 |
|
| 18 |
interface assignment(=) |
| 19 |
module procedure op_assign_CH_VS |
| 20 |
module procedure op_assign_VS_CH |
| 21 |
end interface assignment(=) |
| 22 |
|
| 23 |
interface char |
| 24 |
module procedure char_auto |
| 25 |
module procedure char_fixed |
| 26 |
end interface char |
| 27 |
|
| 28 |
interface len |
| 29 |
module procedure len_ |
| 30 |
end interface len |
| 31 |
|
| 32 |
interface var_str |
| 33 |
module procedure var_str_ |
| 34 |
end interface var_str |
| 35 |
|
| 36 |
public :: assignment(=) |
| 37 |
public :: char |
| 38 |
public :: len |
| 39 |
public :: var_str |
| 40 |
|
| 41 |
private :: op_assign_CH_VS |
| 42 |
private :: op_assign_VS_CH |
| 43 |
private :: char_auto |
| 44 |
private :: char_fixed |
| 45 |
private :: len_ |
| 46 |
private :: var_str_ |
| 47 |
|
| 48 |
contains |
| 49 |
|
| 50 |
elemental function len_ (string) result (length) |
| 51 |
type(varying_string), intent(in) :: string |
| 52 |
integer :: length |
| 53 |
if(ALLOCATED(string%chars)) then |
| 54 |
length = SIZE(string%chars) |
| 55 |
else |
| 56 |
length = 0 |
| 57 |
endif |
| 58 |
end function len_ |
| 59 |
|
| 60 |
elemental subroutine op_assign_CH_VS (var, exp) |
| 61 |
character(LEN=*), intent(out) :: var |
| 62 |
type(varying_string), intent(in) :: exp |
| 63 |
var = char(exp) |
| 64 |
end subroutine op_assign_CH_VS |
| 65 |
|
| 66 |
elemental subroutine op_assign_VS_CH (var, exp) |
| 67 |
type(varying_string), intent(out) :: var |
| 68 |
character(LEN=*), intent(in) :: exp |
| 69 |
var = var_str(exp) |
| 70 |
end subroutine op_assign_VS_CH |
| 71 |
|
| 72 |
pure function char_auto (string) result (char_string) |
| 73 |
type(varying_string), intent(in) :: string |
| 74 |
character(LEN=len(string)) :: char_string |
| 75 |
integer :: i_char |
| 76 |
forall(i_char = 1:len(string)) |
| 77 |
char_string(i_char:i_char) = string%chars(i_char) |
| 78 |
end forall |
| 79 |
end function char_auto |
| 80 |
|
| 81 |
pure function char_fixed (string, length) result (char_string) |
| 82 |
type(varying_string), intent(in) :: string |
| 83 |
integer, intent(in) :: length |
| 84 |
character(LEN=length) :: char_string |
| 85 |
char_string = char(string) |
| 86 |
end function char_fixed |
| 87 |
|
| 88 |
elemental function var_str_ (char) result (string) |
| 89 |
character(LEN=*), intent(in) :: char |
| 90 |
type(varying_string) :: string |
| 91 |
integer :: length |
| 92 |
integer :: i_char |
| 93 |
length = LEN(char) |
| 94 |
ALLOCATE(string%chars(length)) |
| 95 |
forall(i_char = 1:length) |
| 96 |
string%chars(i_char) = char(i_char:i_char) |
| 97 |
end forall |
| 98 |
end function var_str_ |
| 99 |
|
| 100 |
end module iso_varying_string |
| 101 |
|
| 102 |
module model_data |
| 103 |
use, intrinsic :: iso_c_binding !NODEP! |
| 104 |
use iso_varying_string, string_t => varying_string |
| 105 |
|
| 106 |
implicit none |
| 107 |
private |
| 108 |
|
| 109 |
public :: field_data_t |
| 110 |
public :: model_data_t |
| 111 |
|
| 112 |
type :: field_data_t |
| 113 |
private |
| 114 |
type(string_t) :: longname |
| 115 |
integer :: pdg = 0 |
| 116 |
logical :: has_anti = .false. |
| 117 |
type(string_t), dimension(:), allocatable :: name, anti |
| 118 |
type(string_t) :: tex_name |
| 119 |
integer :: multiplicity = 1 |
| 120 |
contains |
| 121 |
procedure :: init => field_data_init |
| 122 |
procedure :: set => field_data_set |
| 123 |
procedure :: get_longname => field_data_get_longname |
| 124 |
procedure :: get_name_array => field_data_get_name_array |
| 125 |
end type field_data_t |
| 126 |
|
| 127 |
type :: model_data_t |
| 128 |
private |
| 129 |
type(field_data_t), dimension(:), allocatable :: field |
| 130 |
contains |
| 131 |
generic :: init => model_data_init |
| 132 |
procedure, private :: model_data_init |
| 133 |
procedure :: get_field_array_ptr => model_data_get_field_array_ptr |
| 134 |
procedure :: get_field_ptr_by_index => model_data_get_field_ptr_index |
| 135 |
procedure :: init_sm_test => model_data_init_sm_test |
| 136 |
end type model_data_t |
| 137 |
|
| 138 |
|
| 139 |
contains |
| 140 |
|
| 141 |
subroutine field_data_init (prt, longname, pdg) |
| 142 |
class(field_data_t), intent(out) :: prt |
| 143 |
type(string_t), intent(in) :: longname |
| 144 |
integer, intent(in) :: pdg |
| 145 |
prt%longname = longname |
| 146 |
prt%pdg = pdg |
| 147 |
prt%tex_name = "" |
| 148 |
end subroutine field_data_init |
| 149 |
|
| 150 |
subroutine field_data_set (prt, & |
| 151 |
name, anti, tex_name) |
| 152 |
class(field_data_t), intent(inout) :: prt |
| 153 |
type(string_t), dimension(:), intent(in), optional :: name, anti |
| 154 |
type(string_t), intent(in), optional :: tex_name |
| 155 |
if (present (name)) then |
| 156 |
if (allocated (prt%name)) deallocate (prt%name) |
| 157 |
allocate (prt%name (size (name)), source = name) |
| 158 |
end if |
| 159 |
if (present (anti)) then |
| 160 |
if (allocated (prt%anti)) deallocate (prt%anti) |
| 161 |
allocate (prt%anti (size (anti)), source = anti) |
| 162 |
prt%has_anti = .true. |
| 163 |
end if |
| 164 |
if (present (tex_name)) prt%tex_name = tex_name |
| 165 |
end subroutine field_data_set |
| 166 |
|
| 167 |
pure function field_data_get_longname (prt) result (name) |
| 168 |
type(string_t) :: name |
| 169 |
class(field_data_t), intent(in) :: prt |
| 170 |
name = prt%longname |
| 171 |
end function field_data_get_longname |
| 172 |
|
| 173 |
subroutine field_data_get_name_array (prt, is_antiparticle, name) |
| 174 |
class(field_data_t), intent(in) :: prt |
| 175 |
logical, intent(in) :: is_antiparticle |
| 176 |
type(string_t), dimension(:), allocatable, intent(inout) :: name |
| 177 |
if (allocated (name)) deallocate (name) |
| 178 |
if (is_antiparticle) then |
| 179 |
if (prt%has_anti) then |
| 180 |
allocate (name (size (prt%anti))) |
| 181 |
name = prt%anti |
| 182 |
else |
| 183 |
allocate (name (0)) |
| 184 |
end if |
| 185 |
else |
| 186 |
allocate (name (size (prt%name))) |
| 187 |
name = prt%name |
| 188 |
end if |
| 189 |
end subroutine field_data_get_name_array |
| 190 |
|
| 191 |
subroutine model_data_init (model, n_field) |
| 192 |
class(model_data_t), intent(out) :: model |
| 193 |
integer, intent(in) :: n_field |
| 194 |
allocate (model%field (n_field)) |
| 195 |
end subroutine model_data_init |
| 196 |
|
| 197 |
function model_data_get_field_array_ptr (model) result (ptr) |
| 198 |
class(model_data_t), intent(in), target :: model |
| 199 |
type(field_data_t), dimension(:), pointer :: ptr |
| 200 |
ptr => model%field |
| 201 |
end function model_data_get_field_array_ptr |
| 202 |
|
| 203 |
function model_data_get_field_ptr_index (model, i) result (ptr) |
| 204 |
class(model_data_t), intent(in), target :: model |
| 205 |
integer, intent(in) :: i |
| 206 |
type(field_data_t), pointer :: ptr |
| 207 |
ptr => model%field(i) |
| 208 |
end function model_data_get_field_ptr_index |
| 209 |
|
| 210 |
subroutine model_data_init_sm_test (model) |
| 211 |
class(model_data_t), intent(out) :: model |
| 212 |
type(field_data_t), pointer :: field |
| 213 |
integer :: i |
| 214 |
call model%init (2) |
| 215 |
i = 0 |
| 216 |
i = i + 1 |
| 217 |
field => model%get_field_ptr_by_index (i) |
| 218 |
call field%init (var_str ("W_BOSON"), 24) |
| 219 |
call field%set (name = [var_str ("W+")], anti = [var_str ("W-")]) |
| 220 |
i = i + 1 |
| 221 |
field => model%get_field_ptr_by_index (i) |
| 222 |
call field%init (var_str ("HIGGS"), 25) |
| 223 |
call field%set (name = [var_str ("H")]) |
| 224 |
end subroutine model_data_init_sm_test |
| 225 |
|
| 226 |
end module model_data |
| 227 |
|
| 228 |
|
| 229 |
module models |
| 230 |
use, intrinsic :: iso_c_binding !NODEP! |
| 231 |
use iso_varying_string, string_t => varying_string |
| 232 |
use model_data |
| 233 |
! use parser |
| 234 |
! use variables |
| 235 |
implicit none |
| 236 |
private |
| 237 |
public :: model_t |
| 238 |
|
| 239 |
type, extends (model_data_t) :: model_t |
| 240 |
private |
| 241 |
contains |
| 242 |
procedure :: append_field_vars => model_append_field_vars |
| 243 |
end type model_t |
| 244 |
|
| 245 |
contains |
| 246 |
|
| 247 |
subroutine model_append_field_vars (model) |
| 248 |
class(model_t), intent(inout) :: model |
| 249 |
type(field_data_t), dimension(:), pointer :: field_array |
| 250 |
type(field_data_t), pointer :: field |
| 251 |
type(string_t) :: name |
| 252 |
type(string_t), dimension(:), allocatable :: name_array |
| 253 |
integer :: i, j |
| 254 |
field_array => model%get_field_array_ptr () |
| 255 |
do i = 1, size (field_array) |
| 256 |
name = field_array(i)%get_longname () |
| 257 |
call field_array(i)%get_name_array (.false., name_array) |
| 258 |
end do |
| 259 |
end subroutine model_append_field_vars |
| 260 |
|
| 261 |
end module models |
| 262 |
|
| 263 |
|
| 264 |
program main_ut |
| 265 |
use iso_varying_string, string_t => varying_string |
| 266 |
use model_data |
| 267 |
use models |
| 268 |
implicit none |
| 269 |
|
| 270 |
class(model_data_t), pointer :: model |
| 271 |
model => null () |
| 272 |
allocate (model_t :: model) |
| 273 |
select type (model) |
| 274 |
type is (model_t) |
| 275 |
call model%init_sm_test () |
| 276 |
call model%append_field_vars () |
| 277 |
end select |
| 278 |
end program main_ut |
| 279 |
! { dg-final { scan-tree-dump-times "__result->span = \[12\].." 1 "original" } } |