Skip to content

Commit 2ff8c1b

Browse files
committed
GDScript: add this_class keyword
1 parent d4a222c commit 2ff8c1b

File tree

7 files changed

+45
-0
lines changed

7 files changed

+45
-0
lines changed

core/math/expression.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,8 @@ Error Expression::_get_token(Token &r_token) {
12221222
r_token.type = TK_OP_AND;
12231223
} else if (id == "self") {
12241224
r_token.type = TK_SELF;
1225+
} else if (id == "this_class") {
1226+
r_token.type = TK_THIS_CLASS;
12251227
} else {
12261228

12271229
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
@@ -1485,6 +1487,11 @@ Expression::ENode *Expression::_parse_expression() {
14851487
SelfNode *self = alloc_node<SelfNode>();
14861488
expr = self;
14871489
} break;
1490+
case TK_THIS_CLASS: {
1491+
1492+
ThisClassNode *this_class = alloc_node<ThisClassNode>();
1493+
expr = this_class;
1494+
} break;
14881495
case TK_CONSTANT: {
14891496
ConstantNode *constant = alloc_node<ConstantNode>();
14901497
constant->value = tk.value;
@@ -1959,6 +1966,14 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression:
19591966
}
19601967
r_ret = p_instance;
19611968
} break;
1969+
case Expression::ENode::TYPE_THIS_CLASS: {
1970+
1971+
if (!p_instance) {
1972+
r_error_str = RTR("this_class can't be used because instance is null (not passed)");
1973+
return true;
1974+
}
1975+
r_ret = p_instance->get_class();
1976+
} break;
19621977
case Expression::ENode::TYPE_OPERATOR: {
19631978

19641979
const Expression::OperatorNode *op = static_cast<const Expression::OperatorNode *>(p_node);

core/math/expression.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class Expression : public Reference {
149149
TK_IDENTIFIER,
150150
TK_BUILTIN_FUNC,
151151
TK_SELF,
152+
TK_THIS_CLASS,
152153
TK_CONSTANT,
153154
TK_BASIC_TYPE,
154155
TK_COLON,
@@ -206,6 +207,7 @@ class Expression : public Reference {
206207
TYPE_INPUT,
207208
TYPE_CONSTANT,
208209
TYPE_SELF,
210+
TYPE_THIS_CLASS,
209211
TYPE_OPERATOR,
210212
TYPE_INDEX,
211213
TYPE_NAMED_INDEX,
@@ -273,6 +275,13 @@ class Expression : public Reference {
273275
}
274276
};
275277

278+
struct ThisClassNode : public ENode {
279+
280+
ThisClassNode() {
281+
type = TYPE_THIS_CLASS;
282+
}
283+
};
284+
276285
struct IndexNode : public ENode {
277286
ENode *base;
278287
ENode *index;

modules/gdscript/gdscript_compiler.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
391391
}
392392
return (GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
393393
} break;
394+
case GDScriptParser::Node::TYPE_THIS_CLASS: {
395+
//return constant
396+
return (GDScriptFunction::ADDR_TYPE_CLASS << GDScriptFunction::ADDR_BITS);
397+
} break;
394398
case GDScriptParser::Node::TYPE_ARRAY: {
395399

396400
const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);

modules/gdscript/gdscript_parser.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,11 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
616616
SelfNode *self = alloc_node<SelfNode>();
617617
tokenizer->advance();
618618
expr = self;
619+
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_THIS_CLASS) {
620+
//constant defined by tokenizer
621+
ThisClassNode *this_class = alloc_node<ThisClassNode>();
622+
tokenizer->advance();
623+
expr = this_class;
619624
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {
620625

621626
Variant::Type bi_type = tokenizer->get_token_type();
@@ -5576,6 +5581,10 @@ bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) {
55765581
full_name = r_type.native_type;
55775582
}
55785583
} break;
5584+
case GDScriptTokenizer::TK_THIS_CLASS: {
5585+
r_type.kind = DataType::CLASS;
5586+
r_type.class_type = current_class;
5587+
} break;
55795588
default: {
55805589
return false;
55815590
}

modules/gdscript/gdscript_parser.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class GDScriptParser {
118118
TYPE_ARRAY,
119119
TYPE_DICTIONARY,
120120
TYPE_SELF,
121+
TYPE_THIS_CLASS,
121122
TYPE_OPERATOR,
122123
TYPE_CONTROL_FLOW,
123124
TYPE_LOCAL_VAR,
@@ -345,6 +346,10 @@ class GDScriptParser {
345346
SelfNode() { type = TYPE_SELF; }
346347
};
347348

349+
struct ThisClassNode : public Node {
350+
ThisClassNode() { type = TYPE_THIS_CLASS; }
351+
};
352+
348353
struct OperatorNode : public Node {
349354
enum Operator {
350355
//call/constructor operator

modules/gdscript/gdscript_tokenizer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ static const _kws _keyword_list[] = {
227227
{ GDScriptTokenizer::TK_CF_MATCH, "match" },
228228
{ GDScriptTokenizer::TK_CF_PASS, "pass" },
229229
{ GDScriptTokenizer::TK_SELF, "self" },
230+
{ GDScriptTokenizer::TK_THIS_CLASS, "this_class" },
230231
{ GDScriptTokenizer::TK_CONST_PI, "PI" },
231232
{ GDScriptTokenizer::TK_CONST_TAU, "TAU" },
232233
{ GDScriptTokenizer::TK_WILDCARD, "_" },
@@ -291,6 +292,7 @@ bool GDScriptTokenizer::is_token_literal(int p_offset, bool variable_safe) const
291292
case TK_CF_MATCH:
292293
case TK_CF_PASS:
293294
case TK_SELF:
295+
case TK_THIS_CLASS:
294296
case TK_CONST_PI:
295297
case TK_CONST_TAU:
296298
case TK_WILDCARD:

modules/gdscript/gdscript_tokenizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class GDScriptTokenizer {
4646
TK_IDENTIFIER,
4747
TK_CONSTANT,
4848
TK_SELF,
49+
TK_THIS_CLASS,
4950
TK_BUILT_IN_TYPE,
5051
TK_BUILT_IN_FUNC,
5152
TK_OP_IN,

0 commit comments

Comments
 (0)