*: remove newly superfluous static types on attributes
[nit.git] / contrib / objcwrapper / src / objc_model.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 # Model of the parsed Objective-C files
16 module objc_model
17
18 # Model of all the analyzed Objective-C classes
19 class ObjcModel
20 # All analyzed classes
21 var classes = new Array[ObjcClass]
22
23 # Is `objc_type` known by this model?
24 fun knows_type(objc_type: Text): Bool
25 do
26 for c in classes do
27 if c.name == objc_type then return true
28 end
29
30 return imported_types.has(objc_type)
31 end
32
33 # Are all types in the signature or `method` known by this model?
34 fun knows_all_types(method: ObjcMethod): Bool
35 do
36 for param in method.params do
37 if param.is_single then break
38 if not knows_type(param.return_type) then return false
39 end
40
41 var r = method.return_type
42 return r == "void" or r == "id" or knows_type(r)
43 end
44
45 # Objective-C types available in imported modules
46 #
47 # TODO seach in existing wrappers
48 private var imported_types = ["NSObject", "NSString"]
49 end
50
51 # Objective-C class
52 class ObjcClass
53 # Name of the super classes
54 var super_names = new Array[String]
55
56 # Name of this Objective-C class
57 var name: String
58
59 # Attributes of this Objective-C class
60 var attributes = new Array[ObjcAttribute]
61
62 # Methods of this Objective-C class
63 var methods = new Array[ObjcMethod]
64 end
65
66 # Method of an `ObjcClass`
67 class ObjcMethod
68 super ObjcProperty
69
70 # Is this a static class method declared with '+'? Otherwise it's an instance method.
71 var is_class_property: Bool = false is writable
72
73 # Parameters of the method
74 var params = new Array[ObjcParam]
75
76 # Return type as a `String`
77 var return_type: String is noinit, writable
78
79 # Does this method look like a constructor/method?
80 fun is_init: Bool do return params.first.name.has_prefix("init") and not is_class_property
81 end
82
83 # Attribute of an `ObjcClass`
84 class ObjcAttribute
85 super ObjcProperty
86
87 # Name of this attribute
88 var name: String is noinit, writable
89
90 # Type of this attribute
91 var return_type: String is noinit, writable
92 end
93
94 # Property of an `ObjcClass`
95 class ObjcProperty
96 # Introducing class
97 var objc_class: ObjcClass
98
99 # Is this property to be commented out?
100 var is_commented = false is writable
101 end
102
103 # Parameter of an `ObjcMethod`
104 class ObjcParam
105 # Parameter name, used by the caller (e.g. `withObject` in `withObject: (NSObject*) obj`)
106 var name: String is noinit, writable
107
108 # Type of the parameter name
109 var return_type: String is noinit, writable
110
111 # Argument name, used within the body (e.g. `obj` in `withObject: (NSObject*) obj`)
112 var variable_name: String is noinit, writable
113
114 # Is this a primitive array? with at least one `[]`.
115 var is_table = false is writable
116
117 # Is this a pointer type?
118 var is_pointer = false is writable
119
120 # Is this a parameter with only a `name`?
121 var is_single = false is writable
122
123 redef fun to_s
124 do
125 if is_single then return name
126 return "{name}:({return_type}){variable_name}"
127 end
128 end