Commit graph

198 commits

Author SHA1 Message Date
Graeme Geldenhuys a1a2f1fa71 test(parser): add chained postfix access tests
Four tests covering Obj.Method().Field, Arr[i].Field,
A.GetB().GetC().Val, and GetList()[0] patterns.
2026-05-08 08:56:30 +01:00
Graeme Geldenhuys 3476f6c9a4 feat(parser): add postfix chaining loop for Expr.Field, Expr[i], Expr^
Adds a unified postfix chaining loop at the end of ParseFactor that
handles .Field, .Method(...), [subscript], and ^ dereference after any
expression. Previously Ident.Method(...) and Expr[i] exited without
checking for further postfix access, so patterns like Obj.GetRec().X,
Tokens[0].Kind, and A.GetB().GetC().Val failed to parse.
2026-05-08 08:56:20 +01:00
Graeme Geldenhuys d8c0ef4ef6 fix: we don't have any code for migration-analyser yet. Disable tests for it. 2026-05-08 00:52:44 +01:00
Graeme Geldenhuys eacaf1e192 fix(compiler): restore poUsePipes for FPC TProcess in RunProcess
The poUsePipes option was removed during self-hosting adaptation since
the Blaise RTL TProcess always uses pipes. FPC's TProcess requires
the option explicitly, causing an access violation on Output.Read when
compiling with --output.
2026-05-08 00:33:50 +01:00
Graeme Geldenhuys da857ecc8e refactor(e2e): remove TStringStream from RunProc, replace sLineBreak with #10
Simplify RunProc() output capture: accumulate pipe bytes directly into a
string via SetString instead of routing through TStringStream.  Replace
sLineBreak with #10 in error messages to reduce SysUtils dependencies
ahead of the Blaise test-suite migration.
2026-05-07 23:55:13 +01:00
Graeme Geldenhuys 3e4d591c1d chore(doc): PasBuild rename --fpc to --compiler [already done] 2026-05-07 23:25:19 +01:00
Graeme Geldenhuys 06882bf432 feat(compiler,rtl): add OS utility builtins for test suite migration
Add GetProcessID, DirectoryExists, GetTempDir, ForceDirectories, and
Sleep as compiler builtins backed by C RTL functions. These are needed
by the e2e test infrastructure (cp.test.e2e.pas) before switching the
test suite from FPC to Blaise compilation (v0.6.0 Step 2a).
2026-05-07 23:19:52 +01:00
Graeme Geldenhuys 9300fe8a46 chore(doc): new future improvement - If Conditional Operator (ternary operator) 2026-05-07 23:19:03 +01:00
Graeme Geldenhuys db7660a95e chore(docs): Remove Multi-Line string literal from future-improvements
It already landed in the compiler.
2026-05-07 23:18:24 +01:00
Graeme Geldenhuys 11f657caef feat(lexer): implement triple single-quote text blocks
Add '''...''' multi-line string literal syntax to the lexer and
tokeniser.  Opening ''' followed by a newline starts a text block;
closing ''' on its own line sets the indentation baseline for margin
stripping.  Single quotes inside the block require no escaping.

Disambiguation: ''' followed by a newline opens a text block; followed
by any other character falls back to the classic '' escape parse.

11 new tests in cp.test.textblock.pas cover basic blocks, margin
stripping, embedded quotes, empty blocks, relative indentation
preservation, disambiguation from '''', trailing content, tabs,
and blank line preservation.  All 1264 tests pass; fixpoint verified.
2026-05-07 19:52:23 +01:00
Graeme Geldenhuys 8f0a844d07 feat(compiler,rtl): implement TObject.InheritsFrom
Add _InheritsFrom RTL helper in blaise_arc.pas that walks the typeinfo
parent chain to check class identity. Wire IsBuiltinInheritsFrom through
the semantic analyser (tyPointer, tyMetaClass, tyClass receivers) and
codegen (emits call $_InheritsFrom with correct typeinfo loading).

Update punit with AssertInheritsFrom, AssertInheritsFromClass, and class
identity checks in AssertException and RunTestHandler. Update testpunit2
so DoTest21 correctly fails on a class mismatch (EError expected, EFail
raised). Add 5 unit tests and 6 e2e tests; all 1253 tests pass, fixpoint
confirmed.
2026-05-07 09:53:47 +01:00
Graeme Geldenhuys f3f0036381 docs: remove implemented entries from future-improvements.adoc
Removes const sections, Abs(), ClassName/ClassType (all confirmed
implemented). Updates Str() workaround note now that DoubleToStr/
SingleToStr exist. Updates InheritsFrom effort note now that ClassType
vmt slots are in place.
2026-05-07 08:36:28 +01:00
Graeme Geldenhuys 2f28051956 chore: begin v0.6.0-dev cycle 2026-05-07 08:25:10 +01:00
Graeme Geldenhuys d2209afd0e release: v0.5.0
Fixpoint achieved on the full multi-file source (105865 lines of QBE IR).
Ships indexed-property getter codegen and string-field char byte-load, fixing
the self-hosting crash introduced by overloading support in v0.4.0.
2026-05-07 08:24:50 +01:00
Graeme Geldenhuys ded893493a fix(semantic,codegen): emit char byte-load for string field subscripts
When the parser sees Rec.Field[N] and Field is a string, it stores the
[N] subscript in TFieldAccessExpr.PropIndexExpr.  The semantic analyser
was ignoring this index and returning tyString, so Field[N] = '$' was
compiled as a string equality comparison (_StringEquals) instead of a
byte-load comparison (loadub + ceqw 36).

Add IsCharAccess flag to TFieldAccessExpr.  In AnalyseFieldAccess, when
PropIndexExpr is non-nil and the field type is a string, set IsCharAccess
and return tyInteger so that comparison with a char literal triggers the
existing char-coercion path in AnalyseBinaryExpr.

In the codegen, handle IsCharAccess by loading the string field pointer
and emitting loadub at the 0-based byte offset (N-1), matching the
existing EmitStringSubscriptExpr logic for string character access.

This fixes the vtable emission bug where E.ImplName[1] = '$' was being
compiled as _StringEquals(ImplName, "$"), causing inherited TObject vtable
slots to be emitted as _D_TObject_Destroy instead of $TObject_Destroy
when running stage-2 → stage-3.  Fixpoint now produces identical IR.
2026-05-07 08:06:06 +01:00
Graeme Geldenhuys 4061e4e249 fix(semantic): emit indexed-property getter for subscripted class fields
When the parser sees Rec.Field[I] and Field is a direct class field
(not itself a property), the semantic analyser was silently ignoring
the [I] subscript and returning the field value — causing the codegen
to emit a raw field load instead of calling the field type's indexed
property getter.

Add TRecordTypeDesc.FindIndexedProperty to locate the first indexed
read-method property on a class type.  In AnalyseFieldAccess, after
resolving a direct field, if PropIndexExpr is non-nil and the field
type is a class with an indexed property, redirect to that property's
getter and keep FieldInfo set so the codegen knows to load the field
value first before calling the getter.

Update the codegen's PropRead path to handle the combined case: when
both PropRead and FieldInfo are set, load the field from the record
first and use that loaded value as Self for the getter call.

This fixes the self-hosting crash where TES.Handlers[I] in uSemantic.pas
was passing the TObjectList itself (instead of element I) as the TypeName
string to FindTypeOrInstantiate, corrupting the argument with the list's
FCount/FCapacity word.
2026-05-07 07:49:00 +01:00
Graeme Geldenhuys b7aa65e9fb fix(codegen): pop exception frames on early control-flow exits
`exit`, `break`, and `continue` inside a `try…finally` or
`try…except` body jumped directly to the target label without
popping the exception frames they were nested inside. That left
`g_exc_top` pointing to dead stack memory, and the next push of an
unrelated frame at the same address would set `prev` to itself,
turning the chain into a self-loop. The next pop then read garbage
back into `g_exc_top`, eventually segfaulting in `_PopExcFrame`.

