src: cleanup importations
[nit.git] / src / annotation.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 # Management and utilities on annotations
16 module annotation
17
18 import modelbuilder
19 private import literal
20 import model::mmodule_data
21
22 redef class Prod
23 super ANode
24
25 # Try to get its single annotation with a given name
26 # If there is no such an annotation, null is returned.
27 # If there is more than one annotation, a error message is printed and the first annotation is returned
28 fun get_single_annotation(name: String, modelbuilder: ModelBuilder): nullable AAnnotation
29 do
30 var res = get_annotations(name)
31 if res.is_empty then return null
32 if res.length > 1 then
33 modelbuilder.error(res[1], "Error: multiple annotation `{name}`. A previous one is defined line {res[0].location.line_start}")
34 end
35 return res.first
36 end
37
38 # Return all its annotations of a given name in the order of their declaration
39 # Retun an empty array if no such an annotation.
40 fun get_annotations(name: String): Array[AAnnotation]
41 do
42 var res = new Array[AAnnotation]
43 var nas = n_annotations
44 if nas == null then return res
45 for na in nas.n_items do
46 if na.name != name then continue
47 res.add(na)
48 end
49 return res
50 end
51 end
52
53 redef class AAnnotation
54 # The name of the annotation
55 fun name: String
56 do
57 return n_atid.n_id.text
58 end
59
60 # Get the single argument of `self` as a `String`.
61 # Raise error and return null on any inconsistency.
62 fun arg_as_string(modelbuilder: ModelBuilder): nullable String
63 do
64 var args = n_args
65 if args.length == 1 then
66 var arg = args.first.as_string
67 if arg != null then return arg
68 end
69
70 modelbuilder.error(self, "Annotation error: \"{name}\" expects a single String as argument.")
71 return null
72 end
73
74 # Get the single argument of `self` as an `Int`.
75 # Raise error and return null on any inconsistency.
76 fun arg_as_int(modelbuilder: ModelBuilder): nullable Int
77 do
78 var args = n_args
79 if args.length == 1 then
80 var arg = args.first.as_int
81 if arg != null then return arg
82 end
83
84 modelbuilder.error(self, "Annotation error: \"{name}\" expects a single Int as argument.")
85 return null
86 end
87
88 # Get the single argument of `self` as an identifier.
89 # Raise error and return null on any inconsistency.
90 fun arg_as_id(modelbuilder: ModelBuilder): nullable String
91 do
92 var args = n_args
93 if args.length == 1 then
94 var arg = args.first.as_id
95 if arg != null then return arg
96 end
97
98 modelbuilder.error(self, "Annotation error: \"{name}\" expects a single identifier as argument.")
99 return null
100 end
101 end
102
103 redef class AAtArg
104 # Get `self` as a `String`.
105 # Return null if not a string.
106 fun as_string: nullable String
107 do
108 if not self isa AExprAtArg then return null
109 var nexpr = n_expr
110 if not nexpr isa AStringFormExpr then return null
111 return nexpr.value.as(not null)
112 end
113
114 # Get `self` as an `Int`.
115 # Return null if not an integer.
116 fun as_int: nullable Int
117 do
118 if not self isa AExprAtArg then return null
119 var nexpr = n_expr
120 if not nexpr isa AIntExpr then return null
121 return nexpr.value.as(not null)
122 end
123
124 # Get `self` as a single identifier.
125 # Return null if not a single identifier.
126 fun as_id: nullable String
127 do
128 if not self isa AExprAtArg then return null
129 var nexpr = n_expr
130 if not nexpr isa ACallExpr then return null
131 if not nexpr.n_expr isa AImplicitSelfExpr then return null
132 if not nexpr.n_args.n_exprs.is_empty then return null
133 return nexpr.n_id.text
134 end
135 end
136
137 redef class ModelBuilder
138 # Collect all annotations by `name` assocated to `mmodule` and its imported modules.
139 # Note that visibility is not considered.
140 fun collect_annotations_on_modules(name: String, mmodule: MModule): Array[AAnnotation]
141 do
142 var annotations = new Array[AAnnotation]
143 for mmod in mmodule.in_importation.greaters do
144 if not mmodule2nmodule.keys.has(mmod) then continue
145 var amod = mmodule2nmodule[mmod]
146 var module_decl = amod.n_moduledecl
147 if module_decl == null then continue
148 var aas = module_decl.get_annotations(name)
149 annotations.add_all aas
150 end
151 return annotations
152 end
153
154 # Return the single annotation `name` locally assocated to `mmodule`, if any.
155 # Obviously, if there is no ast associated to `mmodule`, then nothing is returned.
156 fun get_mmodule_annotation(name: String, mmodule: MModule): nullable AAnnotation
157 do
158 if not mmodule2nmodule.keys.has(mmodule) then return null
159 var amod = mmodule2nmodule[mmodule]
160 var module_decl = amod.n_moduledecl
161 if module_decl == null then return null
162 var res = module_decl.get_single_annotation(name, self)
163 return res
164 end
165
166 private var collect_annotations_data_cache = new HashMap[String, MModuleData[AAnnotation]]
167
168 # Collect all annotations by `name` in `mmodule` and its importations (direct and indirect)
169 # Note that visibility is not considered.
170 fun collect_annotations_data(name: String, mmodule: MModule): MModuleData[AAnnotation]
171 do
172 var res = collect_annotations_data_cache.get_or_null(name)
173 if res == null then
174 res = new MModuleData[AAnnotation](model)
175 collect_annotations_data_cache[name] = res
176 end
177
178 for mmod in mmodule.in_importation.greaters do
179 if res.has_mmodule(mmod) then continue
180 var ass = get_mmodule_annotation(name, mmod)
181 if ass == null then continue
182 res[mmod] = ass
183 end
184 return res
185 end
186
187 # Get an annotation by name from `mmodule` and its super modules. Will recursively search
188 # in imported module to find the "latest" declaration and detects priority conflicts.
189 fun lookup_annotation_on_modules(name: String, mmodule: MModule): nullable AAnnotation
190 do
191 var data = collect_annotations_data(name, mmodule)
192 var annotations = data.lookup_values(mmodule, none_visibility)
193 if annotations.is_empty then return null
194 if annotations.length > 1 then
195 var locs = new Array[Location]
196 for annot in annotations do locs.add(annot.location)
197
198 toolcontext.error(mmodule.location,
199 "Priority conflict on annotation {name}, it has been defined in: {locs.join(", ")}")
200 end
201 return annotations.first
202 end
203 end