@@ -194,7 +194,7 @@ The `import` declaration imports a
194
194
[ library from a package] ( #files-libraries-packages ) . It must appear at the top
195
195
of a Carbon source file, the first thing after the
196
196
[ 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
198
198
C++'s header and source files but without requiring a source file in any cases.
199
199
This declaration from the example:
200
200
@@ -963,13 +963,14 @@ forward declaration that introduces the name before it is defined, plus any
963
963
number of declarations in a
964
964
[ ` match_first ` block] ( generics/details.md#prioritization-rule ) . Forward
965
965
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
973
974
[ parsed as if they appeared after the definition] ( #class-functions-and-factory-functions ) .
974
975
975
976
A name is valid until the end of the innermost enclosing
@@ -2190,13 +2191,15 @@ selected yet.
2190
2191
### Files, libraries, packages
2191
2192
2192
2193
- ** 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.
2194
2197
- ** Libraries** are the granularity of code reuse through imports.
2195
2198
- ** Packages** are the unit of distribution.
2196
2199
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.
2200
2203
2201
2204
Every package has its own namespace. This means libraries within a package need
2202
2205
to coordinate to avoid name conflicts, but not across packages.
@@ -2211,37 +2214,38 @@ to coordinate to avoid name conflicts, but not across packages.
2211
2214
2212
2215
Files start with an optional package declaration, consisting of:
2213
2216
2217
+ - optionally, the ` impl ` modifier keyword, indicating the file is an
2218
+ implementation file rather than an API file,
2214
2219
- optionally, the ` package ` keyword followed by an identifier specifying the
2215
2220
package name,
2216
2221
- optionally, the ` library ` keyword followed by a string with the library
2217
- name,
2218
- - either ` api ` or ` impl ` , and
2222
+ name, and
2219
2223
- a terminating semicolon (` ; ` ).
2220
2224
2221
2225
For example:
2222
2226
2223
2227
``` carbon
2224
2228
// Package name is `Geometry`.
2225
2229
// 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";
2228
2232
```
2229
2233
2230
2234
Parts of this declaration may be omitted:
2231
2235
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
2233
2237
file contributes to the ` Main ` package. No other package may import from the
2234
2238
` Main ` package, and it cannot be named explicitly.
2235
2239
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
2237
2241
file contributes to the default library.
2238
2242
2239
2243
- 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.
2245
2249
2246
2250
If the default library of the ` Main ` package contains a function named ` Run ` ,
2247
2251
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
2282
2286
[ ` private ` ] ( #name-visibility ) names, and similarly for names in
2283
2287
[ namespaces] ( #namespaces ) .
2284
2288
2285
- Every ` impl ` file automatically imports the ` api ` file for its library.
2289
+ Every implementation file automatically imports the API file for its library.
2286
2290
Attempting to perform an import of the current library is invalid.
2287
2291
2288
2292
```
2289
- package MyPackage library "Widgets" impl ;
2293
+ impl package MyPackage library "Widgets";
2290
2294
2291
2295
// ❌ Error, this import is performed implicitly.
2292
2296
import MyPackage library "Widgets";
@@ -2309,7 +2313,7 @@ When the package name is specified, the `import` declaration imports a library
2309
2313
from another package.
2310
2314
2311
2315
``` carbon
2312
- package MyPackage impl ;
2316
+ impl package MyPackage;
2313
2317
2314
2318
// Import the "Vector" library from the `LinearAlgebra` package.
2315
2319
import LinearAlgebra library "Vector";
@@ -2344,17 +2348,17 @@ package can only be imported from within that package.
2344
2348
2345
2349
The names visible from an imported library are determined by these rules:
2346
2350
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
2349
2353
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.
2353
2357
- 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
2358
2362
must be marked with a ` private ` prefix. ** TODO:** This needs to be finalized
2359
2363
in a proposal to resolve inconsistency between
2360
2364
[ #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:
2363
2367
to: two different libraries can have different private names ` foo ` without
2364
2368
conflict, but a private name conflicts with a public name in the same scope.
2365
2369
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
2367
2371
given name public.
2368
2372
2369
2373
> References:
@@ -2383,9 +2387,9 @@ given name public.
2383
2387
The top-level scope in a file is the scope of the package. This means:
2384
2388
2385
2389
- 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.
2389
2393
- In scopes where package members might have a name conflict with something
2390
2394
else, the syntax ` package.Foo ` can be used to name the ` Foo ` member of the
2391
2395
current package.
@@ -2441,7 +2445,7 @@ namespace are considered in scope and may be found by
2441
2445
package ` P ` defines some of its members inside a namespace ` N ` :
2442
2446
2443
2447
``` carbon
2444
- package P api ;
2448
+ package P;
2445
2449
2446
2450
// Defines namespace `N` within the current package.
2447
2451
namespace N;
@@ -2642,8 +2646,8 @@ fn F();
2642
2646
2643
2647
Common types that we expect to be used universally will be provided for every
2644
2648
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
2647
2651
[ "all APIs are library APIs" principle] ( /docs/project/principles/library_apis_only.md ) .
2648
2652
2649
2653
> ** TODO:** Prelude provisionally imports the ` Carbon ` package which includes
0 commit comments