Track active exc-frame depth in `FExcDepth` and emit `_PopExcFrame()`
for each nesting level we're unwinding past. The loop label stack
now records the depth at loop entry via `AddObject` so `break` and
`continue` pop only the frames added inside the loop, while `exit`
unwinds back to depth 0.

Caught while debugging the self-hosting fixpoint: stage-2
`AnalyseUnitForExport` segfaulted in `_PopExcFrame` after
`ResolveMethodOverload` returned via `if TotalCnt = 0 then Exit;`
(uSemantic.pas:2500) inside its `try…finally`.
2026-05-06 17:29:44 +01:00
Graeme Geldenhuys 4dd71ba16f docs: ditch the TOC in the readme.
It looks weird on Github.
2026-05-06 13:10:56 +01:00
Graeme Geldenhuys 6af11d2a09 doc: reorder the README sections.
More technical details at the end.
2026-05-06 13:09:11 +01:00
Graeme Geldenhuys 36e16752f3 docs(readme): update README for public repo launch
- Rewrite intro with tagline and project summary
- Fix licence: BSD 3-Clause → Apache 2.0 with Runtime Library Exception
- Fix LICENSE filename reference (American spelling matches actual file)
- Update test count to 1200+
- Add status snapshot (self-hosting, test coverage, backend)
- Correct phase 4 and 5 status to Complete / In-Progress
- Add Community section linking to GitHub Discussions
- Add closing attribution line
2026-05-06 12:53:36 +01:00
Graeme Geldenhuys 5b9bc375ea fix(codegen): eliminate dynamic stack allocs — phi for short-circuit bools, hoist exc frames
Two codegen improvements that eliminate all non-@start `alloc`
instructions from the emitted QBE IR.  Both are prerequisite work for
the self-hosting fixpoint (currently being debugged).

(1) Short-circuit boolean operators now use QBE phi instead of alloc4.

Previously `A and B` / `A or B` emitted:
  %slot =l alloc4 1          <- dynamic alloc, grows stack per execution
  storew %L, %slot
  jnz %L, @rhs, @end
  @rhs  storew %R, %slot  jmp @end
  @end  %T =w loadw %slot

The slot was allocated in whichever basic block the expression appeared
in.  QBE emits a runtime `sub $16, %rsp` for every alloc outside @start
— inside a while-condition this ran on every loop iteration, growing the
stack by 16 bytes per iteration indefinitely.

New form uses a phi node at the join block:
  jnz %L, @rhs, @end
  @rhs  (evaluate R)  jmp @end
  @end  %T =w phi @lhs %L, @rhs %R

No alloc needed; %T is always well-defined at @end by SSA.
FCurrentBlockLabel tracks the current basic block name so the phi can
correctly name its predecessors.

(2) Exception frames pre-allocated at @start via EmitExcFrameAllocs.

Previously EmitTryFinallyStmt / EmitTryExceptStmt emitted `alloc16 512`
inline at the point of the try statement.  Inside a loop (e.g. `for I
:= 0 to N-1 do begin try ... finally ... end end`), each iteration
allocated 512 bytes of stack that was never released until the function
returned.  Over hundreds of iterations this drifted the stack pointer
into parent exception frame territory, corrupting the exc-frame chain.

New approach: CountTryStmts recursively counts all try blocks in the
function body, EmitExcFrameAllocs pre-allocates %_exc_frame_0 …
%_exc_frame_N at @start (QBE hoists these to the static prologue sub),
and EmitTryFinallyStmt/Except index into the pool via FExcFrameNext.

Result: zero alloc instructions outside @start blocks in the emitted IR.

All 1242 tests pass.  Stage-1 IR and stage-2 IR are now byte-for-byte
identical (md5 verified) — the fixpoint is correct at the IR level.
The stage-2 BINARY still crashes during stage-3 generation; this is
under active investigation and may be a QBE vs FPC code-generation
quality issue for the same IR.
2026-05-06 11:47:52 +01:00
Graeme Geldenhuys 9613135345 feat(lang): case-on-string selector
Step 11f prerequisite.  The 'case' statement now accepts a string-
typed selector.  Each branch label must also resolve to a string
expression (typically a string literal); the runtime comparison
lowers to a call to _StringEquals against the selector value, so
existing string-equality semantics apply unchanged.

[source,pascal]
----
case Tag of
  'w': StoreInstr := 'storew';
  'd': StoreInstr := 'stored';
  's': StoreInstr := 'stores';
else   StoreInstr := 'storel';
end;
----

This unblocks self-hosted compilation of uCodeGenQBE.pas, which
already uses case-on-QType in four sites.  The internal motivation
matters: forcing those sites into hand-rolled if-elseif chains just
to make the compiler compilable by itself was rejected as a step
backwards from idiomatic Object Pascal.

Implementation:
  * uAST:  TCaseStmt gains an IsStringCase: Boolean flag set by the
            analyser so codegen can pick the right comparison.
  * uSem:  AnalyseCaseStmt now allows tyString selectors in addition
            to ordinal types; per-branch CheckTypesMatch already does
            the right thing for string vs string.
  * uCG:   EmitCaseStmt emits a 'call $_StringEquals(l SelTemp, l
            ValTemp)' result and reuses the existing 'jnz' fall-
            through pattern.  Ordinal cases still use 'ceqw'.

Tests added to cp.test.caseenum.pas:
  TestSemantic_CaseString_AcceptsStringSelector
  TestSemantic_CaseString_RejectsIntLabelOnStringSelector
  TestCodegen_CaseString_EmitsStringEqualsCalls
  TestCodegen_CaseString_OrdinalCaseStillUsesCEQW

Rationale documented in docs/language-rationale.adoc.

All 1242 tests pass (1238 + 4 new).
2026-05-06 08:00:00 +01:00
Graeme Geldenhuys 630082f36f feat(rtl): bcl.testing.runner.text — plain-text xUnit runner (Step 11e)
The fourth and final logical step of Step 11e.  New unit at
compiler/src/main/pascal/bcl.testing.runner.text.pas drives the
bcl.testing framework end-to-end:

  * PublishedMethodCount / PublishedMethodName walk the class's
    typeinfo[3] published-method table inline, climbing the parent
    chain so inherited test methods are visible.  Same loop shape as
    blaise_arc's _MethodAddress.

  * RunRegisteredTests iterates the GRegistry list of TTestCaseClass
    values that bcl.testing.RegisterTest collected, ClassCreate's one
    instance per published method (with the method name as the
    constructor arg), and dispatches via the standard TTestCase.Run
    path landed in 11d.

  * PrintSummary writes the OK / FAIL header line, plus per-failure
    and per-error detail.

  * RunAll wraps the two: program ends with 'Halt(RunAll)' for a
    clean shell-driven exit code (0 = all green, 1 = anything else).

ARC: TTestCase instances are released at loop-iteration scope end;
no explicit Free call.  This matches the behaviour the user noted —
Blaise's full ARC makes Free unnecessary for short-lived locals.

testbcl.pas rewritten to be the canonical user shape:
RegisterTest(TSampleTests) + Halt(RunAll).  The previous manual
two-instance dance is gone.  Verified scenarios:

  * Mixed pass/fail   — prints failures + exit 1
  * All green         — prints 'OK (N tests)' + exit 0
  * Inherited methods — TChildTests sees TBaseTests's published
                        methods plus its own

All 1238 compiler tests still pass.

