nitg: use libgc only if platform supports it
[nit.git] / src / 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 according
16 # to the target platform. Also detects conflicts between targetted platforms.
17 module platform
18
19 import modelize_property
20 import parser_util
21 import modelbuilder
22
23 redef class ToolContext
24 var platform_phase: Phase = new PlatformPhase(self, [modelize_property_phase])
25
26 protected fun platform_from_name(name: String): nullable Platform
27 do
28 return null
29 end
30 end
31
32 private class PlatformPhase
33 super Phase
34
35 redef fun process_annotated_node(nmoduledecl, nat)
36 do
37 var annotation_name = "platform"
38
39 # Skip if we are not interested
40 if nat.n_atid.n_id.text != annotation_name then return
41
42 # Do some validity checks and print errors if the annotation is used incorrectly
43 var modelbuilder = toolcontext.modelbuilder
44 if not nmoduledecl isa AModuledecl then
45 modelbuilder.error(nat, "Syntax error: only the declaration of modules may use \"{annotation_name}\".")
46 return
47 end
48
49 var args = nat.n_args
50 var platform_name
51 if args.length > 1 then
52 modelbuilder.error(nat, "Syntax error: \"{annotation_name}\" expects at most a single argument.")
53 return
54 else if args.is_empty then
55 platform_name = nmoduledecl.n_name.collect_text
56 else
57 var arg = args.first
58 var format_error = "Syntax error: \"{annotation_name}\" expects its argument to be the name of the target platform as a String literal."
59
60 if not arg isa AExprAtArg then
61 modelbuilder.error(nat, format_error)
62 return
63 end
64
65 var expr = arg.n_expr
66 if not expr isa AStringFormExpr then
67 modelbuilder.error(nat, format_error)
68 return
69 end
70 var target = expr.collect_text
71 platform_name = target.substring(1, target.length-2)
72 end
73
74 var nmodule = nmoduledecl.parent.as(AModule)
75 var mmodule = nmodule.mmodule
76
77 var platform = toolcontext.platform_from_name(platform_name)
78 if platform == null then
79 toolcontext.error(nat.location, "Error: target platform \"{platform_name}\" unknown")
80 return
81 end
82
83 var previous_target_platform = mmodule.target_platform
84 if previous_target_platform != null and previous_target_platform != platform then
85 modelbuilder.error(nat, "Syntax error: a target platform has already been defined as \"{previous_target_platform}\".")
86 end
87
88 mmodule.local_target_platform = platform
89 end
90 end
91
92 redef class MModule
93 private var local_target_platform: nullable Platform = null
94
95 # Recusively get the platform targetted by this module or imported modules
96 fun target_platform: nullable Platform
97 do
98 var ltp = local_target_platform
99 if ltp != null then return ltp
100
101 for mmodule in in_importation.greaters do
102 ltp = mmodule.local_target_platform
103 if ltp != null then return ltp
104 end
105
106 return null
107 end
108 end
109
110 # Sub-classes of `Platform` represent the target platform of a compilation
111 #
112 # Services will be added to this class in other modules.
113 abstract class Platform
114 fun supports_libunwind: Bool do return true
115
116 fun supports_libgc: Bool do return true
117
118 # Does this platform declare its own main function? If so, we won't generate one in Nit.
119 fun no_main: Bool do return false
120 end