frontend: new phase and annotation `no_warning` to disable warning per module
[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, "multiple-annotation-declarations", "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 deprecated
79 fixed
80 lazy
81 noinit
82 readonly
83 writable
84 autoinit
85 cached
86 nosuper
87 old_style_init
88 abstract
89 intern
90 extern
91 no_warning
92
93 pkgconfig
94 c_compiler_option
95 c_linker_option
96
97 platform
98 """
99
100 # Efficient set build from `primtives_annotations_list`
101 var primtives_annotations = new HashSet[String].from(primtives_annotations_list.split("\n"))
102
103 # All user-declared annotations for each mmodule
104 var user_annotations = new HashMap[MModule, HashSet[String]]
105
106 redef fun process_annotated_node(node, nat)
107 do
108 var name = nat.name
109 if primtives_annotations.has(name) then return
110
111 var mmodule = self.mmodule
112 assert mmodule != null
113
114 # Lazily build the full user-list
115 var annots = user_annotations.get_or_null(mmodule)
116 if annots == null then
117 annots = new HashSet[String]
118 annots.add_all(declared_annotations.lookup_joined_values(mmodule, private_visibility))
119 user_annotations[mmodule] = annots
120 end
121 #nat.debug "for {mmodule}: {annots.join(" ")}"
122
123 if annots.has(name) then return
124
125 toolcontext.modelbuilder.warning(nat, "unknown-annotation", "Warning: unknown annotation `{name}`")
126
127 annots.add(name) # to avoid multiple errors on the same name
128 end
129 end