Filtering (--suite / --test) is intentionally deferred — once Step
11f rewires the cp.test.* uses-clauses, the same RunAll entry point
serves TestRunner.pas with ~50 fixtures, at which point ParamStr-
driven filters become more useful.
2026-05-06 07:45:50 +01:00
Graeme Geldenhuys 30753f818d docs(lang): grammar + rationale for ClassCreate (Step 11e)
Document the new ClassCreate builtin landed in 1fb2648:

  * grammar.ebnf — add ClassCreate and MethodAddress to the built-in
    functions reference table.
  * language-rationale.adoc — new section describing the decision
    (split RTL helper + static constructor call), why a per-class
    factory pointer was rejected, why metaclass.Create(args) wasn't
    made a method-call shape, and why we don't introduce a virtual
    constructor slot.
2026-05-06 07:37:18 +01:00
Graeme Geldenhuys 1fb2648f66 feat(lang): ClassCreate(Cls, ...args) builtin for runtime construction
Step 11e, part 3.  New compiler builtin that constructs an instance
from a metaclass value at runtime.  Lowers to:

  %p =l call $_ClassCreate(l <classvalue>)
  call $<BaseClass>_Create(l %p, args...)
  result = %p

The first argument must be tyMetaClass; remaining args are forwarded
to the resolved constructor.  Result type is the metaclass's BaseClass,
so 'F := ClassCreate(C, 7)' is well-typed when C: class of TFoo and
F: TFoo.  The constructor lookup is currently by name 'Create' (no
overload resolution yet — fpcunit's Create(string) and TObject's
parameter-less Create are the only shapes the bcl.testing runner
needs).  When the resolved class declares no Create method, only
the alloc call is emitted; fields stay in their _ClassAlloc-zeroed
default state.

This is the missing half that makes 'class of TTestCase' useful: the
runner (Step 11e, part 4) will iterate the published-method table
of each registered test class and call ClassCreate(Cls, MethodName)
to instantiate one TTestCase per test method, exactly the shape
fpcunit's class-method 'Suite' produces.

Symbol table: 'ClassCreate' added as skFunction in uSymbolTable.pas
              (return type filled in semantically).
Semantic   : AnalyseFuncCallExpr handles SameText('ClassCreate') —
              validates first arg is metaclass, analyses remaining
              args, resolves Create on BaseClass, stores it on
              ResolvedDecl, returns BaseClass as result type.
Codegen    : EmitExpr handles SameText('ClassCreate') — emits the
              two-call sequence above; reuses the ConstructorCall
              arg-coercion pattern (CoerceArg).

Three regression tests added to cp.test.classof.pas:
  TestSemantic_ClassCreate_RejectsNonMetaclassFirstArg
  TestCodegen_ClassCreate_EmitsAllocAndCtorCall
  TestCodegen_ClassCreate_NoCtor_OnlyAllocCalled

Verified end-to-end: ClassCreate(TCounter, 100); .Bump; .Bump;
prints 102 — argument flowed through the constructor, the instance
runs methods, and Free cleans up correctly.

All 1238 tests pass (1235 prior + 3 new).
2026-05-06 07:36:00 +01:00
Graeme Geldenhuys 113baa71f3 feat(rtl): add _ClassCreate(TInfo) helper
Step 11e, part 2.  New function in blaise_arc.pas that performs at
runtime what EmitConstructorCall does inline for the static
'TFoo.Create' form: read totalsize / fieldcleanup / vtable from the
expanded typeinfo (slots 4-6, landed in 58ff2a4), call _ClassAlloc,
install the vtable pointer at instance[0], bump the refcount once,
return the user pointer.

Caller responsibility: a metaclass value (the typeinfo pointer for
'class of T') is passed in; the caller subsequently invokes the
constructor via a static $T_Create call against the returned pointer.
This split keeps _ClassCreate independent of any specific
constructor signature; the upcoming ClassCreate compiler builtin
will glue the two halves together.

Verified end-to-end via a hand-written program that calls the helper
through 'class of TFoo' typecast and exercises the resulting
instance through both a non-virtual method and Free / Destroy.
All 1235 compiler tests still pass; testbcl smoke green.
2026-05-06 01:00:22 +01:00
Graeme Geldenhuys 6a41ec1c5e fix(self-hosting): parser cast-chain indexed prop + drop TList in uSemantic
Two incidental fixes uncovered while running the self-hosting fixpoint
check after the typeinfo expansion in 58ff2a4.  Neither blocks any
test that uses fpcunit (FPC has both already), but both block
Blaise compiling its own source after the 11d strict-PropIndexExpr
analyser change.

(1) uParser.pas:

The 'FuncOrCast(...).A.B.C' chain branch in ParseAtom never picked up
'[idx]' after each chained member, leaving indexed-property writes of
the form 'TClassTypeDef(TD.Def).Properties.Items[J]' to fall through
to TStringSubscriptExpr — which the analyser rewrites into a
PropIndexExpr-less FieldAccess and now (post-11d) flags as 'Indexed
property requires an index expression'.

The fix mirrors the pattern already in the regular A.B.C chain branch:
after each '.IDENT', check for '[' and attach the parsed index to the
just-built FieldAccess as PropIndexExpr.

(2) uSemantic.pas:

Two ResolveOverload helpers used Classes.TList for a temporary
TMethodDecl candidate list.  TList is FPC-only (Blaise's RTL
Classes unit only declares TStringList).  Swap to TObjectList(False)
from Contnrs (which Blaise both vendors and uSemantic already
imports) and switch [K] indexing to .Items[K] (Blaise has no default
indexed-property directive yet).

Both changes are net-neutral under FPC and unblock self-hosted
compilation of uParser/uSemantic on Blaise.
2026-05-06 00:58:22 +01:00
Graeme Geldenhuys 58ff2a429c feat(codegen): expand typeinfo with totalsize, fieldcleanup, vtable
Step 11e foundation.  Class typeinfo data blocks now carry three new
l-slots after the existing { parent, impllist, name, methods } prefix:

  Slot 4 (offset 32): total instance size in bytes (vptr + fields)
  Slot 5 (offset 40): pointer to $_FieldCleanup_<T> for this class
  Slot 6 (offset 48): pointer to $vtable_<T> for this class

These are the inputs needed by the upcoming _ClassCreate(TInfo) RTL
helper to allocate, install the vtable, and arrange ARC field cleanup
on release — the runtime equivalent of the inline lowering
EmitConstructorCall produces for the static 'TFoo.Create' form.

TObject also gets a vtable stub ($vtable_TObject = { l $typeinfo_TObject,
l $TObject_Destroy, l $TObject_ToString }) and an empty no-op
$_FieldCleanup_TObject function so its typeinfo can name them.  Both
were previously unemitted because TObject is built-in, not a user
type declaration; the new typeinfo slots force them to be real
linker-visible symbols.

Tests pinning the old four-slot layout updated to assert on the
prefix rather than the full record:
  cp.test.publishedrtti — TestCodegen_TypeInfo_HasFourSlots,
                          TestCodegen_NoPublishedMethods_MethodsSlotZero,
                          TestCodegen_PublishedMethods_TableEmitted
  cp.test.typetests    — TestCodegen_TypeInfo_ParentPtr_IsZero_ForRoot,
                          TestCodegen_TypeInfo_ParentPtr_ForDerived
  cp.test.interfaces   — TestCodegen_Typeinfo_ClassHasImpllistField

All 1235 tests pass.  testbcl smoke driver still green.
2026-05-06 00:57:20 +01:00
Graeme Geldenhuys bee768f290 feat(rtl): bcl.testing v0 — fpcunit runtime surface ported to Blaise
Step 11d.  Direct port of fpcunit's runtime surface, scoped to the
slice the 54 cp.test.*.pas regression units actually use.

bcl.testing.pas (compiler/src/main/pascal/) provides:

  * TTest, TAssert (AssertTrue/False, AssertEquals overloads for
    Integer/Int64/string/Pointer/Boolean, AssertNotNull/Null/Same,
    Fail), TTestCase (SetUp/TearDown virtual, RunTest dispatching
    via MethodAddress + TMethod cast to TRunMethod), TTestResult
    (counters + failure/error TStringLists + Summary),
    TTestCaseClass = class of TTestCase, EAssertionFailed
    (extends TObject; ToString override).

  * RegisterTest stores classes in a global TStringList for the
    runner (Step 11e) to enumerate.  Per-class published-method
    walking via metaclass-based virtual construction is deferred
    to 11e.

  * AssertException / ExpectException intentionally absent — no
    cp.test.* unit currently relies on them.

