Debian Codegraph @

modules/gdscript/gdscript_cache.h

raw symbols SCIP
1 /**************************************************************************/
2 /* gdscript_cache.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31 #pragma once
32
33 #include "gdscript.h"
34
35 #include "core/object/ref_counted.h"
36 #include "core/os/safe_binary_mutex.h"
37 #include "core/templates/hash_map.h"
38 #include "core/templates/hash_set.h"
39
40 class GDScriptAnalyzer;
41 class GDScriptParser;
42
43 class GDScriptParserRef : public RefCounted {
44 GDSOFTCLASS(GDScriptParserRef, RefCounted);
45
46 public:
47 enum Status {
48 EMPTY,
49 PARSED,
50 INHERITANCE_SOLVED,
51 INTERFACE_SOLVED,
52 FULLY_SOLVED,
53 };
54
55 private:
56 GDScriptParser *parser = nullptr;
57 GDScriptAnalyzer *analyzer = nullptr;
58 Status status = EMPTY;
59 Error result = OK;
60 String path;
61 uint32_t source_hash = 0;
62 bool clearing = false;
63 bool abandoned = false;
64
65 friend class GDScriptCache;
66 friend class GDScript;
67
68 public:
69 Status get_status() const;
70 String get_path() const;
71 uint32_t get_source_hash() const;
72 GDScriptParser *get_parser();
73 GDScriptAnalyzer *get_analyzer();
74 Error raise_status(Status p_new_status);
75 void clear();
76
77 GDScriptParserRef() {}
78 ~GDScriptParserRef();
79 };
80
81 #ifdef TESTS_ENABLED
82 namespace GDScriptTests {
83 class TestGDScriptCacheAccessor;
84 }
85 #endif // TESTS_ENABLED
86
87 class GDScriptCache {
88 // String key is full path.
89 HashMap<String, GDScriptParserRef *> parser_map;
90 HashMap<String, Vector<ObjectID>> abandoned_parser_map;
91 HashMap<String, Ref<GDScript>> shallow_gdscript_cache;
92 HashMap<String, Ref<GDScript>> full_gdscript_cache;
93 HashMap<String, Ref<GDScript>> static_gdscript_cache;
94 HashMap<String, HashSet<String>> dependencies;
95 HashMap<String, HashSet<String>> parser_inverse_dependencies;
96
97 friend class GDScript;
98 friend class GDScriptParserRef;
99 friend class GDScriptInstance;
100 #ifdef TESTS_ENABLED
101 friend class GDScriptTests::TestGDScriptCacheAccessor;
102 #endif // TESTS_ENABLED
103
104 static GDScriptCache *singleton;
105
106 bool cleared = false;
107
108 public:
109 static const int BINARY_MUTEX_TAG = 2;
110
111 private:
112 static SafeBinaryMutex<BINARY_MUTEX_TAG> mutex;
113 friend SafeBinaryMutex<BINARY_MUTEX_TAG> &_get_gdscript_cache_mutex();
114
115 public:
116 static void move_script(const String &p_from, const String &p_to);
117 static void remove_script(const String &p_path);
118 static Ref<GDScriptParserRef> get_parser(const String &p_path, GDScriptParserRef::Status status, Error &r_error, const String &p_owner = String());
119 static bool has_parser(const String &p_path);
120 static void remove_parser(const String &p_path);
121 static String get_source_code(const String &p_path);
122 static Vector<uint8_t> get_binary_tokens(const String &p_path);
123 static Ref<GDScript> get_shallow_script(const String &p_path, Error &r_error, const String &p_owner = String());
124 /**
125 * Returns a fully loaded GDScript using an already cached script if one exists.
126 *
127 * The returned instance is present in GDScriptCache and ResourceCache.
128 */
129 static Ref<GDScript> get_full_script(const String &p_path, Error &r_error, const String &p_owner = String(), bool p_update_from_disk = false);
130 static Ref<GDScript> get_cached_script(const String &p_path);
131 static Error finish_compiling(const String &p_owner);
132 static void add_static_script(Ref<GDScript> p_script);
133 static void remove_static_script(const String &p_fqcn);
134
135 static void clear();
136
137 GDScriptCache();
138 ~GDScriptCache();
139 };