Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / platform / platform.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Platform system, used to customize the behavior of the compiler.
16 #
17 # Customisation is done accordingly to the target platform.
18 # Also detects conflicts between targeted platforms.
19 module platform
20
21 import modelize
22 private import parser_util
23 private import annotation
24
25 redef class ToolContext
26 # Detects the `platform` annotation to set a mobile target platform
27 var platform_phase: Phase = new PlatformPhase(self, [modelize_property_phase])
28
29 # Get platform compilation settings from its `name`
30 protected fun platform_from_name(name: String): nullable Platform
31 do
32 return null
33 end
34 end
35
36 private class PlatformPhase
37 super Phase
38
39 redef fun process_annotated_node(nmoduledecl, nat)
40 do
41 var annotation_name = "platform"
42
43 # Skip if we are not interested
44 if nat.name != annotation_name then return
45
46 # Do some validity checks and print errors if the annotation is used incorrectly
47 var modelbuilder = toolcontext.modelbuilder
48 if not nmoduledecl isa AModuledecl then
49 modelbuilder.error(nat, "Syntax Error: only the declaration of modules may use `{annotation_name}`.")
50 return
51 end
52
53 var args = nat.n_args
54 var platform_name
55 if args.length > 1 then
56 modelbuilder.error(nat, "Syntax Error: `{annotation_name}` expects at most a single argument.")
57 return
58 else if args.is_empty then
59 platform_name = nmoduledecl.n_name.collect_text
60 else
61 platform_name = args.first.as_string
62 if platform_name == null then
63 var format_error = "Syntax Error: `{annotation_name}` expects its argument to be the name of the target platform as a String literal."
64 modelbuilder.error(nat, format_error)
65 return
66 end
67 end
68
69 var nmodule = nmoduledecl.parent.as(AModule)
70 var mmodule = nmodule.mmodule
71
72 var platform = toolcontext.platform_from_name(platform_name)
73 if platform == null then
74 toolcontext.error(nat.location, "Error: target platform `{platform_name}` unknown.")
75 return
76 end
77
78 var previous_target_platform = mmodule.target_platform
79 if previous_target_platform != null and previous_target_platform != platform then
80 modelbuilder.error(nat, "Syntax Error: a target platform has already been defined as `{previous_target_platform}`.")
81 end
82
83 mmodule.local_target_platform = platform
84 end
85 end
86
87 redef class MModule
88 private var local_target_platform: nullable Platform = null
89
90 # Recursively get the platform targeted by this module or imported modules
91 fun target_platform: nullable Platform
92 do
93 var ltp = local_target_platform
94 if ltp != null then return ltp
95
96 for mmodule in in_importation.greaters do
97 ltp = mmodule.local_target_platform
98 if ltp != null then return ltp
99 end
100
101 return null
102 end
103 end
104
105 # Sub-classes of `Platform` represent the target platform of a compilation
106 #
107 # Services will be added to this class in other modules.
108 class Platform
109
110 # Simple lower-case name of the platform
111 fun name: nullable String do return null
112
113 # Does the platform provide and support the library `unwind`?
114 fun supports_libunwind: Bool do return true
115
116 # Does the platform provide and supports the Boehm's GC library?
117 fun supports_libgc: Bool do return true
118
119 # Does this platform declare its own main function? If so, we won't generate one in Nit.
120 fun no_main: Bool do return false
121
122 # Does the platform accepts linker scripts?
123 fun supports_linker_script: Bool do return true
124 end