testbcl.pas smoke driver declares one TTestCase fixture with two
published methods (one passes, one fails), runs each via the
MethodAddress + procedure-of-object dispatch hot path, and prints
PASS/FAIL summary.  Compiles and runs end-to-end through the
QBE+gcc pipeline.

Two compiler bugfixes folded into the same step, both uncovered
while building the smoke driver and both with regression tests:

  1. ParseUnit now accepts dotted unit-name headers
     ('unit bcl.testing;') matching the UsesClause shape.  The
     parser only allowed a single IDENT after 'unit', which
     prevented any dotted-name unit (Generics.Collections, etc.)
     from being loaded through the unit loader.  grammar.ebnf
     updated to use UnitName in both positions.

  2. AnalyseFieldAccessExpr's chained Base<>nil branch was
     skipping AnalyseExpr on PropIndexExpr for method-backed
     indexed properties.  PropIndexExpr.ResolvedType remained
     nil and codegen segfaulted dereferencing it — visible as
     "Code generation error: Access violation" on inputs like
     R.Failures.Strings[I].  Constant indices ([0]) worked by
     accident because the literal-emission path didn't require
     ResolvedType.  Fixed: chained-base method-backed property
     reads now validate IndexParamName and analyse PropIndexExpr
     in the same shape as the non-chained and implicit-Self
     paths.  Regression test
     TestCodegen_IndexedProperty_ChainedBase_VarIndex_Compiles
     added to cp.test.properties.pas.

All 1235 compiler tests pass (1234 prior + 1 regression).
2026-05-06 00:32:11 +01:00
Graeme Geldenhuys a936ed1bd8 docs(lang): grammar + rationale for procedure of object (Step 11c)
grammar.ebnf:
* ProceduralType extended with the optional 'of object' modifier on
  both function and procedure variants.  Comment block describes the
  16-byte (Code, Data) layout, the byte-equivalence with TMethod that
  makes the TRunMethod(m) cast a no-op, and the call-site convention
  (Data passed as the implicit first argument).

language-rationale.adoc:
* Procedural Types section rewritten to cover both bare and method-
  pointer forms instead of listing 'of object' as deferred.  Records
  the IsMethodPtr compatibility rule (bare vs method pointers do not
  inter-operate even with identical signatures), the byte-equivalence
  with TMethod, and the canonical xUnit dispatch pattern.
* 'reference to' (closures) and calling-convention markers remain
  deferred; rationale updated accordingly.
2026-05-05 19:10:50 +01:00
Graeme Geldenhuys 3c5d466f78 feat(lang): procedure of object + TMethod record — Step 11c
Method-pointer types are now first-class.  Both fpcunit and fptest
dispatch test methods via 'procedure of object' values; this is the
abstraction that binds (Code, Data) into a callable.  With this in
place the bcl.testing port can reproduce fpcunit's RunTest line for
line — no inline-asm trampoline, no work-around RTL primitive.

Layout: a method-pointer value is a 16-byte block with Code at offset
0 and Data (Self) at offset 8.  This is byte-for-byte identical to
TMethod, so the cast 'TMyMethod(m)' (m: TMethod) is a no-op at the
QBE level.  A method-pointer call site loads both halves and emits
'call code(l data, args...)' — Data passes as the implicit first
argument so the callee sees it as Self.

Parser:
* ParseProceduralTypeDef now accepts an optional 'of object' suffix
  after the parameter list and (for functions) the return type.  Sets
  TProceduralTypeDef.IsMethodPtr := True.

Symbol table:
* TProceduralTypeDesc gains IsMethodPtr.  IsCompatibleWith requires
  the flag to match (a bare proc and a method-ptr are incompatible
  even with identical signatures).
* TMethod registered as an intrinsic record type (Code, Data: Pointer)
  alongside the TObject root class.  Reachable from any program
  without 'uses System' — same convention as TObject, Integer, etc.

Semantic:
* ResolveProceduralTypeDef propagates IsMethodPtr from the AST def
  to the type descriptor.

Codegen:
* Method-pointer locals: 'alloc8 16' + memset(0).  The variable's
  QBE name refers to the address of the 16-byte block, mirroring
  records.
* Method-pointer globals: 'export data $X = { z 16 }'.
* Method-pointer assignment: 'memcpy(dest_addr, src_addr, 16)' —
  works for both method-ptr-to-method-ptr and TRunMethod(TMethod)
  cast assignments since the layouts match.
* Indirect calls (both procedure-call and function-call expression
  forms) check IsMethodPtr: if set, load Code at slot+0 and Data at
  slot+8, then prepend Data as the first argument.

Tests: 13 new tests in cp.test.proctypes_ofobject.pas covering
parser shapes (of-object on procedure and function variants),
semantic propagation (TMethod registration, field offsets,
IsMethodPtr on the descriptor), codegen (alloc-16, global-data
shape, memcpy on assignment, Code+Data load on call), and three
end-to-end tests that compile-link-run a method-pointer dispatch
with no args, with args, and verifying Self is bound.

Total tests: 1234 (1221 + 13), all passing.  testpunit2 still
produces its expected pass/fail/inactive pattern.
2026-05-05 19:09:23 +01:00
Graeme Geldenhuys 31415c90df feat(lang): published-method RTTI + MethodAddress builtin — Step 11b
Adds the minimum-viable RTTI surface that fpcunit's discovery model
needs: a per-class table mapping published-method names to code
addresses, plus a MethodAddress(Obj, Name) builtin that walks the
typeinfo chain at runtime.

This is the only compiler-side work the bcl.testing port still
needed before the framework itself can be written.

Parser:
* TMethodDecl.IsPublished: Boolean.  ParseClassDef tracks the current
  visibility section and stamps each method declared inside a
  'published' block.

Codegen:
* Class typeinfo gains a 4th slot: l methods (or 0 for none).  Layout
  is now { l parent, l impllist, l name, l methods }.
* For each class with at least one published method, emit
    data $methods_<TName> = { l count, [l name, l addr] x count }
  where 'name' is a Blaise immortal string-data pointer (reusing
  EmitClassNameRef — same pattern as class-name strings) and 'addr'
  is the method's $TName_Method symbol.
* Three pre-existing tests asserted the literal three-slot typeinfo
  shape; updated to expect the new four-slot form.

Symbol table:
* MethodAddress registered as a builtin function returning Pointer.

Semantic:
* AnalyseFuncCallExpr handles MethodAddress(Obj, Name): asserts arity
  2, first arg tyClass, second arg tyString, return type Pointer.

RTL (blaise_arc.pas):
* _MethodAddress(Self, Name): Pointer walks
    Self -> vptr -> vtable[0] -> typeinfo
  then for each class on the parent chain reads typeinfo[3] (the
  methods table) and linearly searches its (name, addr) entries
  using _StringEquals.  Returns nil when not found.

Tests: 13 new tests in cp.test.publishedrtti.pas.  Parser tests
verify visibility tagging including the published->public boundary.
Codegen tests verify the typeinfo's 4th slot, the methods-table
shape (count, name+addr pairs), and the MethodAddress call site.
Four end-to-end tests compile the program with QBE+cc and exercise
the runtime helper: name found, name not found, parent-chain walk
across an inheritance pair, and distinct methods having distinct
addresses.

