# complex annotation that modify both the model and the AST of a Nit program
module cached
-import modelize_property
-import parser_util
+import modelize
+private import parser_util
import simple_misc_analysis
+private import annotation
redef class ToolContext
var cached_phase: Phase = new CachedPhase(self, [modelize_property_phase])
redef fun process_annotated_node(npropdef, nat)
do
# Skip if we are not interested
- if nat.n_atid.n_id.text != "cached" then return
+ if nat.name != "cached" then return
# Do some validity checks and print errors if the annotation is used incorrectly
var modelbuilder = toolcontext.modelbuilder
var location = npropdef.location
var name = mpropdef.mproperty.name
- var nclassdef = npropdef.parent.as(AStdClassdef)
+ var nclassdef = npropdef.parent.as(AClassdef)
var mclassdef = nclassdef.mclassdef.as(not null)
+ if not mclassdef.mclass.kind.need_init then
+ modelbuilder.error(npropdef, "Error: only abstract and concrete classes can have cached functions.")
+ return
+ end
+
# Create a new private attribute to store the cache
var cache_mpropdef = new MAttributeDef(mclassdef, new MAttribute(mclassdef, "@{name}<cache>", private_visibility), location)
cache_mpropdef.static_mtype = mtype.as_nullable