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