Total tests: 1221 (1208 + 13), all passing.  testpunit2 still
produces its expected pass/fail/inactive pattern.
2026-05-05 18:28:58 +01:00
Graeme Geldenhuys 1af933f526 feat(lang): class of TFoo metaclass type — Step 11a of bcl.testing port
Adds first-class metaclass types so 'class of TBase' is a real
type-system concept, not a typedef alias for Pointer.  This is the
foundation for fpcunit-style 'TTestCaseClass = class of TTestCase'
discovery patterns that the test-suite migration in Step 11 needs.

Symbol table:
* New tyMetaClass type kind and TMetaClassTypeDesc(BaseClass).
* NewMetaClassType factory mirrors NewPointerType.

Parser:
* ParseTypeName now recognises 'class of TName' and encodes it as
  the type-name string 'class of <Name>'.
* Type-decl 'type TFooClass = class of TFoo;' disambiguates 'class' +
  'of' from a class-definition body via a single-token PeekKind.

Semantic:
* FindTypeOrInstantiate creates TMetaClassTypeDesc on demand from any
  'class of <Name>' type-name string; rejects non-class bases.
* Type-alias resolution routes 'class of T' aliases through the same
  on-demand path.
* CheckTypesMatch admits metaclass-of-TDerived → metaclass-of-TBase
  where TDerived is-a TBase, and metaclass ↔ untyped Pointer in both
  directions (so 'AClass: Pointer' parameters keep accepting class
  identifiers as before).
* Comparison rules accept (metaclass, metaclass), (metaclass, Pointer),
  and (metaclass, nil) operand pairs.
* The IsMetaclassRef branch now types the bare class identifier as
  'class of TFoo' instead of untyped Pointer; backwards-compat with the
  old Pointer typing flows through the new metaclass↔Pointer rule.

Codegen:
* QbeTypeOf(tyMetaClass) = 'l'.
* Var allocation, parameter spill, static-array element load/store,
  global-data emission and array-literal allocation all extended to
  accept tyMetaClass as a pointer-shaped value.
* The integer-comparison branch in EmitBinaryExpr now picks ceql/cnel
  whenever EITHER operand is pointer-shaped (class/nil/Pointer/
  metaclass), not just the left operand — fixes a latent issue where
  'X = SomeClass' with X on the left as Pointer compiled to ceqw.

Tests: 14 new tests in cp.test.classof.pas covering parser shapes
(alias / var-decl / field), semantic rules (kind, base, accepted /
rejected assignments, Pointer-arg interop, comparisons), and codegen
(typeinfo emission, global-data shape, ceql for equality).

1208 tests total (1194 + 14), all passing.  testpunit2 still produces
its expected pass/fail/inactive pattern.
2026-05-05 17:59:42 +01:00
Graeme Geldenhuys 6a22aa7828 feat(lang): default parameter values + metaclass refs uncovered by testpunit2
Three compiler additions, exercised end-to-end by testpunit2.pas:

* Default parameter values — single-name, non-var, non-open-array params
  may carry '= literal-or-named-constant'. Overload resolution accepts
  any arity in [MinArity, ParamCount]; the resolver tie-breaks toward
  the candidate that needs fewer defaulted slots. AnalyseProcCall and
  AnalyseFuncCallExpr clone the default expression into the call's Args
  list. Defaults declared on a unit-interface forward decl are
  ownership-transferred to the matching impl param at reconciliation.
* Metaclass references — a bare class type identifier in a value
  position (e.g. 'EError' as an argument or in 'Pointer(EError)') is
  now retyped to Pointer with codegen emitting '$typeinfo_<Name>' as
  an immediate. Matches the value held by vtable[0], so 'A.ClassType =
  EError' is true exactly when A is an instance of that exact class.
* Numeric widening at call sites — the existing CoerceArg helper now
  covers Integer→Double (swtof/sltof), Single→Double (exts) and
  Integer→Single, in addition to the pre-existing w→l. Inherited-,
  constructor-, method-, and function-call sites all route through it.

punit.pas gains DefaultDoubleDelta, two Double AssertEquals overloads,
and '= ''' defaults on the four-arity AddTest declarations.

Docs: grammar.ebnf extends ParamGroup with the optional default; a new
DefaultValue rule lists the permitted forms. language-rationale.adoc
adds 'Default Parameter Values' and 'Metaclass References' sections
recording the decision, alternatives rejected, and the overload
tie-break formula.

All 1194 existing compiler tests pass; testpunit2 compiles, assembles,
links and runs (31 tests; the expected pass/fail/inactive pattern).
2026-05-05 17:08:51 +01:00
Graeme Geldenhuys e6b8e1e532 fix(semantic): type-check indirect-call arguments; doc record-param ABI
Indirect calls through a procedural-typed variable (the 'IsIndirectCall'
path added when 'RunTest(@DoTest)' first compiled) only validated arg
count.  The argument types were never checked against the procedural
signature, so e.g. 'H("oops")' against 'procedure(N: Integer)' compiled
silently and miscompiled at the QBE level.

Both call sites — TProcCall (statement form) and TFuncCallExpr
(expression form) — now run each actual through CheckTypesMatch against
the corresponding TProcParamInfo, and reject non-L-value actuals for
var-typed parameters with the same diagnostic as the regular-call path.

Two semantic tests in cp.test.proctypes.pas pin the behaviour: one for
the statement form, one for the expression form.

Docs

- language-rationale.adoc: update the overload-mangling example so the
  emitted symbol matches what codegen actually produces ($Log_D_Si, not
  $Log$Si), and document the QBEMangle escape table for the three
  sigil characters that the QBE symbol grammar disallows mid-identifier.
- design.adoc: clarify the var-parameter ABI entry — record and static
  array value parameters share the by-pointer convention with var-params,
  which is what makes IsVarParam the right flag for codegen to key off.
2026-05-05 00:42:11 +01:00
Graeme Geldenhuys d324c6b07f fix(compiler): record-param ABI, virtual ToString dispatch, mangling consistency
Three classes of bugs surfaced while getting punit's testpunit1 to run.

Record params passed by reference (ABI fix)

The QBE ABI passes record/static-array parameters as pointers regardless
of var/const/value semantics, so the local _var_X slot holds a pointer,
not the aggregate bytes. Codegen was only dereferencing for var-params,
producing silently-wrong code for value-record params: F.A := 42 wrote
into the local 8-byte slot, ZeroMem(@ARun, SizeOf(...)) corrupted the
stack via 100+-byte memset, and indirect calls passed &slot rather than
*slot.

Semantic now sets IsVarParam on TIdentExpr, TFieldAccessExpr, and
TFieldAssignment for both skVarParameter and skParameter-of-aggregate.
Codegen dereferences the slot in EmitFieldAssignment, EmitInstancePtr,
EmitLValueAddr, EmitAddrOfExpr, and the TIdentExpr aggregate path.

Virtual dispatch on Obj.Method (no parens)

The TFieldAccessExpr IsMethodCall path emitted a static call ignoring
MDecl.VTableSlot. With user-overridden ToString and static type TFoo /
runtime type TBar, B.ToString resolved statically to TFoo's body. Now
checks VTableSlot >= 0 and emits vtable[(slot+1)*8] dispatch. Affects
every zero-arg virtual method called via Obj.Method, not just ToString.

Mangling consistency

EmitMethodDef and the two vtable emission sites bypassed QBEMangle, so
method definitions wrote \$TFoo_Show\$i while call sites wrote
\$TFoo_Show_D_i — a latent link error. All three sites now route through
QBEMangle (preserving the leading \$ sigil); cp.test.overload.pas
expectations updated to the consistent _D_ separator.

Three e2e tests in cp.test.e2e.pas pin the ToString behaviour: default
returns class name, override is reached through static base type,
inherited override still resolves at runtime. 1192 tests pass.

