src: create groups for related things
[nit.git] / src / frontend / check_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 # Check that annotation present in the AST are either primitive or user-declared
16 # There is no verification on the syntax or the semantic of the annotation: just the name is checked
17 #
18 # Specific phases or tools have to process their annotation and check them correclty.
19 module check_annotation
20
21 import phase
22 private import annotation
23
24 redef class ToolContext
25 var check_annotation_phase: Phase = new CheckAnnotationPhase(self, null)
26 end
27
28 private class CheckAnnotationPhase
29 super Phase
30
31 # The map of all declared user annotation
32 # note: lazy because toolcontex is set in the init
33 var declared_annotations = new MModuleMultiData[String](toolcontext.modelbuilder.model) is lazy
34
35 # The current module.
36 var mmodule: nullable MModule = null
37
38 redef fun process_nmodule(nmodule)
39 do
40 # Get the mmodule
41 var mmodule = nmodule.mmodule
42 assert mmodule != null
43 self.mmodule = mmodule
44
45 # If no decl block then quit
46 var nmoduledecl = nmodule.n_moduledecl
47 if nmoduledecl == null then return
48
49 var modelbuilder = toolcontext.modelbuilder
50
51 # Get all the new annotations
52 var annots = nmoduledecl.get_annotations("new_annotation")
53
54 var super_mmodules = declared_annotations.lookup_all_modules(mmodule, private_visibility)
55
56 # Add each new annotations in the map
57 for annot in annots do
58 var name = annot.arg_as_id(modelbuilder)
59 if name == null then continue
60
61 for m in super_mmodules do
62 if declared_annotations[m].has(name) then
63 modelbuilder.warning(annot, "Warning: an annotation `{name}` is already declared in module `{m}`")
64 break label
65 end
66 end
67
68 declared_annotations[mmodule].add(name)
69 #annot.debug "add {mmodule}: {name}"
70 end label
71 end
72
73 # Raw new-line separated list of primitive annotation
74 # Note: empty-lines will be ignored since there is no annotation named by the empty string.
75 var primtives_annotations_list = """
76 new_annotation
77
78 fixed
79 lazy
80 noinit
81 readonly
82 writable
83 cached
84 nosuper
85 old_style_init
86
87 pkgconfig
88 c_compiler_option
89 c_linker_option
90
91 platform
92 """
93
94 # Efficient set build from `primtives_annotations_list`
95 var primtives_annotations = new HashSet[String].from(primtives_annotations_list.split("\n"))
96
97 # All user-declared annotations for each mmodule
98 var user_annotations = new HashMap[MModule, HashSet[String]]
99
100 redef fun process_annotated_node(node, nat)
101 do
102 var name = nat.name
103 if primtives_annotations.has(name) then return
104
105 var mmodule = self.mmodule
106 assert mmodule != null
107
108 # Lazily build the full user-list
109 var annots = user_annotations.get_or_null(mmodule)
110 if annots == null then
111 annots = new HashSet[String]
112 annots.add_all(declared_annotations.lookup_joined_values(mmodule, private_visibility))
113 user_annotations[mmodule] = annots
114 end
115 #nat.debug "for {mmodule}: {annots.join(" ")}"
116
117 if annots.has(name) then return
118
119 toolcontext.modelbuilder.warning(nat, "Warning: unknown annotation `{name}`")
120
121 annots.add(name) # to avoid multiple errors on the same name
122 end
123 end