Skip to content

Commit 6bbbd4e

Browse files
authored
More consistent package syntax (#3927)
Change the syntax for `package` declarations from: ```carbon [package Foo] [library "bar"] api; [package Foo] [library "bar"] impl; ``` to ```carbon [package Foo] [library "bar"]; impl [package Foo] [library "bar"]; ```
1 parent 73f8490 commit 6bbbd4e

File tree

11 files changed

+341
-163
lines changed

11 files changed

+341
-163
lines changed

docs/design/README.md

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ The `import` declaration imports a
194194
[library from a package](#files-libraries-packages). It must appear at the top
195195
of a Carbon source file, the first thing after the
196196
[optional `package` declaration](#package-declaration). Libraries can optionally
197-
be split into [api and implementation files](#files-libraries-packages), like
197+
be split into [API and implementation files](#files-libraries-packages), like
198198
C++'s header and source files but without requiring a source file in any cases.
199199
This declaration from the example:
200200

@@ -963,13 +963,14 @@ forward declaration that introduces the name before it is defined, plus any
963963
number of declarations in a
964964
[`match_first` block](generics/details.md#prioritization-rule). Forward
965965
declarations can be used to separate interface from implementation, such as to
966-
declare a name in an [api file](#files-libraries-packages) that is defined in an
967-
[impl file](#files-libraries-packages). Forward declarations also allow entities
968-
to be used before they are defined, such as to allow cyclic references. A name
969-
that has been declared but not defined is called _incomplete_, and in some cases
970-
there are limitations on what can be done with an incomplete name. Within a
971-
definition, the defined name is incomplete until the end of the definition is
972-
reached, but is complete in the bodies of member functions because they are
966+
declare a name in an [API file](#files-libraries-packages) that is defined in an
967+
[implementation file](#files-libraries-packages). Forward declarations also
968+
allow entities to be used before they are defined, such as to allow cyclic
969+
references. A name that has been declared but not defined is called
970+
_incomplete_, and in some cases there are limitations on what can be done with
971+
an incomplete name. Within a definition, the defined name is incomplete until
972+
the end of the definition is reached, but is complete in the bodies of member
973+
functions because they are
973974
[parsed as if they appeared after the definition](#class-functions-and-factory-functions).
974975

975976
A name is valid until the end of the innermost enclosing
@@ -2190,13 +2191,15 @@ selected yet.
21902191
### Files, libraries, packages
21912192

21922193
- **Files** are grouped into libraries, which are in turn grouped into
2193-
packages.
2194+
packages. There are two kinds of files:
2195+
- API files describe the interface of a library.
2196+
- Implementation files implement part of the interface of a library.
21942197
- **Libraries** are the granularity of code reuse through imports.
21952198
- **Packages** are the unit of distribution.
21962199

2197-
Each library must have exactly one `api` file. This file includes declarations
2198-
for all public names of the library. Definitions for those declarations must be
2199-
in some file in the library, either the `api` file or an `impl` file.
2200+
Each library must have exactly one API file. This file includes declarations for
2201+
all public names of the library. Definitions for those declarations must be in
2202+
some file in the library, either the API file or an implementation file.
22002203

22012204
Every package has its own namespace. This means libraries within a package need
22022205
to coordinate to avoid name conflicts, but not across packages.
@@ -2211,37 +2214,38 @@ to coordinate to avoid name conflicts, but not across packages.
22112214

22122215
Files start with an optional package declaration, consisting of:
22132216

2217+
- optionally, the `impl` modifier keyword, indicating the file is an
2218+
implementation file rather than an API file,
22142219
- optionally, the `package` keyword followed by an identifier specifying the
22152220
package name,
22162221
- optionally, the `library` keyword followed by a string with the library
2217-
name,
2218-
- either `api` or `impl`, and
2222+
name, and
22192223
- a terminating semicolon (`;`).
22202224

22212225
For example:
22222226

22232227
```carbon
22242228
// Package name is `Geometry`.
22252229
// Library name is "Shapes".
2226-
// This file is an `api` file, not an `impl` file.
2227-
package Geometry library "Shapes" api;
2230+
// This file is an API file, not an implementation file.
2231+
package Geometry library "Shapes";
22282232
```
22292233

22302234
Parts of this declaration may be omitted:
22312235

2232-
- If the package keyword is not specified, as in `library "Widgets" api;`, the
2236+
- If the package keyword is not specified, as in `library "Widgets";`, the
22332237
file contributes to the `Main` package. No other package may import from the
22342238
`Main` package, and it cannot be named explicitly.
22352239

2236-
- If the library keyword is not specified, as in `package Geometry api;`, this
2240+
- If the library keyword is not specified, as in `package Geometry;`, this
22372241
file contributes to the default library.
22382242

22392243
- If both keywords are omitted, the package declaration must be omitted
2240-
entirely. In this case, the file is an `impl` file belonging to the default
2241-
library of the `Main` package, which implicitly has an empty `api` file.
2242-
This library is used to define the entry point for the program, and tests
2243-
and smaller examples may choose to reside entirely within this library. No
2244-
other library can import this library even from within the default package.
2244+
entirely. In this case, the file is the API file for the default library of
2245+
the `Main` package, which cannot have any implementation files. This library
2246+
is used to define the entry point for the program, and tests and smaller
2247+
examples may choose to reside entirely within this library. No other library
2248+
can import this library even from within the default package.
22452249

22462250
If the default library of the `Main` package contains a function named `Run`,
22472251
that function is the program entry point. Otherwise, the program's entry point
@@ -2282,11 +2286,11 @@ given library to the top-level scope of the current file as
22822286
[`private`](#name-visibility) names, and similarly for names in
22832287
[namespaces](#namespaces).
22842288

2285-
Every `impl` file automatically imports the `api` file for its library.
2289+
Every implementation file automatically imports the API file for its library.
22862290
Attempting to perform an import of the current library is invalid.
22872291

22882292
```
2289-
package MyPackage library "Widgets" impl;
2293+
impl package MyPackage library "Widgets";
22902294
22912295
// ❌ Error, this import is performed implicitly.
22922296
import MyPackage library "Widgets";
@@ -2309,7 +2313,7 @@ When the package name is specified, the `import` declaration imports a library
23092313
from another package.
23102314

23112315
```carbon
2312-
package MyPackage impl;
2316+
impl package MyPackage;
23132317
23142318
// Import the "Vector" library from the `LinearAlgebra` package.
23152319
import LinearAlgebra library "Vector";
@@ -2344,17 +2348,17 @@ package can only be imported from within that package.
23442348

23452349
The names visible from an imported library are determined by these rules:
23462350

2347-
- Declarations in an `api` file are by default _public_, which means visible
2348-
to any file that imports that library. This matches class members, which are
2351+
- Declarations in an API file are by default _public_, which means visible to
2352+
any file that imports that library. This matches class members, which are
23492353
also [default public](#access-control).
2350-
- A `private` prefix on a declaration in an `api` file makes the name _library
2351-
private_. This means the name is visible in the file and all `impl` files
2352-
for the same library.
2354+
- A `private` prefix on a declaration in an API file makes the name _library
2355+
private_. This means the name is visible in the file and all implementation
2356+
files for the same library.
23532357
- The visibility of a name is determined by its first declaration, considering
2354-
`api` files before `impl` files. The `private` prefix is only allowed on the
2355-
first declaration.
2356-
- A name declared in an `impl` file and not the corresponding `api` file is
2357-
_file private_, meaning visible in just that file. Its first declaration
2358+
API files before implementation files. The `private` prefix is only allowed
2359+
on the first declaration.
2360+
- A name declared in an implementation file and not the corresponding API file
2361+
is _file private_, meaning visible in just that file. Its first declaration
23582362
must be marked with a `private` prefix. **TODO:** This needs to be finalized
23592363
in a proposal to resolve inconsistency between
23602364
[#665](https://github.com/carbon-language/carbon-lang/issues/665#issuecomment-914661914)
@@ -2363,7 +2367,7 @@ The names visible from an imported library are determined by these rules:
23632367
to: two different libraries can have different private names `foo` without
23642368
conflict, but a private name conflicts with a public name in the same scope.
23652369

2366-
At most one `api` file in a package transitively used in a program may declare a
2370+
At most one API file in a package transitively used in a program may declare a
23672371
given name public.
23682372

23692373
> References:
@@ -2383,9 +2387,9 @@ given name public.
23832387
The top-level scope in a file is the scope of the package. This means:
23842388

23852389
- Within this scope (and its sub-namespaces), all visible names from the same
2386-
package appear. This includes names from the same file, names from the `api`
2387-
file of a library when inside an `impl` file, and names from imported
2388-
libraries of the same package.
2390+
package appear. This includes names from the same file, names from the API
2391+
file of a library when inside an implementation file, and names from
2392+
imported libraries of the same package.
23892393
- In scopes where package members might have a name conflict with something
23902394
else, the syntax `package.Foo` can be used to name the `Foo` member of the
23912395
current package.
@@ -2441,7 +2445,7 @@ namespace are considered in scope and may be found by
24412445
package `P` defines some of its members inside a namespace `N`:
24422446

24432447
```carbon
2444-
package P api;
2448+
package P;
24452449
24462450
// Defines namespace `N` within the current package.
24472451
namespace N;
@@ -2642,8 +2646,8 @@ fn F();
26422646

26432647
Common types that we expect to be used universally will be provided for every
26442648
file are made available as if there was a special "prelude" package that was
2645-
imported automatically into every `api` file. Dedicated type literal syntaxes
2646-
like `i32` and `bool` refer to types defined within this package, based on the
2649+
imported automatically into every API file. Dedicated type literal syntaxes like
2650+
`i32` and `bool` refer to types defined within this package, based on the
26472651
["all APIs are library APIs" principle](/docs/project/principles/library_apis_only.md).
26482652

26492653
> **TODO:** Prelude provisionally imports the `Carbon` package which includes

docs/design/classes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2137,7 +2137,7 @@ implications:
21372137
However, there are likely to be differences between computed properties and
21382138
other data members, such as the ability to take the address of them. We might
21392139
want to support "read only" data members, that can be read through the public
2140-
api but only modified with private access, for data members which may need to
2140+
API but only modified with private access, for data members which may need to
21412141
evolve into a computed property. There are also questions regarding how to
21422142
support assigning or modifying computed properties, such as using `+=`.
21432143

0 commit comments

Comments
 (0)