diff --git a/packages/builtin/source_loc.pony b/packages/builtin/source_loc.pony index f8a42ae371b..d2cc4920de5 100644 --- a/packages/builtin/source_loc.pony +++ b/packages/builtin/source_loc.pony @@ -7,6 +7,11 @@ interface val SourceLoc Name and path of source file. """ + fun typ(): String + """ + Name of containing class, actor, primitive, struct, interface, or trait. + """ + fun method(): String """ Name of containing method. @@ -23,4 +28,3 @@ interface val SourceLoc Character position on line. Character positions start at 1. """ - \ No newline at end of file diff --git a/src/libponyc/pass/sugar.c b/src/libponyc/pass/sugar.c index 11151f98cf9..170eeb9cdc1 100644 --- a/src/libponyc/pass/sugar.c +++ b/src/libponyc/pass/sugar.c @@ -1065,6 +1065,21 @@ ast_t* expand_location(ast_t* location) } } + // Find name of containing type. + const char* type_name = ""; + for(ast_t* typ = location; typ != NULL; typ = ast_parent(typ)) + { + token_id variety = ast_id(typ); + + if(variety == TK_INTERFACE || variety == TK_TRAIT || + variety == TK_PRIMITIVE || variety == TK_STRUCT || + variety == TK_CLASS || variety == TK_ACTOR) + { + type_name = ast_name(ast_child(typ)); + break; + } + } + // Create an object literal. BUILD(ast, location, NODE(TK_OBJECT, DATA("__loc") @@ -1077,6 +1092,12 @@ ast_t* expand_location(ast_t* location) NONE NODE(TK_SEQ, STRING(file_name)) NONE) + NODE(TK_FUN, AST_SCOPE + NODE(TK_TAG) ID("typ") NONE NONE + NODE(TK_NOMINAL, NONE ID("String") NONE NONE NONE) + NONE + NODE(TK_SEQ, STRING(type_name)) + NONE) NODE(TK_FUN, AST_SCOPE NODE(TK_TAG) ID("method") NONE NONE NODE(TK_NOMINAL, NONE ID("String") NONE NONE NONE) diff --git a/test/libponyc/sugar_expr.cc b/test/libponyc/sugar_expr.cc index 836e14d75c6..59bc4169e3c 100644 --- a/test/libponyc/sugar_expr.cc +++ b/test/libponyc/sugar_expr.cc @@ -564,6 +564,7 @@ TEST_F(SugarExprTest, Location) " fun box bar(): None =>\n" " object\n" " fun tag file(): String => \"\"\n" + " fun tag typ(): String => \"Foo\"\n" " fun tag method(): String => \"bar\"\n" " fun tag line(): USize => 4\n" " fun tag pos(): USize => 5\n" @@ -578,6 +579,7 @@ TEST_F(SugarExprTest, LocationDefaultArg) const char* short_form = "interface val SourceLoc\n" " fun file(): String\n" + " fun typ(): String\n" " fun method(): String\n" " fun line(): USize\n" " fun pos(): USize\n" @@ -585,13 +587,15 @@ TEST_F(SugarExprTest, LocationDefaultArg) "class Foo\n" " var create: U32\n" " fun bar() =>\n" - " wombat()\n" - " fun wombat(x: SourceLoc = __loc) =>\n" - " None"; + " Log.apply()" + + "primitive Log\n" + " fun apply(x: SourceLoc = __loc) => None\n"; const char* full_form = "interface val SourceLoc\n" " fun file(): String\n" + " fun typ(): String\n" " fun method(): String\n" " fun line(): USize\n" " fun pos(): USize\n" @@ -599,14 +603,16 @@ TEST_F(SugarExprTest, LocationDefaultArg) "class Foo\n" " var create: U32\n" " fun bar() =>\n" - " wombat(object\n" + " Log.apply(object\n" " fun tag file(): String => \"\"\n" + " fun tag typ(): String => \"Foo\"\n" " fun tag method(): String => \"bar\"\n" - " fun tag line(): USize => 9\n" - " fun tag pos(): USize => 11\n" - " end)\n" - " fun wombat(x: SourceLoc = __loc) =>\n" - " None"; + " fun tag line(): USize => 10\n" + " fun tag pos(): USize => 14\n" + " end)" + + "primitive Log\n" + " fun apply(x: SourceLoc = __loc) => None\n"; TEST_EQUIV(short_form, full_form); }