Debian Codegraph @

blueprintcompiler/language/types.py

raw
1 # types.py
2 #
3 # Copyright 2022 James Westman <[email protected]>
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This file is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # SPDX-License-Identifier: LGPL-3.0-or-later
19
20
21 from ..gir import Class, ExternType, Interface, Repository
22 from .common import *
23
24
25 class TypeName(AstNode):
26 grammar = AnyOf(
27 [
28 Optional(["$", UseLiteral("extern", True)]),
29 UseIdent("namespace"),
30 ".",
31 UseIdent("class_name"),
32 ],
33 [
34 AnyOf("$", [".", UseLiteral("old_extern", True)]),
35 UseIdent("class_name"),
36 UseLiteral("extern", True),
37 ],
38 UseIdent("class_name"),
39 )
40
41 @property
42 def is_extern(self) -> bool:
43 return self.tokens["extern"] is True
44
45 @property
46 def class_name(self) -> str:
47 return self.tokens["class_name"]
48
49 @property
50 def namespace(self) -> T.Optional[str]:
51 return self.tokens["namespace"]
52
53 @validate()
54 def old_extern(self):
55 if self.tokens["old_extern"]:
56 raise UpgradeWarning(
57 "Use the '$' extern syntax introduced in blueprint 0.8.0",
58 actions=[CodeAction("Use '$' syntax", "$" + self.tokens["class_name"])],
59 )
60
61 @validate("class_name")
62 def type_exists(self):
63 if not self.is_extern and self.gir_ns is not None:
64 self.root.gir.validate_type(self.class_name, self.namespace)
65
66 @validate("namespace")
67 def gir_ns_exists(self):
68 if not self.is_extern:
69 try:
70 self.root.gir.validate_ns(self.namespace)
71 except CompileError as e:
72 ns = self.namespace
73 e.actions = [
74 self.root.import_code_action(n, version)
75 for n, version in gir.get_available_namespaces()
76 if n == ns
77 ]
78 raise e
79
80 @validate()
81 def deprecated(self) -> None:
82 if self.gir_type and self.gir_type.deprecated:
83 hints = []
84 if self.gir_type.deprecated_doc:
85 hints.append(self.gir_type.deprecated_doc)
86 raise DeprecatedWarning(
87 f"{self.gir_type.full_name} is deprecated",
88 hints=hints,
89 )
90
91 @property
92 def gir_ns(self) -> T.Optional[gir.Namespace]:
93 if not self.is_extern:
94 return self.root.gir.namespaces.get(self.namespace or "Gtk")
95 return None
96
97 @property
98 def gir_type(self) -> T.Optional[gir.GirType]:
99 if self.class_name is not None and not self.is_extern:
100 return self.root.gir.get_type(self.class_name, self.namespace)
101
102 return gir.ExternType(
103 self.root.gir.get_type("Widget", "Gtk").get_containing(Repository),
104 self.namespace,
105 self.class_name,
106 )
107
108 @property
109 def glib_type_name(self) -> str:
110 if gir_type := self.gir_type:
111 return gir_type.glib_type_name
112 else:
113 if self.namespace:
114 return self.namespace + self.class_name
115 else:
116 return self.class_name
117
118 @docs("namespace")
119 def namespace_docs(self):
120 if ns := self.root.gir.namespaces.get(self.namespace):
121 return ns.doc
122
123 @docs("class_name")
124 def class_docs(self):
125 if self.gir_type:
126 return self.gir_type.doc
127
128 @property
129 def as_string(self) -> str:
130 name = self.class_name
131 if self.namespace:
132 name = f"{self.namespace}.{name}"
133 if self.is_extern:
134 name = f"${name}"
135 return name
136
137
138 class ClassName(TypeName):
139 @validate("namespace", "class_name")
140 def gir_class_exists(self):
141 if (
142 self.gir_type is not None
143 and not isinstance(self.gir_type, ExternType)
144 and not isinstance(self.gir_type, Class)
145 ):
146 if isinstance(self.gir_type, Interface):
147 raise CompileError(
148 f"{self.gir_type.full_name} is an interface, not a class"
149 )
150 else:
151 raise CompileError(f"{self.gir_type.full_name} is not a class")
152
153
154 class ConcreteClassName(ClassName):
155 @validate("namespace", "class_name")
156 def not_abstract(self):
157 if isinstance(self.gir_type, Class) and self.gir_type.abstract:
158 raise CompileError(
159 f"{self.gir_type.full_name} can't be instantiated because it's abstract",
160 hints=[f"did you mean to use a subclass of {self.gir_type.full_name}?"],
161 )
162
163
164 class TemplateClassName(ClassName):
165 """Handles the special case of a template type. The old syntax uses an identifier,
166 which is ambiguous with the new syntax. So this class displays an appropriate
167 upgrade warning instead of a class not found error."""
168
169 @property
170 def is_legacy(self):
171 return (
172 not self.is_extern
173 and self.namespace is None
174 and self.root.gir.get_type(self.class_name, "Gtk") is None
175 )
176
177 @property
178 def gir_type(self) -> T.Optional[gir.GirType]:
179 if self.is_legacy:
180 return gir.ExternType(self.root.gir, self.namespace, self.class_name)
181 else:
182 return super().gir_type
183
184 @validate("class_name")
185 def type_exists(self):
186 if self.is_legacy:
187 if type := self.root.gir.get_type_by_cname(self.tokens["class_name"]):
188 replacement = type.full_name
189 else:
190 replacement = "$" + self.tokens["class_name"]
191
192 raise UpgradeWarning(
193 "Use type syntax here (introduced in blueprint 0.8.0)",
194 actions=[CodeAction("Use type syntax", replace_with=replacement)],
195 )
196
197 if not self.is_extern and self.gir_ns is not None:
198 self.root.gir.validate_type(self.class_name, self.namespace)