Skip to content
PaskalCPP
Language

Modules

Every source file is one module. Its first line names the kind of module and its name; the kind decides what the build makes.

Shape of a module

A file is module <kind> <name>;, then build directives, at most one import clause, the declarations, an optional initialize and finalize section, an optional begin body, and end. Test blocks may follow end.; anything else after it is PR0020.

  • Source files use the .pcpp extension.
  • The module name equals the file name without .pcpp, in exact case; a mismatch is PR0003.
  • A missing kind after module is PR0002.

The smallest program is an exe whose body prints one line:

hello.pcpp
module exe hello;

begin
  println("Hello from PaskalCPP!");
end.

Kinds

KindWhat the build makes
exean executable
dlla shared library: a .dll on Windows, a .so on Linux
liba static library
unitnothing of its own: source compiled into the build of every module that imports it

Only an exe has a begin body, and an exe must have one: an exe without one is PR0017, a body in a dll, lib or unit is PR0016. A unit given to the compiler as the main module is PR0004.

Imports

import A, B; names PaskalCPP units only, and stands once, right after the directive prologue; an import after a declaration is PR0010.

C++ side

Importing an exe, dll or lib is PR0013. C and C++ libraries are used through #include, never import: see C++ mixing.

import Foo looks for Foo.pcpp in the importing file's folder, then in the module paths in the order they were added: the standard library folder ($P:res/libs/std), then the main module's @addmodulepath folders. A unit that is not found is PR0012 at its name. The lookup takes the file's exact name, on Windows too: import Mod_kinds_unit does not find mod_kinds_unit.pcpp.

  • A unit named twice in one clause is PR0014 at the second name; two units that import each other are PR0011.
  • A unit imported along several paths is one unit. Two files that declare the same module are AS0001.
  • Two modules whose names differ only in case (std's Math and a unit math) cannot be in one program, since their generated files would be one file on a case-blind file system: the second is AS0002 at its own header, and the first is kept.
  • An imported unit's names are reached qualified, modules_shapes.Area, modules_shapes.TRect, which always works, or bare, Area, when exactly one unit of the module's own import clause exports the name. That holds in every bare position: a value, a type, a call, a for iterator, an external clause's library constant.
  • A qualified type has exactly two parts, Module.Type: mod_qual_unit.TPt.X used as a type is SM0008.
  • A bare name is looked up in this order: the module's own scopes (innermost first), the intrinsics, an own name declared later (SM0002), the imports, a bare choice value (SM0015), then C/C++. So the module's own names win over an import, and a PaskalCPP name wins over a C/C++ one of the same spelling (write cpp("...") for the C/C++ one).
  • A bare name that two or more imported units export is SM0020 at the use, with a note at each unit's declaration: qualify it (a.Name). Import order never decides, and two units' overload sets never merge: the same routine name in two units is ambiguous whatever the parameter types.
  • Only what a unit marks exported (an exported routine, an exported type section, ...) is visible to its importers. A declared but unexported name is SM0006, an undeclared one SM0005.
  • Imports are not transitive: a unit's own import is not its importer's (SM0007).

Here is a unit and the exe that imports it:

modules_shapes.pcpp
module unit modules_shapes;

exported type
  TRect = record
    Width: int32;
    Height: int32;
  end;

// Not exported: only this unit can call it
routine Multiply(const AX: int32; const AY: int32): int32;
begin
  return AX * AY;
end;

exported routine Area(const ARect: TRect): int32;
begin
  return Multiply(ARect.Width, ARect.Height);
end;

initialize
begin
  println("modules_shapes initialize");
end;

finalize
begin
  println("modules_shapes finalize");
end;

end.
output on x86_64-windows-gnu and x86_64-linux-gnu
modules_shapes initialize
modules_main initialize
area = 12
modules_main finalize
modules_shapes finalize
[Heap] Allocs: 0, Frees: 0, Leaked: 0

The standard library folder

The standard library folder, res\libs\std, holds PaskalCPP's own units SysUtils, StrUtils, Math, Console, ResData, CustomIO, HookIO, HookABI, Diagnostics, raylib_ext, rlimgui_ext, raven_raylib, sfml_ext, mojoal_ext, sqlite_ext, tmxlite_ext, ldtk_ext, mir_ext and llama_ext beside vendor units such as zlib, minizip, raylib, sdl3 and sfml. import SysUtils; finds one with no @addmodulepath. What each unit gives is on Standard library, the bundled C and C++ libraries on Vendor libraries.

  • ResolvePath(APath) and ResolvePath(APath, ABaseDir) (SysUtils) turn any spelling of a path into one full, normalized path, with the prefixes the directives take: $P: the executable's folder, $D: the current folder, $S: or none the given base, else the current folder ($R:, the compiler's build-time project root, gives "": a running program has no project). The prefix is read at the start only, in either case; blanks are trimmed and . / .. folded as text. Each takes a string or a ptr to const char (nil = "") and returns a string.
  • On Windows the result has forward slashes up to 247 UTF-16 units and the long-path spelling past it (\\?\C:\..., \\?\UNC\srv\share\...); on Linux it is the plain /... path at any length. Resolving a result again gives it back.
  • ExeDir() (SysUtils) is the running program's folder, with no trailing separator.
  • FindResData(AKey) (ResData) gives the data an @addresdata of the build added under that key (see Built-in data) as a ResDataEntry: Data (a ptr to const uint8) points at the bytes and Size (int64) counts them. One zero byte follows them, not counted, so a text file reads as a C string; an empty file gives a non-nil Data with Size 0; a key the binary does not hold gives Data nil and Size 0.
  • HasResData(AKey) tells whether the key is there. Both take a string or a ptr to const char (nil names no key), compare keys exactly, case included, and take keys built at run time. ResDataCount() is the number of keys and ResDataKey(I) the key at place I in byte-wise UTF-8 key order, the same on both targets, '' outside 0 .. ResDataCount() - 1. A binary file comes back byte for byte.
  • Each exe and dll reads its own binary's data: the entries of every module it was built from and of every static PaskalCPP lib it links. A dll it links keeps its own, which the program does not see, nor the dll the program's.
  • A string literal key passed to FindResData or HasResData that no @addresdata of the build and no linked static library adds is the warning SM0122: the lookup finds nothing. A key built at run time is not judged.
  • To judge such a key the compiler reads the static libraries the build links for their keys. A library file that cannot be opened is the warning CMP0007, once, where the source names the library (its @linklibrary or external clause), and the build goes on.
  • zlib, minizip and raylib are vendor units: each sets the include path, library path and link libraries its C library needs on each official target and #includes its headers, so a program writes only import zlib; and calls the C API (zlibVersion(), compress2, zipOpen64, InitWindow). The libraries are static and linked into the program: bin\ holds only the program, and its .pdb at debug on Windows.
  • The bundled examples in res\examples use them: zlib\Compress.pcpp compresses and uncompresses a text and checks its crc32, minizip\PasswordZip.pcpp writes a password-encrypted .zip beside the program and reads it back, and raylib\BasicWindow.pcpp opens a window with a line of text.

Initialize and finalize

Every module kind may have an initialize and a finalize section, each at most once, initialize first, after the declarations; a repeat or a wrong order is PR0018.

A section is a routine with no parameters and no result: local var, const and type blocks, then begin ... end;. return; leaves it early; return with a value is SM0611.

Every imported unit's initialize runs before its importer's, in dependency order (not the order of the import clause), then the importer's, then the exe body. finalize runs in the reverse order. The output above shows the order.

A static lib's initialize runs before the program's body and its finalize after it, before the debug heap report, on both official targets. A dll's initialize runs when it is loaded and its finalize when it is unloaded, after the program's heap report; the dll prints its own.