5cfe9b28fb7ec7e63e7acea63887d0d72b964692
[nit.git] / src / model / model_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # The abstract concept of model and related common things
18 module model_base
19
20 # The container class of a Nit object-oriented model.
21 # A model knows modules, classes and properties and can retrieve them.
22 class Model
23 super MEntity
24
25 redef fun model do return self
26 end
27
28 # A named and possibly documented entity in the model.
29 # This class is useful to generalize presentation of entities to the human.
30 abstract class MEntity
31 # The short (unqualified) name of this model entity.
32 #
33 # The short-name is based from the identifiers used to declare or denote the entity.
34 # It is usually globally ambiguous but is often enough in a precise local context.
35 #
36 # It is suitable to use the short-name in message to the user.
37 # However, special care must be used in case of potential ambiguities or name conflict.
38 fun name: String is abstract
39
40 # A fully-qualified name of this model entity.
41 #
42 # The full-name is based on the short name and is usually prefixed by the name of an outer entity.
43 # Usually the quad (`::`) is used to separate the different names.
44 #
45 # The full-name is expected to be unique and unambiguous in lawful Nit models for the same kind of entity.
46 #
47 # It is often suitable to use it in message to the user.
48 # However, some full-name could be long and verbose,
49 #
50 # See the specific implementation in subclasses for details.
51 fun full_name: String is abstract
52
53 # A fully-qualified C-like identifier of this model entity.
54 #
55 # The C-name is a name that respects the rule of identifiers in the C language:
56 # it is only made of alphanumeric characters and starts with a letter (or a underscore).
57 #
58 # The C-name can be seen as a mangled version of the `full_name`.
59 # Therefore, it is expected to be unique and unambiguous in lawful Nit models for the same kind of entity.
60 #
61 # The C-name is used by tools that need some identifiers in generated files to designate the
62 # entity.
63 #
64 # Is is not suitable to use it directly with the user (e.g. in message) and
65 # indirect use should be restricted (e.g. to name a web-page)
66 fun c_name: String is abstract
67
68 # A Model Entity has a direct link to its model
69 fun model: Model is abstract
70
71 # The indication that the entity did not pass some semantic verifications.
72 #
73 # This simple flag is set by a given analysis to say that the entity is broken and unusable in
74 # an execution.
75 # When an entity status is set to broken, it is usually associated with a error message.
76 #
77 # If it is safe to do so, clients of the model SHOULD just skip broken entities in their processing.
78 # Clients that do not care about the executability (e.g. metrics) MAY still process the entity or
79 # perform specific checks to determinate the validity of the entity.
80 #
81 # Note that the broken status is not propagated to enclosing and enclosed entities.
82 # e.g. a broken method does not make the whole module broken.
83 var is_broken = false is writable
84
85 # Is `self` created for internal purpose?
86 #
87 # Fictive entities are used internally but they should not be
88 # exposed to the final user.
89 var is_fictive: Bool = false is writable
90 end
91
92 # Something that represents a concern
93 abstract class MConcern
94 super MEntity
95 # The concern that contains `self` or null if `self` is the root of the concern hierarchy
96 fun parent_concern: nullable MConcern is abstract
97 end
98
99 # A visibility (for modules, class and properties)
100 # Valid visibility are:
101 #
102 # * `intrude_visibility`
103 # * `public_visibility`
104 # * `protected_visibility`
105 # * `none_visibility`
106 # * `private_visiblity`
107 #
108 # Note this class is basically an enum.
109 # FIXME: use a real enum once user-defined enums are available
110 class MVisibility
111 super Comparable
112 redef type OTHER: MVisibility
113
114 redef var to_s: String
115
116 private var level: Int
117
118 # TODO: private init because enumeration.
119
120 # Is self give less visibility than other
121 # none < private < protected < public < intrude
122 redef fun <(other)
123 do
124 return self.level < other.level
125 end
126 end
127
128 # A `Comparator` to sort mentities by their names.
129 class MEntityNameSorter
130 super Comparator
131
132 redef type COMPARED: MEntity
133
134 # Returns `a.name <=> b.name`.
135 redef fun compare(a, b) do return a.name <=> b.name
136 end
137
138 # The visibility level `intrude`
139 fun intrude_visibility: MVisibility do return once new MVisibility("intrude", 5)
140 # The visibility level `public`
141 fun public_visibility: MVisibility do return once new MVisibility("public", 4)
142 # The visibility level `protected`
143 fun protected_visibility: MVisibility do return once new MVisibility("protected", 3)
144 # The visibility level `private`
145 fun private_visibility: MVisibility do return once new MVisibility("private", 2)
146 # The visibility level `none` (no visibility)
147 fun none_visibility: MVisibility do return once new MVisibility("none", 1)