// Check that the given method is compatible with the existing definition. // Return the method that ends up being in the entity or NULL on error. // Stages 2A, 2B and 2C. static ast_t* add_method(ast_t* entity, ast_t* existing_method, ast_t* new_method, ast_t** last_method) { assert(entity != NULL); assert(new_method != NULL); assert(last_method != NULL); const char* name = ast_name(ast_childidx(new_method, 1)); const char* entity_name = ast_name(ast_child(entity)); if(existing_method == NULL) { // This method is new to the entity. // Stage 2C. ast_t* case_clash = ast_get_case(entity, name, NULL); if(case_clash != NULL) { ast_error(case_clash, "in %s method name %s differs only in case", entity_name, name); ast_error(new_method, "clashing method is here"); return NULL; } attach_method_t(new_method, NULL, false); ast_list_append(ast_childidx(entity, 4), last_method, new_method); ast_set(entity, name, new_method, SYM_DEFINED); return *last_method; } method_t* info = (method_t*)ast_data(existing_method); assert(info != NULL); if(info->local_def) { // Existing method is a local definition, new method must be a subtype // Stage 2A if(is_subtype(existing_method, new_method, true)) return existing_method; ast_error(existing_method, "local method %s is not compatible with provided version", name); ast_error(new_method, "clashing method is here"); return NULL; } // Both method versions came from the provides list, their signatures must // match exactly // Stage 2B if(compare_signatures(existing_method, new_method)) return existing_method; ast_error(entity, "clashing definitions of method %s provided, local " "disambiguation required", name); ast_error(existing_method, "provided here"); ast_error(new_method, "and here"); return NULL; }
static const char* suggest_alt_name(ast_t* ast, const char* name) { assert(ast != NULL); assert(name != NULL); size_t name_len = strlen(name); if(is_name_private(name)) { // Try without leading underscore const char* try_name = stringtab(name + 1); if(ast_get(ast, try_name, NULL) != NULL) return try_name; } else { // Try with a leading underscore char* buf = (char*)ponyint_pool_alloc_size(name_len + 2); buf[0] = '_'; strncpy(buf + 1, name, name_len + 1); const char* try_name = stringtab_consume(buf, name_len + 2); if(ast_get(ast, try_name, NULL) != NULL) return try_name; } // Try with a different case (without crossing type/value boundary) ast_t* case_ast = ast_get_case(ast, name, NULL); if(case_ast != NULL) { ast_t* id = case_ast; if(ast_id(id) != TK_ID) id = ast_child(id); assert(ast_id(id) == TK_ID); const char* try_name = ast_name(id); if(ast_get(ast, try_name, NULL) != NULL) return try_name; } // Give up return NULL; }
bool ast_set(ast_t* ast, const char* name, ast_t* value, sym_status_t status, bool allow_shadowing) { while(ast->symtab == NULL) ast = ast->parent; if(allow_shadowing) { // Only check the local scope. if(symtab_find_case(ast->symtab, name, NULL) != NULL) return false; } else { // Check the local scope and all parent scopes. if(ast_get_case(ast, name, NULL) != NULL) return false; } return symtab_add(ast->symtab, name, value, status); }
static const char* suggest_alt_name(ast_t* ast, const char* name) { assert(ast != NULL); assert(name != NULL); size_t name_len = strlen(name); VLA(char, buf, name_len + 2); if(name[0] == '_') { // Try without leading underscore const char* try_name = stringtab(name + 1); if(ast_get(ast, try_name, NULL) != NULL) return try_name; } else { // Try with a leading underscore buf[0] = '_'; strncpy(buf + 1, name, name_len + 1); const char* try_name = stringtab(buf); if(ast_get(ast, try_name, NULL) != NULL) return try_name; } // Try with a different case (without crossing type/value boundary) ast_t* case_ast = ast_get_case(ast, name, NULL); if(case_ast != NULL) { assert(ast_child(case_ast) != NULL); const char* try_name = ast_name(ast_child(case_ast)); if(ast_get(ast, try_name, NULL) != NULL) return try_name; } // Give up return NULL; }
/** * Insert a name->AST mapping into the specified scope. */ static bool set_scope(typecheck_t* t, ast_t* scope, ast_t* name, ast_t* value) { assert(ast_id(name) == TK_ID); const char* s = ast_name(name); sym_status_t status = SYM_NONE; switch(ast_id(value)) { case TK_ID: { if((t != NULL) && (t->frame->pattern != NULL)) status = SYM_DEFINED; else status = SYM_UNDEFINED; break; } case TK_FFIDECL: case TK_TYPE: case TK_INTERFACE: case TK_TRAIT: case TK_PRIMITIVE: case TK_CLASS: case TK_ACTOR: case TK_TYPEPARAM: case TK_PACKAGE: case TK_NEW: case TK_BE: case TK_FUN: break; case TK_FVAR: case TK_FLET: case TK_EMBED: status = SYM_DEFINED; break; case TK_PARAM: status = SYM_DEFINED; break; default: assert(0); return false; } if(!ast_set(scope, s, value, status)) { ast_t* prev = ast_get(scope, s, NULL); ast_t* prev_nocase = ast_get_case(scope, s, NULL); ast_error(name, "can't reuse name '%s'", s); ast_error(prev_nocase, "previous use of '%s'%s", s, (prev == NULL) ? " differs only by case" : ""); return false; } return true; }
// Add a new method to the given entity, based on the specified method from // the specified type. // The trait_ref is the entry in the provides / delegates list that causes this // method inclusion. Needed for error reporting. // The basis_method is the reified method in the trait to add. // The adjective parameter is used for error reporting and should be "provided" // or similar. // Return the newly added method or NULL on error. static ast_t* add_method(ast_t* entity, ast_t* trait_ref, ast_t* basis_method, const char* adjective, pass_opt_t* opt) { assert(entity != NULL); assert(trait_ref != NULL); assert(basis_method != NULL); assert(adjective != NULL); const char* name = ast_name(ast_childidx(basis_method, 1)); // Check behaviour compatability. if(ast_id(basis_method) == TK_BE) { switch(ast_id(entity)) { case TK_PRIMITIVE: ast_error(opt->check.errors, trait_ref, "cannot add a behaviour (%s) to a primitive", name); return NULL; case TK_STRUCT: ast_error(opt->check.errors, trait_ref, "cannot add a behaviour (%s) to a struct", name); return NULL; case TK_CLASS: ast_error(opt->check.errors, trait_ref, "cannot add a behaviour (%s) to a class", name); return NULL; default: break; } } // Check for existing method of the same name. ast_t* existing = ast_get(entity, name, NULL); if(existing != NULL) { assert(is_field(existing)); // Should already have checked for methods. ast_error(opt->check.errors, trait_ref, "%s method '%s' clashes with field", adjective, name); ast_error_continue(opt->check.errors, basis_method, "method is defined here"); return NULL; } // Check for clash with existing method. ast_t* case_clash = ast_get_case(entity, name, NULL); if(case_clash != NULL) { const char* clash_name = ""; switch(ast_id(case_clash)) { case TK_FUN: case TK_BE: case TK_NEW: clash_name = ast_name(ast_childidx(case_clash, 1)); break; case TK_LET: case TK_VAR: case TK_EMBED: clash_name = ast_name(ast_child(case_clash)); break; default: assert(0); break; } ast_error(opt->check.errors, trait_ref, "%s method '%s' differs only in case from '%s'", adjective, name, clash_name); ast_error_continue(opt->check.errors, basis_method, "clashing method is defined here"); return NULL; } AST_GET_CHILDREN(basis_method, cap, id, typeparams, params, result, can_error, body, doc); // Ignore docstring. if(ast_id(doc) == TK_STRING) { ast_set_name(doc, ""); ast_setid(doc, TK_NONE); } ast_t* local = ast_append(ast_childidx(entity, 4), basis_method); ast_set(entity, name, local, SYM_DEFINED, false); ast_t* body_donor = (ast_t*)ast_data(basis_method); method_t* info = attach_method_t(local); info->trait_ref = trait_ref; if(ast_id(body) != TK_NONE) info->body_donor = body_donor; return local; }