Skip to content

Commit

Permalink
Added error messages to some compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rune-scape committed Aug 23, 2024
1 parent 568589c commit 00b64c0
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D

#ifdef DEBUG_ENABLED
if (unlikely(!GDScriptLanguage::get_singleton()->get_global_map().has(result.native_type))) {
ERR_PRINT(vformat(R"(GDScript bug: Native class "%s" not found.)", result.native_type));
result.native_type = Object::get_class_static();
_set_error(vformat(R"(GDScript bug (please report): Native class "%s" not found.)", result.native_type), nullptr);
return GDScriptDataType();
}
#endif
} break;
Expand Down Expand Up @@ -161,6 +161,7 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D
script = GDScriptCache::get_shallow_script(p_datatype.script_path, err, p_owner->path);
if (err) {
_set_error(vformat(R"(Could not find script "%s": %s)", p_datatype.script_path, error_names[err]), nullptr);
return GDScriptDataType();
}
}

Expand Down Expand Up @@ -193,7 +194,7 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D
break;
case GDScriptParser::DataType::RESOLVING:
case GDScriptParser::DataType::UNRESOLVED: {
ERR_PRINT("Parser bug: converting unresolved type.");
_set_error("Parser bug (please report): converting unresolved type.", nullptr);
return GDScriptDataType();
}
}
Expand Down Expand Up @@ -725,6 +726,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
return GDScriptCodeGenerator::Address();
}
} else {
_set_error("Compiler bug (please report): incorrect callee type in call node!", call->callee);
r_error = ERR_COMPILATION_FAILED;
return GDScriptCodeGenerator::Address();
}
Expand Down Expand Up @@ -1275,7 +1277,11 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
}
} else {
// Regular assignment.
ERR_FAIL_COND_V_MSG(assignment->assignee->type != GDScriptParser::Node::IDENTIFIER, GDScriptCodeGenerator::Address(), "Expected the assignee to be an identifier here.");
if (assignment->assignee->type != GDScriptParser::Node::IDENTIFIER) {
_set_error("Compiler bug (please report): Expected the assignee to be an identifier here.", assignment->assignee);
r_error = ERR_COMPILATION_FAILED;
return GDScriptCodeGenerator::Address();
}
GDScriptCodeGenerator::Address member;
bool is_member = false;
bool has_setter = false;
Expand Down Expand Up @@ -1416,7 +1422,9 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
return result;
} break;
default: {
ERR_FAIL_V_MSG(GDScriptCodeGenerator::Address(), "Bug in bytecode compiler, unexpected node in parse tree while parsing expression."); // Unreachable code.
_set_error("Compiler bug (please report): Unexpected node in parse tree while parsing expression.", p_expression); // Unreachable code.
r_error = ERR_COMPILATION_FAILED;
return GDScriptCodeGenerator::Address();
} break;
}
}
Expand Down Expand Up @@ -1853,7 +1861,10 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_match_pattern(CodeGen &c
}
return p_previous_test;
}
ERR_FAIL_V_MSG(p_previous_test, "Reaching the end of pattern compilation without matching a pattern.");

_set_error("Compiler bug (please report): Reaching the end of pattern compilation without matching a pattern.", p_pattern);
r_error = ERR_COMPILATION_FAILED;
return p_previous_test;
}

List<GDScriptCodeGenerator::Address> GDScriptCompiler::_add_block_locals(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block) {
Expand Down Expand Up @@ -2221,7 +2232,8 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Sui
codegen.generator->pop_temporary();
}
} else {
ERR_FAIL_V_MSG(ERR_INVALID_DATA, "Bug in bytecode compiler, unexpected node in parse tree while parsing statement."); // Unreachable code.
_set_error("Compiler bug (please report): unexpected node in parse tree while parsing statement.", s); // Unreachable code.
return ERR_INVALID_DATA;
}
} break;
}
Expand Down Expand Up @@ -2696,12 +2708,18 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP

GDScriptDataType base_type = _gdtype_from_datatype(p_class->base_type, p_script, false);

ERR_FAIL_COND_V_MSG(base_type.native_type == StringName(), ERR_BUG, vformat(R"(Failed to get base class for "%s")", p_script->path));
if (base_type.native_type == StringName()) {
_set_error(vformat(R"(Parser bug (please report): Empty native type in base class "%s")", p_script->path), p_class);
return ERR_BUG;
}

int native_idx = GDScriptLanguage::get_singleton()->get_global_map()[base_type.native_type];

p_script->native = GDScriptLanguage::get_singleton()->get_global_array()[native_idx];
ERR_FAIL_COND_V(p_script->native.is_null(), ERR_BUG);
if (p_script->native.is_null()) {
_set_error("Compiler bug (please report): script native type is null!", nullptr);
return ERR_BUG;
}

// Inheritance
switch (base_type.kind) {
Expand All @@ -2711,7 +2729,8 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP
case GDScriptDataType::GDSCRIPT: {
Ref<GDScript> base = Ref<GDScript>(base_type.script_type);
if (base.is_null()) {
return ERR_COMPILATION_FAILED;
_set_error("Compiler bug (please report): base script type is null!", nullptr);
return ERR_BUG;
}

if (main_script->has_class(base.ptr())) {
Expand Down Expand Up @@ -2746,7 +2765,7 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP
p_script->member_indices = base->member_indices;
} break;
default: {
_set_error("Parser bug: invalid inheritance.", nullptr);
_set_error("Parser bug (please report): invalid inheritance.", nullptr);
return ERR_BUG;
} break;
}
Expand Down Expand Up @@ -3259,7 +3278,11 @@ Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_scri
GDScriptCache::add_static_script(p_script);
}

return GDScriptCache::finish_compiling(main_script->path);
err = GDScriptCache::finish_compiling(main_script->path);
if (err) {
_set_error(R"(Failed to compile depended scripts.)", nullptr);
}
return err;
}

String GDScriptCompiler::get_error() const {
Expand Down

0 comments on commit 00b64c0

Please sign in to comment.