Groundwork for closure lowering, run raw function pointer type

This commit is contained in:
Nathan Braswell
2017-01-26 23:58:48 -05:00
parent caba8b310f
commit dad0f780bb
5 changed files with 46 additions and 35 deletions

View File

@@ -5,7 +5,7 @@ unorderd_list_part = import WS unorderd_list_part | function WS unorderd_list_pa
type = "ref" WS pre_reffed | pre_reffed ;
pre_reffed = "\*" WS pre_reffed | "void" | "char" | "uchar" | "short" | "ushort" | "int" | "uint" | "long" | "ulong" | "float" | "double" | scoped_identifier | scoped_identifier WS template_inst | function_type ;
function_type = "fun" WS "\(" WS opt_type_list WS "\)" WS ":" WS type ;
function_type = "fun" WS "\(" WS opt_type_list WS "\)" WS ":" WS type | "run" WS "\(" WS opt_type_list WS "\)" WS ":" WS type ;
dec_type = ":" WS type ;
opt_type_list = type_list | ;
@@ -144,7 +144,7 @@ floating_literal = "[0-9]+.[0-9]+(f|d)?" ;
bool = "true" | "false" ;
character = "'(`|[0-9]|-|=|(\\t)|[a-z]|\[|]|(\\\\)|;|(\\')|(\\n)|,|.|/|~|!|@|#|$|%|^|&|\*|\(|\)|_|\+|[A-Z]|{|}|\||:|\"|<|>|\?| |(\\0))'" ;
keywords_also_identifiers = "obj" | "def" | "fun" | "var" | "ref" | "adt" | "cast" | "import" | "simple_passthrough" ;
keywords_also_identifiers = "obj" | "def" | "fun" | "run" | "var" | "ref" | "adt" | "cast" | "import" | "simple_passthrough" ;
alpha_alphanumeric = "([a-z]|[A-Z]|_)([a-z]|[A-Z]|_|[0-9])*" ;
augmented_alpha_alphanumeric = alpha_alphanumeric augmented_alpha_alphanumeric | keywords_also_identifiers augmented_alpha_alphanumeric | alpha_alphanumeric | keywords_also_identifiers ;

View File