Other items folded in (prior WIP, surfaced together):
- Parser propagates Line/Col onto TBinaryExpr for diagnostics
- Pass 3 forward pointer alias resolution (PFoo = ^TFoo before TFoo)
- TProceduralTypeDesc structural pointer equivalence
- Indirect call statements through procedural variables
- Init/finalization stmt analysis
- Int64 boAnd/boOr operand extension
- TObject vtable slot 1 = ToString; RTL TObject_ToString helper
- EmitGlobalVarData for unit interface block
- punit workarounds (Copy for indexed string, lifted nested proc)
2026-05-05 00:12:22 +01:00
Graeme Geldenhuys 1a1614012d feat(lang): Delete + SetLength + ClassType builtins, plus L-value var-arg widening
Adds three builtins requested by punit's port and the language-feature
gaps surfaced while wiring them through:

Delete(var S: string; Idx, Count: Integer)
  RTL helper _StringDelete returns a new string with the slice removed
  (1-based Idx; out-of-range / non-positive Count yields a copy).
  Codegen emits the standard string-mutator ARC sequence: load old →
  call helper → addref new → release old → store new through the var
  slot.  Factored out as TCodeGenQBE.EmitStringMutator for reuse.

SetLength(var S: string; N: Integer)
  RTL helper _StringSetLength returns a new string of length N (truncate
  or NUL-pad).  Same codegen path as Delete via EmitStringMutator.

obj.ClassType : Pointer
  TClass registered as a built-in alias of Pointer (placeholder until
  a real tyMetaClass arrives).  AnalyseFieldAccess detects ClassType
  on tyClass receivers and sets TFieldAccessExpr.IsClassTypeAccess.
  Codegen emits two indirections — instance → vtable → typeinfo —
  yielding the typeinfo data pointer.  Equality comparison of two
  ClassType results correctly distinguishes dynamic types.

Assigned(P) : Boolean
  Added as a sibling of the above because it's the natural companion
  for the metaclass / pointer code in punit (and otherwise a missing
  standard Pascal builtin).  Emits a 'cnel %ptr, 0' comparison.

Incidentals (bug fixes uncovered while testing):
  - AnalyseFieldAssignment now routes set-literal RHS into a tySet
    field through AnalyseSetLiteralExpr, so 'R.Options := []' and
    'P^.Options := []' are accepted.  Previously the empty literal
    was rejected as an array literal with zero elements.
  - Var-argument validation: adds TDerefExpr to the accepted L-value
    forms (P^ as a var argument) alongside TIdentExpr and
    TFieldAccessExpr.  TCodeGenQBE.EmitLValueAddr generalises
    EmitVarArgAddr to cover all three forms; var-arg call sites in
    EmitProcCall, EmitFuncCall paths, and EmitInheritedCall now go
    through it.
  - CheckTypesMatch: nil literal now compatible with tyProcedural
    parameter slots (already done in Phase D for procedural-handler
    nil-clear idioms; unchanged here, included for completeness in
    the var-arg/builtin context).

Tests: 1189 → 1192 pass, 0 errors, 0 failures.
  - cp.test.stringops: Delete/SetLength semantic OK + non-string
    rejection + codegen calls _StringDelete / _StringSetLength.
  - cp.test.typetests: ClassType semantic OK, resolves to tyPointer,
    TClass alias usable, codegen emits loadl chain.
2026-05-04 11:26:01 +01:00
Graeme Geldenhuys dbe1f37b65 feat(unit/lang): unit infrastructure fixes uncovered by punit re-port (Phase D)
Re-restoring 'overload' directives across punit.pas (the Phase A
blocker called out by the handover) surfaced a series of unit-section
gaps that were silently broken because no test exercised them.  Each
fix is small and bounded; together they let punit progress past the
overload duplicate-identifier wall and ten subsequent declarations,
into the next genuine missing-feature blocker (Delete-as-builtin).

Compiler changes:

- ParseForwardDecl (uParser.pas): now accepts the same directive set
  as ParseMethodDecl — overload, external, inline, stdcall, cdecl,
  register, pascal, safecall, forward, deprecated, platform,
  experimental.  Previously only 'external' was recognised, so any
  unit interface forward decl carrying 'overload' raised
  "Expected token tkImplementation".
- AnalyseUnit / AnalyseUnitForExport (uSemantic.pas): symbol
  registration for both forward and impl-only standalone proc/func
  decls now propagates IsOverload, sets ResolvedQbeName, and uses
  signature-aware matching to pair an impl with its forward decl
  when both are overloaded.
- AnalyseUnit: now also processes interface-section variable decls
  (registering them as IsGlobal symbols visible to impl bodies),
  interface-section const decls, impl-section const + type decls,
  and impl-section global var decls.  AnalyseUnitForExport got the
  matching impl-section type-decl + interface-section var-decl
  passes.  Without these, any unit whose impl bodies referenced an
  impl-section enum or an interface-section global silently raised
  "Unknown type" / "Undeclared variable".
- AnalyseStandaloneDecl: skip body analysis when ADecl.Body = nil.
  Prevents an AV when a forward-only decl reaches the body pass
  (e.g. a forward overload whose impl lives in another section).
- TFieldAssignment: accept skVarParameter (in addition to skVariable
  and skParameter) as a valid receiver kind so 'Suites.Count := 0'
  is permitted inside 'procedure InitSuiteList(var Suites: TSuiteList)'.
- Var-argument check: an L-value var argument may now be a
  TFieldAccessExpr (R.F or P^.F) in addition to a plain TIdentExpr.
  The existing field-access typing already produces ResolvedType so
  the subsequent CheckTypesMatch handles it correctly.
- CheckTypesMatch: nil literal now compatible with tyProcedural
  parameter slots (procedural-type fields are routinely nil-cleared
  via SetXxxHandler(nil) idioms).

punit changes:

- Re-applied 'overload;' to every interface and implementation
  declaration of the 17 names that participate in overload sets
  (AddSuite, AddTest, AssertEquals, AssertPassed, DoRunSysTests,
  ExpectMessage, FreeSuiteList, GetSuite, GetSuiteCount,
  GetSuiteIndex, GetTest, GetTestCount, GetTestIndex, RunSuite,
  RunTest, SetTestResult, SetTestResultRec).

Tests: 1179 pass, 0 errors, 0 failures.  New regression coverage:

- cp.test.varparams: var-arg accepts R.F and P^.F field accesses.
- cp.test.units: interface var visible in impl, impl-section type
  decl in scope, forward decl with 'overload' parses + analyses.

Punit still does not compile end-to-end: the next blocker is the
Delete(S, Idx, Count) string built-in (line 844), unrelated to
overloading.  That is a separate language-feature task.
2026-05-04 11:06:12 +01:00
Graeme Geldenhuys 0298a92e97 feat(lang): function overloading — Phase C (class methods + virtual)
Class method overloading and overloaded virtual/override dispatch.

Forward / impl matching now keys on (TypeName, Name, ParamSig) instead
of just (TypeName, Name): LinkClassMethodImpls walks all FMethodIndex
entries with the matching key and pairs each impl with the forward
decl whose signature equals the impl's.  Method registration enforces
the 'overload' directive — same-class same-name pairs without it now
produce a clean "Duplicate method" error rather than the misleading
"already has an inline body".

Each (Name, ParamSig) pair becomes its own vtable slot, so two virtual
overloads dispatch independently and overrides target the slot whose
mangled signature matches.  Required moving parameter-type resolution
ahead of the vtable pre-pass — Pass 1 of AnalyseTypeDecls already
registers every type name, so referencing other types in method
parameters before the host class's own fields are resolved is safe.

ResolveMethodOverload mirrors ResolveStandaloneOverload: walks the
inheritance chain, filters by arity, scores by argument type using
the existing ArgMatchScore, picks the best match, raises on ambiguity.
Three call sites in AnalyseMethodCall (receiver-expression,
implicit-self, variable receiver) and AnalyseMethodCallExpr now go
through it; the redundant CheckTypesMatch loops afterwards are gone
since the resolver already enforced compatibility.

