gdscript_analyzer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*************************************************************************/
  2. /* gdscript_analyzer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_analyzer.h"
  31. #include "core/class_db.h"
  32. #include "core/hash_map.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/script_language.h"
  35. Error GDScriptAnalyzer::resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive) {
  36. GDScriptParser::DataType result;
  37. if (p_class->base_type.is_set()) {
  38. // Already resolved
  39. return OK;
  40. }
  41. if (!p_class->extends_used) {
  42. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  43. result.kind = GDScriptParser::DataType::NATIVE;
  44. result.native_type = "Reference";
  45. } else {
  46. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  47. GDScriptParser::DataType base;
  48. if (!p_class->extends_path.empty()) {
  49. base.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  50. base.kind = GDScriptParser::DataType::CLASS;
  51. // TODO: Don't load the script here to avoid the issue with cycles.
  52. base.script_type = ResourceLoader::load(p_class->extends_path);
  53. if (base.script_type.is_null() || !base.script_type->is_valid()) {
  54. parser->push_error(vformat(R"(Could not load the parent script at "%s".)", p_class->extends_path));
  55. }
  56. // TODO: Get this from cache singleton.
  57. base.gdscript_type = nullptr;
  58. } else {
  59. const StringName &name = p_class->extends[0];
  60. base.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  61. if (ScriptServer::is_global_class(name)) {
  62. base.kind = GDScriptParser::DataType::CLASS;
  63. // TODO: Get this from cache singleton.
  64. base.gdscript_type = nullptr;
  65. // TODO: Try singletons (create main unified source for those).
  66. } else if (p_class->members_indices.has(name)) {
  67. GDScriptParser::ClassNode::Member member = p_class->get_member(name);
  68. if (member.type == member.CLASS) {
  69. base.kind = GDScriptParser::DataType::CLASS;
  70. base.gdscript_type = member.m_class;
  71. } else if (member.type == member.CONSTANT) {
  72. // FIXME: This could also be a native type or preloaded GDScript.
  73. base.kind = GDScriptParser::DataType::CLASS;
  74. base.gdscript_type = nullptr;
  75. }
  76. } else {
  77. if (ClassDB::class_exists(name)) {
  78. base.kind = GDScriptParser::DataType::NATIVE;
  79. base.native_type = name;
  80. }
  81. }
  82. }
  83. // TODO: Extends with attributes (A.B.C).
  84. result = base;
  85. }
  86. if (!result.is_set()) {
  87. // TODO: More specific error messages.
  88. parser->push_error(vformat(R"(Could not resolve inheritance for class "%s".)", p_class->identifier == nullptr ? "<main>" : p_class->identifier->name), p_class);
  89. return ERR_PARSE_ERROR;
  90. }
  91. p_class->set_datatype(result);
  92. if (p_recursive) {
  93. for (int i = 0; i < p_class->members.size(); i++) {
  94. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  95. resolve_inheritance(p_class->members[i].m_class, true);
  96. }
  97. }
  98. }
  99. return OK;
  100. }
  101. Error GDScriptAnalyzer::resolve_inheritance() {
  102. return resolve_inheritance(parser->head);
  103. }
  104. // TODO: Move this to a central location (maybe core?).
  105. static HashMap<StringName, StringName> underscore_map;
  106. static const char *underscore_classes[] = {
  107. "ClassDB",
  108. "Directory",
  109. "Engine",
  110. "File",
  111. "Geometry",
  112. "GodotSharp",
  113. "JSON",
  114. "Marshalls",
  115. "Mutex",
  116. "OS",
  117. "ResourceLoader",
  118. "ResourceSaver",
  119. "Semaphore",
  120. "Thread",
  121. "VisualScriptEditor",
  122. nullptr,
  123. };
  124. static StringName get_real_class_name(const StringName &p_source) {
  125. if (underscore_map.empty()) {
  126. const char **class_name = underscore_classes;
  127. while (*class_name != nullptr) {
  128. underscore_map[*class_name] = String("_") + *class_name;
  129. class_name++;
  130. }
  131. }
  132. if (underscore_map.has(p_source)) {
  133. return underscore_map[p_source];
  134. }
  135. return p_source;
  136. }
  137. GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(const GDScriptParser::TypeNode *p_type) {
  138. GDScriptParser::DataType result;
  139. if (p_type == nullptr) {
  140. return result;
  141. }
  142. result.type_source = result.ANNOTATED_EXPLICIT;
  143. if (p_type->type_base == nullptr) {
  144. // void.
  145. result.kind = GDScriptParser::DataType::BUILTIN;
  146. result.builtin_type = Variant::NIL;
  147. return result;
  148. }
  149. StringName first = p_type->type_base->name;
  150. if (GDScriptParser::get_builtin_type(first) != Variant::NIL) {
  151. // Built-in types.
  152. // FIXME: I'm probably using this wrong here (well, I'm not really using it). Specifier *includes* the base the type.
  153. if (p_type->type_specifier != nullptr) {
  154. parser->push_error(R"(Built-in types don't contain subtypes.)", p_type->type_specifier);
  155. return GDScriptParser::DataType();
  156. }
  157. result.kind = GDScriptParser::DataType::BUILTIN;
  158. result.builtin_type = GDScriptParser::get_builtin_type(first);
  159. } else if (ClassDB::class_exists(get_real_class_name(first))) {
  160. // Native engine classes.
  161. if (p_type->type_specifier != nullptr) {
  162. parser->push_error(R"(Engine classes don't contain subtypes.)", p_type->type_specifier);
  163. return GDScriptParser::DataType();
  164. }
  165. result.kind = GDScriptParser::DataType::NATIVE;
  166. result.native_type = first;
  167. } else if (ScriptServer::is_global_class(first)) {
  168. // Global class_named classes.
  169. // TODO: Global classes and singletons.
  170. parser->push_error("GDScript analyzer: global class type not implemented.", p_type);
  171. ERR_FAIL_V_MSG(GDScriptParser::DataType(), "GDScript analyzer: global class type not implemented.");
  172. } else {
  173. // Classes in current scope.
  174. GDScriptParser::ClassNode *script_class = parser->current_class;
  175. bool found = false;
  176. while (!found && script_class != nullptr) {
  177. if (script_class->members_indices.has(first)) {
  178. GDScriptParser::ClassNode::Member member = script_class->members[script_class->members_indices[first]];
  179. switch (member.type) {
  180. case GDScriptParser::ClassNode::Member::CLASS:
  181. result.kind = GDScriptParser::DataType::CLASS;
  182. result.gdscript_type = member.m_class;
  183. found = true;
  184. break;
  185. default:
  186. // TODO: Get constants as types, disallow others explicitly.
  187. parser->push_error(vformat(R"("%s" is a %s but does not contain a type.)", first, member.get_type_name()), p_type);
  188. return GDScriptParser::DataType();
  189. }
  190. }
  191. script_class = script_class->outer;
  192. }
  193. parser->push_error(vformat(R"("%s" is not a valid type.)", first), p_type);
  194. return GDScriptParser::DataType();
  195. }
  196. // TODO: Allow subtypes.
  197. if (p_type->type_specifier != nullptr) {
  198. parser->push_error(R"(Subtypes are not yet supported.)", p_type->type_specifier);
  199. return GDScriptParser::DataType();
  200. }
  201. return result;
  202. }
  203. Error GDScriptAnalyzer::resolve_datatypes(GDScriptParser::ClassNode *p_class) {
  204. GDScriptParser::ClassNode *previous_class = parser->current_class;
  205. parser->current_class = p_class;
  206. for (int i = 0; i < p_class->members.size(); i++) {
  207. GDScriptParser::ClassNode::Member member = p_class->members[i];
  208. switch (member.type) {
  209. case GDScriptParser::ClassNode::Member::VARIABLE: {
  210. GDScriptParser::DataType datatype = resolve_datatype(member.variable->datatype_specifier);
  211. if (datatype.is_set()) {
  212. member.variable->set_datatype(datatype);
  213. if (member.variable->export_info.hint == PROPERTY_HINT_TYPE_STRING) {
  214. // @export annotation.
  215. switch (datatype.kind) {
  216. case GDScriptParser::DataType::BUILTIN:
  217. member.variable->export_info.hint_string = Variant::get_type_name(datatype.builtin_type);
  218. break;
  219. case GDScriptParser::DataType::NATIVE:
  220. if (ClassDB::is_parent_class(get_real_class_name(datatype.native_type), "Resource")) {
  221. member.variable->export_info.hint_string = get_real_class_name(datatype.native_type);
  222. } else {
  223. parser->push_error(R"(Export type can only be built-in or a resource.)", member.variable);
  224. }
  225. break;
  226. default:
  227. // TODO: Allow custom user resources.
  228. parser->push_error(R"(Export type can only be built-in or a resource.)", member.variable);
  229. break;
  230. }
  231. }
  232. }
  233. break;
  234. }
  235. default:
  236. // TODO
  237. break;
  238. }
  239. }
  240. parser->current_class = previous_class;
  241. return parser->errors.size() > 0 ? ERR_PARSE_ERROR : OK;
  242. }
  243. Error GDScriptAnalyzer::analyze() {
  244. parser->errors.clear();
  245. Error err = resolve_inheritance(parser->head);
  246. if (err) {
  247. return err;
  248. }
  249. return resolve_datatypes(parser->head);
  250. }
  251. GDScriptAnalyzer::GDScriptAnalyzer(GDScriptParser *p_parser) {
  252. parser = p_parser;
  253. }