diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-17 06:58:13 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-17 06:58:13 +0000 |
| commit | c372c08dc940db58f1da9cadeb64b4cb3c837704 (patch) | |
| tree | 70636fd3a485eaf16fb7f442c294ac066c76e1a1 /doc | |
| parent | 74b6d06711afd3efbbc47436f1491900994313f3 (diff) | |
fix(classes): preserve method-only heap objects and document NEW
Allocate at least one byte for method-only classes so their heap addresses
remain usable through pointers. Complete constructor diagnostics, field-order
and exception coverage, document construction and rebinding, and highlight NEW.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/language/keyword/cpsc-keyword-class.7.md | 25 | ||||
| -rw-r--r-- | doc/language/keyword/cpsc-keyword-declare.7.md | 2 |
2 files changed, 22 insertions, 5 deletions
diff --git a/doc/language/keyword/cpsc-keyword-class.7.md b/doc/language/keyword/cpsc-keyword-class.7.md index faa84df..f0d2e12 100644 --- a/doc/language/keyword/cpsc-keyword-class.7.md +++ b/doc/language/keyword/cpsc-keyword-class.7.md @@ -2,7 +2,7 @@ # NAME -cpsc-keyword-class, cpsc-keyword-public, cpsc-keyword-private, cpsc-keyword-endclass - define a class with fields and methods +cpsc-keyword-class, cpsc-keyword-public, cpsc-keyword-private, cpsc-keyword-endclass, cpsc-keyword-new - define a class with fields and methods # SYNOPSIS @@ -10,6 +10,9 @@ cpsc-keyword-class, cpsc-keyword-public, cpsc-keyword-private, cpsc-keyword-endc CLASS name PUBLIC publicField : type PRIVATE privateField : type + PUBLIC PROCEDURE NEW(parameters) + statements + ENDPROCEDURE PUBLIC PROCEDURE method(parameters) statements ENDPROCEDURE @@ -17,6 +20,8 @@ CLASS name RETURN expression ENDFUNCTION ENDCLASS +DECLARE object : ClassName(arguments) +object <- NEW ClassName(arguments) ``` # DESCRIPTION @@ -29,22 +34,32 @@ Within a method, bare field names refer to the current object's fields. The comp Parameters and local variables, arrays, or constants may shadow fields; this produces a warning with the source line and does not prevent compilation. The local name takes precedence; `this.Field` continues to access the field. The name `this` cannot be a class member or a method parameter or local declaration. -Classes retain record value semantics: zero initialization, whole-value copying, arrays, pointers, and ordinary function/procedure parameters. Methods modify the receiver's storage. The object reference `this` may be read, returned, or passed as an object, including BYREF to a procedure; `this <- Other` copies Other into the current object without rebinding the reference. PRIVATE fields still initialize and copy with the whole value. Constructors, destructors, NEW, INHERITS, and array fields are unsupported. +Classes retain record value semantics: zero initialization, whole-value copying, arrays, pointers, and ordinary function/procedure parameters. Methods modify the receiver's storage. The object reference `this` may be read, returned, or passed as an object, including BYREF to a procedure; `this <- Other` copies Other into the current object without rebinding the reference. PRIVATE fields still initialize and copy with the whole value. + +A constructor is a PROCEDURE named NEW, following guide section 10.2. It may be PUBLIC or PRIVATE; FUNCTION NEW and global PROCEDURE NEW are errors. Member names are case insensitive, so New and new also name the constructor. Only one NEW is allowed per class. Explicit `Object.NEW(arguments)`, `CALL Object.NEW(arguments)` and `this.NEW(arguments)` behave like ordinary member calls and may be repeated; they do not zero the object or reconstruct its fields. + +`DECLARE Object : ClassName`, `DECLARE Object : ClassName()` and `DECLARE Object : ClassName(arguments)` zero the object's storage, construct its fields recursively in declaration order, then call its NEW procedure. These automatic DECLARE, field and array calls are compiler extensions. Array elements and class-valued fields in records or classes use parameterless construction, before the enclosing constructor runs. A required constructor argument cannot be omitted; supplying arguments to a class without NEW is an error. Empty parentheses are allowed for a class without NEW. Constructor arguments after ARRAY OF are unsupported. A declaration executed again, such as inside a loop, zeroes and constructs its original storage again. + +The keyword NEW is allowed only as the complete right side of assignment: `Target <- NEW ClassName`, `Target <- NEW ClassName()` or `Target <- NEW ClassName(arguments)`. Target must have exactly the named class type. NEW uses the checked MALLOC allocation path, zeroes storage and constructs fields before calling NEW. A method-only class receives at least one byte of heap storage so its object address is non-null. Allocation failure reports a runtime error and throws; constructor exceptions also propagate to TRY/CATCH. Private constructor access and argument checks follow ordinary member-call rules. + +When Target is a declared class variable, including a function local or BYVAL parameter, NEW rebinds that variable to the heap object. No `^` dereference is needed to access it. Address-of, BYREF calls and method calls use its current object. Pointers taken before rebinding continue to refer to the previous object. Ordinary `A <- B` remains a value copy into A's current object and does not rebind A. BYVAL parameters copy the caller's value and can rebind independently. + +For an array element, field (including a bare field inside a method), BYREF parameter, this, or pointer dereference, NEW constructs a heap object and copies its value into the existing target. That temporary allocation is retained. There is no automatic reclamation, destructor or DELETE support; INHERITS, SUPER and array fields remain unsupported. # EXAMPLE ```text CLASS Counter PRIVATE Value : INTEGER - PUBLIC PROCEDURE Set(Number : INTEGER) + PUBLIC PROCEDURE NEW(Number : INTEGER) this.Value <- Number ENDPROCEDURE FUNCTION Get() RETURNS INTEGER RETURN Value ENDFUNCTION ENDCLASS -DECLARE Item : Counter -Item.Set(3) +DECLARE Item : Counter(3) +Item <- NEW Counter(5) OUTPUT Item.Get() ``` diff --git a/doc/language/keyword/cpsc-keyword-declare.7.md b/doc/language/keyword/cpsc-keyword-declare.7.md index 0fd5cf3..428a934 100644 --- a/doc/language/keyword/cpsc-keyword-declare.7.md +++ b/doc/language/keyword/cpsc-keyword-declare.7.md @@ -19,6 +19,8 @@ C_INT, C_CHAR, C_STRING and C_DOUBLE are native C aliases for int, char, char* a ARRAY bounds are inclusive and may be expressions; multiple ranges create multiple dimensions. Access elements with `name[index, ...]`. Bounds are checked at run time; an invalid index throws an exception that TRY/CATCH can handle. Whole-array assignment is unsupported. Declare arrays before using them; arrays declared inside a control-flow block stay in that block. +For class variables, an optional argument list after the type supplies constructor arguments, such as `DECLARE Item : Counter(3)`. Declarations zero storage and automatically construct class fields and array elements before calling the outer constructor. See [cpsc-keyword-class(7)](cpsc-keyword-class.7.md) for construction and NEW rebinding rules. + # EXAMPLE ```text |
