Skip to content

Commit

Permalink
Add SourceLoc.type_name method, showing the name of the containing ty…
Browse files Browse the repository at this point in the history
…pe. (ponylang#2643)

* Add SourceLoc.typ method, showing the name of the containing type.

SourceLoc already shows the method name of the __loc object,
but not the type name. Adding the type name is a logical next step,
since it is available.

The method is named typ, because type is a reserved keyword that
cannot be used as a method name.

* Rename SourceLoc.{typ, method} as {type_name, method_name}.

* Add manual changelog entry for PR ponylang#2643.
  • Loading branch information
jemc authored and dipinhora committed Jun 5, 2018
1 parent 5dae90a commit 42fcc7d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ All notable changes to the Pony compiler and standard library will be documented

### Added

- `SourceLoc.type_name` method ([PR #2643](https://github.com/ponylang/ponyc/pull/2643))
- Update with experimental support for LLVM 6.0.0 ([PR #2595](https://github.com/ponylang/ponyc/pull/2595))
- Improve pattern matching error reporting ([PR #2628](https://github.com/ponylang/ponyc/pull/2628))
- Allow recovering at most one element of a tuple to mutable capability. ([PR #2585](https://github.com/ponylang/ponyc/pull/2585))
Expand All @@ -49,6 +50,7 @@ All notable changes to the Pony compiler and standard library will be documented

### Changed

- `SourceLoc.method` renamed as `SourceLoc.method_name` method ([PR #2643](https://github.com/ponylang/ponyc/pull/2643))
- New Ponybench API (RFC 52) ([PR #2578](https://github.com/ponylang/ponyc/pull/2578))
- Forbid impossible pattern matching on generic capabilities ([PR #2499](https://github.com/ponylang/ponyc/pull/2499))
- Remove case functions ([PR #2542](https://github.com/ponylang/ponyc/pull/2542))
Expand Down
8 changes: 6 additions & 2 deletions packages/builtin/source_loc.pony
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ interface val SourceLoc
Name and path of source file.
"""

fun method(): String
fun type_name(): String
"""
Name of nearest class, actor, primitive, struct, interface, or trait.
"""

fun method_name(): String
"""
Name of containing method.
"""
Expand All @@ -23,4 +28,3 @@ interface val SourceLoc
Character position on line.
Character positions start at 1.
"""

23 changes: 22 additions & 1 deletion src/libponyc/pass/sugar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -1078,7 +1093,13 @@ ast_t* expand_location(ast_t* location)
NODE(TK_SEQ, STRING(file_name))
NONE)
NODE(TK_FUN, AST_SCOPE
NODE(TK_TAG) ID("method") NONE NONE
NODE(TK_TAG) ID("type_name") 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_name") NONE NONE
NODE(TK_NOMINAL, NONE ID("String") NONE NONE NONE)
NONE
NODE(TK_SEQ, STRING(method_name))
Expand Down
32 changes: 19 additions & 13 deletions test/libponyc/sugar_expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ TEST_F(SugarExprTest, Location)
" fun box bar(): None =>\n"
" object\n"
" fun tag file(): String => \"\"\n"
" fun tag method(): String => \"bar\"\n"
" fun tag type_name(): String => \"Foo\"\n"
" fun tag method_name(): String => \"bar\"\n"
" fun tag line(): USize => 4\n"
" fun tag pos(): USize => 5\n"
" end";
Expand All @@ -578,35 +579,40 @@ TEST_F(SugarExprTest, LocationDefaultArg)
const char* short_form =
"interface val SourceLoc\n"
" fun file(): String\n"
" fun method(): String\n"
" fun type_name(): String\n"
" fun method_name(): String\n"
" fun line(): USize\n"
" fun pos(): USize\n"

"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 method(): String\n"
" fun type_name(): String\n"
" fun method_name(): String\n"
" fun line(): USize\n"
" fun pos(): USize\n"

"class Foo\n"
" var create: U32\n"
" fun bar() =>\n"
" wombat(object\n"
" Log.apply(object\n"
" fun tag file(): String => \"\"\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 type_name(): String => \"Foo\"\n"
" fun tag method_name(): String => \"bar\"\n"
" 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);
}
Expand Down

0 comments on commit 42fcc7d

Please sign in to comment.