Skip to content

Commit 86d06b4

Browse files
committed
Spec: Refactoring: Merge "definitions" and "declarations".
Previously, abstract members were syntactically separate from concrete members, and referred to "declarations", as opposed to "definitions". In this commit, all of them become "definitions". There are abstract definitions and concrete definitions. They share their syntactic productions. This will allow to more easily introduce more kinds of definitions that can be abstract or concrete in deeper productions of the grammar, such as extension methods.
1 parent 98b452d commit 86d06b4

11 files changed

+143
-163
lines changed

Diff for: compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+11-19
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ object Parsers {
17551755
while in.token == TYPE do tdefs += typeBlockStat()
17561756
tdefs.toList
17571757

1758-
/** TypeBlockStat ::= ‘type’ {nl} TypeDcl
1758+
/** TypeBlockStat ::= ‘type’ {nl} TypeDef
17591759
*/
17601760
def typeBlockStat(): Tree =
17611761
val mods = defAnnotsMods(BitSet())
@@ -3578,12 +3578,8 @@ object Parsers {
35783578
/** Def ::= val PatDef
35793579
* | var VarDef
35803580
* | def DefDef
3581-
* | type {nl} TypeDcl
3581+
* | type {nl} TypeDef
35823582
* | TmplDef
3583-
* Dcl ::= val ValDcl
3584-
* | var ValDcl
3585-
* | def DefDcl
3586-
* | type {nl} TypeDcl
35873583
* EnumCase ::= `case' (id ClassConstr [`extends' ConstrApps]] | ids)
35883584
*/
35893585
def defOrDcl(start: Int, mods: Modifiers): Tree = in.token match {
@@ -3604,12 +3600,10 @@ object Parsers {
36043600
tmplDef(start, mods)
36053601
}
36063602

3607-
/** PatDef ::= ids [‘:’ Type] ‘=’ Expr
3608-
* | Pattern2 [‘:’ Type] ‘=’ Expr
3603+
/** PatDef ::= ids [‘:’ Type] [‘=’ Expr]
3604+
* | Pattern2 [‘:’ Type] [‘=’ Expr]
36093605
* VarDef ::= PatDef
3610-
* | id {`,' id} `:' Type `=' `_' (deprecated in 3.x)
3611-
* ValDcl ::= id {`,' id} `:' Type
3612-
* VarDcl ::= id {`,' id} `:' Type
3606+
* | id {`,' id} `:' Type `=' `_' (deprecated in 3.x)
36133607
*/
36143608
def patDefOrDcl(start: Offset, mods: Modifiers): Tree = atSpan(start, nameStart) {
36153609
val first = pattern2(Location.InPattern)
@@ -3658,9 +3652,8 @@ object Parsers {
36583652
}
36593653
}
36603654

3661-
/** DefDef ::= DefSig [‘:’ Type] ‘=’ Expr
3655+
/** DefDef ::= DefSig [‘:’ Type] [‘=’ Expr]
36623656
* | this TypelessClauses [DefImplicitClause] `=' ConstrExpr
3663-
* DefDcl ::= DefSig `:' Type
36643657
* DefSig ::= id [DefTypeParamClause] DefTermParamClauses
36653658
*
36663659
* if clauseInterleaving is enabled:
@@ -3758,7 +3751,7 @@ object Parsers {
37583751
argumentExprss(mkApply(Ident(nme.CONSTRUCTOR), argumentExprs()))
37593752
}
37603753

3761-
/** TypeDcl ::= id [TypeParamClause] {FunParamClause} TypeBounds [‘=’ Type]
3754+
/** TypeDef ::= id [TypeParamClause] {FunParamClause} TypeBounds [‘=’ Type]
37623755
*/
37633756
def typeDefOrDcl(start: Offset, mods: Modifiers): Tree = {
37643757
newLinesOpt()
@@ -4247,7 +4240,6 @@ object Parsers {
42474240
* TemplateStat ::= Import
42484241
* | Export
42494242
* | Annotations Modifiers Def
4250-
* | Annotations Modifiers Dcl
42514243
* | Extension
42524244
* | Expr1
42534245
* |
@@ -4277,10 +4269,10 @@ object Parsers {
42774269
}
42784270

42794271
/** RefineStatSeq ::= RefineStat {semi RefineStat}
4280-
* RefineStat ::= ‘val’ VarDcl
4281-
* | ‘def’ DefDcl
4282-
* | ‘type’ {nl} TypeDcl
4283-
* (in reality we admit Defs and vars and filter them out afterwards in `checkLegal`)
4272+
* RefineStat ::= ‘val’ VarDef
4273+
* | ‘def’ DefDef
4274+
* | ‘type’ {nl} TypeDef
4275+
* (in reality we admit class defs and vars and filter them out afterwards in `checkLegal`)
42844276
*/
42854277
def refineStatSeq(): List[Tree] = {
42864278
val stats = new ListBuffer[Tree]

Diff for: docs/_docs/internals/syntax.md

+12-17
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ ParamType ::= [‘=>’] ParamValueType
208208
ParamValueType ::= [‘into’] ExactParamType Into(t)
209209
ExactParamType ::= ParamValueType [‘*’] PostfixOp(t, "*")
210210
TypeArgs ::= ‘[’ Types ‘]’ ts
211-
Refinement ::= :<<< [RefineDcl] {semi [RefineDcl]} >>> ds
211+
Refinement ::= :<<< [RefineDef] {semi [RefineDef]} >>> ds
212212
TypeBounds ::= [‘>:’ Type] [‘<:’ Type] TypeBoundsTree(lo, hi)
213213
TypeParamBounds ::= TypeBounds {‘:’ Type} ContextBounds(typeBounds, tps)
214214
Types ::= Type {‘,’ Type}
@@ -412,29 +412,24 @@ EndMarkerTag ::= id | ‘if’ | ‘while’ | ‘for’ | ‘match’ |
412412
| ‘new’ | ‘this’ | ‘given’ | ‘extension’ | ‘val’
413413
```
414414

415-
### Declarations and Definitions
415+
### Definitions
416416
```ebnf
417-
RefineDcl ::= ‘val’ ValDcl
418-
| ‘def’ DefDcl
419-
| ‘type’ {nl} TypeDcl
420-
Dcl ::= RefineDcl
421-
| ‘var’ VarDcl
422-
ValDcl ::= ids ‘:’ Type PatDef(_, ids, tpe, EmptyTree)
423-
VarDcl ::= ids ‘:’ Type PatDef(_, ids, tpe, EmptyTree)
424-
DefDcl ::= DefSig ‘:’ Type DefDef(_, name, paramss, tpe, EmptyTree)
425-
DefSig ::= id [DefParamClauses] [DefImplicitClause]
426-
TypeDcl ::= id [TypeParamClause] {FunParamClause} TypeBounds TypeDefTree(_, name, tparams, bound
427-
[‘=’ Type]
417+
RefineDef ::= ‘val’ ValDef
418+
| ‘def’ DefDef
419+
| ‘type’ {nl} TypeDef
428420
429421
Def ::= ‘val’ PatDef
430422
| ‘var’ PatDef
431423
| ‘def’ DefDef
432-
| ‘type’ {nl} TypeDcl
424+
| ‘type’ {nl} TypeDef
433425
| TmplDef
434-
PatDef ::= ids [‘:’ Type] ‘=’ Expr
435-
| Pattern2 [‘:’ Type] ‘=’ Expr PatDef(_, pats, tpe?, expr)
436-
DefDef ::= DefSig [‘:’ Type] ‘=’ Expr DefDef(_, name, paramss, tpe, expr)
426+
PatDef ::= ids [‘:’ Type] [‘=’ Expr]
427+
| Pattern2 [‘:’ Type] [‘=’ Expr] PatDef(_, pats, tpe?, expr)
428+
DefDef ::= DefSig [‘:’ Type] [‘=’ Expr] DefDef(_, name, paramss, tpe, expr)
437429
| ‘this’ TypelessClauses [DefImplicitClause] ‘=’ ConstrExpr DefDef(_, <init>, vparamss, EmptyTree, expr | Block)
430+
DefSig ::= id [DefParamClauses] [DefImplicitClause]
431+
TypeDef ::= id [TypeParamClause] {FunParamClause} TypeBounds TypeDefTree(_, name, tparams, bound
432+
[‘=’ Type]
438433
439434
TmplDef ::= ([‘case’] ‘class’ | ‘trait’) ClassDef
440435
| [‘case’] ‘object’ ObjectDef

Diff for: docs/_spec/01-lexical-syntax.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ Multiple newline tokens are accepted in the following places (note that a semico
218218

219219
- between the condition of a [conditional expression](06-expressions.html#conditional-expressions) or [while loop](06-expressions.html#while-loop-expressions) and the next following expression,
220220
- between the enumerators of a [for-comprehension](06-expressions.html#for-comprehensions-and-for-loops) and the next following expression, and
221-
- after the initial `type` keyword in a [type definition or declaration](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases).
221+
- after the initial `type` keyword in a [type definition](04-basic-definitions.html#type-member-definitions).
222222

223223
A single new line token is accepted
224224

225225
- in front of an opening brace ‘{’, if that brace is a legal continuation of the current statement or expression,
226226
- after an [infix operator](06-expressions.html#prefix,-infix,-and-postfix-operations), if the first token on the next line can start an expression,
227-
- in front of a [parameter clause](04-basic-declarations-and-definitions.html#function-declarations-and-definitions), and
227+
- in front of a [parameter clause](04-basic-definitions.html#method-definitions), and
228228
- after an [annotation](11-annotations.html#user-defined-annotations).
229229

230230
> The newline tokens between the two lines are not treated as statement separators.

Diff for: docs/_spec/02-identifiers-names-and-scopes.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ chapter: 2
88

99
Names in Scala identify types, values, methods, and classes which are collectively called _entities_.
1010
Names are introduced by local
11-
[definitions and declarations](04-basic-declarations-and-definitions.html#basic-declarations-and-definitions),
11+
[definitions](04-basic-definitions.html#basic-definitions),
1212
[inheritance](05-classes-and-objects.html#class-members),
13-
[import clauses](04-basic-declarations-and-definitions.html#import-clauses), or
13+
[import clauses](04-basic-definitions.html#import-clauses), or
1414
[package clauses](09-top-level-definitions.html#packagings)
1515
which are collectively called _bindings_.
1616

1717
Bindings of different kinds have precedence defined on them:
1818

19-
1. Definitions and declarations that are local, inherited, or made available by a package clause and also defined in the same compilation unit as the reference to them, have the highest precedence.
19+
1. Definitions that are local, inherited, or made available by a package clause and also defined in the same compilation unit as the reference to them, have the highest precedence.
2020
1. Explicit imports have the next highest precedence.
2121
1. Wildcard imports have the next highest precedence.
2222
1. Definitions made available by a package clause, but not also defined in the same compilation unit as the reference to them, as well as imports which are supplied by the compiler but not explicitly written in source code, have the lowest precedence.
@@ -48,7 +48,7 @@ A reference to an unqualified (type- or term-) identifier ´x´ is bound by the
4848

4949
It is an error if no such binding exists.
5050
If ´x´ is bound by an import clause, then the simple name ´x´ is taken to be equivalent to the qualified name to which ´x´ is mapped by the import clause.
51-
If ´x´ is bound by a definition or declaration, then ´x´ refers to the entity introduced by that binding.
51+
If ´x´ is bound by a definition, then ´x´ refers to the entity introduced by that binding.
5252
In that case, the type of ´x´ is the type of the referenced entity.
5353

5454
A reference to a qualified (type- or term-) identifier ´e.x´ refers to the member of the type ´T´ of ´e´ which has the name ´x´ in the same namespace as the identifier.

Diff for: docs/_spec/03-types.md

+21-20
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SimpleRef ::= id
3535
ParamType ::= [‘=>’] ParamValueType
3636
ParamValueType ::= ParamValueType [‘*’]
3737
TypeArgs ::= ‘[’ TypesOrWildcards ‘]’
38-
Refinement ::= :<<< [RefineDcl] {semi [RefineDcl]} >>>
38+
Refinement ::= :<<< [RefineDef] {semi [RefineDef]} >>>
3939
4040
FunTypeArgs ::= InfixType
4141
| ‘(’ [ FunArgTypes ] ‘)’
@@ -51,9 +51,9 @@ TypeLambdaParam ::= {Annotation} (id | ‘_’) [TypeParamClause] TypeBou
5151
TypeParamClause ::= ‘[’ VariantTypeParam {‘,’ VariantTypeParam} ‘]’
5252
VariantTypeParam ::= {Annotation} [‘+’ | ‘-’] (id | ‘_’) [TypeParamClause] TypeBounds
5353
54-
RefineDcl ::= ‘val’ ValDcl
55-
| ‘def’ DefDcl
56-
| ‘type’ {nl} TypeDcl
54+
RefineDef ::= ‘val’ ValDef
55+
| ‘def’ DefDef
56+
| ‘type’ {nl} TypeDef
5757
5858
TypeBounds ::= [‘>:’ Type] [‘<:’ Type]
5959
@@ -193,11 +193,11 @@ TypedFunParam ::= id ‘:’ Type
193193

194194
The concrete function type ´(T_1, ..., T_n) \Rightarrow R´ represents the set of function values that take arguments of types ´T_1, ..., Tn´ and yield results of type ´R´.
195195
The case of exactly one argument type ´T \Rightarrow R´ is a shorthand for ´(T) \Rightarrow R´.
196-
An argument type of the form ´\Rightarrow T´ represents a [call-by-name parameter](04-basic-declarations-and-definitions.html#by-name-parameters) of type ´T´.
196+
An argument type of the form ´\Rightarrow T´ represents a [call-by-name parameter](04-basic-definitions.html#by-name-parameters) of type ´T´.
197197

198198
Function types associate to the right, e.g. ´S \Rightarrow T \Rightarrow R´ is the same as ´S \Rightarrow (T \Rightarrow R)´.
199199

200-
Function types are [covariant](04-basic-declarations-and-definitions.md#variance-annotations) in their result type and [contravariant](04-basic-declarations-and-definitions.md#variance-annotations) in their argument types.
200+
Function types are [covariant](04-basic-definitions.md#variance-annotations) in their result type and [contravariant](04-basic-definitions.md#variance-annotations) in their argument types.
201201

202202
Function types translate into internal class types that define an `apply` method.
203203
Specifically, the ´n´-ary function type ´(T_1, ..., T_n) \Rightarrow R´ translates to the internal class type `scala.Function´_n´[´T_1´, ..., ´T_n´, ´R´]`.
@@ -253,23 +253,24 @@ Notes:
253253
RefinedType ::= AnnotType {[nl] Refinement}
254254
SimpleType1 ::= ...
255255
| Refinement
256-
Refinement ::= :<<< [RefineDcl] {semi [RefineDcl]} >>>
256+
Refinement ::= :<<< [RefineDef] {semi [RefineDef]} >>>
257257
258-
RefineDcl ::= ‘val’ ValDcl
259-
| ‘def’ DefDcl
260-
| ‘type’ {nl} TypeDcl
258+
RefineDef ::= ‘val’ ValDef
259+
| ‘def’ DefDef
260+
| ‘type’ {nl} TypeDef
261261
```
262262

263-
In the concrete syntax of types, refinements can contain several refined declarations.
264-
Moreover, the refined declarations can refer to each other as well as to members of the parent type, i.e., they have access to `this`.
263+
In the concrete syntax of types, refinements can contain several refined definitions.
264+
They must all be abstract.
265+
Moreover, the refined definitions can refer to each other as well as to members of the parent type, i.e., they have access to `this`.
265266

266-
In the internal types, each refinement defines exactly one refined declaration, and references to `this` must be made explicit in a recursive type.
267+
In the internal types, each refinement defines exactly one refined definition, and references to `this` must be made explicit in a recursive type.
267268

268269
The conversion from the concrete syntax to the abstract syntax works as follows:
269270

270271
1. Create a fresh recursive this name ´\alpha´.
271-
2. Replace every implicit or explicit reference to `this` in the refinement declarations by ´\alpha´.
272-
3. Create nested [refined types](#refined-types), one for every refined declaration.
272+
2. Replace every implicit or explicit reference to `this` in the refinement definitions by ´\alpha´.
273+
3. Create nested [refined types](#refined-types), one for every refined definition.
273274
4. Unless ´\alpha´ was never actually used, wrap the result in a [recursive type](#recursive-types) `{ ´\alpha´ => ´...´ }`.
274275

275276
### Concrete Type Lambdas
@@ -361,7 +362,7 @@ To each type constructor corresponds an _inferred type parameter clause_ which i
361362

362363
### Type Definitions
363364

364-
A _type definition_ ´D´ represents the right-hand-side of a `type` declaration or the bounds of a type parameter.
365+
A _type definition_ ´D´ represents the right-hand-side of a `type` member definition or the bounds of a type parameter.
365366
It is either:
366367

367368
- a type alias of the form ´= U´, or
@@ -465,7 +466,7 @@ If the class is monomorphic, the type designator is a value type denoting the se
465466
Otherwise it is a type constructor with the same type parameters as the class definition.
466467
All class types are concrete, non-stable types.
467468

468-
If a type designator ´p.T´ is not a class type, it refers to a type definition `T` (a type parameter or a `type` declaration) and has an _underlying [type definition](#type-definitions)_.
469+
If a type designator ´p.T´ is not a class type, it refers to a type definition `T` (a type parameter or a `type` member definition) and has an _underlying [type definition](#type-definitions)_.
469470
If ´p = \epsilon´ or ´p´ is a package ref, the underlying type definition is the _declared type definition_ of `T`.
470471
Otherwise, it is determined by [`memberType`](#member-type)`(´p´, ´T´)`.
471472
A non-class type designator is concrete (resp. stable) if and only if its underlying type definition is an alias ´U´ and ´U´ is itself concrete (resp. stable).
@@ -860,7 +861,7 @@ If a method name is used as a value, its type is [implicitly converted](06-expre
860861

861862
###### Example
862863

863-
The declarations
864+
The definitions
864865

865866
```scala
866867
def a: Int
@@ -889,7 +890,7 @@ This type represents named methods that take type arguments `´S_1, ..., S_n´`
889890

890891
###### Example
891892

892-
The declarations
893+
The definitions
893894

894895
```scala
895896
def empty[A]: List[A]
@@ -1030,7 +1031,7 @@ We define `memberType(´T´, ´id´, ´p´)` as follows:
10301031
- If ´T´ is a possibly parameterized class type of the form ´q.C[T_1, ..., T_n]´ (with ´n \geq 0´):
10311032
- Let ´m´ be the [class member](05-classes-and-objects.html#class-members) of ´C´ with name ´id´.
10321033
- If ´m´ is not defined, the result is undefined.
1033-
- If ´m´ is a class declaration, the result is a class result with class ´m´.
1034+
- If ´m´ is a class definition, the result is a class result with class ´m´.
10341035
- If ´m´ is a term definition in class ´D´ with declared type ´U´, the result is a term result with underlying type [`asSeenFrom`](#as-seen-from)`(´U´, ´D´, ´p´)` and stable flag true if and only if ´m´ is stable.
10351036
- If ´m´ is a type member definition in class ´D´, the result is a type result with underlying type definition [`asSeenFrom`](#as-seen-from)`(´U´, ´D´, ´p´)` where ´U´ is defined as follows:
10361037
- If ´m´ is an opaque type alias member definition with declared definition ´>: L <: H = V´, then

0 commit comments

Comments
 (0)