/*************************************************************************/ /* gdscript_compiler.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "cscript_compiler.h" #include "cscript.h" bool CScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) { if (codegen.function_node && codegen.function_node->_static) { return false; } if (codegen.stack_identifiers.has(p_name)) { return false; //shadowed } return _is_class_member_property(codegen.script, p_name); } bool CScriptCompiler::_is_class_member_property(CScript *owner, const StringName &p_name) { CScript *scr = owner; CScriptNativeClass *nc = nullptr; while (scr) { if (scr->native.is_valid()) { nc = scr->native.ptr(); } scr = scr->_base; } ERR_FAIL_COND_V(!nc, false); return ClassDB::has_property(nc->get_name(), p_name); } void CScriptCompiler::_set_error(const String &p_error, const CScriptParser::Node *p_node) { if (error != "") { return; } error = p_error; if (p_node) { err_line = p_node->line; err_column = p_node->column; } else { err_line = 0; err_column = 0; } } bool CScriptCompiler::_create_unary_operator(CodeGen &codegen, const CScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level) { ERR_FAIL_COND_V(on->arguments.size() != 1, false); int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level); if (src_address_a < 0) { return false; } codegen.opcodes.push_back(CScriptFunction::OPCODE_OPERATOR); // perform operator codegen.opcodes.push_back(op); //which operator codegen.opcodes.push_back(src_address_a); // argument 1 codegen.opcodes.push_back(src_address_a); // argument 2 (repeated) //codegen.opcodes.push_back(CScriptFunction::ADDR_TYPE_NIL); // argument 2 (unary only takes one parameter) return true; } bool CScriptCompiler::_create_binary_operator(CodeGen &codegen, const CScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer, int p_index_addr) { ERR_FAIL_COND_V(on->arguments.size() != 2, false); int src_address_a = _parse_expression(codegen, on->arguments[0], p_stack_level, false, p_initializer, p_index_addr); if (src_address_a < 0) { return false; } if (src_address_a & CScriptFunction::ADDR_TYPE_STACK << CScriptFunction::ADDR_BITS) { p_stack_level++; //uses stack for return, increase stack } int src_address_b = _parse_expression(codegen, on->arguments[1], p_stack_level, false, p_initializer); if (src_address_b < 0) { return false; } codegen.opcodes.push_back(CScriptFunction::OPCODE_OPERATOR); // perform operator codegen.opcodes.push_back(op); //which operator codegen.opcodes.push_back(src_address_a); // argument 1 codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter) return true; } CScriptDataType CScriptCompiler::_gdtype_from_datatype(const CScriptParser::DataType &p_datatype, CScript *p_owner) const { if (!p_datatype.has_type) { return CScriptDataType(); } CScriptDataType result; result.has_type = true; switch (p_datatype.kind) { case CScriptParser::DataType::BUILTIN: { result.kind = CScriptDataType::BUILTIN; result.builtin_type = p_datatype.builtin_type; } break; case CScriptParser::DataType::NATIVE: { result.kind = CScriptDataType::NATIVE; result.native_type = p_datatype.native_type; } break; case CScriptParser::DataType::SCRIPT: { result.kind = CScriptDataType::SCRIPT; result.script_type_ref = Ref