Debian Codegraph @

src/fpm/manifest/fortran.f90

raw
1 module fpm_manifest_fortran
2 use fpm_error, only : error_t, syntax_error, fatal_error
3 use tomlf, only : toml_table, toml_key, toml_stat
4 use fpm_toml, only : get_value, serializable_t, set_value, set_string
5 implicit none
6 private
7
8 public :: fortran_config_t, new_fortran_config, default_fortran_config
9
10 !> Configuration data for Fortran
11 type, extends(serializable_t) :: fortran_config_t
12
13 !> Enable default implicit typing
14 logical :: implicit_typing = .false.
15
16 !> Enable implicit external interfaces
17 logical :: implicit_external = .false.
18
19 !> Form to use for all Fortran sources
20 character(:), allocatable :: source_form
21
22 contains
23
24 !> Serialization interface
25 procedure :: serializable_is_same => fortran_is_same
26 procedure :: dump_to_toml
27 procedure :: load_from_toml
28
29 end type fortran_config_t
30
31 character(len=*), parameter, private :: class_name = 'fortran_config_t'
32
33 contains
34
35 !> Initialize fortran config
36 subroutine default_fortran_config(self)
37 type(fortran_config_t), intent(inout) :: self
38
39 self%implicit_external = .false.
40 self%implicit_typing = .false.
41 self%source_form = 'free'
42
43 end subroutine default_fortran_config
44
45 !> Construct a new build configuration from a TOML data structure
46 subroutine new_fortran_config(self, table, error)
47
48 !> Instance of the fortran configuration
49 type(fortran_config_t), intent(out) :: self
50
51 !> Instance of the TOML data structure
52 type(toml_table), intent(inout) :: table
53
54 !> Error handling
55 type(error_t), allocatable, intent(out) :: error
56
57 integer :: stat
58 character(:), allocatable :: source_form
59
60 call check(table, error)
61 if (allocated(error)) return
62
63 call get_value(table, "implicit-typing", self%implicit_typing, .false., stat=stat)
64
65 if (stat /= toml_stat%success) then
66 call fatal_error(error,"Error while reading value for 'implicit-typing' in fpm.toml, expecting logical")
67 return
68 end if
69
70 call get_value(table, "implicit-external", self%implicit_external, .false., stat=stat)
71
72 if (stat /= toml_stat%success) then
73 call fatal_error(error,"Error while reading value for 'implicit-external' in fpm.toml, expecting logical")
74 return
75 end if
76
77 call get_value(table, "source-form", source_form, "free", stat=stat)
78
79 if (stat /= toml_stat%success) then
80 call fatal_error(error,"Error while reading value for 'source-form' in fpm.toml, expecting logical")
81 return
82 end if
83 select case(source_form)
84 case default
85 call fatal_error(error,"Value of source-form cannot be '"//source_form//"'")
86 return
87 case("free", "fixed", "default")
88 self%source_form = source_form
89 end select
90
91 end subroutine new_fortran_config
92
93 !> Check local schema for allowed entries
94 subroutine check(table, error)
95
96 !> Instance of the TOML data structure
97 type(toml_table), intent(inout) :: table
98
99 !> Error handling
100 type(error_t), allocatable, intent(out) :: error
101
102 type(toml_key), allocatable :: list(:)
103 integer :: ikey
104
105 call table%get_keys(list)
106
107 ! table can be empty
108 if (size(list) < 1) return
109
110 do ikey = 1, size(list)
111 select case(list(ikey)%key)
112
113 case("implicit-typing", "implicit-external", "source-form")
114 continue
115
116 case default
117 call syntax_error(error, "Key "//list(ikey)%key//" is not allowed in fortran")
118 exit
119
120 end select
121 end do
122
123 end subroutine check
124
125 logical function fortran_is_same(this,that)
126 class(fortran_config_t), intent(in) :: this
127 class(serializable_t), intent(in) :: that
128
129 fortran_is_same = .false.
130
131 select type (other=>that)
132 type is (fortran_config_t)
133 if (this%implicit_typing.neqv.other%implicit_typing) return
134 if (this%implicit_external.neqv.other%implicit_external) return
135 if (allocated(this%source_form).neqv.allocated(other%source_form)) return
136 if (allocated(this%source_form)) then
137 if (.not.this%source_form==other%source_form) return
138 end if
139 class default
140 ! Not the same type
141 return
142 end select
143
144 !> All checks passed!
145 fortran_is_same = .true.
146
147 end function fortran_is_same
148
149 !> Dump install config to toml table
150 subroutine dump_to_toml(self, table, error)
151
152 !> Instance of the serializable object
153 class(fortran_config_t), intent(inout) :: self
154
155 !> Data structure
156 type(toml_table), intent(inout) :: table
157
158 !> Error handling
159 type(error_t), allocatable, intent(out) :: error
160
161 call set_value(table, "implicit-typing", self%implicit_typing, error, class_name)
162 if (allocated(error)) return
163 call set_value(table, "implicit-external", self%implicit_external, error, class_name)
164 if (allocated(error)) return
165 call set_string(table, "source-form", self%source_form, error, class_name)
166 if (allocated(error)) return
167
168 end subroutine dump_to_toml
169
170 !> Read install config from toml table (no checks made at this stage)
171 subroutine load_from_toml(self, table, error)
172
173 !> Instance of the serializable object
174 class(fortran_config_t), intent(inout) :: self
175
176 !> Data structure
177 type(toml_table), intent(inout) :: table
178
179 !> Error handling
180 type(error_t), allocatable, intent(out) :: error
181
182 call get_value(table, "implicit-typing", self%implicit_typing, error, class_name)
183 if (allocated(error)) return
184 call get_value(table, "implicit-external", self%implicit_external, error, class_name)
185 if (allocated(error)) return
186 call get_value(table, "source-form", self%source_form)
187
188 end subroutine load_from_toml
189
190
191 end module fpm_manifest_fortran