2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* gd_compiler.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 12:11:45 +00:00
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
2017-01-01 21:01:57 +00:00
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
2017-04-07 22:45:00 +00:00
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 01:10:30 +00:00
/* */
/* 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 "gd_compiler.h"
# include "gd_script.h"
2017-03-18 23:36:26 +00:00
void GDCompiler : : _set_error ( const String & p_error , const GDParser : : Node * p_node ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( error ! = " " )
2014-02-10 01:10:30 +00:00
return ;
2017-03-18 23:36:26 +00:00
error = p_error ;
2015-01-03 03:57:02 +00:00
if ( p_node ) {
2017-03-18 23:36:26 +00:00
err_line = p_node - > line ;
err_column = p_node - > column ;
2015-01-03 03:57:02 +00:00
} else {
2017-03-18 23:36:26 +00:00
err_line = 0 ;
err_column = 0 ;
2015-01-03 03:57:02 +00:00
}
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
bool GDCompiler : : _create_unary_operator ( CodeGen & codegen , const GDParser : : OperatorNode * on , Variant : : Operator op , int p_stack_level ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) ! = 1 , false ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int src_address_a = _parse_expression ( codegen , on - > arguments [ 0 ] , p_stack_level ) ;
if ( src_address_a < 0 )
2014-02-10 01:10:30 +00:00
return false ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_OPERATOR ) ; // perform operator
codegen . opcodes . push_back ( op ) ; //which operator
codegen . opcodes . push_back ( src_address_a ) ; // argument 1
2014-09-15 14:33:30 +00:00
codegen . opcodes . push_back ( src_address_a ) ; // argument 2 (repeated)
//codegen.opcodes.push_back(GDFunction::ADDR_TYPE_NIL); // argument 2 (unary only takes one parameter)
2014-02-10 01:10:30 +00:00
return true ;
}
2017-03-18 23:36:26 +00:00
bool GDCompiler : : _create_binary_operator ( CodeGen & codegen , const GDParser : : OperatorNode * on , Variant : : Operator op , int p_stack_level , bool p_initializer ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) ! = 2 , false ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int src_address_a = _parse_expression ( codegen , on - > arguments [ 0 ] , p_stack_level , false , p_initializer ) ;
if ( src_address_a < 0 )
2014-02-10 01:10:30 +00:00
return false ;
2017-03-18 23:36:26 +00:00
if ( src_address_a & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS )
2014-02-10 01:10:30 +00:00
p_stack_level + + ; //uses stack for return, increase stack
2017-03-18 23:36:26 +00:00
int src_address_b = _parse_expression ( codegen , on - > arguments [ 1 ] , p_stack_level , false , p_initializer ) ;
if ( src_address_b < 0 )
2014-02-10 01:10:30 +00:00
return false ;
codegen . opcodes . push_back ( GDFunction : : 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 ;
}
/*
int GDCompiler : : _parse_subexpression ( CodeGen & codegen , const GDParser : : Node * p_expression ) {
int ret = _parse_expression ( codegen , p_expression ) ;
if ( ret < 0 )
return ret ;
if ( ret & ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ) {
codegen . stack_level + + ;
codegen . check_max_stack_level ( ) ;
//stack was used, keep value
}
return ret ;
}
*/
2017-03-18 23:36:26 +00:00
int GDCompiler : : _parse_assign_right_expression ( CodeGen & codegen , const GDParser : : OperatorNode * p_expression , int p_stack_level ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
Variant : : Operator var_op = Variant : : OP_MAX ;
2014-11-02 14:31:01 +00:00
2017-03-18 23:36:26 +00:00
switch ( p_expression - > op ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
case GDParser : : OperatorNode : : OP_ASSIGN_ADD : var_op = Variant : : OP_ADD ; break ;
case GDParser : : OperatorNode : : OP_ASSIGN_SUB : var_op = Variant : : OP_SUBSTRACT ; break ;
case GDParser : : OperatorNode : : OP_ASSIGN_MUL : var_op = Variant : : OP_MULTIPLY ; break ;
case GDParser : : OperatorNode : : OP_ASSIGN_DIV : var_op = Variant : : OP_DIVIDE ; break ;
case GDParser : : OperatorNode : : OP_ASSIGN_MOD : var_op = Variant : : OP_MODULE ; break ;
case GDParser : : OperatorNode : : OP_ASSIGN_SHIFT_LEFT : var_op = Variant : : OP_SHIFT_LEFT ; break ;
case GDParser : : OperatorNode : : OP_ASSIGN_SHIFT_RIGHT : var_op = Variant : : OP_SHIFT_RIGHT ; break ;
case GDParser : : OperatorNode : : OP_ASSIGN_BIT_AND : var_op = Variant : : OP_BIT_AND ; break ;
case GDParser : : OperatorNode : : OP_ASSIGN_BIT_OR : var_op = Variant : : OP_BIT_OR ; break ;
case GDParser : : OperatorNode : : OP_ASSIGN_BIT_XOR : var_op = Variant : : OP_BIT_XOR ; break ;
2014-11-02 14:31:01 +00:00
case GDParser : : OperatorNode : : OP_INIT_ASSIGN :
2014-02-10 01:10:30 +00:00
case GDParser : : OperatorNode : : OP_ASSIGN : {
//none
} break ;
2017-03-18 23:36:26 +00:00
default : {
2014-02-10 01:10:30 +00:00
ERR_FAIL_V ( - 1 ) ;
}
}
2017-03-18 23:36:26 +00:00
bool initializer = p_expression - > op = = GDParser : : OperatorNode : : OP_INIT_ASSIGN ;
2014-11-02 14:31:01 +00:00
2017-03-18 23:36:26 +00:00
if ( var_op = = Variant : : OP_MAX ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
return _parse_expression ( codegen , p_expression - > arguments [ 1 ] , p_stack_level , false , initializer ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
if ( ! _create_binary_operator ( codegen , p_expression , var_op , p_stack_level , initializer ) )
2014-02-10 01:10:30 +00:00
return - 1 ;
2017-03-18 23:36:26 +00:00
int dst_addr = ( p_stack_level ) | ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( dst_addr ) ; // append the stack level as destination address of the opcode
codegen . alloc_stack ( p_stack_level ) ;
return dst_addr ;
}
2017-03-18 23:36:26 +00:00
int GDCompiler : : _parse_expression ( CodeGen & codegen , const GDParser : : Node * p_expression , int p_stack_level , bool p_root , bool p_initializer ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
switch ( p_expression - > type ) {
2014-02-10 01:10:30 +00:00
//should parse variable declaration and adjust stack accordingly...
case GDParser : : Node : : TYPE_IDENTIFIER : {
//return identifier
//wait, identifier could be a local variable or something else... careful here, must reference properly
//as stack may be more interesting to work with
//This could be made much simpler by just indexing "self", but done this way (with custom self-addressing modes) increases peformance a lot.
2017-03-18 23:36:26 +00:00
const GDParser : : IdentifierNode * in = static_cast < const GDParser : : IdentifierNode * > ( p_expression ) ;
2014-02-10 01:10:30 +00:00
StringName identifier = in - > name ;
// TRY STACK!
2014-11-02 14:31:01 +00:00
if ( ! p_initializer & & codegen . stack_identifiers . has ( identifier ) ) {
2014-02-10 01:10:30 +00:00
int pos = codegen . stack_identifiers [ identifier ] ;
2017-03-18 23:36:26 +00:00
return pos | ( GDFunction : : ADDR_TYPE_STACK_VARIABLE < < GDFunction : : ADDR_BITS ) ;
2014-02-10 01:10:30 +00:00
}
2014-11-02 14:31:01 +00:00
//TRY MEMBERS!
2014-02-10 01:10:30 +00:00
if ( ! codegen . function_node | | ! codegen . function_node - > _static ) {
// TRY MEMBER VARIABLES!
//static function
if ( codegen . script - > member_indices . has ( identifier ) ) {
2014-10-28 01:54:32 +00:00
int idx = codegen . script - > member_indices [ identifier ] . index ;
2017-03-18 23:36:26 +00:00
return idx | ( GDFunction : : ADDR_TYPE_MEMBER < < GDFunction : : ADDR_BITS ) ; //argument (stack root)
2014-02-10 01:10:30 +00:00
}
}
//TRY CLASS CONSTANTS
2014-05-02 15:40:34 +00:00
GDScript * owner = codegen . script ;
while ( owner ) {
2014-02-10 01:10:30 +00:00
2014-05-02 15:40:34 +00:00
GDScript * scr = owner ;
2017-03-18 23:36:26 +00:00
GDNativeClass * nc = NULL ;
while ( scr ) {
2014-02-10 01:10:30 +00:00
2014-05-02 15:40:34 +00:00
if ( scr - > constants . has ( identifier ) ) {
//int idx=scr->constants[identifier];
int idx = codegen . get_name_map_pos ( identifier ) ;
2017-03-18 23:36:26 +00:00
return idx | ( GDFunction : : ADDR_TYPE_CLASS_CONSTANT < < GDFunction : : ADDR_BITS ) ; //argument (stack root)
2014-05-02 15:40:34 +00:00
}
if ( scr - > native . is_valid ( ) )
2017-03-18 23:36:26 +00:00
nc = scr - > native . ptr ( ) ;
scr = scr - > _base ;
2014-02-10 01:10:30 +00:00
}
2014-05-02 15:40:34 +00:00
// CLASS C++ Integer Constant
2014-02-10 01:10:30 +00:00
2014-05-02 15:40:34 +00:00
if ( nc ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
bool success = false ;
int constant = ObjectTypeDB : : get_integer_constant ( nc - > get_name ( ) , identifier , & success ) ;
2014-05-02 15:40:34 +00:00
if ( success ) {
2017-03-18 23:36:26 +00:00
Variant key = constant ;
2014-05-02 15:40:34 +00:00
int idx ;
2014-02-10 01:10:30 +00:00
2014-05-02 15:40:34 +00:00
if ( ! codegen . constant_map . has ( key ) ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
idx = codegen . constant_map . size ( ) ;
codegen . constant_map [ key ] = idx ;
2014-02-10 01:10:30 +00:00
2014-05-02 15:40:34 +00:00
} else {
2017-03-18 23:36:26 +00:00
idx = codegen . constant_map [ key ] ;
2014-05-02 15:40:34 +00:00
}
2017-03-18 23:36:26 +00:00
return idx | ( GDFunction : : ADDR_TYPE_LOCAL_CONSTANT < < GDFunction : : ADDR_BITS ) ; //make it a local constant (faster access)
2014-02-10 01:10:30 +00:00
}
}
2017-03-18 23:36:26 +00:00
owner = owner - > _owner ;
2014-02-10 01:10:30 +00:00
}
2014-05-02 15:40:34 +00:00
/*
handled in constants now
if ( codegen . script - > subclasses . has ( identifier ) ) {
2014-02-10 01:10:30 +00:00
//same with a subclass, make it a local constant.
int idx = codegen . get_constant_pos ( codegen . script - > subclasses [ identifier ] ) ;
return idx | ( GDFunction : : ADDR_TYPE_LOCAL_CONSTANT < < GDFunction : : ADDR_BITS ) ; //make it a local constant (faster access)
2014-05-02 15:40:34 +00:00
} */
2014-02-10 01:10:30 +00:00
if ( GDScriptLanguage : : get_singleton ( ) - > get_global_map ( ) . has ( identifier ) ) {
int idx = GDScriptLanguage : : get_singleton ( ) - > get_global_map ( ) [ identifier ] ;
2017-03-18 23:36:26 +00:00
return idx | ( GDFunction : : ADDR_TYPE_GLOBAL < < GDFunction : : ADDR_BITS ) ; //argument (stack root)
2014-02-10 01:10:30 +00:00
}
//not found, error
2017-03-18 23:36:26 +00:00
_set_error ( " Identifier not found: " + String ( identifier ) , p_expression ) ;
2014-02-10 01:10:30 +00:00
return - 1 ;
} break ;
case GDParser : : Node : : TYPE_CONSTANT : {
//return constant
2017-03-18 23:36:26 +00:00
const GDParser : : ConstantNode * cn = static_cast < const GDParser : : ConstantNode * > ( p_expression ) ;
2014-02-10 01:10:30 +00:00
int idx ;
if ( ! codegen . constant_map . has ( cn - > value ) ) {
2017-03-18 23:36:26 +00:00
idx = codegen . constant_map . size ( ) ;
codegen . constant_map [ cn - > value ] = idx ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
idx = codegen . constant_map [ cn - > value ] ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
return idx | ( GDFunction : : ADDR_TYPE_LOCAL_CONSTANT < < GDFunction : : ADDR_BITS ) ; //argument (stack root)
2014-02-10 01:10:30 +00:00
} break ;
case GDParser : : Node : : TYPE_SELF : {
//return constant
if ( codegen . function_node & & codegen . function_node - > _static ) {
2017-03-18 23:36:26 +00:00
_set_error ( " 'self' not present in static function! " , p_expression ) ;
2014-02-10 01:10:30 +00:00
return - 1 ;
}
2017-03-18 23:36:26 +00:00
return ( GDFunction : : ADDR_TYPE_SELF < < GDFunction : : ADDR_BITS ) ;
2014-02-10 01:10:30 +00:00
} break ;
case GDParser : : Node : : TYPE_ARRAY : {
2017-03-18 23:36:26 +00:00
const GDParser : : ArrayNode * an = static_cast < const GDParser : : ArrayNode * > ( p_expression ) ;
2014-02-10 01:10:30 +00:00
Vector < int > values ;
2017-03-18 23:36:26 +00:00
int slevel = p_stack_level ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < an - > elements . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , an - > elements [ i ] , slevel ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ret ;
2017-03-18 23:36:26 +00:00
if ( ret & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
values . push_back ( ret ) ;
}
codegen . opcodes . push_back ( GDFunction : : OPCODE_CONSTRUCT_ARRAY ) ;
codegen . opcodes . push_back ( values . size ( ) ) ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < values . size ( ) ; i + + )
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( values [ i ] ) ;
2017-03-18 23:36:26 +00:00
int dst_addr = ( p_stack_level ) | ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( dst_addr ) ; // append the stack level as destination address of the opcode
codegen . alloc_stack ( p_stack_level ) ;
return dst_addr ;
} break ;
case GDParser : : Node : : TYPE_DICTIONARY : {
2017-03-18 23:36:26 +00:00
const GDParser : : DictionaryNode * dn = static_cast < const GDParser : : DictionaryNode * > ( p_expression ) ;
2014-02-10 01:10:30 +00:00
Vector < int > values ;
2017-03-18 23:36:26 +00:00
int slevel = p_stack_level ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < dn - > elements . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , dn - > elements [ i ] . key , slevel ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ret ;
2017-03-18 23:36:26 +00:00
if ( ret & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
values . push_back ( ret ) ;
2017-03-18 23:36:26 +00:00
ret = _parse_expression ( codegen , dn - > elements [ i ] . value , slevel ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ret ;
2017-03-18 23:36:26 +00:00
if ( ret & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
values . push_back ( ret ) ;
}
codegen . opcodes . push_back ( GDFunction : : OPCODE_CONSTRUCT_DICTIONARY ) ;
codegen . opcodes . push_back ( dn - > elements . size ( ) ) ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < values . size ( ) ; i + + )
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( values [ i ] ) ;
2017-03-18 23:36:26 +00:00
int dst_addr = ( p_stack_level ) | ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( dst_addr ) ; // append the stack level as destination address of the opcode
codegen . alloc_stack ( p_stack_level ) ;
return dst_addr ;
} break ;
case GDParser : : Node : : TYPE_OPERATOR : {
//hell breaks loose
2017-03-18 23:36:26 +00:00
const GDParser : : OperatorNode * on = static_cast < const GDParser : : OperatorNode * > ( p_expression ) ;
switch ( on - > op ) {
2014-02-10 01:10:30 +00:00
//call/constructor operator
case GDParser : : OperatorNode : : OP_PARENT_CALL : {
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) < 1 , - 1 ) ;
2014-02-10 01:10:30 +00:00
const GDParser : : IdentifierNode * in = ( const GDParser : : IdentifierNode * ) on - > arguments [ 0 ] ;
Vector < int > arguments ;
int slevel = p_stack_level ;
2017-03-18 23:36:26 +00:00
for ( int i = 1 ; i < on - > arguments . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , on - > arguments [ i ] , slevel ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ret ;
2017-03-18 23:36:26 +00:00
if ( ret & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
arguments . push_back ( ret ) ;
}
//push call bytecode
codegen . opcodes . push_back ( GDFunction : : OPCODE_CALL_SELF_BASE ) ; // basic type constructor
codegen . opcodes . push_back ( codegen . get_name_map_pos ( in - > name ) ) ; //instance
codegen . opcodes . push_back ( arguments . size ( ) ) ; //argument count
codegen . alloc_call ( arguments . size ( ) ) ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < arguments . size ( ) ; i + + )
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( arguments [ i ] ) ; //arguments
} break ;
case GDParser : : OperatorNode : : OP_CALL : {
2017-03-18 23:36:26 +00:00
if ( on - > arguments [ 0 ] - > type = = GDParser : : Node : : TYPE_TYPE ) {
2014-02-10 01:10:30 +00:00
//construct a basic type
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) < 1 , - 1 ) ;
2014-02-10 01:10:30 +00:00
const GDParser : : TypeNode * tn = ( const GDParser : : TypeNode * ) on - > arguments [ 0 ] ;
int vtype = tn - > vtype ;
Vector < int > arguments ;
int slevel = p_stack_level ;
2017-03-18 23:36:26 +00:00
for ( int i = 1 ; i < on - > arguments . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , on - > arguments [ i ] , slevel ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ret ;
2017-03-18 23:36:26 +00:00
if ( ret & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
arguments . push_back ( ret ) ;
}
//push call bytecode
codegen . opcodes . push_back ( GDFunction : : OPCODE_CONSTRUCT ) ; // basic type constructor
codegen . opcodes . push_back ( vtype ) ; //instance
codegen . opcodes . push_back ( arguments . size ( ) ) ; //argument count
codegen . alloc_call ( arguments . size ( ) ) ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < arguments . size ( ) ; i + + )
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( arguments [ i ] ) ; //arguments
2017-03-18 23:36:26 +00:00
} else if ( on - > arguments [ 0 ] - > type = = GDParser : : Node : : TYPE_BUILT_IN_FUNCTION ) {
2014-02-10 01:10:30 +00:00
//built in function
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) < 1 , - 1 ) ;
2014-02-10 01:10:30 +00:00
Vector < int > arguments ;
int slevel = p_stack_level ;
2017-03-18 23:36:26 +00:00
for ( int i = 1 ; i < on - > arguments . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , on - > arguments [ i ] , slevel ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ret ;
2017-03-18 23:36:26 +00:00
if ( ret & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
arguments . push_back ( ret ) ;
}
codegen . opcodes . push_back ( GDFunction : : OPCODE_CALL_BUILT_IN ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( static_cast < const GDParser : : BuiltInFunctionNode * > ( on - > arguments [ 0 ] ) - > function ) ;
codegen . opcodes . push_back ( on - > arguments . size ( ) - 1 ) ;
codegen . alloc_call ( on - > arguments . size ( ) - 1 ) ;
for ( int i = 0 ; i < arguments . size ( ) ; i + + )
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( arguments [ i ] ) ;
} else {
//regular function
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) < 2 , - 1 ) ;
2014-02-10 01:10:30 +00:00
const GDParser : : Node * instance = on - > arguments [ 0 ] ;
2017-03-18 23:36:26 +00:00
if ( instance - > type = = GDParser : : Node : : TYPE_SELF ) {
2014-02-10 01:10:30 +00:00
//room for optimization
}
Vector < int > arguments ;
int slevel = p_stack_level ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < on - > arguments . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
int ret ;
2017-03-18 23:36:26 +00:00
if ( i = = 0 & & on - > arguments [ i ] - > type = = GDParser : : Node : : TYPE_SELF & & codegen . function_node & & codegen . function_node - > _static ) {
2014-05-01 14:34:10 +00:00
//static call to self
2017-03-18 23:36:26 +00:00
ret = ( GDFunction : : ADDR_TYPE_CLASS < < GDFunction : : ADDR_BITS ) ;
} else if ( i = = 1 ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( on - > arguments [ i ] - > type ! = GDParser : : Node : : TYPE_IDENTIFIER ) {
_set_error ( " Attempt to call a non-identifier. " , on ) ;
2014-02-10 01:10:30 +00:00
return - 1 ;
}
2017-03-18 23:36:26 +00:00
GDParser : : IdentifierNode * id = static_cast < GDParser : : IdentifierNode * > ( on - > arguments [ i ] ) ;
ret = codegen . get_name_map_pos ( id - > name ) ;
2014-02-10 01:10:30 +00:00
} else {
2014-05-01 14:34:10 +00:00
2017-03-18 23:36:26 +00:00
ret = _parse_expression ( codegen , on - > arguments [ i ] , slevel ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ret ;
2017-03-18 23:36:26 +00:00
if ( ret & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
}
arguments . push_back ( ret ) ;
}
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( p_root ? GDFunction : : OPCODE_CALL : GDFunction : : OPCODE_CALL_RETURN ) ; // perform operator
codegen . opcodes . push_back ( on - > arguments . size ( ) - 2 ) ;
codegen . alloc_call ( on - > arguments . size ( ) - 2 ) ;
for ( int i = 0 ; i < arguments . size ( ) ; i + + )
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( arguments [ i ] ) ;
}
} break ;
2014-09-15 14:33:30 +00:00
case GDParser : : OperatorNode : : OP_YIELD : {
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) & & on - > arguments . size ( ) ! = 2 , - 1 ) ;
2014-09-15 14:33:30 +00:00
Vector < int > arguments ;
int slevel = p_stack_level ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < on - > arguments . size ( ) ; i + + ) {
2014-09-15 14:33:30 +00:00
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , on - > arguments [ i ] , slevel ) ;
if ( ret < 0 )
2014-09-15 14:33:30 +00:00
return ret ;
2017-03-18 23:36:26 +00:00
if ( ret & ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ) {
2014-09-15 14:33:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
arguments . push_back ( ret ) ;
}
//push call bytecode
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( arguments . size ( ) = = 0 ? GDFunction : : OPCODE_YIELD : GDFunction : : OPCODE_YIELD_SIGNAL ) ; // basic type constructor
for ( int i = 0 ; i < arguments . size ( ) ; i + + )
2014-09-15 14:33:30 +00:00
codegen . opcodes . push_back ( arguments [ i ] ) ; //arguments
codegen . opcodes . push_back ( GDFunction : : OPCODE_YIELD_RESUME ) ;
//next will be where to place the result :)
} break ;
2014-02-10 01:10:30 +00:00
//indexing operator
case GDParser : : OperatorNode : : OP_INDEX :
case GDParser : : OperatorNode : : OP_INDEX_NAMED : {
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) ! = 2 , - 1 ) ;
2014-02-10 01:10:30 +00:00
int slevel = p_stack_level ;
2017-03-18 23:36:26 +00:00
bool named = ( on - > op = = GDParser : : OperatorNode : : OP_INDEX_NAMED ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int from = _parse_expression ( codegen , on - > arguments [ 0 ] , slevel ) ;
if ( from < 0 )
2014-02-10 01:10:30 +00:00
return from ;
int index ;
if ( named ) {
2017-03-18 23:36:26 +00:00
if ( on - > arguments [ 0 ] - > type = = GDParser : : Node : : TYPE_SELF & & codegen . script & & codegen . function_node & & ! codegen . function_node - > _static ) {
2015-04-18 20:55:04 +00:00
2017-03-18 23:36:26 +00:00
GDParser : : IdentifierNode * identifier = static_cast < GDParser : : IdentifierNode * > ( on - > arguments [ 1 ] ) ;
const Map < StringName , GDScript : : MemberInfo > : : Element * MI = codegen . script - > member_indices . find ( identifier - > name ) ;
2016-06-25 13:59:39 +00:00
# ifdef DEBUG_ENABLED
2017-03-18 23:36:26 +00:00
if ( MI & & MI - > get ( ) . getter = = codegen . function_node - > name ) {
String n = static_cast < GDParser : : IdentifierNode * > ( on - > arguments [ 1 ] ) - > name ;
_set_error ( " Must use ' " + n + " ' instead of 'self. " + n + " ' in getter. " , on ) ;
2015-04-18 20:55:04 +00:00
return - 1 ;
}
# endif
2016-06-25 13:59:39 +00:00
2017-03-18 23:36:26 +00:00
if ( MI & & MI - > get ( ) . getter = = " " ) {
2016-06-25 13:59:39 +00:00
// Faster than indexing self (as if no self. had been used)
2017-03-18 23:36:26 +00:00
return ( MI - > get ( ) . index ) | ( GDFunction : : ADDR_TYPE_MEMBER < < GDFunction : : ADDR_BITS ) ;
2016-06-25 13:59:39 +00:00
}
}
2017-03-18 23:36:26 +00:00
index = codegen . get_name_map_pos ( static_cast < GDParser : : IdentifierNode * > ( on - > arguments [ 1 ] ) - > name ) ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
if ( on - > arguments [ 1 ] - > type = = GDParser : : Node : : TYPE_CONSTANT & & static_cast < const GDParser : : ConstantNode * > ( on - > arguments [ 1 ] ) - > value . get_type ( ) = = Variant : : STRING ) {
2014-02-10 01:10:30 +00:00
//also, somehow, named (speed up anyway)
2017-03-18 23:36:26 +00:00
StringName name = static_cast < const GDParser : : ConstantNode * > ( on - > arguments [ 1 ] ) - > value ;
index = codegen . get_name_map_pos ( name ) ;
named = true ;
2014-02-10 01:10:30 +00:00
} else {
//regular indexing
2017-03-18 23:36:26 +00:00
if ( from & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
2017-03-18 23:36:26 +00:00
index = _parse_expression ( codegen , on - > arguments [ 1 ] , slevel ) ;
if ( index < 0 )
2014-02-10 01:10:30 +00:00
return index ;
}
}
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( named ? GDFunction : : OPCODE_GET_NAMED : GDFunction : : OPCODE_GET ) ; // perform operator
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( from ) ; // argument 1
codegen . opcodes . push_back ( index ) ; // argument 2 (unary only takes one parameter)
} break ;
case GDParser : : OperatorNode : : OP_AND : {
// AND operator with early out on failure
2017-03-18 23:36:26 +00:00
int res = _parse_expression ( codegen , on - > arguments [ 0 ] , p_stack_level ) ;
if ( res < 0 )
2014-02-10 01:10:30 +00:00
return res ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP_IF_NOT ) ;
codegen . opcodes . push_back ( res ) ;
2017-03-18 23:36:26 +00:00
int jump_fail_pos = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( 0 ) ;
2017-03-18 23:36:26 +00:00
res = _parse_expression ( codegen , on - > arguments [ 1 ] , p_stack_level ) ;
if ( res < 0 )
2014-02-10 01:10:30 +00:00
return res ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP_IF_NOT ) ;
codegen . opcodes . push_back ( res ) ;
2017-03-18 23:36:26 +00:00
int jump_fail_pos2 = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( 0 ) ;
codegen . alloc_stack ( p_stack_level ) ; //it will be used..
codegen . opcodes . push_back ( GDFunction : : OPCODE_ASSIGN_TRUE ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( p_stack_level | GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( codegen . opcodes . size ( ) + 3 ) ;
codegen . opcodes [ jump_fail_pos ] = codegen . opcodes . size ( ) ;
codegen . opcodes [ jump_fail_pos2 ] = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( GDFunction : : OPCODE_ASSIGN_FALSE ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( p_stack_level | GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
return p_stack_level | GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ;
2014-02-10 01:10:30 +00:00
} break ;
case GDParser : : OperatorNode : : OP_OR : {
// OR operator with early out on success
2017-03-18 23:36:26 +00:00
int res = _parse_expression ( codegen , on - > arguments [ 0 ] , p_stack_level ) ;
if ( res < 0 )
2014-02-10 01:10:30 +00:00
return res ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP_IF ) ;
codegen . opcodes . push_back ( res ) ;
2017-03-18 23:36:26 +00:00
int jump_success_pos = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( 0 ) ;
2017-03-18 23:36:26 +00:00
res = _parse_expression ( codegen , on - > arguments [ 1 ] , p_stack_level ) ;
if ( res < 0 )
2014-02-10 01:10:30 +00:00
return res ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP_IF ) ;
codegen . opcodes . push_back ( res ) ;
2017-03-18 23:36:26 +00:00
int jump_success_pos2 = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( 0 ) ;
codegen . alloc_stack ( p_stack_level ) ; //it will be used..
codegen . opcodes . push_back ( GDFunction : : OPCODE_ASSIGN_FALSE ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( p_stack_level | GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( codegen . opcodes . size ( ) + 3 ) ;
codegen . opcodes [ jump_success_pos ] = codegen . opcodes . size ( ) ;
codegen . opcodes [ jump_success_pos2 ] = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( GDFunction : : OPCODE_ASSIGN_TRUE ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( p_stack_level | GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
return p_stack_level | GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ;
2014-02-10 01:10:30 +00:00
2016-08-25 18:18:35 +00:00
} break ;
// ternary operators
case GDParser : : OperatorNode : : OP_TERNARY_IF : {
// x IF a ELSE y operator with early out on failure
2017-03-18 23:36:26 +00:00
int res = _parse_expression ( codegen , on - > arguments [ 0 ] , p_stack_level ) ;
if ( res < 0 )
2016-08-25 18:18:35 +00:00
return res ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP_IF_NOT ) ;
codegen . opcodes . push_back ( res ) ;
2017-03-18 23:36:26 +00:00
int jump_fail_pos = codegen . opcodes . size ( ) ;
2016-08-25 18:18:35 +00:00
codegen . opcodes . push_back ( 0 ) ;
2017-03-18 23:36:26 +00:00
res = _parse_expression ( codegen , on - > arguments [ 1 ] , p_stack_level ) ;
if ( res < 0 )
2016-08-25 18:18:35 +00:00
return res ;
2017-03-18 23:36:26 +00:00
2016-08-25 18:18:35 +00:00
codegen . alloc_stack ( p_stack_level ) ; //it will be used..
codegen . opcodes . push_back ( GDFunction : : OPCODE_ASSIGN ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( p_stack_level | GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
2016-08-25 18:18:35 +00:00
codegen . opcodes . push_back ( res ) ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
2017-03-18 23:36:26 +00:00
int jump_past_pos = codegen . opcodes . size ( ) ;
2016-08-25 18:18:35 +00:00
codegen . opcodes . push_back ( 0 ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes [ jump_fail_pos ] = codegen . opcodes . size ( ) ;
res = _parse_expression ( codegen , on - > arguments [ 2 ] , p_stack_level ) ;
if ( res < 0 )
2016-08-25 18:18:35 +00:00
return res ;
2017-03-18 23:36:26 +00:00
2016-08-25 18:18:35 +00:00
codegen . opcodes . push_back ( GDFunction : : OPCODE_ASSIGN ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( p_stack_level | GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
2016-08-25 18:18:35 +00:00
codegen . opcodes . push_back ( res ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes [ jump_past_pos ] = codegen . opcodes . size ( ) ;
return p_stack_level | GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ;
2016-08-25 18:18:35 +00:00
2014-02-10 01:10:30 +00:00
} break ;
//unary operators
2017-03-18 23:36:26 +00:00
case GDParser : : OperatorNode : : OP_NEG : {
if ( ! _create_unary_operator ( codegen , on , Variant : : OP_NEGATE , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_NOT : {
if ( ! _create_unary_operator ( codegen , on , Variant : : OP_NOT , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_BIT_INVERT : {
if ( ! _create_unary_operator ( codegen , on , Variant : : OP_BIT_NEGATE , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_PREINC : {
} break ; //?
case GDParser : : OperatorNode : : OP_PREDEC : {
} break ;
case GDParser : : OperatorNode : : OP_INC : {
} break ;
case GDParser : : OperatorNode : : OP_DEC : {
} break ;
2014-02-10 01:10:30 +00:00
//binary operators (in precedence order)
2017-03-18 23:36:26 +00:00
case GDParser : : OperatorNode : : OP_IN : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_IN , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_EQUAL : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_EQUAL , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_NOT_EQUAL : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_NOT_EQUAL , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_LESS : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_LESS , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_LESS_EQUAL : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_LESS_EQUAL , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_GREATER : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_GREATER , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_GREATER_EQUAL : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_GREATER_EQUAL , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_ADD : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_ADD , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_SUB : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_SUBSTRACT , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_MUL : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_MULTIPLY , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_DIV : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_DIVIDE , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_MOD : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_MODULE , p_stack_level ) ) return - 1 ;
} break ;
2014-02-10 01:10:30 +00:00
//case GDParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
//case GDParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
2017-03-18 23:36:26 +00:00
case GDParser : : OperatorNode : : OP_BIT_AND : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_BIT_AND , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_BIT_OR : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_BIT_OR , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_BIT_XOR : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_BIT_XOR , p_stack_level ) ) return - 1 ;
} break ;
//shift
case GDParser : : OperatorNode : : OP_SHIFT_LEFT : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_SHIFT_LEFT , p_stack_level ) ) return - 1 ;
} break ;
case GDParser : : OperatorNode : : OP_SHIFT_RIGHT : {
if ( ! _create_binary_operator ( codegen , on , Variant : : OP_SHIFT_RIGHT , p_stack_level ) ) return - 1 ;
} break ;
2014-02-10 01:10:30 +00:00
//assignment operators
case GDParser : : OperatorNode : : OP_ASSIGN_ADD :
case GDParser : : OperatorNode : : OP_ASSIGN_SUB :
case GDParser : : OperatorNode : : OP_ASSIGN_MUL :
case GDParser : : OperatorNode : : OP_ASSIGN_DIV :
case GDParser : : OperatorNode : : OP_ASSIGN_MOD :
case GDParser : : OperatorNode : : OP_ASSIGN_SHIFT_LEFT :
case GDParser : : OperatorNode : : OP_ASSIGN_SHIFT_RIGHT :
case GDParser : : OperatorNode : : OP_ASSIGN_BIT_AND :
case GDParser : : OperatorNode : : OP_ASSIGN_BIT_OR :
case GDParser : : OperatorNode : : OP_ASSIGN_BIT_XOR :
2014-11-02 14:31:01 +00:00
case GDParser : : OperatorNode : : OP_INIT_ASSIGN :
2014-02-10 01:10:30 +00:00
case GDParser : : OperatorNode : : OP_ASSIGN : {
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) ! = 2 , - 1 ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( on - > arguments [ 0 ] - > type = = GDParser : : Node : : TYPE_OPERATOR & & ( static_cast < GDParser : : OperatorNode * > ( on - > arguments [ 0 ] ) - > op = = GDParser : : OperatorNode : : OP_INDEX | | static_cast < GDParser : : OperatorNode * > ( on - > arguments [ 0 ] ) - > op = = GDParser : : OperatorNode : : OP_INDEX_NAMED ) ) {
//SET (chained) MODE!!
2015-04-18 20:55:04 +00:00
# ifdef DEBUG_ENABLED
2017-03-18 23:36:26 +00:00
if ( static_cast < GDParser : : OperatorNode * > ( on - > arguments [ 0 ] ) - > op = = GDParser : : OperatorNode : : OP_INDEX_NAMED ) {
const GDParser : : OperatorNode * inon = static_cast < GDParser : : OperatorNode * > ( on - > arguments [ 0 ] ) ;
2015-04-18 20:55:04 +00:00
2017-03-18 23:36:26 +00:00
if ( inon - > arguments [ 0 ] - > type = = GDParser : : Node : : TYPE_SELF & & codegen . script & & codegen . function_node & & ! codegen . function_node - > _static ) {
2015-04-18 20:55:04 +00:00
2017-03-18 23:36:26 +00:00
const Map < StringName , GDScript : : MemberInfo > : : Element * MI = codegen . script - > member_indices . find ( static_cast < GDParser : : IdentifierNode * > ( inon - > arguments [ 1 ] ) - > name ) ;
if ( MI & & MI - > get ( ) . setter = = codegen . function_node - > name ) {
String n = static_cast < GDParser : : IdentifierNode * > ( inon - > arguments [ 1 ] ) - > name ;
_set_error ( " Must use ' " + n + " ' instead of 'self. " + n + " ' in setter. " , inon ) ;
2015-04-18 20:55:04 +00:00
return - 1 ;
}
}
}
# endif
2017-03-18 23:36:26 +00:00
int slevel = p_stack_level ;
2015-04-18 20:55:04 +00:00
2017-03-18 23:36:26 +00:00
GDParser : : OperatorNode * op = static_cast < GDParser : : OperatorNode * > ( on - > arguments [ 0 ] ) ;
2014-02-10 01:10:30 +00:00
/* Find chain of sets */
2017-03-18 23:36:26 +00:00
List < GDParser : : OperatorNode * > chain ;
2014-02-10 01:10:30 +00:00
{
//create get/set chain
2017-03-18 23:36:26 +00:00
GDParser : : OperatorNode * n = op ;
while ( true ) {
2014-02-10 01:10:30 +00:00
chain . push_back ( n ) ;
2017-03-18 23:36:26 +00:00
if ( n - > arguments [ 0 ] - > type ! = GDParser : : Node : : TYPE_OPERATOR )
2014-02-10 01:10:30 +00:00
break ;
2017-03-18 23:36:26 +00:00
n = static_cast < GDParser : : OperatorNode * > ( n - > arguments [ 0 ] ) ;
if ( n - > op ! = GDParser : : OperatorNode : : OP_INDEX & & n - > op ! = GDParser : : OperatorNode : : OP_INDEX_NAMED )
2014-02-10 01:10:30 +00:00
break ;
}
}
/* Chain of gets */
//get at (potential) root stack pos, so it can be returned
2017-03-18 23:36:26 +00:00
int prev_pos = _parse_expression ( codegen , chain . back ( ) - > get ( ) - > arguments [ 0 ] , slevel ) ;
if ( prev_pos < 0 )
2014-02-10 01:10:30 +00:00
return prev_pos ;
2017-03-18 23:36:26 +00:00
int retval = prev_pos ;
2014-02-10 01:10:30 +00:00
2014-09-22 03:50:48 +00:00
//print_line("retval: "+itos(retval));
2017-03-18 23:36:26 +00:00
if ( retval & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
Vector < int > setchain ;
2017-03-18 23:36:26 +00:00
for ( List < GDParser : : OperatorNode * > : : Element * E = chain . back ( ) ; E ; E = E - > prev ( ) ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( E = = chain . front ( ) ) //ignore first
2014-02-10 01:10:30 +00:00
break ;
2017-03-18 23:36:26 +00:00
bool named = E - > get ( ) - > op = = GDParser : : OperatorNode : : OP_INDEX_NAMED ;
2014-02-10 01:10:30 +00:00
int key_idx ;
if ( named ) {
2017-03-18 23:36:26 +00:00
key_idx = codegen . get_name_map_pos ( static_cast < const GDParser : : IdentifierNode * > ( E - > get ( ) - > arguments [ 1 ] ) - > name ) ;
2014-09-22 03:50:48 +00:00
//printf("named key %x\n",key_idx);
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
if ( prev_pos & ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
2014-09-22 03:50:48 +00:00
GDParser : : Node * key = E - > get ( ) - > arguments [ 1 ] ;
2017-03-18 23:36:26 +00:00
key_idx = _parse_expression ( codegen , key , slevel ) ;
2014-09-22 03:50:48 +00:00
//printf("expr key %x\n",key_idx);
//stack was raised here if retval was stack but..
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
if ( key_idx < 0 )
2014-02-10 01:10:30 +00:00
return key_idx ;
codegen . opcodes . push_back ( named ? GDFunction : : OPCODE_GET_NAMED : GDFunction : : OPCODE_GET ) ;
codegen . opcodes . push_back ( prev_pos ) ;
codegen . opcodes . push_back ( key_idx ) ;
slevel + + ;
codegen . alloc_stack ( slevel ) ;
2017-03-18 23:36:26 +00:00
int dst_pos = ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) | slevel ;
2014-09-22 03:50:48 +00:00
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( dst_pos ) ;
//add in reverse order, since it will be reverted
setchain . push_back ( dst_pos ) ;
setchain . push_back ( key_idx ) ;
setchain . push_back ( prev_pos ) ;
setchain . push_back ( named ? GDFunction : : OPCODE_SET_NAMED : GDFunction : : OPCODE_SET ) ;
2017-03-18 23:36:26 +00:00
prev_pos = dst_pos ;
2014-02-10 01:10:30 +00:00
}
setchain . invert ( ) ;
int set_index ;
2017-03-18 23:36:26 +00:00
bool named = false ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( static_cast < const GDParser : : OperatorNode * > ( op ) - > op = = GDParser : : OperatorNode : : OP_INDEX_NAMED ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
set_index = codegen . get_name_map_pos ( static_cast < const GDParser : : IdentifierNode * > ( op - > arguments [ 1 ] ) - > name ) ;
named = true ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
set_index = _parse_expression ( codegen , op - > arguments [ 1 ] , slevel + 1 ) ;
named = false ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
if ( set_index < 0 )
2014-02-10 01:10:30 +00:00
return set_index ;
2017-03-18 23:36:26 +00:00
if ( set_index & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
2017-03-18 23:36:26 +00:00
int set_value = _parse_assign_right_expression ( codegen , on , slevel + 1 ) ;
if ( set_value < 0 )
2014-02-10 01:10:30 +00:00
return set_value ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( named ? GDFunction : : OPCODE_SET_NAMED : GDFunction : : OPCODE_SET ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( prev_pos ) ;
codegen . opcodes . push_back ( set_index ) ;
codegen . opcodes . push_back ( set_value ) ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < setchain . size ( ) ; i + = 4 ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( setchain [ i + 0 ] ) ;
codegen . opcodes . push_back ( setchain [ i + 1 ] ) ;
codegen . opcodes . push_back ( setchain [ i + 2 ] ) ;
codegen . opcodes . push_back ( setchain [ i + 3 ] ) ;
2014-02-10 01:10:30 +00:00
}
return retval ;
} else {
//ASSIGNMENT MODE!!
int slevel = p_stack_level ;
2017-03-18 23:36:26 +00:00
int dst_address_a = _parse_expression ( codegen , on - > arguments [ 0 ] , slevel , false , on - > op = = GDParser : : OperatorNode : : OP_INIT_ASSIGN ) ;
if ( dst_address_a < 0 )
2014-02-10 01:10:30 +00:00
return - 1 ;
2017-03-18 23:36:26 +00:00
if ( dst_address_a & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) {
2014-02-10 01:10:30 +00:00
slevel + + ;
codegen . alloc_stack ( slevel ) ;
}
2017-03-18 23:36:26 +00:00
int src_address_b = _parse_assign_right_expression ( codegen , on , slevel ) ;
if ( src_address_b < 0 )
2014-02-10 01:10:30 +00:00
return - 1 ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_ASSIGN ) ; // perform operator
codegen . opcodes . push_back ( dst_address_a ) ; // argument 1
codegen . opcodes . push_back ( src_address_b ) ; // argument 2 (unary only takes one parameter)
return dst_address_a ; //if anything, returns wathever was assigned or correct stack position
}
} break ;
case GDParser : : OperatorNode : : OP_EXTENDS : {
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( on - > arguments . size ( ) ! = 2 , false ) ;
2014-02-10 01:10:30 +00:00
int slevel = p_stack_level ;
2017-03-18 23:36:26 +00:00
int src_address_a = _parse_expression ( codegen , on - > arguments [ 0 ] , slevel ) ;
if ( src_address_a < 0 )
2014-02-10 01:10:30 +00:00
return - 1 ;
2017-03-18 23:36:26 +00:00
if ( src_address_a & GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS )
2014-02-10 01:10:30 +00:00
slevel + + ; //uses stack for return, increase stack
2017-03-18 23:36:26 +00:00
int src_address_b = _parse_expression ( codegen , on - > arguments [ 1 ] , slevel ) ;
if ( src_address_b < 0 )
2014-02-10 01:10:30 +00:00
return - 1 ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_EXTENDS_TEST ) ; // perform operator
codegen . opcodes . push_back ( src_address_a ) ; // argument 1
codegen . opcodes . push_back ( src_address_b ) ; // argument 2 (unary only takes one parameter)
} break ;
default : {
2017-03-18 23:36:26 +00:00
ERR_EXPLAIN ( " Bug in bytecode compiler, unexpected operator # " + itos ( on - > op ) + " in parse tree while parsing expression. " ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_V ( 0 ) ; //unreachable code
} break ;
}
2017-03-18 23:36:26 +00:00
int dst_addr = ( p_stack_level ) | ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( dst_addr ) ; // append the stack level as destination address of the opcode
codegen . alloc_stack ( p_stack_level ) ;
return dst_addr ;
} break ;
//TYPE_TYPE,
default : {
ERR_EXPLAIN ( " Bug in bytecode compiler, unexpected node in parse tree while parsing expression. " ) ;
ERR_FAIL_V ( - 1 ) ; //unreachable code
} break ;
}
ERR_FAIL_V ( - 1 ) ; //unreachable code
}
2017-03-18 23:36:26 +00:00
Error GDCompiler : : _parse_block ( CodeGen & codegen , const GDParser : : BlockNode * p_block , int p_stack_level , int p_break_addr , int p_continue_addr ) {
2014-02-10 01:10:30 +00:00
codegen . push_stack_identifiers ( ) ;
2017-03-18 23:36:26 +00:00
int new_identifiers = 0 ;
codegen . current_line = p_block - > line ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_block - > statements . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
const GDParser : : Node * s = p_block - > statements [ i ] ;
2017-03-18 23:36:26 +00:00
switch ( s - > type ) {
2014-02-10 01:10:30 +00:00
case GDParser : : Node : : TYPE_NEWLINE : {
2016-09-20 11:54:17 +00:00
# ifdef DEBUG_ENABLED
2017-03-18 23:36:26 +00:00
const GDParser : : NewLineNode * nl = static_cast < const GDParser : : NewLineNode * > ( s ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( GDFunction : : OPCODE_LINE ) ;
codegen . opcodes . push_back ( nl - > line ) ;
2017-03-18 23:36:26 +00:00
codegen . current_line = nl - > line ;
2016-09-20 11:54:17 +00:00
# endif
2014-02-10 01:10:30 +00:00
} break ;
case GDParser : : Node : : TYPE_CONTROL_FLOW : {
// try subblocks
2017-03-18 23:36:26 +00:00
const GDParser : : ControlFlowNode * cf = static_cast < const GDParser : : ControlFlowNode * > ( s ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
switch ( cf - > cf_type ) {
2014-02-10 01:10:30 +00:00
case GDParser : : ControlFlowNode : : CF_IF : {
# ifdef DEBUG_ENABLED
codegen . opcodes . push_back ( GDFunction : : OPCODE_LINE ) ;
codegen . opcodes . push_back ( cf - > line ) ;
2017-03-18 23:36:26 +00:00
codegen . current_line = cf - > line ;
2014-02-10 01:10:30 +00:00
# endif
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , cf - > arguments [ 0 ] , p_stack_level , false ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ERR_PARSE_ERROR ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP_IF_NOT ) ;
codegen . opcodes . push_back ( ret ) ;
2017-03-18 23:36:26 +00:00
int else_addr = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( 0 ) ; //temporary
2017-03-18 23:36:26 +00:00
Error err = _parse_block ( codegen , cf - > body , p_stack_level , p_break_addr , p_continue_addr ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
if ( cf - > body_else ) {
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
2017-03-18 23:36:26 +00:00
int end_addr = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( 0 ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes [ else_addr ] = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
Error err = _parse_block ( codegen , cf - > body_else , p_stack_level , p_break_addr , p_continue_addr ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
2017-03-18 23:36:26 +00:00
codegen . opcodes [ end_addr ] = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
} else {
//end without else
2017-03-18 23:36:26 +00:00
codegen . opcodes [ else_addr ] = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
}
} break ;
case GDParser : : ControlFlowNode : : CF_FOR : {
2017-03-18 23:36:26 +00:00
int slevel = p_stack_level ;
int iter_stack_pos = slevel ;
int iterator_pos = ( slevel + + ) | ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
int counter_pos = ( slevel + + ) | ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
int container_pos = ( slevel + + ) | ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) ;
2014-02-10 01:10:30 +00:00
codegen . alloc_stack ( slevel ) ;
2017-03-18 23:36:26 +00:00
codegen . push_stack_identifiers ( ) ;
codegen . add_stack_identifier ( static_cast < const GDParser : : IdentifierNode * > ( cf - > arguments [ 0 ] ) - > name , iter_stack_pos ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , cf - > arguments [ 1 ] , slevel , false ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ERR_COMPILATION_FAILED ;
//assign container
codegen . opcodes . push_back ( GDFunction : : OPCODE_ASSIGN ) ;
codegen . opcodes . push_back ( container_pos ) ;
codegen . opcodes . push_back ( ret ) ;
//begin loop
codegen . opcodes . push_back ( GDFunction : : OPCODE_ITERATE_BEGIN ) ;
codegen . opcodes . push_back ( counter_pos ) ;
codegen . opcodes . push_back ( container_pos ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( codegen . opcodes . size ( ) + 4 ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( iterator_pos ) ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ; //skip code for next
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( codegen . opcodes . size ( ) + 8 ) ;
2014-02-10 01:10:30 +00:00
//break loop
2017-03-18 23:36:26 +00:00
int break_pos = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ; //skip code for next
codegen . opcodes . push_back ( 0 ) ; //skip code for next
//next loop
2017-03-18 23:36:26 +00:00
int continue_pos = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( GDFunction : : OPCODE_ITERATE ) ;
codegen . opcodes . push_back ( counter_pos ) ;
codegen . opcodes . push_back ( container_pos ) ;
codegen . opcodes . push_back ( break_pos ) ;
codegen . opcodes . push_back ( iterator_pos ) ;
2017-03-18 23:36:26 +00:00
Error err = _parse_block ( codegen , cf - > body , slevel , break_pos , continue_pos ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
codegen . opcodes . push_back ( continue_pos ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes [ break_pos + 1 ] = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . pop_stack_identifiers ( ) ;
} break ;
case GDParser : : ControlFlowNode : : CF_WHILE : {
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( codegen . opcodes . size ( ) + 3 ) ;
int break_addr = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
codegen . opcodes . push_back ( 0 ) ;
2017-03-18 23:36:26 +00:00
int continue_addr = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , cf - > arguments [ 0 ] , p_stack_level , false ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ERR_PARSE_ERROR ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP_IF_NOT ) ;
codegen . opcodes . push_back ( ret ) ;
codegen . opcodes . push_back ( break_addr ) ;
2017-03-18 23:36:26 +00:00
Error err = _parse_block ( codegen , cf - > body , p_stack_level , break_addr , continue_addr ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
codegen . opcodes . push_back ( continue_addr ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes [ break_addr + 1 ] = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
} break ;
case GDParser : : ControlFlowNode : : CF_SWITCH : {
} break ;
case GDParser : : ControlFlowNode : : CF_BREAK : {
2017-03-18 23:36:26 +00:00
if ( p_break_addr < 0 ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
_set_error ( " 'break'' not within loop " , cf ) ;
2014-02-10 01:10:30 +00:00
return ERR_COMPILATION_FAILED ;
}
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
codegen . opcodes . push_back ( p_break_addr ) ;
} break ;
case GDParser : : ControlFlowNode : : CF_CONTINUE : {
2017-03-18 23:36:26 +00:00
if ( p_continue_addr < 0 ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
_set_error ( " 'continue' not within loop " , cf ) ;
2014-02-10 01:10:30 +00:00
return ERR_COMPILATION_FAILED ;
}
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP ) ;
codegen . opcodes . push_back ( p_continue_addr ) ;
} break ;
case GDParser : : ControlFlowNode : : CF_RETURN : {
int ret ;
if ( cf - > arguments . size ( ) ) {
2017-03-18 23:36:26 +00:00
ret = _parse_expression ( codegen , cf - > arguments [ 0 ] , p_stack_level , false ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ERR_PARSE_ERROR ;
} else {
2017-03-18 23:36:26 +00:00
ret = GDFunction : : ADDR_TYPE_NIL < < GDFunction : : ADDR_BITS ;
2014-02-10 01:10:30 +00:00
}
codegen . opcodes . push_back ( GDFunction : : OPCODE_RETURN ) ;
codegen . opcodes . push_back ( ret ) ;
} break ;
}
} break ;
case GDParser : : Node : : TYPE_ASSERT : {
2017-03-12 23:17:13 +00:00
# ifdef DEBUG_ENABLED
2014-02-10 01:10:30 +00:00
// try subblocks
2017-03-18 23:36:26 +00:00
const GDParser : : AssertNode * as = static_cast < const GDParser : : AssertNode * > ( s ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , as - > condition , p_stack_level , false ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ERR_PARSE_ERROR ;
codegen . opcodes . push_back ( GDFunction : : OPCODE_ASSERT ) ;
codegen . opcodes . push_back ( ret ) ;
2017-03-12 23:17:13 +00:00
# endif
2014-02-10 01:10:30 +00:00
} break ;
2015-12-29 15:11:21 +00:00
case GDParser : : Node : : TYPE_BREAKPOINT : {
2016-09-20 11:54:17 +00:00
# ifdef DEBUG_ENABLED
2015-12-29 15:11:21 +00:00
// try subblocks
codegen . opcodes . push_back ( GDFunction : : OPCODE_BREAKPOINT ) ;
2016-09-20 11:54:17 +00:00
# endif
2015-12-29 15:11:21 +00:00
} break ;
2014-02-10 01:10:30 +00:00
case GDParser : : Node : : TYPE_LOCAL_VAR : {
2017-03-18 23:36:26 +00:00
const GDParser : : LocalVarNode * lv = static_cast < const GDParser : : LocalVarNode * > ( s ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
codegen . add_stack_identifier ( lv - > name , p_stack_level + + ) ;
2014-02-10 01:10:30 +00:00
codegen . alloc_stack ( p_stack_level ) ;
new_identifiers + + ;
} break ;
default : {
//expression
2017-03-18 23:36:26 +00:00
int ret = _parse_expression ( codegen , s , p_stack_level , true ) ;
if ( ret < 0 )
2014-02-10 01:10:30 +00:00
return ERR_PARSE_ERROR ;
} break ;
}
}
codegen . pop_stack_identifiers ( ) ;
return OK ;
}
2017-03-18 23:36:26 +00:00
Error GDCompiler : : _parse_function ( GDScript * p_script , const GDParser : : ClassNode * p_class , const GDParser : : FunctionNode * p_func , bool p_for_ready ) {
2014-02-10 01:10:30 +00:00
Vector < int > bytecode ;
CodeGen codegen ;
2017-03-18 23:36:26 +00:00
codegen . class_node = p_class ;
codegen . script = p_script ;
codegen . function_node = p_func ;
codegen . stack_max = 0 ;
codegen . current_line = 0 ;
codegen . call_max = 0 ;
codegen . debug_stack = ScriptDebugger : : get_singleton ( ) ! = NULL ;
2014-12-17 01:31:57 +00:00
Vector < StringName > argnames ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
int stack_level = 0 ;
2014-02-10 01:10:30 +00:00
if ( p_func ) {
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_func - > arguments . size ( ) ; i + + ) {
codegen . add_stack_identifier ( p_func - > arguments [ i ] , i ) ;
2014-12-17 01:31:57 +00:00
# ifdef TOOLS_ENABLED
argnames . push_back ( p_func - > arguments [ i ] ) ;
# endif
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
stack_level = p_func - > arguments . size ( ) ;
2014-02-10 01:10:30 +00:00
}
codegen . alloc_stack ( stack_level ) ;
/* Parse initializer -if applies- */
2017-03-18 23:36:26 +00:00
bool is_initializer = ! p_for_ready & & ! p_func ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( is_initializer | | ( p_func & & String ( p_func - > name ) = = " _init " ) ) {
2014-02-10 01:10:30 +00:00
//parse initializer for class members
2017-03-18 23:36:26 +00:00
if ( ! p_func & & p_class - > extends_used & & p_script - > native . is_null ( ) ) {
2014-02-10 01:10:30 +00:00
//call implicit parent constructor
codegen . opcodes . push_back ( GDFunction : : OPCODE_CALL_SELF_BASE ) ;
codegen . opcodes . push_back ( codegen . get_name_map_pos ( " _init " ) ) ;
codegen . opcodes . push_back ( 0 ) ;
2017-03-18 23:36:26 +00:00
codegen . opcodes . push_back ( ( GDFunction : : ADDR_TYPE_STACK < < GDFunction : : ADDR_BITS ) | 0 ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
Error err = _parse_block ( codegen , p_class - > initializer , stack_level ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
2017-03-18 23:36:26 +00:00
is_initializer = true ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
if ( p_for_ready | | ( p_func & & String ( p_func - > name ) = = " _ready " ) ) {
2015-12-28 22:31:52 +00:00
//parse initializer for class members
if ( p_class - > ready - > statements . size ( ) ) {
2017-03-18 23:36:26 +00:00
Error err = _parse_block ( codegen , p_class - > ready , stack_level ) ;
2015-12-28 22:31:52 +00:00
if ( err )
return err ;
}
}
2014-02-10 01:10:30 +00:00
/* Parse default argument code -if applies- */
Vector < int > defarg_addr ;
StringName func_name ;
if ( p_func ) {
2016-01-04 00:11:11 +00:00
2014-02-10 01:10:30 +00:00
if ( p_func - > default_values . size ( ) ) {
codegen . opcodes . push_back ( GDFunction : : OPCODE_JUMP_TO_DEF_ARGUMENT ) ;
defarg_addr . push_back ( codegen . opcodes . size ( ) ) ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_func - > default_values . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
_parse_expression ( codegen , p_func - > default_values [ i ] , stack_level , true ) ;
2014-02-10 01:10:30 +00:00
defarg_addr . push_back ( codegen . opcodes . size ( ) ) ;
}
defarg_addr . invert ( ) ;
}
2017-03-18 23:36:26 +00:00
Error err = _parse_block ( codegen , p_func - > body , stack_level ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
2017-03-18 23:36:26 +00:00
func_name = p_func - > name ;
2014-02-10 01:10:30 +00:00
} else {
2015-12-28 22:31:52 +00:00
if ( p_for_ready )
2017-03-18 23:36:26 +00:00
func_name = " _ready " ;
2015-12-28 22:31:52 +00:00
else
2017-03-18 23:36:26 +00:00
func_name = " _init " ;
2014-02-10 01:10:30 +00:00
}
codegen . opcodes . push_back ( GDFunction : : OPCODE_END ) ;
2017-03-18 23:36:26 +00:00
GDFunction * gdfunc = NULL ;
2014-02-10 01:10:30 +00:00
//if (String(p_func->name)=="") { //initializer func
// gdfunc = &p_script->initializer;
//} else { //regular func
2017-03-18 23:36:26 +00:00
p_script - > member_functions [ func_name ] = memnew ( GDFunction ) ;
gdfunc = p_script - > member_functions [ func_name ] ;
2014-02-10 01:10:30 +00:00
//}
if ( p_func )
2017-03-18 23:36:26 +00:00
gdfunc - > _static = p_func - > _static ;
2014-02-10 01:10:30 +00:00
2014-12-17 01:31:57 +00:00
# ifdef TOOLS_ENABLED
2017-03-18 23:36:26 +00:00
gdfunc - > arg_names = argnames ;
2014-12-17 01:31:57 +00:00
# endif
2014-02-10 01:10:30 +00:00
//constants
if ( codegen . constant_map . size ( ) ) {
2017-03-18 23:36:26 +00:00
gdfunc - > _constant_count = codegen . constant_map . size ( ) ;
2014-02-10 01:10:30 +00:00
gdfunc - > constants . resize ( codegen . constant_map . size ( ) ) ;
2017-03-18 23:36:26 +00:00
gdfunc - > _constants_ptr = & gdfunc - > constants [ 0 ] ;
const Variant * K = NULL ;
while ( ( K = codegen . constant_map . next ( K ) ) ) {
2014-02-10 01:10:30 +00:00
int idx = codegen . constant_map [ * K ] ;
2017-03-18 23:36:26 +00:00
gdfunc - > constants [ idx ] = * K ;
2014-02-10 01:10:30 +00:00
}
} else {
2017-03-18 23:36:26 +00:00
gdfunc - > _constants_ptr = NULL ;
gdfunc - > _constant_count = 0 ;
2014-02-10 01:10:30 +00:00
}
//global names
if ( codegen . name_map . size ( ) ) {
gdfunc - > global_names . resize ( codegen . name_map . size ( ) ) ;
gdfunc - > _global_names_ptr = & gdfunc - > global_names [ 0 ] ;
2017-03-18 23:36:26 +00:00
for ( Map < StringName , int > : : Element * E = codegen . name_map . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
gdfunc - > global_names [ E - > get ( ) ] = E - > key ( ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
gdfunc - > _global_names_count = gdfunc - > global_names . size ( ) ;
2014-02-10 01:10:30 +00:00
} else {
gdfunc - > _global_names_ptr = NULL ;
2017-03-18 23:36:26 +00:00
gdfunc - > _global_names_count = 0 ;
2014-02-10 01:10:30 +00:00
}
if ( codegen . opcodes . size ( ) ) {
2017-03-18 23:36:26 +00:00
gdfunc - > code = codegen . opcodes ;
gdfunc - > _code_ptr = & gdfunc - > code [ 0 ] ;
gdfunc - > _code_size = codegen . opcodes . size ( ) ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
gdfunc - > _code_ptr = NULL ;
gdfunc - > _code_size = 0 ;
2014-02-10 01:10:30 +00:00
}
if ( defarg_addr . size ( ) ) {
2017-03-18 23:36:26 +00:00
gdfunc - > default_arguments = defarg_addr ;
gdfunc - > _default_arg_count = defarg_addr . size ( ) - 1 ;
gdfunc - > _default_arg_ptr = & gdfunc - > default_arguments [ 0 ] ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
gdfunc - > _default_arg_count = 0 ;
gdfunc - > _default_arg_ptr = NULL ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
gdfunc - > _argument_count = p_func ? p_func - > arguments . size ( ) : 0 ;
gdfunc - > _stack_size = codegen . stack_max ;
gdfunc - > _call_size = codegen . call_max ;
gdfunc - > name = func_name ;
2016-05-22 00:18:16 +00:00
# ifdef DEBUG_ENABLED
2017-03-18 23:36:26 +00:00
if ( ScriptDebugger : : get_singleton ( ) ) {
2016-05-22 00:18:16 +00:00
String signature ;
//path
2017-03-18 23:36:26 +00:00
if ( p_script - > get_path ( ) ! = String ( ) )
signature + = p_script - > get_path ( ) ;
2016-05-22 00:18:16 +00:00
//loc
if ( p_func ) {
2017-03-18 23:36:26 +00:00
signature + = " :: " + itos ( p_func - > body - > line ) ;
2016-05-22 00:18:16 +00:00
} else {
2017-03-18 23:36:26 +00:00
signature + = " ::0 " ;
2016-05-22 00:18:16 +00:00
}
//funciton and class
if ( p_class - > name ) {
2017-03-18 23:36:26 +00:00
signature + = " :: " + String ( p_class - > name ) + " . " + String ( func_name ) ;
2016-05-22 00:18:16 +00:00
} else {
2017-03-18 23:36:26 +00:00
signature + = " :: " + String ( func_name ) ;
2016-05-22 00:18:16 +00:00
}
2017-03-18 23:36:26 +00:00
gdfunc - > profile . signature = signature ;
2016-05-22 00:18:16 +00:00
}
# endif
2017-03-18 23:36:26 +00:00
gdfunc - > _script = p_script ;
gdfunc - > source = source ;
2014-05-24 04:35:47 +00:00
# ifdef DEBUG_ENABLED
{
2017-03-18 23:36:26 +00:00
gdfunc - > func_cname = ( String ( source ) + " - " + String ( func_name ) ) . utf8 ( ) ;
gdfunc - > _func_cname = gdfunc - > func_cname . get_data ( ) ;
2014-05-24 04:35:47 +00:00
}
# endif
2014-02-10 01:10:30 +00:00
if ( p_func ) {
2017-03-18 23:36:26 +00:00
gdfunc - > _initial_line = p_func - > line ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
gdfunc - > _initial_line = 0 ;
2014-02-10 01:10:30 +00:00
}
if ( codegen . debug_stack )
2017-03-18 23:36:26 +00:00
gdfunc - > stack_debug = codegen . stack_debug ;
2014-02-10 01:10:30 +00:00
if ( is_initializer )
2017-03-18 23:36:26 +00:00
p_script - > initializer = gdfunc ;
2014-02-10 01:10:30 +00:00
return OK ;
}
2016-06-01 23:22:02 +00:00
Error GDCompiler : : _parse_class ( GDScript * p_script , GDScript * p_owner , const GDParser : : ClassNode * p_class , bool p_keep_state ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
Map < StringName , Ref < GDScript > > old_subclasses ;
2016-06-01 23:22:02 +00:00
if ( p_keep_state ) {
2017-03-18 23:36:26 +00:00
old_subclasses = p_script - > subclasses ;
2016-06-01 23:22:02 +00:00
}
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
p_script - > native = Ref < GDNativeClass > ( ) ;
p_script - > base = Ref < GDScript > ( ) ;
p_script - > _base = NULL ;
2014-02-10 01:10:30 +00:00
p_script - > members . clear ( ) ;
p_script - > constants . clear ( ) ;
2017-03-18 23:36:26 +00:00
for ( Map < StringName , GDFunction * > : : Element * E = p_script - > member_functions . front ( ) ; E ; E = E - > next ( ) ) {
2016-05-22 00:18:16 +00:00
memdelete ( E - > get ( ) ) ;
}
2014-02-10 01:10:30 +00:00
p_script - > member_functions . clear ( ) ;
p_script - > member_indices . clear ( ) ;
p_script - > member_info . clear ( ) ;
2016-07-17 16:49:22 +00:00
p_script - > _signals . clear ( ) ;
2017-03-18 23:36:26 +00:00
p_script - > initializer = NULL ;
2016-06-01 23:22:02 +00:00
2014-02-10 01:10:30 +00:00
p_script - > subclasses . clear ( ) ;
2017-03-18 23:36:26 +00:00
p_script - > _owner = p_owner ;
p_script - > tool = p_class - > tool ;
p_script - > name = p_class - > name ;
2014-02-10 01:10:30 +00:00
2015-06-24 16:29:23 +00:00
Ref < GDNativeClass > native ;
2014-02-10 01:10:30 +00:00
if ( p_class - > extends_used ) {
//do inheritance
String path = p_class - > extends_file ;
Ref < GDScript > script ;
2015-06-24 16:29:23 +00:00
2017-03-18 23:36:26 +00:00
if ( path ! = " " ) {
2014-02-10 01:10:30 +00:00
//path (and optionally subclasses)
2014-05-24 16:25:56 +00:00
if ( path . is_rel_path ( ) ) {
2016-04-09 02:17:02 +00:00
String base ;
if ( p_owner ) {
GDScript * current_class = p_owner ;
while ( current_class ! = NULL ) {
2017-03-18 23:36:26 +00:00
base = current_class - > get_path ( ) ;
if ( base = = " " )
2016-04-09 02:17:02 +00:00
current_class = current_class - > _owner ;
else
break ;
}
2017-03-18 23:36:26 +00:00
} else {
2016-04-09 02:17:02 +00:00
base = p_script - > get_path ( ) ;
}
2017-03-18 23:36:26 +00:00
if ( base = = " " | | base . is_rel_path ( ) ) {
_set_error ( " Could not resolve relative path for parent class: " + path , p_class ) ;
2014-05-24 16:25:56 +00:00
return ERR_FILE_NOT_FOUND ;
}
2017-03-18 23:36:26 +00:00
path = base . get_base_dir ( ) . plus_file ( path ) . simplify_path ( ) ;
2014-05-24 16:25:56 +00:00
}
2014-02-10 01:10:30 +00:00
script = ResourceLoader : : load ( path ) ;
if ( script . is_null ( ) ) {
2017-03-18 23:36:26 +00:00
_set_error ( " Could not load base class: " + path , p_class ) ;
2014-02-10 01:10:30 +00:00
return ERR_FILE_NOT_FOUND ;
}
2014-06-16 13:22:26 +00:00
if ( ! script - > valid ) {
2017-03-18 23:36:26 +00:00
_set_error ( " Script not fully loaded (cyclic preload?): " + path , p_class ) ;
2014-06-16 13:22:26 +00:00
return ERR_BUSY ;
}
//print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
2014-02-10 01:10:30 +00:00
if ( p_class - > extends_class . size ( ) ) {
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_class - > extends_class . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
String sub = p_class - > extends_class [ i ] ;
if ( script - > subclasses . has ( sub ) ) {
2016-06-28 13:44:38 +00:00
Ref < Script > subclass = script - > subclasses [ sub ] ; //avoid reference from dissapearing
2017-03-18 23:36:26 +00:00
script = subclass ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
_set_error ( " Could not find subclass: " + sub , p_class ) ;
2014-02-10 01:10:30 +00:00
return ERR_FILE_NOT_FOUND ;
}
}
}
} else {
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( p_class - > extends_class . size ( ) = = 0 , ERR_BUG ) ;
2014-02-10 01:10:30 +00:00
//look around for the subclasses
2017-03-18 23:36:26 +00:00
String base = p_class - > extends_class [ 0 ] ;
2014-02-10 01:10:30 +00:00
GDScript * p = p_owner ;
Ref < GDScript > base_class ;
2017-03-18 23:36:26 +00:00
while ( p ) {
2014-02-10 01:10:30 +00:00
if ( p - > subclasses . has ( base ) ) {
2017-03-18 23:36:26 +00:00
base_class = p - > subclasses [ base ] ;
2014-02-10 01:10:30 +00:00
break ;
}
2017-03-18 23:36:26 +00:00
p = p - > _owner ;
2014-02-10 01:10:30 +00:00
}
if ( base_class . is_valid ( ) ) {
2017-03-18 23:36:26 +00:00
for ( int i = 1 ; i < p_class - > extends_class . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
String subclass = p_class - > extends_class [ i ] ;
2014-02-10 01:10:30 +00:00
if ( base_class - > subclasses . has ( subclass ) ) {
2017-03-18 23:36:26 +00:00
base_class = base_class - > subclasses [ subclass ] ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
_set_error ( " Could not find subclass: " + subclass , p_class ) ;
2014-02-10 01:10:30 +00:00
return ERR_FILE_NOT_FOUND ;
}
}
2017-03-18 23:36:26 +00:00
script = base_class ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
if ( p_class - > extends_class . size ( ) > 1 ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
_set_error ( " Invalid inheritance (unknown class+subclasses) " , p_class ) ;
2014-02-10 01:10:30 +00:00
return ERR_FILE_NOT_FOUND ;
}
//if not found, try engine classes
if ( ! GDScriptLanguage : : get_singleton ( ) - > get_global_map ( ) . has ( base ) ) {
2017-03-18 23:36:26 +00:00
_set_error ( " Unknown class: ' " + base + " ' " , p_class ) ;
2014-02-10 01:10:30 +00:00
return ERR_FILE_NOT_FOUND ;
}
int base_idx = GDScriptLanguage : : get_singleton ( ) - > get_global_map ( ) [ base ] ;
native = GDScriptLanguage : : get_singleton ( ) - > get_global_array ( ) [ base_idx ] ;
if ( ! native . is_valid ( ) ) {
2017-03-18 23:36:26 +00:00
_set_error ( " Global not a class: ' " + base + " ' " , p_class ) ;
2014-02-10 01:10:30 +00:00
return ERR_FILE_NOT_FOUND ;
}
}
}
if ( script . is_valid ( ) ) {
2017-03-18 23:36:26 +00:00
p_script - > base = script ;
p_script - > _base = p_script - > base . ptr ( ) ;
p_script - > member_indices = script - > member_indices ;
2014-02-10 01:10:30 +00:00
} else if ( native . is_valid ( ) ) {
2017-03-18 23:36:26 +00:00
p_script - > native = native ;
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
_set_error ( " Could not determine inheritance " , p_class ) ;
2014-02-10 01:10:30 +00:00
return ERR_FILE_NOT_FOUND ;
}
}
2014-08-14 13:31:38 +00:00
//print_line("Script: "+p_script->get_path()+" indices: "+itos(p_script->member_indices.size()));
2014-06-16 13:22:26 +00:00
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_class - > variables . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
StringName name = p_class - > variables [ i ] . identifier ;
if ( p_script - > member_indices . has ( name ) ) {
2017-03-18 23:36:26 +00:00
_set_error ( " Member ' " + name + " ' already exists (in current or parent class) " , p_class ) ;
2014-02-10 01:10:30 +00:00
return ERR_ALREADY_EXISTS ;
}
2017-03-18 23:36:26 +00:00
if ( p_class - > variables [ i ] . _export . type ! = Variant : : NIL ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
p_script - > member_info [ name ] = p_class - > variables [ i ] . _export ;
2014-02-10 01:10:30 +00:00
# ifdef TOOLS_ENABLED
2017-03-18 23:36:26 +00:00
if ( p_class - > variables [ i ] . default_value . get_type ( ) ! = Variant : : NIL ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
p_script - > member_default_values [ name ] = p_class - > variables [ i ] . default_value ;
2014-02-10 01:10:30 +00:00
}
# endif
2016-06-11 21:31:22 +00:00
} else {
2017-03-18 23:36:26 +00:00
p_script - > member_info [ name ] = PropertyInfo ( Variant : : NIL , name , PROPERTY_HINT_NONE , " " , PROPERTY_USAGE_SCRIPT_VARIABLE ) ;
2014-02-10 01:10:30 +00:00
}
2014-10-28 01:54:32 +00:00
//int new_idx = p_script->member_indices.size();
GDScript : : MemberInfo minfo ;
minfo . index = p_script - > member_indices . size ( ) ;
minfo . setter = p_class - > variables [ i ] . setter ;
2016-03-08 23:00:52 +00:00
minfo . getter = p_class - > variables [ i ] . getter ;
2017-03-18 23:36:26 +00:00
p_script - > member_indices [ name ] = minfo ;
2014-02-10 01:10:30 +00:00
p_script - > members . insert ( name ) ;
}
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_class - > constant_expressions . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
StringName name = p_class - > constant_expressions [ i ] . identifier ;
2017-03-18 23:36:26 +00:00
ERR_CONTINUE ( p_class - > constant_expressions [ i ] . expression - > type ! = GDParser : : Node : : TYPE_CONSTANT ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
GDParser : : ConstantNode * constant = static_cast < GDParser : : ConstantNode * > ( p_class - > constant_expressions [ i ] . expression ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
p_script - > constants . insert ( name , constant - > value ) ;
2014-02-10 01:10:30 +00:00
//p_script->constants[constant->value].make_const();
}
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_class - > _signals . size ( ) ; i + + ) {
2015-06-24 16:29:23 +00:00
StringName name = p_class - > _signals [ i ] . name ;
GDScript * c = p_script ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
while ( c ) {
2015-06-24 16:29:23 +00:00
if ( c - > _signals . has ( name ) ) {
2017-03-18 23:36:26 +00:00
_set_error ( " Signal ' " + name + " ' redefined (in current or parent class) " , p_class ) ;
2015-06-24 16:29:23 +00:00
return ERR_ALREADY_EXISTS ;
}
if ( c - > base . is_valid ( ) ) {
2017-03-18 23:36:26 +00:00
c = c - > base . ptr ( ) ;
2015-06-24 16:29:23 +00:00
} else {
2017-03-18 23:36:26 +00:00
c = NULL ;
2015-06-24 16:29:23 +00:00
}
}
if ( native . is_valid ( ) ) {
2017-03-18 23:36:26 +00:00
if ( ObjectTypeDB : : has_signal ( native - > get_name ( ) , name ) ) {
_set_error ( " Signal ' " + name + " ' redefined (original in native class ' " + String ( native - > get_name ( ) ) + " ') " , p_class ) ;
2015-06-24 16:29:23 +00:00
return ERR_ALREADY_EXISTS ;
}
}
2017-03-18 23:36:26 +00:00
p_script - > _signals [ name ] = p_class - > _signals [ i ] . arguments ;
2015-06-24 16:29:23 +00:00
}
2014-02-10 01:10:30 +00:00
//parse sub-classes
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_class - > subclasses . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
StringName name = p_class - > subclasses [ i ] - > name ;
2016-06-01 23:22:02 +00:00
Ref < GDScript > subclass ;
if ( old_subclasses . has ( name ) ) {
2017-03-18 23:36:26 +00:00
subclass = old_subclasses [ name ] ;
2016-06-01 23:22:02 +00:00
} else {
subclass . instance ( ) ;
}
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
Error err = _parse_class ( subclass . ptr ( ) , p_script , p_class - > subclasses [ i ] , p_keep_state ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
2014-05-02 15:40:34 +00:00
2017-03-18 23:36:26 +00:00
p_script - > constants . insert ( name , subclass ) ; //once parsed, goes to the list of constants
p_script - > subclasses . insert ( name , subclass ) ;
2014-02-10 01:10:30 +00:00
}
//parse methods
2017-03-18 23:36:26 +00:00
bool has_initializer = false ;
bool has_ready = false ;
2015-12-28 22:31:52 +00:00
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_class - > functions . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( ! has_initializer & & p_class - > functions [ i ] - > name = = " _init " )
has_initializer = true ;
if ( ! has_ready & & p_class - > functions [ i ] - > name = = " _ready " )
has_ready = true ;
Error err = _parse_function ( p_script , p_class , p_class - > functions [ i ] ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
}
//parse static methods
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_class - > static_functions . size ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
Error err = _parse_function ( p_script , p_class , p_class - > static_functions [ i ] ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
}
if ( ! has_initializer ) {
//create a constructor
2017-03-18 23:36:26 +00:00
Error err = _parse_function ( p_script , p_class , NULL ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
}
2015-12-28 22:31:52 +00:00
if ( ! has_ready & & p_class - > ready - > statements . size ( ) ) {
//create a constructor
2017-03-18 23:36:26 +00:00
Error err = _parse_function ( p_script , p_class , NULL , true ) ;
2015-12-28 22:31:52 +00:00
if ( err )
return err ;
}
2015-01-03 03:57:02 +00:00
# ifdef DEBUG_ENABLED
//validate setters/getters if debug is enabled
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_class - > variables . size ( ) ; i + + ) {
2015-01-03 03:57:02 +00:00
if ( p_class - > variables [ i ] . setter ) {
2017-03-18 23:36:26 +00:00
const Map < StringName , GDFunction * > : : Element * E = p_script - > get_member_functions ( ) . find ( p_class - > variables [ i ] . setter ) ;
2015-01-03 03:57:02 +00:00
if ( ! E ) {
2017-03-18 23:36:26 +00:00
_set_error ( " Setter function ' " + String ( p_class - > variables [ i ] . setter ) + " ' not found in class. " , NULL ) ;
err_line = p_class - > variables [ i ] . line ;
err_column = 0 ;
2015-01-03 03:57:02 +00:00
return ERR_PARSE_ERROR ;
}
2016-05-22 00:18:16 +00:00
if ( E - > get ( ) - > is_static ( ) ) {
2015-01-03 03:57:02 +00:00
2017-03-18 23:36:26 +00:00
_set_error ( " Setter function ' " + String ( p_class - > variables [ i ] . setter ) + " ' is static. " , NULL ) ;
err_line = p_class - > variables [ i ] . line ;
err_column = 0 ;
2015-01-03 03:57:02 +00:00
return ERR_PARSE_ERROR ;
}
}
if ( p_class - > variables [ i ] . getter ) {
2017-03-18 23:36:26 +00:00
const Map < StringName , GDFunction * > : : Element * E = p_script - > get_member_functions ( ) . find ( p_class - > variables [ i ] . getter ) ;
2015-01-03 03:57:02 +00:00
if ( ! E ) {
2017-03-18 23:36:26 +00:00
_set_error ( " Getter function ' " + String ( p_class - > variables [ i ] . getter ) + " ' not found in class. " , NULL ) ;
err_line = p_class - > variables [ i ] . line ;
err_column = 0 ;
2015-01-03 03:57:02 +00:00
return ERR_PARSE_ERROR ;
}
2016-05-22 00:18:16 +00:00
if ( E - > get ( ) - > is_static ( ) ) {
2015-01-03 03:57:02 +00:00
2017-03-18 23:36:26 +00:00
_set_error ( " Getter function ' " + String ( p_class - > variables [ i ] . getter ) + " ' is static. " , NULL ) ;
err_line = p_class - > variables [ i ] . line ;
err_column = 0 ;
2015-01-03 03:57:02 +00:00
return ERR_PARSE_ERROR ;
}
}
}
2016-06-01 23:22:02 +00:00
//validate instances if keeping state
if ( p_keep_state ) {
2017-03-18 23:36:26 +00:00
print_line ( " RELOAD KEEP " + p_script - > path ) ;
for ( Set < Object * > : : Element * E = p_script - > instances . front ( ) ; E ; ) {
2016-06-01 23:22:02 +00:00
2017-03-18 23:36:26 +00:00
Set < Object * > : : Element * N = E - > next ( ) ;
2016-06-01 23:22:02 +00:00
ScriptInstance * si = E - > get ( ) - > get_script_instance ( ) ;
if ( si - > is_placeholder ( ) ) {
2016-06-01 23:44:34 +00:00
# ifdef TOOLS_ENABLED
2017-03-18 23:36:26 +00:00
PlaceHolderScriptInstance * psi = static_cast < PlaceHolderScriptInstance * > ( si ) ;
2016-06-01 23:22:02 +00:00
if ( p_script - > is_tool ( ) ) {
//re-create as an instance
p_script - > placeholders . erase ( psi ) ; //remove placeholder
2017-03-18 23:36:26 +00:00
GDInstance * instance = memnew ( GDInstance ) ;
instance - > base_ref = E - > get ( ) - > cast_to < Reference > ( ) ;
2016-06-01 23:22:02 +00:00
instance - > members . resize ( p_script - > member_indices . size ( ) ) ;
2017-03-18 23:36:26 +00:00
instance - > script = Ref < GDScript > ( p_script ) ;
instance - > owner = E - > get ( ) ;
2016-06-01 23:22:02 +00:00
//needed for hot reloading
2017-03-18 23:36:26 +00:00
for ( Map < StringName , GDScript : : MemberInfo > : : Element * E = p_script - > member_indices . front ( ) ; E ; E = E - > next ( ) ) {
instance - > member_indices_cache [ E - > key ( ) ] = E - > get ( ) . index ;
2016-06-01 23:22:02 +00:00
}
instance - > owner - > set_script_instance ( instance ) ;
/* STEP 2, INITIALIZE AND CONSRTUCT */
Variant : : CallError ce ;
2017-03-18 23:36:26 +00:00
p_script - > initializer - > call ( instance , NULL , 0 , ce ) ;
2016-06-01 23:22:02 +00:00
2017-03-18 23:36:26 +00:00
if ( ce . error ! = Variant : : CallError : : CALL_OK ) {
2016-06-01 23:22:02 +00:00
//well, tough luck, not goinna do anything here
}
}
2016-06-01 23:44:34 +00:00
# endif
2016-06-01 23:22:02 +00:00
} else {
2016-06-01 23:44:34 +00:00
2017-03-18 23:36:26 +00:00
GDInstance * gi = static_cast < GDInstance * > ( si ) ;
2016-06-01 23:22:02 +00:00
gi - > reload_members ( ) ;
}
2017-03-18 23:36:26 +00:00
E = N ;
2016-06-01 23:22:02 +00:00
}
}
2015-01-03 03:57:02 +00:00
# endif
2016-02-29 12:56:36 +00:00
2017-03-18 23:36:26 +00:00
p_script - > valid = true ;
2014-02-10 01:10:30 +00:00
return OK ;
}
2017-03-18 23:36:26 +00:00
Error GDCompiler : : compile ( const GDParser * p_parser , GDScript * p_script , bool p_keep_state ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
err_line = - 1 ;
err_column = - 1 ;
error = " " ;
parser = p_parser ;
const GDParser : : Node * root = parser - > get_parse_tree ( ) ;
ERR_FAIL_COND_V ( root - > type ! = GDParser : : Node : : TYPE_CLASS , ERR_INVALID_DATA ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
source = p_script - > get_path ( ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
Error err = _parse_class ( p_script , NULL , static_cast < const GDParser : : ClassNode * > ( root ) , p_keep_state ) ;
2014-02-10 01:10:30 +00:00
if ( err )
return err ;
return OK ;
}
String GDCompiler : : get_error ( ) const {
return error ;
}
2017-03-18 23:36:26 +00:00
int GDCompiler : : get_error_line ( ) const {
2014-02-10 01:10:30 +00:00
return err_line ;
}
2017-03-18 23:36:26 +00:00
int GDCompiler : : get_error_column ( ) const {
2014-02-10 01:10:30 +00:00
return err_column ;
}
2017-03-18 23:36:26 +00:00
GDCompiler : : GDCompiler ( ) {
2014-02-10 01:10:30 +00:00
}