@@ -172,9 +172,9 @@ obj ast_transformation (Object) {
var function_node = null<ast_node>()
if (type_syntax) {
var identifier_param = ast_identifier_ptr(option_name, ident_type, node)
function_node = ast_function_ptr(option_name, type_ptr(vector(get_ast_type(identifier_param)), node->adt_def.self_type), vector(identifier_param), false)
function_node = ast_function_ptr(option_name, type_ptr(vector(get_ast_type(identifier_param)), node->adt_def.self_type, 0, false, false, false), vector(identifier_param), false)
} else {
function_node = ast_function_ptr(option_name, type_ptr(vector<*type>(), node->adt_def.self_type), vector<*ast_node>(), false)
function_node = ast_function_ptr(option_name, type_ptr(vector<*type>(), node->adt_def.self_type, 0, false, false, false), vector<*ast_node>(), false)
}
add_to_scope(option_name, function_node, node)
add_to_scope("~enclosing_scope", node, function_node)
@@ -189,12 +189,12 @@ obj ast_transformation (Object) {
var copy_construct_param = ast_identifier_ptr(string("in"), node->adt_def.self_type->clone_with_indirection(1,false), node)
var assign_param = ast_identifier_ptr(string("in"), node->adt_def.self_type->clone_with_indirection(0,true), node)
vector(
make_pair("operator==", ast_function_ptr(string("operator=="), type_ptr(vector(equals_param->identifier.type), type_ptr(base_type::boolean())), vector(equals_param), false)),
make_pair("operator!=", ast_function_ptr(string("operator!="), type_ptr(vector(nequals_param->identifier.type), type_ptr(base_type::boolean())), vector(nequals_param), false)),
make_pair("construct", ast_function_ptr(string("construct"), type_ptr(vector<*type>(), node->adt_def.self_type->clone_with_increased_indirection()), vector<*ast_node>(), false)),
make_pair("copy_construct", ast_function_ptr(string("copy_construct"), type_ptr(vector(copy_construct_param->identifier.type), type_ptr(base_type::void_return())), vector(copy_construct_param), false)),
make_pair("operator=", ast_function_ptr(string("operator="), type_ptr(vector(assign_param->identifier.type), type_ptr(base_type::void_return())), vector(assign_param), false)),
make_pair("destruct", ast_function_ptr(string("destruct"), type_ptr(vector<*type>(), type_ptr(base_type::void_return())), vector<*ast_node>(), false))
make_pair("operator==", ast_function_ptr(string("operator=="), type_ptr(vector(equals_param->identifier.type), type_ptr(base_type::boolean()), 0, false, false, false), vector(equals_param), false)),
make_pair("operator!=", ast_function_ptr(string("operator!="), type_ptr(vector(nequals_param->identifier.type), type_ptr(base_type::boolean()), 0, false, false, false), vector(nequals_param), false)),
make_pair("construct", ast_function_ptr(string("construct"), type_ptr(vector<*type>(), node->adt_def.self_type->clone_with_increased_indirection(), 0, false, false, false), vector<*ast_node>(), false)),
make_pair("copy_construct", ast_function_ptr(string("copy_construct"), type_ptr(vector(copy_construct_param->identifier.type), type_ptr(base_type::void_return()), 0, false, false, false), vector(copy_construct_param), false)),
make_pair("operator=", ast_function_ptr(string("operator="), type_ptr(vector(assign_param->identifier.type), type_ptr(base_type::void_return()), 0, false, false, false), vector(assign_param), false)),
make_pair("destruct", ast_function_ptr(string("destruct"), type_ptr(vector<*type>(), type_ptr(base_type::void_return()), 0, false, false, false), vector<*ast_node>(), false))
).for_each(fun(func_pair: pair<*char, *ast_node>) {
node->adt_def.regular_funcs.add(func_pair.second)
add_to_scope(string(func_pair.first), func_pair.second, node)
@@ -240,10 +240,11 @@ obj ast_transformation (Object) {
parameters.add(ast_identifier_ptr(concat_symbol_tree(get_node("identifier", child)), param_type, null<ast_node>()))
})
var is_variadic = get_node("\"...\"", node) != null<tree<symbol>>()
var is_raw = false
// figure out function type and make function_node
var function_node = ast_function_ptr(function_name,
type_ptr(parameters.map(fun(parameter: *ast_node): *type return parameter->identifier.type;),
return_type, 0, false, is_variadic), parameters, get_node("\"ext\"", node) != null<tree<symbol>>(), is_variadic)
return_type, 0, false, is_variadic, is_raw), parameters, get_node("\"ext\"", node) != null<tree<symbol>>(), is_variadic)
// fix up the enclosing_scope's
parameters.for_each(fun(n: *ast_node) n->identifier.enclosing_scope = function_node;)
// add to scope
@@ -426,8 +427,9 @@ obj ast_transformation (Object) {
else if (type_syntax_str == "double")
return type_ptr(base_type::double_precision(), indirection, is_ref)
else if (get_node("function_type", real_node)) {
var types = get_nodes("type", get_node("function_type", real_node)).map(fun(node: *tree<symbol>): *type transform_type(node, scope, template_replacements);)
return type_ptr(types.slice(0,-2), types.last(), indirection, is_ref)
var function_type = get_node("function_type", real_node)
var types = get_nodes("type", function_type).map(fun(node: *tree<symbol>): *type transform_type(node, scope, template_replacements);)
return type_ptr(types.slice(0,-2), types.last(), indirection, is_ref, false, get_node("\"run\"", function_type) != null<tree<symbol>>()) // check for raw function pointer
} else {
// do lookup for objects, ADTs, templates, etc
var possibilities = scope_lookup(type_syntax_str, scope)

View File

@@ -787,15 +787,19 @@ obj c_generator (Object) {
// maybe disregard indirection in the future?
type = type->clone_with_indirection(0,false)
if (!function_type_map.contains_key(*type)) {
var temp_name = string("function_struct") + get_id()
var temp_name = string("function") + get_id()
var temp = string()
type->parameter_types.for_each(fun(parameter_type: *type) temp += string(", ") + type_to_c(parameter_type) + " ";)
var with_data = string("typedef ") + type_to_c(type->return_type) + " (*" + temp_name + "_with_data)(void*" + temp + ");\n"
var without_data = string("typedef ") + type_to_c(type->return_type) + " (*" + temp_name + "_without_data)(" + temp.slice(1,-1) + ");\n"
function_typedef_string += with_data
function_typedef_string += without_data
function_typedef_string += string("typedef struct {\nvoid* data;\n") + temp_name + "_with_data func;\n} " + temp_name + ";\n"
function_typedef_string += string("/* ") + type->to_string() + " */\n"
if (type->is_raw) {
function_typedef_string += string("typedef ") + type_to_c(type->return_type) + " (*" + temp_name + ")(" + temp.slice(1,-1) + ");\n"
} else {
var with_data = string("typedef ") + type_to_c(type->return_type) + " (*" + temp_name + "_with_data)(void*" + temp + ");\n"
var without_data = string("typedef ") + type_to_c(type->return_type) + " (*" + temp_name + "_without_data)(" + temp.slice(1,-1) + ");\n"
function_typedef_string += with_data
function_typedef_string += without_data
function_typedef_string += string("typedef struct {\nvoid* data;\n") + temp_name + "_with_data func;\n} " + temp_name + ";\n"
function_typedef_string += string("/* ") + type->to_string() + " */\n"
}
// again, the indirection
function_type_map[*type] = temp_name
}

View File

@@ -179,32 +179,32 @@ fun get_builtin_function(name: string, param_types: vector<*type>, syntax: *tree
// none of the builtin functions should take in references
param_types = param_types.map(fun(t: *type): *type return t->clone_without_ref();)
if (name == "==" || name == "!=" || name == ">" || name == "<" || name == "<=" || name == ">" || name == ">=" || name == "&&" || name == "||" || name == "!")
return ast_function_ptr(name, type_ptr(param_types, type_ptr(base_type::boolean())), vector<*ast_node>(), false)
return ast_function_ptr(name, type_ptr(param_types, type_ptr(base_type::boolean()), 0, false, false, false), vector<*ast_node>(), false)
if (name == "." || name == "->") {
if (name == "->" && param_types[0]->indirection == 0)
error(syntax, string("drereferencing not a pointer: ") + name)
else if (name == "." && param_types[0]->indirection != 0)
error(syntax, string("dot operator on a pointer: ") + name)
else
return ast_function_ptr(name, type_ptr(param_types, param_types[1]), vector<*ast_node>(), false)
return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, false), vector<*ast_node>(), false)
}
if (name == "[]") {
if (param_types[0]->indirection == 0)
error(syntax, string("drereferencing not a pointer: ") + name)
else
return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection()), vector<*ast_node>(), false)
return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, false), vector<*ast_node>(), false)
}
if (name == "&" && param_types.size == 1)
return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_increased_indirection()), vector<*ast_node>(), false)
return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_increased_indirection(), 0, false, false, false), vector<*ast_node>(), false)
if (name == "*" && param_types.size == 1) {
if (param_types[0]->indirection == 0)
error(syntax, string("drereferencing not a pointer: ") + name)
else
return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection()), vector<*ast_node>(), false)
return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, false), vector<*ast_node>(), false)
}
if (param_types.size > 1 && param_types[1]->rank() > param_types[0]->rank())
return ast_function_ptr(name, type_ptr(param_types, param_types[1]), vector<*ast_node>(), false)
return ast_function_ptr(name, type_ptr(param_types, param_types[0]), vector<*ast_node>(), false)
return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, false), vector<*ast_node>(), false)
return ast_function_ptr(name, type_ptr(param_types, param_types[0], 0, false, false, false), vector<*ast_node>(), false)
}
fun possible_object_equality(lvalue: *ast_node, rvalue: *ast_node): *ast_node {
var ltype = get_ast_type(lvalue)

View File

@@ -40,12 +40,8 @@ fun type_ptr(base: base_type, indirection: int): *type return type_ptr(base, ind
fun type_ptr(base: base_type, indirection: int, is_ref: bool): *type {
return new<type>()->construct(base, indirection, is_ref)
}
fun type_ptr(parameters: vector<*type>, return_type: *type): *type return type_ptr(parameters, return_type, 0, false, false);
fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int): *type return type_ptr(parameters, return_type, indirection, false, false)
fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int, is_ref: bool): *type
return new<type>()->construct(parameters, return_type, indirection, is_ref, false)
fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int, is_ref: bool, is_variadic: bool): *type
return new<type>()->construct(parameters, return_type, indirection, is_ref, is_variadic)
fun type_ptr(parameters: vector<*type>, return_type: *type, indirection: int, is_ref: bool, is_variadic: bool, is_raw: bool): *type
return new<type>()->construct(parameters, return_type, indirection, is_ref, is_variadic, is_raw)
fun type_ptr(traits: set<string>): *type {
return new<type>()->construct(traits)
@@ -55,6 +51,7 @@ obj type (Object) {
var base: base_type
var parameter_types: vector<*type>
var is_variadic: bool
var is_raw: bool
var return_type: *type
var indirection: int
var type_def: *ast_node
@@ -69,6 +66,7 @@ obj type (Object) {
traits.construct()
is_ref = false
is_variadic = false
is_raw = false
return this
}
fun construct(traits_in: set<string>): *type {
@@ -80,6 +78,7 @@ obj type (Object) {
traits.copy_construct(&traits_in)
is_ref = false
is_variadic = false
is_raw = false
return this
}
fun construct(base_in: base_type, indirection_in: int, is_ref_in: bool): *type {
@@ -91,6 +90,7 @@ obj type (Object) {
traits.construct()
is_ref = is_ref_in
is_variadic = false
is_raw = false
return this
}
fun construct(type_def_in: *ast_node, traits_in: set<string>): *type {
@@ -102,9 +102,10 @@ obj type (Object) {
traits.copy_construct(&traits_in)
is_ref = false
is_variadic = false
is_raw = false
return this
}
fun construct(parameter_types_in: vector<*type>, return_type_in: *type, indirection_in: int, is_ref_in: bool, is_variadic_in: bool): *type {
fun construct(parameter_types_in: vector<*type>, return_type_in: *type, indirection_in: int, is_ref_in: bool, is_variadic_in: bool, is_raw_in: bool): *type {
base.copy_construct(&base_type::function())
parameter_types.copy_construct(&parameter_types_in)
return_type = return_type_in
@@ -113,6 +114,7 @@ obj type (Object) {
traits.construct()
is_ref = is_ref_in
is_variadic = is_variadic_in
is_raw = is_raw_in
return this
}
fun copy_construct(old: *type) {
@@ -124,6 +126,7 @@ obj type (Object) {
traits.copy_construct(&old->traits)
is_ref = old->is_ref
is_variadic = old->is_variadic
is_raw = old->is_raw
}
fun operator=(other: ref type) {
destruct()
@@ -138,7 +141,7 @@ obj type (Object) {
fun operator==(other: ref type):bool return equality(other, true);
fun equality(other: *type, care_about_ref: bool):bool return equality(*other, care_about_ref);
fun equality(other: ref type, care_about_ref: bool):bool {
if (parameter_types.size != other.parameter_types.size || is_variadic != other.is_variadic)
if (parameter_types.size != other.parameter_types.size || is_variadic != other.is_variadic || is_raw != other.is_raw)
return false
for (var i = 0; i < parameter_types.size; i++;)
if (!deref_equality(parameter_types[i], other.parameter_types[i]))
@@ -162,6 +165,8 @@ obj type (Object) {
indr_string += " ref "
if (is_variadic)
indr_string += " variadic "
if (is_raw)
indr_string += " raw "
for (var i = 0; i < indirection; i++;) indr_string += "*"
match (base) {
base_type::none() return indr_string + string("none") + trait_string