1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Platform system, used to customize the behavior of the compiler.
17 # Customisation is done accordingly to the target platform.
18 # Also detects conflicts between targeted platforms.
22 private import parser_util
23 private import annotation
25 redef class ToolContext
26 var platform_phase
: Phase = new PlatformPhase(self, [modelize_property_phase
])
28 protected fun platform_from_name
(name
: String): nullable Platform
34 private class PlatformPhase
37 redef fun process_annotated_node
(nmoduledecl
, nat
)
39 var annotation_name
= "platform"
41 # Skip if we are not interested
42 if nat
.name
!= annotation_name
then return
44 # Do some validity checks and print errors if the annotation is used incorrectly
45 var modelbuilder
= toolcontext
.modelbuilder
46 if not nmoduledecl
isa AModuledecl then
47 modelbuilder
.error
(nat
, "Syntax error: only the declaration of modules may use \"{annotation_name}\
".")
53 if args
.length
> 1 then
54 modelbuilder
.error
(nat
, "Syntax error: \"{annotation_name}\
" expects at most a single argument.")
56 else if args
.is_empty
then
57 platform_name
= nmoduledecl
.n_name
.collect_text
59 platform_name
= args
.first
.as_string
60 if platform_name
== null then
61 var format_error
= "Syntax error: \"{annotation_name}\
" expects its argument to be the name of the target platform as a String literal."
62 modelbuilder
.error
(nat
, format_error
)
67 var nmodule
= nmoduledecl
.parent
.as(AModule)
68 var mmodule
= nmodule
.mmodule
70 var platform
= toolcontext
.platform_from_name
(platform_name
)
71 if platform
== null then
72 toolcontext
.error
(nat
.location
, "Error: target platform \"{platform_name}\
" unknown")
76 var previous_target_platform
= mmodule
.target_platform
77 if previous_target_platform
!= null and previous_target_platform
!= platform
then
78 modelbuilder
.error
(nat
, "Syntax error: a target platform has already been defined as \"{previous_target_platform}\
".")
81 mmodule
.local_target_platform
= platform
86 private var local_target_platform
: nullable Platform = null
88 # Recursively get the platform targeted by this module or imported modules
89 fun target_platform
: nullable Platform
91 var ltp
= local_target_platform
92 if ltp
!= null then return ltp
94 for mmodule
in in_importation
.greaters
do
95 ltp
= mmodule
.local_target_platform
96 if ltp
!= null then return ltp
103 # Sub-classes of `Platform` represent the target platform of a compilation
105 # Services will be added to this class in other modules.
106 abstract class Platform
107 # Does the platform provide and support the library `unwind`?
108 fun supports_libunwind
: Bool do return true
110 # Does the platform provide and supports the Boehm's GC library?
111 fun supports_libgc
: Bool do return true
113 # Does this platform declare its own main function? If so, we won't generate one in Nit.
114 fun no_main
: Bool do return false