src: update most tools to new constructors
[nit.git] / src / frontend / cached.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 # Implementation of the method-related annotation `cached`
16 #
17 # Note this module can be used as a reference on how to implements
18 # complex annotation that modify both the model and the AST of a Nit program
19 module cached
20
21 import modelize
22 private import parser_util
23 import simple_misc_analysis
24 private import annotation
25 intrude import modelize::modelize_property
26
27 redef class ToolContext
28 var cached_phase: Phase = new CachedPhase(self, [modelize_property_phase])
29 end
30
31 private class CachedPhase
32 super Phase
33
34 init
35 do
36 # FIXME The phase has to be executed just after `modelize_property_phase`
37 # But there is no simple way to express this
38 # So, for the moment, I just looked at the linearization and see what phase is after `modelize_property_phase`
39 # And inserted before it
40 toolcontext.phases.add_edge(toolcontext.simple_misc_analysis_phase, self)
41 end
42
43 redef fun process_annotated_node(npropdef, nat)
44 do
45 # Skip if we are not interested
46 if nat.name != "cached" then return
47
48 # Do some validity checks and print errors if the annotation is used incorrectly
49 var modelbuilder = toolcontext.modelbuilder
50
51 if not npropdef isa AMethPropdef then
52 modelbuilder.error(npropdef, "Syntax error: only a function can be cached.")
53 return
54 end
55
56 var mpropdef = npropdef.mpropdef.as(not null)
57
58 var mtype = mpropdef.msignature.return_mtype
59 if mtype == null then
60 modelbuilder.error(npropdef, "Syntax error: only a function can be cached.")
61 return
62 end
63
64 if not npropdef.n_signature.n_params.is_empty then
65 modelbuilder.error(npropdef, "Syntax error: only a function without arguments can be cached.")
66 return
67 end
68
69 # OK, let we do some meta-programming...
70
71 var location = npropdef.location
72 var name = mpropdef.mproperty.name
73 var nclassdef = npropdef.parent.as(AClassdef)
74 var mclassdef = nclassdef.mclassdef.as(not null)
75
76 if not mclassdef.mclass.kind.need_init then
77 modelbuilder.error(npropdef, "Error: only abstract and concrete classes can have cached functions.")
78 return
79 end
80
81 # Create a new private attribute to store the cache
82 var cache_mpropdef = new MAttributeDef(mclassdef, new MAttribute(mclassdef, "@{name}<cache>", private_visibility), location)
83 cache_mpropdef.static_mtype = mtype.as_nullable
84
85 # Create another new private attribute to store the boolean «is the function cached?»
86 # The point is to manage the case where `null` is a genuine return value of the method
87 var is_cached_mpropdef = new MAttributeDef(mclassdef, new MAttribute(mclassdef, "@{name}<is_cached>", private_visibility), location)
88 is_cached_mpropdef.static_mtype = mclassdef.mmodule.get_primitive_class("Bool").mclass_type
89 # FIXME? Because there is a default value ("false") a real propdef is required
90 var is_cached_npropdef = toolcontext.parse_propdef("var is_cached = false").as(AAttrPropdef)
91 associate_propdef(is_cached_mpropdef, is_cached_npropdef)
92
93 # Create a new private method to do the real work
94 var real_mpropdef = new MMethodDef(mclassdef, new MMethod(mclassdef, "{name}<real>", private_visibility), location)
95 real_mpropdef.msignature = mpropdef.msignature
96 # FIXME: Again, if the engine require a real propdef even if it is empty
97 var real_npropdef = toolcontext.parse_propdef("fun real do end").as(AMethPropdef)
98 associate_propdef(real_mpropdef, real_npropdef)
99 # Note: the body is set at the last line of this function
100
101 # Save the original body
102 var real_body = npropdef.n_block.as(not null)
103
104 # Replace the original body with a new body that do the proxy'n'cache work
105 var proxy_body = toolcontext.parse_stmts("if self._is_cached then return self._cache.as(not null)\nvar res = call_real\nself._cache_write = res\nself._is_cached_write = true\nreturn res")
106 real_body.replace_with(proxy_body)
107
108 # Do some transformation on the identifiers used on the proxy body so that correct entities are designated
109 # FIXME: we just trick the following phases into associating by name some tokens with some model-entities
110 # But this is bad at at least two levels
111 # - we already know the real model-entities, so why doing latter the association and not now?
112 # - associating by names may cause a useless fragility (name-conflicts, etc.)
113 proxy_body.collect_tokens_by_text("_is_cached").first.text = is_cached_mpropdef.mproperty.name
114 proxy_body.collect_tokens_by_text("_is_cached_write").first.text = is_cached_mpropdef.mproperty.name
115 proxy_body.collect_tokens_by_text("_cache").first.text = cache_mpropdef.mproperty.name
116 proxy_body.collect_tokens_by_text("_cache_write").first.text = cache_mpropdef.mproperty.name
117 proxy_body.collect_tokens_by_text("call_real").first.text = real_mpropdef.mproperty.name
118
119 # FIXME a last transformation cannot be done yet. So, the call to `super` (`ASuperExpr`) is broken in cached methods.
120
121 # Give the original body to the private real methoddef
122 real_npropdef.n_block.replace_with(real_body)
123 end
124
125 # Detach `n` from its original AST and attach it to `m` (and its related AST)
126 # `n` must not be already attached to an existing model entity
127 # `m` must not be already attached to an existing AST node
128 fun associate_propdef(m: MPropDef, n: APropdef)
129 do
130 # FIXME: the model-AST relations **must** be rationalized:
131 # * 1- fragility: the risk of inconsistencies is too hight
132 # * 2- complexity: there is too much paths to access the same things
133
134 # Easy attach
135 assert n.mpropdef == null
136 n.mpropdef = m
137
138 # Required to so that look-for implementation works
139 assert not toolcontext.modelbuilder.mpropdef2npropdef.has_key(m)
140 toolcontext.modelbuilder.mpropdef2npropdef[m] = n
141
142 var mclassdef = m.mclassdef
143 var nclassdef = toolcontext.modelbuilder.mclassdef2nclassdef[mclassdef]
144 # Sanity checks
145 assert nclassdef.mclassdef == mclassdef
146
147 if n isa AAttrPropdef then
148 n.has_value = n.n_expr != null or n.n_block != null
149 end
150
151 # Required so that propdef are visited in visitors
152 if not nclassdef.n_propdefs.has(n) then nclassdef.n_propdefs.add(n)
153 end
154 end