Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,17 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type
}
}

if (!result.is_set() && first == SNAME("Self")) {
// Strong type for the `self` identifier. Resolves to the current class
// meta-type so it behaves like using the current class name directly.
if (parser->current_class) {
result = parser->current_class->datatype;
} else {
push_error(R"("Self" can only be used inside a class.)", p_type);
return bad_type;
}
}

if (!result.is_set()) {
push_error(vformat(R"(Could not find type "%s" in the current scope.)", first), p_type);
return bad_type;
Expand Down Expand Up @@ -4650,6 +4661,24 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
return;
}

// Allow "Self" here, as a value it resolves to the current class meta-type.
// This enables expressions like `Self.new()` or `Self.my_static()` to work.
if (name == SNAME("Self")) {
if (parser->current_class == nullptr) {
push_error(R"("Self" can only be used inside a class.)", p_identifier);
} else {
p_identifier->datatype = parser->current_class->datatype;

Error err = OK;
Ref<GDScript> scr = get_depended_shallow_script(parser->current_class->datatype.script_path, err);
if (!err && scr.is_valid()) {
p_identifier->reduced_value = scr->find_class(parser->current_class->datatype.class_type->fqcn);
p_identifier->is_constant = true;
}
}
return;
}

// Not found.
#ifdef SUGGEST_GODOT4_RENAMES
String rename_hint;
Expand Down
5 changes: 5 additions & 0 deletions modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
} break;
}

// Special-case: `Self` refers to the current class meta-type.
if (identifier == SNAME("Self")) {
return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CLASS);
}

// Not found, error.
_set_error("Identifier not found: " + String(identifier), p_expression);
r_error = ERR_COMPILATION_FAILED;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Self:
var value := "OK"

class TestClass:
static func make() -> Self:
return Self.new()

func test():
var x := TestClass.make()
prints(x != null, x is Self)
prints(x.value)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GDTEST_OK
true true
OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var list: Array[Self] = []
var dict: Dictionary[String, Self] = {}

func test():
list.append(self)
print(list.size() == 1 and list.back() is Self)
list.clear()

dict["self"] = self
print(dict.has("self") and dict["self"] is Self)
dict.clear()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GDTEST_OK
true
true
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum MyEnum { A, B }

func get_enum() -> Self.MyEnum:
return MyEnum.A

func test():
var e: Self.MyEnum = get_enum()
print(int(e) == 0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_OK
true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Inner:
var val := "OK"

func make_inner() -> Self.Inner:
return Self.Inner.new()

func test():
var i: Self.Inner = make_inner()
print(i.val)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_OK
OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func test():
var x: Self = self
print(x is Self)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_OK
true
9 changes: 9 additions & 0 deletions modules/gdscript/tests/scripts/parser/self_type_parsing.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var a: Self
var b: Array[Self]
var c: Dictionary[String, Self]

func f(_x: Self) -> Self:
return self

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GDTEST_OK
17 changes: 17 additions & 0 deletions modules/gdscript/tests/scripts/runtime/features/self_type.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class FluentInt:
var value:int

static func make(x:int) -> Self:
return Self.new(x)

func _init(x:int):
value = x
func add_one() -> Self:
value += 1
return self
func add_ten() -> Self:
value += 10
return self

func test():
print(FluentInt.make(100).add_one().add_ten().value)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_OK
111
Loading