Codegen: a small MethodEmitName helper centralises 'use ResolvedQbeName
when set, otherwise <Owner>_<Name>'.  Eight method-call emit sites
(static dispatch, virtual dispatch, sret returns, field-method calls,
inherited calls) now go through it.

Tests: 5 new cases in cp.test.overload.pas — class overload registration,
distinct mangled QBE names, mangled call sites, dup-without-overload
rejection, and (virtual + overload + override) producing distinct
vtable slots.  All 1174 tests pass.

End-to-end verified: 'B := TChild.Create; B.Greet(99); B.Greet(''hi'')'
where B's static type is TBase dispatches both overloads through the
overloaded vtable to TChild's overrides.
2026-05-04 10:47:59 +01:00
Graeme Geldenhuys 89ae192060 fix(semantic): TSemanticAnalyser.AnalyseUnit transfers symbol-table ownership
AnalyseUnit (the test-only entry point that analyses a unit in
isolation, distinct from the production AnalyseUnitForExport +
Analyse(Prog) two-phase pipeline) used to leave the symbol table
behind on the analyser.  When the caller freed the analyser, the
TTypeDesc instances pointed at by Par.ResolvedType /
ResolvedReturnType went with it — leaving every AST node holding a
dangling pointer.  A subsequent QbeTypeOf(AType) inside GenerateUnit
then dereferenced that garbage and segfaulted.

Five TUnitTests (TestCodegen_Unit_NoMainFunction,
TestCodegen_Unit_IntfFunctionsExported,
TestCodegen_Unit_FunctionBodyInIR,
TestCodegen_Unit_ImplOnlyFuncNotExported,
TestCodegen_Unit_CorrectArithmetic) all crashed at this point.

Fix: add TUnit.SymbolTable, transfer ownership at the end of
AnalyseUnit (mirroring TProgram), and free it in TUnit.Destroy.  The
production multi-file pipeline never touches AnalyseUnit and is
unaffected (the program owns the shared table).

All 1169 tests now pass — zero errors, zero failures.
2026-05-04 10:36:32 +01:00
Graeme Geldenhuys 813d4a1fe1 fix(codegen): spill Double/Single params with stored/stores, not storel
EmitFuncDef and EmitMethodDef both fell through to a 'storel' catch-all
when spilling parameters into local slots, so QBE rejected any function
with a Double or Single parameter ('invalid type for first operand
%_par_X in storel').  Each float kind needs its own QBE store width:
'd' parameters use 'stored' over an alloc8, 's' parameters use 'stores'
over an alloc4.

This was masked previously because no test exercised a float parameter
in a callable position end-to-end — the bug surfaced when wiring up an
overload demo with three F(...) variants over Integer/Double/string.

Adds two regression tests in cp.test.procs.pas asserting that the spill
emits the type-correct store opcode.
2026-05-04 10:32:04 +01:00
Graeme Geldenhuys 91ee6919b9 feat(lang): function overloading — Phase B (type-distinct + Delphi scoring)
Replaces Phase A's arity-only resolution with full Delphi-style
overload resolution: exact-type match (score 2) is preferred over
widening (score 1); ambiguous ties at the top score raise a compile-
time error.  QBE mangling switches from the temporary '$N<arity>'
suffix to a type-code suffix:

  i  Integer       l  Int64       u  UInt32      y  Byte
  b  Boolean       d  Double      s  Single      S  string
  C  PChar         p  untyped Pointer            E<Name>  enum
  R<Name>  record  K<Name>  class           I<Name>  interface
  ^X  pointer-to-X            A<X>  open-array of X
  T<Name>  set    F<Name>  procedural type    @X  var/out X

So 'procedure F(N: Integer)' emits as $F$i and 'procedure F(D: Double)'
as $F$d.  The fall-through branch in ArgMatchScore probes the existing
CheckTypesMatch, picking up nil-literal compatibility, class-subtype
widening, untyped-Pointer interop, enum/integer crossover, procedural
signature compat, and open-array forwarding without duplicating that
logic.

- AnalyseProcCall and AnalyseFuncCallExpr now analyse all arguments
  before resolving, so the resolver has Arg.ResolvedType available
  for scoring.
- Non-var argument compatibility is now established by the resolver;
  the redundant CheckTypesMatch loop afterwards is removed.
- Tests: 5 new cases — type-distinct registration, distinct mangled
  QBE names, exact-beats-widening, widening fallback, ambiguous error.
- Phase A tests updated for the new mangling ('$' for zero-arg, '$i'
  for Integer).
2026-05-04 10:31:50 +01:00
Graeme Geldenhuys 3bc91bd035 feat(lang): standalone function overloading — Phase A (arity)
Adds the 'overload' directive and Delphi-style overload semantics for
standalone procedures and functions. Phase A resolves overload sets by
parameter arity only; the QBE mangling uses a temporary '$N<arity>'
suffix that will be replaced by full type-code mangling in Phase B.

- TMethodDecl gains IsOverload and ResolvedQbeName.
- TSymbol gains IsOverload, NextOverload (chain), and Decl back-pointer.
- TScope.Define appends to the overload chain when both old and new
  symbols carry IsOverload; mixing overload + plain duplicates is a
  duplicate-identifier error.
- ResolveStandaloneOverload picks the matching arity from FProcIndex
  and is wired into AnalyseProcCall and AnalyseFuncCallExpr.
- Codegen emits via TMethodDecl.ResolvedQbeName at all four standalone-
  call sites and at EmitFuncDef.
- Grammar: 'overload' added to MethodDirective.
- Rationale: documents Delphi semantics, type-code mangling, alternatives
  rejected.
- Tests: cp.test.overload.pas, 7 cases covering parser flag, chain
  acceptance, mixing rejection, no-matching-arity error, and codegen.
2026-05-04 10:22:21 +01:00
Graeme Geldenhuys 0133f62822 feat(lang): P^.Field deref+access, initialization/finalization, TClass, more
Additional language features and fixes built on the previous commit:

P^.Field — pointer dereference followed by field access in both
  expression and assignment positions. Parser builds TDerefExpr as the
  Base of TFieldAccessExpr; codegen returns the record address directly
  (no double-load for tyRecord base types).

initialization / finalization sections in units — lexer adds
  tkInitialization / tkFinalization tokens; unit parser collects
  statements into TUnit.InitStmts / FinalStmts; codegen emits
  export functions $<Unit>_init() / $<Unit>_fini(); EmitMainHeader
  calls each $<Unit>_init() in import order.

TClass as Pointer alias — punit and testpunit adapted; TClass replaced
  with Pointer where RTTI is not yet available.

punit.pas adapted further:
  * PTest / PSuite / PResultRecord etc. restored as type aliases
  * BlockGet/BlockSet use PPointer typed local variables
  * ^TFoo(expr) casts replaced with PFoo(expr) using new type aliases
  * local type/const sections with typed array constants replaced with
    plain integer stage constants + case functions
  * Exit(expr) replaced with Result := expr; Exit
  * local typed const array (BStrs) replaced with if/else
  * RunSuiteSetup/TearDown/RunTestHandler extract function pointers
    to local vars before calling (workaround for ^.FnField() calls)
  * EFail.ToString changed from override to virtual

Tests: 1155 pass, 5 pre-existing errors, 0 new failures.
2026-05-04 02:33:09 +01:00
Graeme Geldenhuys faee6e6bef feat(lang): type aliases, floats, Abs, ClassName + global record fix
Six missing language features added:

1. type PFoo = ^TFoo — pointer and simple type aliases in type sections.
   Parser dispatches tkCaret/tkIdent to new TTypeAliasDef AST node;
   semantic pass resolves to TPointerTypeDesc or the aliased type.

2. Double / Single float types — lexer emits tkFloatLit; TFloatLiteral
   AST node; tyDouble/tySingle in the type system; QBE 'd'/'s' emit;
   arithmetic, comparison, and integer promotion in codegen;
   DoubleToStr, SingleToStr, StrToDouble, Abs(Double) built-ins;
   _DoubleToStr/_SingleToStr/_StrToDouble/_AbsInt/_AbsInt64 in RTL
   (new blaise_float.c). QBE generates SSE2 instructions automatically.
   Float const declarations supported.

3. Abs() — built-in for Integer, Int64, Double, Single.

4. TObject.ClassName — typeinfo gains a third slot (offset 16) holding
   a pointer to an immortal class-name string ($__cn_TFoo + 12).
   obj.ClassName loads vtable[0] (typeinfo), then typeinfo[16] (nameptr).
   EmitClassNameRef() emits the data-section label+offset relocation.

5. Global record field bug fix — FieldPtr() now accepts AIsGlobal and
   uses VarRef() so $RecordVar is used for global records instead of
   %_var_RecordVar. Both assignment and read paths fixed.

6. future-improvements.adoc — implemented items marked; Currency and
   BigDecimal deferred to BCL packages section added.

Tests: 1155 pass, 5 pre-existing errors (TUnitTests AV), 0 new failures.
2026-05-04 02:02:06 +01:00
Graeme Geldenhuys f256052a2b feat(types): bare procedural types (function/procedure pointers)
Adds support for declaring named procedural types that hold pointers to
standalone functions and procedures:

  type
    TIntFn   = function: Integer;
    TStrFn   = function(const S: string): Integer;
    TLogProc = procedure(Level: Integer; const Msg: string);

  var F: TIntFn;
  begin F := @MyFn; X := F(); end;

A procedural variable is stored as a single QBE 'l' (8-byte code
pointer). @FuncName produces a value of the matching procedural type.
Indirect calls F(args) load the pointer and emit a QBE indirect call.

Compatibility requires return types to match (both nil or both same
TTypeDesc) and parameter lists to match pairwise on type and parameter
mode (var/const/value); names do not participate.

Compiler additions:
* tyProcedural TTypeKind + TProcParamInfo + TProceduralTypeDesc
  (with IsCompatibleWith)
* TProceduralTypeDef AST node
* Parser: type T = function/procedure ... ; reuses ParseParamList
* Semantic: ResolveProceduralTypeDef in pass 2; AnalyseAddrOfExpr
  short-circuits @FuncName to a procedural-typed value;
  AnalyseFuncCallExpr accepts procedural-typed variables as
  indirect-call targets; CheckTypesMatch allows compatible
  procedural assignment
* Codegen: QbeTypeOf(tyProcedural) -> 'l'; EmitVarAllocs emits an
  8-byte slot; EmitAddrOfExpr emits $FuncName for @FuncName;
  EmitFuncCallExpr emits 'call %tmp(...)' for indirect calls,
  placed before the ResolvedDecl=nil type-cast branch

Out of scope (deferred until a use case requires them):
* function ... of object (method pointers — fat pointer ABI)
* reference to function/procedure (anonymous methods / closures)
* cdecl/stdcall calling-convention markers on procedural types

Tests: cp.test.proctypes.pas — 14 tests covering parser (kinds,
return types, params, var/const flags), semantic (compat accept/
reject on return type and arg count), and codegen (var allocation,
@FuncName emission, indirect-call emission).

Grammar and rationale: docs/grammar.ebnf adds the ProceduralType
production; docs/language-rationale.adoc captures the decision and
deferred items.

Motivation: prerequisite for porting Michael Van Canneyt's punit test
framework into rtl/src/test/pascal/, where every test, every
Setup/TearDown, and every hook handler is stored as a function
pointer.

1155 tests pass (1141 pre-existing + 14 new), no regressions.
2026-05-03 23:15:42 +01:00
Graeme Geldenhuys 5894411a35 chore(license): relicense from BSD-3-Clause to Apache 2.0 + Runtime Library Exception
Adopts the Swift/LLVM model: Apache License 2.0 with the Runtime Library
Exception text used verbatim by the Swift project. SPDX identifier:
Apache-2.0 WITH Swift-exception.

Apache 2.0 brings an explicit patent grant and patent-retaliation clause
that BSD-3 lacks. The Runtime Library Exception ensures binaries
produced by the Blaise compiler do not inherit attribution obligations
from the linked-in RTL.

* LICENSE — full Apache 2.0 + RLE text (replaces the deleted LICENCE).
* NOTICE — project header plus QBE attribution (MIT, vendored).
* docs/language-rationale.adoc — new Project Governance section
  capturing the decision, alternatives considered, and rationale.
* SPDX headers updated across all .pas, .pp, .inc, .c source files,
  build scripts, PasBuild plugins, and the Makefile.
* project.xml license field updated.
* tools/migrate_full.py HEADER template updated so generated
  self-hosting source carries the new licence.
2026-05-03 19:45:29 +01:00
Graeme Geldenhuys aafd004973 feat(generics): for..in support on generic collections (Step 9b)
Three fixes to unblock for..in over generic collection types:

1. SubstTypeParam: extend to substitute type params inside generic
   instantiation names.  Previously only bare 'T' and '^T' were
   handled; 'SomeName<T>' was left unsubstituted, so a method
   returning 'TListEnumerator<T>' kept the raw param name after
   cloning.

2. InstantiateGeneric: clone TClassTypeDef.Properties with type-param
   substitution (same pattern as fields and methods).  Without this
   the 'Current: T' property was invisible on instantiated enumerator
   types and FindProperty returned nil during for..in semantic checks.

3. AnalyseFieldAccess + ResolveScopeBoundTypeParams: when a generic
   RecordName such as 'TGenEnum<T>' isn't found in the symbol table,
   resolve each type argument against the current scope (where
   T=Integer is pushed during method body analysis) and retry the
   lookup with the concrete name 'TGenEnum<Integer>'.

New: cp.test.genericforin.pas — 10 tests covering property visibility,
property type after instantiation, and end-to-end for..in IR emission.
1141 tests total, all passing.
2026-05-03 16:33:08 +01:00
Graeme Geldenhuys 5af72daac7 feat(exceptions): typed except handlers and bare re-raise (Step 8)
Implements `on E: TClass do` dispatch inside except blocks and correct
bare `raise` propagation inside typed handlers.

New AST node TExceptHandlerClause carries VarName, TypeName, and Body.
TTryExceptStmt gains a Handlers list and ElseBody for the typed case;
ExceptBody remains for the legacy catch-all form.

Parser detects the 'on' identifier after 'except' to switch between the
typed-handler path and the plain catch-all path.  The no-binding form
`on TFoo do` is also supported.

Semantic pass validates that handler types resolve to classes, injects
a synthetic TVarDecl into the enclosing block for each bound variable
(so EmitVarAllocs allocates a stack slot at function entry), and opens a
local scope so the variable is accessible inside the handler body.

Codegen emits a _CurrentException call (before _PopExcFrame) followed by
an _IsInstance check per handler.  The first matching handler stores the
exception pointer into the variable slot and runs the body.  If no handler
matches and there is no else clause, _Reraise propagates to the enclosing
handler.

RTL: _CurrentException now reads g_current_exception (set by _Raise at
raise time) rather than g_exc_top->exception.  This is correct because
_PopExcFrame has already unwound the handler's own frame by the time a
bare raise is executed inside the handler body.

EmitRaiseStmt bare-raise path now calls _CurrentException then _Reraise
instead of _Raise(0), which previously cleared g_current_exception.

22 new tests (17 unit + 5 E2E): parser, semantic, codegen, and runtime
dispatch (correct handler selection, subclass matching, unmatched reraise,
bare raise propagation, else clause execution).  Total: 1131 tests, 0 failures.
2026-05-03 10:30:15 +01:00
Graeme Geldenhuys 5df182da86 chore: begin v0.5.0-dev cycle 2026-05-02 23:14:30 +01:00