SymTab.def 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. DEFINITION MODULE SymTab;
  2. (* Symbol table with static type checking for SimpleMod2
  3. (simplified Modula-2 without procedures).
  4. - flat scopes with levels: globals at level 0, WITH statement
  5. bodies push the record's fields as an inner scope (shadowing
  6. allowed, duplicates within one level rejected)
  7. - every symbol carries a type descriptor index (InvalidType if
  8. unknown, e.g. imported names); unknown types suppress follow-on
  9. errors to avoid cascades
  10. - type descriptors: aliases (one per TYPE declaration, so
  11. self-references such as POINTER TO Person resolve), subranges,
  12. enumerations, arrays, records (with field lists), sets,
  13. pointers, predefined types, string literals
  14. - INTEGER, CARDINAL and subranges thereof form one lenient
  15. "integer family"; mixed INTEGER/REAL arithmetic is rejected *)
  16. CONST
  17. InvalidType = -1;
  18. (* symbol kinds *)
  19. KindConst = 0;
  20. KindType = 1;
  21. KindVar = 2;
  22. KindImport = 3;
  23. KindModule = 4;
  24. KindPredef = 5;
  25. KindField = 6;
  26. KindProc = 7;
  27. KindParam = 8;
  28. (* type classes returned by ClassOf *)
  29. ClInvalid = 0;
  30. ClInt = 1;
  31. ClReal = 2;
  32. ClChar = 3;
  33. ClBool = 4;
  34. ClEnum = 5;
  35. ClArray = 6;
  36. ClRecord = 7;
  37. ClSet = 8;
  38. ClPtr = 9;
  39. ClStr = 10;
  40. ClClass = 11;
  41. ClNil = 12;
  42. ClLong = 13;
  43. ClProc = 14;
  44. ClUChar = 15;
  45. ClUStr = 16;
  46. ClByte = 17;
  47. ClSByte = 18;
  48. ClWord16 = 19;
  49. ClSWord16 = 20;
  50. (* operator codes for RelCheck *)
  51. OpEq = 0; OpNeq1 = 1; OpNeq2 = 2;
  52. OpLt = 3; OpLe = 4; OpGt = 5; OpGe = 6;
  53. OpIn = 7;
  54. (* operator codes for AddOp / MulOp *)
  55. OpAdd = 0; OpSub = 1; OpOr = 2;
  56. OpTimes = 0; OpSlash = 1; OpDiv = 2; OpMod = 3; OpAnd = 4;
  57. TYPE
  58. Name = ARRAY [0 .. 63] OF CHAR;
  59. TypeIndex = INTEGER;
  60. BoundsArr = ARRAY [0 .. 7] OF INTEGER;
  61. (* Per-dimension bounds for array index lists (grammar locals). *)
  62. (* ---------------- symbols and scopes ---------------- *)
  63. PROCEDURE Init;
  64. (* Clears the table and enters predefined identifiers
  65. (INTEGER, CARDINAL, SHORTINT, LONGINT, SHORTCARD, REAL, LONGREAL,
  66. CHAR, BOOLEAN, TRUE, FALSE, NIL). *)
  67. PROCEDURE Enter (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
  68. (* Enters name at the current scope level (type InvalidType).
  69. Returns FALSE on duplicate within the same level. *)
  70. PROCEDURE ReenterModGlobal (name: ARRAY OF CHAR; kind: INTEGER;
  71. modName: ARRAY OF CHAR): BOOLEAN;
  72. (* Lower helper: enters name at the current scope with an explicit
  73. defining-module name (used when re-emitting a unit after its scope
  74. has closed). FALSE on duplicate. *)
  75. PROCEDURE EnterPending (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
  76. (* Like Enter, but remembers the entry for a later FixPending call
  77. (used for VAR identifier lists whose type is parsed afterwards).
  78. Returns FALSE on duplicate (entry not recorded). *)
  79. PROCEDURE FixPending (t: TypeIndex) : BOOLEAN;
  80. (* Assigns type t to all pending entries, clears the buffer, and
  81. returns TRUE. When the current heading resumed a DEFINITION, the
  82. just-typed formals are compared against the snapshot (strict 231):
  83. FALSE = mismatch. *)
  84. PROCEDURE PendCount (): CARDINAL;
  85. (* Number of entries currently waiting for FixPending. *)
  86. PROCEDURE PendName (i: CARDINAL; VAR n: Name);
  87. (* Name of the i-th pending entry (empty string if out of range).
  88. Used by the QBE backend to emit storage for VAR lists whose
  89. type is parsed after the identifiers. *)
  90. PROCEDURE FieldPending (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  91. (* Records a field name for record descriptor rec (type fixed later
  92. with FixPendingF). Returns FALSE on duplicate field. *)
  93. PROCEDURE FixPendingF (rec: TypeIndex; t: TypeIndex);
  94. (* Assigns type t to pending fields owned by rec. *)
  95. PROCEDURE Lookup (name: ARRAY OF CHAR): BOOLEAN;
  96. (* TRUE if name is visible (innermost scope wins). *)
  97. PROCEDURE SymType (name: ARRAY OF CHAR): TypeIndex;
  98. (* Type of innermost visible entry, InvalidType if absent. *)
  99. PROCEDURE SetSymType (name: ARRAY OF CHAR; t: TypeIndex);
  100. (* Sets type of innermost visible entry. *)
  101. PROCEDURE SymKind (name: ARRAY OF CHAR): INTEGER;
  102. (* Kind of innermost visible entry, -1 if absent. *)
  103. PROCEDURE SetSymVal (name: ARRAY OF CHAR; v: ARRAY OF CHAR);
  104. (* Stores the literal value text of a CONST for constant folding. *)
  105. PROCEDURE GetSymVal (name: ARRAY OF CHAR; VAR v: ARRAY OF CHAR): BOOLEAN;
  106. (* v := stored literal value text of CONST name; FALSE if none. *)
  107. PROCEDURE Equal (a, b: ARRAY OF CHAR): BOOLEAN;
  108. PROCEDURE PushScope;
  109. PROCEDURE PopScope;
  110. (* WITH statement support: PushRecord pushes a record's fields. *)
  111. PROCEDURE PushRecord (t: TypeIndex): BOOLEAN;
  112. (* Pushes a scope containing t's fields (as KindField). FALSE if t
  113. is not a record type (or invalid). Caller must PopScope after. *)
  114. PROCEDURE PrintTable;
  115. (* ---------------- procedures (headings now, bodies step 4) ---------------- *)
  116. PROCEDURE EnterProc (name: ARRAY OF CHAR): BOOLEAN;
  117. (* Enters a KindProc heading and pushes its parameter scope.
  118. FALSE on duplicate (no scope pushed). Resets result to
  119. InvalidType (proper procedure), clears fwd, empties param chain. *)
  120. PROCEDURE ResumeProc (name: ARRAY OF CHAR): BOOLEAN;
  121. (* Resumes a DEFINITION's procedure heading for its
  122. IMPLEMENTATION body: pushes a fresh body scope, clears the
  123. formal chain (the implementation re-enters its own). Only valid
  124. while an IMPLEMENTATION unit is open. *)
  125. PROCEDURE ReenterProc (name: ARRAY OF CHAR): BOOLEAN;
  126. (* Resumes a FORWARD-marked procedure of this scope for its body
  127. heading: clears fwd, pushes a fresh parameter scope. FALSE
  128. when no such forward heading exists (then 200 applies). *)
  129. PROCEDURE EnterParam (name: ARRAY OF CHAR; isVar: BOOLEAN): BOOLEAN;
  130. (* Enters a KindParam in the heading scope and chains it into the
  131. current procedure's positional param list. FALSE on duplicate. *)
  132. PROCEDURE SetProcRes (t: TypeIndex) : BOOLEAN;
  133. (* Sets the current procedure's result type (InvalidType = none) and
  134. returns TRUE. When the heading resumed a DEFINITION
  135. (ResumeProc), it also performs the strict-231 signature check
  136. (result, formal count, formal types, VAR-ness) and returns the
  137. outcome; FALSE means the implementation does not match. *)
  138. PROCEDURE ProcRes (name: ARRAY OF CHAR): TypeIndex;
  139. (* Result type of the named procedure, InvalidType if absent/none. *)
  140. PROCEDURE MarkFwd;
  141. (* Marks the current procedure FORWARD (body must follow). *)
  142. PROCEDURE MarkVirtual;
  143. (* Marks the current procedure VIRTUAL (dynamically dispatched;
  144. dispatch tables arrive with class lowering). *)
  145. PROCEDURE MarkExternal (link: ARRAY OF CHAR);
  146. (* Marks the current procedure EXTERNAL (no body emitted; calls bind
  147. to the C symbol `link`, or to the procedure's own name if ''). *)
  148. PROCEDURE DeclareCProc (name: ARRAY OF CHAR): BOOLEAN;
  149. (* Enter name as an intrinsic external C procedure (varargs, no
  150. result): FROM libc IMPORT. *)
  151. PROCEDURE Varargs (name: ARRAY OF CHAR): BOOLEAN;
  152. (* TRUE when the named procedure is a C-varargs intrinsic. *)
  153. PROCEDURE ProcLink (name: ARRAY OF CHAR; VAR link: Name): BOOLEAN;
  154. (* External-procedure query/link lookup (link name of the named
  155. visible procedure). *)
  156. PROCEDURE CloseProc;
  157. (* Pops the current procedure's scope and its proc-stack entry.
  158. Replaces bare PopScope at the end of headings (DefUnit),
  159. FORWARD clauses and bodies. *)
  160. PROCEDURE InProc (): BOOLEAN;
  161. (* TRUE inside a procedure heading/body (RETURN legal). *)
  162. PROCEDURE CurRes (): TypeIndex;
  163. (* Result type of the innermost open procedure,
  164. InvalidType outside procedures or for proper procedures. *)
  165. PROCEDURE ProcDepthOf (name: ARRAY OF CHAR): CARDINAL;
  166. (* Lexical function-nesting depth of the named procedure
  167. (enclosing procedures; 0 = module level). Drives the static-
  168. link walk count at call sites. *)
  169. PROCEDURE ProcUid (name: ARRAY OF CHAR): CARDINAL;
  170. (* Unique id of the named procedure (0 if absent); drives QBE
  171. name mangling so same-named methods never collide. *)
  172. PROCEDURE ProcNPar (name: ARRAY OF CHAR): CARDINAL;
  173. PROCEDURE ParamType (name: ARRAY OF CHAR; i: CARDINAL): TypeIndex;
  174. PROCEDURE ParamIsVar (name: ARRAY OF CHAR; i: CARDINAL): BOOLEAN;
  175. (* Positional formal access for call checking (233 on mismatch). *)
  176. PROCEDURE ParamName (name: ARRAY OF CHAR; i: CARDINAL;
  177. VAR pname: Name): BOOLEAN;
  178. (* The i-th formal's name, for the re-parse pass QbeGen replay. *)
  179. PROCEDURE VarParamOk (actual, formal: TypeIndex): BOOLEAN;
  180. (* VAR-formal compatibility: same type, fixed array into open
  181. array with same element, or string literal into open CHAR. *)
  182. (* ---------------- modules / separate compilation (step 4.3) ---------------- *)
  183. (* One compiler session compiles several units into one image:
  184. DEFINITION, its IMPLEMENTATION, and one program (last). Each
  185. unit gets its own scope; imports materialize names. *)
  186. CONST
  187. UnitProg = 0; UnitDef = 1; UnitImpl = 2;
  188. PROCEDURE BeginDef (name: ARRAY OF CHAR): BOOLEAN;
  189. (* Opens a DEFINITION module scope. FALSE on duplicate module. *)
  190. PROCEDURE BeginImpl (name: ARRAY OF CHAR): BOOLEAN;
  191. (* Resumes a DEFINITION's scope for its IMPLEMENTATION.
  192. FALSE when the module is undefined or already implemented. *)
  193. PROCEDURE Reenter (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
  194. (* Re-enters the scope of an already-registered module (kind is UnitDef
  195. or UnitImpl) for an instantiation re-parse. *)
  196. PROCEDURE BeginProg (name: ARRAY OF CHAR): BOOLEAN;
  197. (* Opens the program module's scope. *)
  198. PROCEDURE EndUnit;
  199. (* Returns to the global scope. *)
  200. PROCEDURE EnterLowerScope (name: ARRAY OF CHAR): BOOLEAN;
  201. (* Make a unit's module scope current while Lower re-emits it. *)
  202. PROCEDURE LeaveLowerScope;
  203. PROCEDURE HaveProgram (): BOOLEAN;
  204. (* TRUE once a program module has been opened (a session without
  205. one is an error: nothing to link). *)
  206. PROCEDURE ModKnown (name: ARRAY OF CHAR): BOOLEAN;
  207. PROCEDURE ImportFrom (mod, name: ARRAY OF CHAR): BOOLEAN;
  208. (* Materializes an exported name from mod into the current scope
  209. (FROM mod IMPORT name). FALSE when mod/name is unknown. *)
  210. PROCEDURE OpaqueBase (name: ARRAY OF CHAR): TypeIndex;
  211. (* If name is an opaque alias (its target still InvalidType),
  212. returns that alias descriptor so the implementation can complete
  213. it via SetTarget; InvalidType otherwise. *)
  214. PROCEDURE QualType (mod, name: ARRAY OF CHAR): TypeIndex;
  215. PROCEDURE ExportUp (name: ARRAY OF CHAR);
  216. (* Nested-module END: clones the entry `name` from the just-finished
  217. nested scope into the enclosing scope, so a local module's EXPORT
  218. list becomes visible outside it. Must be called before PopScope. *)
  219. PROCEDURE Materialize (mod, name: ARRAY OF CHAR): BOOLEAN;
  220. (* FROM-import: clones mod's export into the current scope under
  221. its plain name. FALSE when mod/name is unknown. *)
  222. PROCEDURE MaterializeReplace (mod, name: ARRAY OF CHAR): BOOLEAN;
  223. (* Like Materialize, but an already-visible name is updated to mod's
  224. symbol (the current unit's global must win for Lower's per-unit
  225. re-emit, where units share names like scopeTop/EOL). *)
  226. PROCEDURE MaterializeAlias (mod, name: ARRAY OF CHAR;
  227. VAR alias: Name): BOOLEAN;
  228. (* Qualified access: clones mod's export under a per-module alias
  229. key, so same-named exports of different modules never collide.
  230. The alias key is used for all subsequent lookups (types, uids,
  231. params); the node remembers its base name and module for QBE
  232. symbol emission. *)
  233. PROCEDURE ProcBaseByUid (uid: CARDINAL; VAR base: Name): BOOLEAN;
  234. (* Base (mangling) name of a procedure by uid. *)
  235. PROCEDURE ProcModuleByUid (uid: CARDINAL; VAR mod: Name;
  236. VAR depth: CARDINAL): BOOLEAN;
  237. (* Defining module and lexical depth of a procedure by uid. *)
  238. PROCEDURE TypeStableName (t: TypeIndex; VAR mod, name: Name): BOOLEAN;
  239. (* Stable (module, type-name) for a named type/class. *)
  240. PROCEDURE ProcClassByUid (uid: CARDINAL): TypeIndex;
  241. (* Class owning a method declared with this uid, or InvalidType. *)
  242. PROCEDURE SymBase (name: ARRAY OF CHAR; VAR base: Name): BOOLEAN;
  243. PROCEDURE QualProcInfo (mod, name: ARRAY OF CHAR; VAR uid, depth: CARDINAL;
  244. VAR res: TypeIndex; VAR ext: BOOLEAN): BOOLEAN;
  245. (* Proc facts of a module-qualified procedure name. *)
  246. PROCEDURE SymMod (name: ARRAY OF CHAR; VAR m: Name): BOOLEAN;
  247. (* Defining module of the visible entry ("" for a local). *)
  248. PROCEDURE AliasProc (formalName: ARRAY OF CHAR; mod, actualName: ARRAY OF CHAR): BOOLEAN;
  249. (* Bind a generic procedure formal to the actual procedure. *)
  250. (* Base symbol name of the visible entry (drops any alias prefix). *)
  251. PROCEDURE GlobalRef (name: ARRAY OF CHAR; VAR g: Name): BOOLEAN;
  252. (* "<mod>_<sym>" QBE global-symbol name for a module-level entry;
  253. FALSE when name is not a module-level symbol. *)
  254. (* ---------------- classes (Clarion form, lowering deferred) ---------------- *)
  255. PROCEDURE EnterThisParam (cls: TypeIndex): BOOLEAN;
  256. (* Enters a method's hidden THIS VAR parameter. *)
  257. PROCEDURE LayoutClass (t: TypeIndex);
  258. (* Computes a class's field offsets / size. *)
  259. PROCEDURE LayoutRecord (t: TypeIndex);
  260. (* Computes a record's field offsets / size now (idempotent). Called at
  261. the record's END so a variant record's size is settled before any
  262. VAR of that type is emitted. *)
  263. PROCEDURE ParentOf (t: TypeIndex): TypeIndex;
  264. (* The single parent class of t (InvalidType when none). *)
  265. PROCEDURE SetParent (t, p: TypeIndex);
  266. (* Records p as the (single) parent class of t. Multiple parents
  267. are rejected in the grammar with 230. *)
  268. PROCEDURE PushClassScope (t: TypeIndex);
  269. (* Pushes the body scope of class t and records it on t. *)
  270. PROCEDURE MethodExists (t: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  271. (* TRUE if the class scope of t declares name as a procedure. *)
  272. PROCEDURE ResumeMethod (ct: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  273. (* Resumes a declared class method for its CLASS IMPLEMENTATION body. *)
  274. PROCEDURE HasVTable (ct: TypeIndex): BOOLEAN;
  275. (* TRUE when the class (or an ancestor) has virtual methods. *)
  276. PROCEDURE VirtSlot (ct: TypeIndex; name: ARRAY OF CHAR): INTEGER;
  277. (* Vtable slot of the method, or -1 when it is not virtual. *)
  278. PROCEDURE IsSubclass (sub, sup: TypeIndex): BOOLEAN;
  279. (* TRUE when sub is sup or inherits (transitively) from sup. *)
  280. PROCEDURE VptrOffset (ct: TypeIndex): INTEGER;
  281. (* Byte offset of the class's vptr field (-1 when it has none). *)
  282. PROCEDURE PushImplClass (ct: TypeIndex);
  283. PROCEDURE PopImplClass;
  284. PROCEDURE CurImplClass (): TypeIndex;
  285. (* The innermost open CLASS IMPLEMENTATION's class (InvalidType none). *)
  286. PROCEDURE VtClassCount (): CARDINAL;
  287. PROCEDURE VtClassAt (k: CARDINAL): TypeIndex;
  288. (* Classes that have a vtable, in build order. *)
  289. PROCEDURE VtCount (ct: TypeIndex): CARDINAL;
  290. PROCEDURE VtName (ct: TypeIndex; k: CARDINAL; VAR name: Name): BOOLEAN;
  291. PROCEDURE VtHasBody (ct: TypeIndex; k: CARDINAL): BOOLEAN;
  292. (* TRUE when slot k's method has an emitted body. *)
  293. PROCEDURE VtUid (ct: TypeIndex; k: CARDINAL): CARDINAL;
  294. (* Virtual-method slot k of ct (inherited slots first). *)
  295. PROCEDURE ClassMethodUid (t: TypeIndex; name: ARRAY OF CHAR): CARDINAL;
  296. (* Unique id of method name in class t or an ancestor (0 if none). *)
  297. PROCEDURE ClassMethodNPar (t: TypeIndex; name: ARRAY OF CHAR): CARDINAL;
  298. (* User formal count of the method (the hidden THIS excluded). *)
  299. PROCEDURE ClassMethodRes (t: TypeIndex; name: ARRAY OF CHAR): TypeIndex;
  300. (* Result type of the method (InvalidType if none/absent). *)
  301. PROCEDURE ClassMethodParamType (t: TypeIndex; name: ARRAY OF CHAR;
  302. i: CARDINAL): TypeIndex;
  303. PROCEDURE ClassMethodParamIsVar (t: TypeIndex; name: ARRAY OF CHAR;
  304. i: CARDINAL): BOOLEAN;
  305. (* User formal i of the method (index 0 skips the hidden THIS). *)
  306. PROCEDURE PushClassMembers (t: TypeIndex): BOOLEAN;
  307. (* Pushes a scope with t's fields (KindField) for method bodies in
  308. CLASS IMPLEMENTATION. FALSE if t is not a class. Method names
  309. are re-entered fresh by each body (copying them would collide);
  310. sibling-method references report 201 until step 4 calls. *)
  311. (* ---------------- type descriptors ---------------- *)
  312. PROCEDURE NewAlias (): TypeIndex;
  313. PROCEDURE EnterTypeParam (name: ARRAY OF CHAR): BOOLEAN;
  314. (* A generic formal type parameter: an unresolved alias type. Its
  315. target is set when the generic is instantiated. *)
  316. PROCEDURE EnterTypeParamBound (name: ARRAY OF CHAR;
  317. actual: TypeIndex): BOOLEAN;
  318. (* A generic formal bound to a concrete actual (instantiation re-parse):
  319. an alias type whose target is the actual. *)
  320. PROCEDURE NewSubR (lo, hi: INTEGER): TypeIndex;
  321. (* Integer subrange [lo..hi] (no base descriptor needed). Bounds
  322. accessors arrive with step 3; the backend cannot lower them yet. *)
  323. PROCEDURE NewSubB (lo, hi: INTEGER; base: TypeIndex): TypeIndex;
  324. (* Subrange [lo..hi] anchored on an ordinal base type (CHAR, BOOLEAN,
  325. enumeration): ClassOf follows the base so assignment compatibility
  326. and set spans treat e.g. ['A'..'Z'] as a character type, not an
  327. integer one. *)
  328. PROCEDURE NewEnum (): TypeIndex;
  329. PROCEDURE NewArrayB (elem: TypeIndex; lo, hi: INTEGER): TypeIndex;
  330. (* Bounded array (one dimension; nest for multi-dim). InvalidType
  331. when the table is full. *)
  332. PROCEDURE NewOpenArray (elem: TypeIndex): TypeIndex;
  333. (* Open ARRAY OF formal: 0-based, runtime count in the header.
  334. Only legal for formal parameters (grammar enforces). *)
  335. PROCEDURE BoundBegin;
  336. PROCEDURE BoundAdd (lo, hi: INTEGER): BOOLEAN;
  337. (* Accumulates one index bound pair for the enclosing ArrayType
  338. (max 8 dimensions, FALSE past that). *)
  339. PROCEDURE BoundCount (): CARDINAL;
  340. PROCEDURE BoundLo (i: CARDINAL): INTEGER;
  341. PROCEDURE BoundHi (i: CARDINAL): INTEGER;
  342. (* Read back the accumulated bounds (so a caller can snapshot them
  343. before parsing a nested element type that reuses the buffer). *)
  344. PROCEDURE NewRecord (): TypeIndex;
  345. PROCEDURE Unresolved (t: TypeIndex): BOOLEAN;
  346. (* TRUE for a not-yet-completed (forward/unbound) type. *)
  347. PROCEDURE IsOrdinal (t: TypeIndex): BOOLEAN;
  348. (* integer family / CHAR / UCHAR / BOOLEAN / enumeration. *)
  349. PROCEDURE ResetFields (rec: TypeIndex);
  350. (* Starts a fresh variant branch (drops pending fields, clears layout). *)
  351. PROCEDURE SetVariantTag (rec: TypeIndex; name : ARRAY OF CHAR);
  352. (* Records the variant CASE selector field (by name). *)
  353. PROCEDURE MarkVariant (rec: TypeIndex);
  354. (* Marks rec as having a variant part (overlapping fields). *)
  355. PROCEDURE IsVariant (rec: TypeIndex): BOOLEAN;
  356. (* TRUE when rec has a variant part. *)
  357. PROCEDURE NewSet (base: TypeIndex): TypeIndex;
  358. PROCEDURE NewPackSet (base: TypeIndex): TypeIndex;
  359. PROCEDURE NewBigSet (base: TypeIndex): TypeIndex;
  360. (* ISO PACKEDSET: same base/span as SET but 1/2/4-byte storage
  361. (spans up to 32 bits in V3). *)
  362. PROCEDURE NewPtr (base: TypeIndex): TypeIndex;
  363. PROCEDURE NewStr (): TypeIndex;
  364. PROCEDURE NewEmptyStr (): TypeIndex;
  365. (* The empty string literal ""/''. It is a string (compatible with a
  366. CHAR array) but, as ISO requires, also compatible with CHAR, where
  367. its value is the terminator 0. Marked by tlo = -2 and tested with
  368. IsEmptyStr. *)
  369. PROCEDURE NewUStr (): TypeIndex;
  370. (* UString literal type: LONGCARD count + count x UCHAR codepoints. *)
  371. PROCEDURE NewClass (): TypeIndex;
  372. (* Fresh class descriptor (holds fields like a record; methods live
  373. in the class scope). Parent and scope via SetParent/PushClassScope. *)
  374. (* Fresh descriptors; base/elem may be InvalidType. InvalidType is
  375. returned when the table is full. *)
  376. PROCEDURE SetTarget (t, base: TypeIndex);
  377. (* Sets an alias target (TYPE declaration completion). *)
  378. PROCEDURE BeginTypeBlock;
  379. PROCEDURE EndTypeBlock;
  380. (* Delimit a TYPE block so ForwardType is only active inside one. *)
  381. PROCEDURE ForwardType (name: ARRAY OF CHAR): TypeIndex;
  382. (* Inside a TYPE block: if name is not yet declared, enters it as an
  383. unresolved forward alias and returns its type index (completed by
  384. the later TypeItem via SetTarget). InvalidType when the name is
  385. already known or forward references are not permitted. *)
  386. PROCEDURE IsUnresolved (t: TypeIndex): BOOLEAN;
  387. (* TRUE for a forward placeholder whose target is not yet declared. *)
  388. PROCEDURE FwdPending (): CARDINAL;
  389. PROCEDURE FwdInfo (i: CARDINAL; VAR name: Name; VAR unresolved: BOOLEAN);
  390. (* Module-level forward type references: enumerate them at module end
  391. and flag those never completed (undeclared type). *)
  392. PROCEDURE FwdClear;
  393. PROCEDURE DeferReset;
  394. (* Fresh deferral table per unit (two-phase parse). *)
  395. PROCEDURE DeferBody (): CARDINAL;
  396. (* Records the current procedure's own scope for a skipped body;
  397. returns the deferral index (65535 on failure). *)
  398. PROCEDURE ReenterDeferred (idx: CARDINAL): BOOLEAN;
  399. (* Restores a deferred body scope for the re-parse pass. *)
  400. PROCEDURE OrderCheckOff;
  401. (* Ends the use-before-declaration window for a deferred body. *)
  402. PROCEDURE IntType (): TypeIndex;
  403. PROCEDURE UCharType (): TypeIndex;
  404. (* UCHAR: 32-bit Unicode codepoint (QBE w). *)
  405. PROCEDURE RealType (): TypeIndex;
  406. PROCEDURE CharType (): TypeIndex;
  407. PROCEDURE BoolType (): TypeIndex;
  408. PROCEDURE LongType (): TypeIndex;
  409. (* LONGINT/LONGCARD: 64-bit integer (QBE l). *)
  410. PROCEDURE AddrType (): TypeIndex;
  411. (* ADDRESS: the pointer-sized opaque type (interchangeable with any
  412. pointer for assignment and VAR parameters). *)
  413. PROCEDURE ObjectSize (t: TypeIndex): CARDINAL;
  414. (* Full allocation footprint (arrays 8 + count*element); drives
  415. SYSTEM.SIZE/TSIZE. *)
  416. PROCEDURE ClassOf (t: TypeIndex): INTEGER;
  417. (* Resolves aliases; InvalidType maps to ClInvalid. *)
  418. PROCEDURE IsIntFamily (t: TypeIndex): BOOLEAN;
  419. (* INTEGER, CARDINAL or subrange thereof (Invalid suppresses). *)
  420. PROCEDURE IsByteType (t: TypeIndex): BOOLEAN;
  421. (* BYTE/SBYTE (8-bit storage; values are passed as 32-bit words). *)
  422. PROCEDURE IsSignedByte (t: TypeIndex): BOOLEAN;
  423. (* SBYTE (sign-extended loads). *)
  424. PROCEDURE IsWord16Type (t: TypeIndex): BOOLEAN;
  425. (* WORD16/INTEGER16 (16-bit storage). *)
  426. PROCEDURE IsSignedWord16 (t: TypeIndex): BOOLEAN;
  427. (* INTEGER16 (sign-extended loads). *)
  428. PROCEDURE IsSmallInt (t: TypeIndex): BOOLEAN;
  429. (* BYTE/SBYTE/WORD16/INTEGER16 (1- or 2-byte storage). *)
  430. PROCEDURE SmallIntBytes (t: TypeIndex): CARDINAL;
  431. (* 1 for BYTE/SBYTE, 2 for WORD16/INTEGER16, 0 otherwise. *)
  432. PROCEDURE IsSignedSmallInt (t: TypeIndex): BOOLEAN;
  433. (* SBYTE/INTEGER16 (sign-extended loads). *)
  434. PROCEDURE IsLongFamily (t: TypeIndex): BOOLEAN;
  435. (* LONGINT/LONGCARD (QBE l). *)
  436. PROCEDURE SameType (a, b: TypeIndex): BOOLEAN;
  437. (* Same resolved descriptor (Invalid suppresses). *)
  438. PROCEDURE FieldExists (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  439. PROCEDURE FieldType (rec: TypeIndex; name: ARRAY OF CHAR): TypeIndex;
  440. PROCEDURE ArrayElem (t: TypeIndex): TypeIndex;
  441. PROCEDURE IsCharArray (t: TypeIndex): BOOLEAN;
  442. (* TRUE for arrays whose elements are CHAR: such arrays reserve one
  443. extra zero byte (index = count) so NUL-scanning works, matching
  444. gm2's NUL-terminated strings. *)
  445. PROCEDURE IsOpenArray (t: TypeIndex): BOOLEAN;
  446. PROCEDURE MarkByteBuf (t: TypeIndex);
  447. (* Mark an open array as an `ARRAY OF BYTE` formal (gm2 ext). *)
  448. PROCEDURE IsByteBuf (t: TypeIndex): BOOLEAN;
  449. (* TRUE for an `ARRAY OF BYTE` open-array formal. *)
  450. PROCEDURE ByteBufOk (at, ft: TypeIndex): BOOLEAN;
  451. (* gm2 ext: a scalar/pointer/string actual is accepted for `ARRAY OF BYTE`. *)
  452. PROCEDURE TypesOverflow (): BOOLEAN;
  453. (* TRUE once MaxTypes was exhausted (an implementation limit). *)
  454. PROCEDURE ArrayLo (t: TypeIndex): INTEGER;
  455. PROCEDURE ArrayHi (t: TypeIndex): INTEGER;
  456. (* Static bounds (open arrays: lo 0, hi undefined — backend reads
  457. the header instead). InvalidType maps to 0. *)
  458. PROCEDURE ArrayLen (t: TypeIndex): CARDINAL;
  459. (* Static element count, 0 for open/invalid. *)
  460. PROCEDURE ArrayDepth (t: TypeIndex): CARDINAL;
  461. (* Nesting depth (open/fixed levels); 0 for non-arrays. *)
  462. PROCEDURE ArrayRank (t: TypeIndex): CARDINAL;
  463. (* Alias for ArrayDepth, named for the open-array ABI. *)
  464. PROCEDURE IsMultiArray (t: TypeIndex): BOOLEAN;
  465. (* TRUE when the array rank is greater than one. *)
  466. PROCEDURE ArrayLeafBytes (t: TypeIndex): CARDINAL;
  467. (* Payload bytes of the scalar/record leaf element of an array; 0 for
  468. non-arrays or invalid types. *)
  469. PROCEDURE ArrayStride (t: TypeIndex; level: CARDINAL): CARDINAL;
  470. (* Bytes between adjacent elements at a 0-based array level for a fixed
  471. array; 0 when the level is invalid or a lower count is open/unknown. *)
  472. PROCEDURE ArrayPayloadSize (t: TypeIndex): CARDINAL;
  473. (* Static payload bytes of a fully fixed array (header excluded); 0 for
  474. open/invalid/non-array types. *)
  475. PROCEDURE TypeSize (t: TypeIndex): CARDINAL;
  476. (* Inline footprint in bytes: scalars 4/8, sets words*4, pointers,
  477. open and fixed arrays 8 (descriptor address), records sum of
  478. members. *)
  479. PROCEDURE FieldOffset (rec: TypeIndex; name: ARRAY OF CHAR): INTEGER;
  480. (* Declaration-order byte offset (-1 if absent). Computed lazily. *)
  481. PROCEDURE FieldCount (rec: TypeIndex): CARDINAL;
  482. PROCEDURE FieldName (rec: TypeIndex; i: CARDINAL; VAR name: Name);
  483. (* Field enumeration for whole-record lowering (order irrelevant). *)
  484. PROCEDURE SubBounds (t: TypeIndex; VAR lo, hi: INTEGER): BOOLEAN;
  485. (* TRUE with the bounds when t resolves to a subrange. *)
  486. PROCEDURE SetBase (t: TypeIndex): TypeIndex;
  487. (* Base type of a set descriptor (InvalidType if none). *)
  488. PROCEDURE SetWords (t: TypeIndex): CARDINAL;
  489. PROCEDURE SetBytes (t: TypeIndex): CARDINAL;
  490. (* Storage bytes: SET = SetWords*4, PACKEDSET = ceil(count/8) (1..4). *)
  491. PROCEDURE IsPackedSet (t: TypeIndex): BOOLEAN;
  492. PROCEDURE IsBigSet (t: TypeIndex): BOOLEAN;
  493. PROCEDURE BigSetSigned (t: TypeIndex): BOOLEAN;
  494. (* Mask words for a set type from its base span (bool 1, char 8,
  495. subrange by bounds); 0 when the base is unsuitable. *)
  496. PROCEDURE SetBaseLo (t: TypeIndex): INTEGER;
  497. (* Base minimum (bool/char 0, subrange lo); 0 when unsuitable. *)
  498. PROCEDURE SetCount (t: TypeIndex): CARDINAL;
  499. (* Base span in values (0 when unsuitable). *)
  500. PROCEDURE IsCardinalType (t: TypeIndex): BOOLEAN;
  501. (* TRUE for the predefined CARDINAL type (wide SET base). *)
  502. PROCEDURE TypeBounds (t: TypeIndex; VAR lo, hi: INTEGER): BOOLEAN;
  503. (* Ordinal bounds of a type (MIN/MAX): subrange/array, CHAR/UCHAR,
  504. BOOLEAN, enumeration, set base, or the integer family. *)
  505. PROCEDURE SetEnumCount (t: TypeIndex; n: CARDINAL);
  506. (* Records the number of literals of an enumeration type. *)
  507. PROCEDURE EnumCount (t: TypeIndex): CARDINAL;
  508. (* Literal count of an enumeration type (0 when not an enum). *)
  509. PROCEDURE NoteEnumLit (t: TypeIndex; name: ARRAY OF CHAR);
  510. (* Registers one enumeration literal (the grammar's Enum action). *)
  511. PROCEDURE EnterEnumLits (t: TypeIndex): CARDINAL;
  512. (* Makes an enum type's literals visible in the current scope (the type
  513. was just declared or imported), returning how many were newly
  514. entered. Modula-2 exports/imports a type's literals with the type. *)
  515. PROCEDURE PtrBase (t: TypeIndex): TypeIndex;
  516. (* ---------------- procedure types (step 8.5) ---------------- *)
  517. (* PROCEDURE (params): result as a value type. Values are code
  518. pointers (pointer-sized); calls through them are indirect. Param
  519. lists are collected with ProcTypeBegin/ProcTypeAdd and finalised
  520. by NewProcType (same pattern as array bounds). *)
  521. PROCEDURE NewProcType (res: TypeIndex): TypeIndex;
  522. (* Fresh procedure type (result may be InvalidType, set later). Its
  523. parameters are appended in order with ProcTypeAdd. *)
  524. PROCEDURE ProcTypeAdd (t: TypeIndex; isVar: BOOLEAN; pt: TypeIndex);
  525. PROCEDURE SetProcTypeRes (t: TypeIndex; res: TypeIndex);
  526. PROCEDURE ProcTypeNPar (t: TypeIndex): CARDINAL;
  527. PROCEDURE ProcTypeParamType (t: TypeIndex; i: CARDINAL): TypeIndex;
  528. PROCEDURE ProcTypeParamIsVar (t: TypeIndex; i: CARDINAL): BOOLEAN;
  529. PROCEDURE ProcTypeRes (t: TypeIndex): TypeIndex;
  530. (* Accessors for a procedure type (0/InvalidType when not one). *)
  531. PROCEDURE ProcTypeOf (name: ARRAY OF CHAR): TypeIndex;
  532. (* Builds the procedure type descriptor for procedure name (for using
  533. a procedure as a value). InvalidType if name is not a procedure. *)
  534. (* ---------------- predicates used by grammar checks ---------------- *)
  535. (* All return TRUE if either operand is InvalidType (no cascades). *)
  536. PROCEDURE Assignable (src, dst: TypeIndex): BOOLEAN;
  537. (* Assignment compatibility (210). *)
  538. PROCEDURE ArithCheck (l, r: TypeIndex; divmod: BOOLEAN;
  539. VAR res: TypeIndex): BOOLEAN;
  540. (* + - * / (divmod FALSE) or DIV MOD (TRUE); res is result type (211). *)
  541. PROCEDURE BoolCheck (t: TypeIndex): BOOLEAN;
  542. (* BOOLEAN required: NOT/AND/OR operands (212), conditions (214). *)
  543. PROCEDURE RelCheck (l, r: TypeIndex; op: INTEGER): BOOLEAN;
  544. (* = # <> < <= > >= IN (213/222 chosen by caller via op). *)
  545. PROCEDURE IsStrType (t: TypeIndex): BOOLEAN;
  546. (* A string literal (ClStr) or a 1-D ARRAY OF CHAR. *)
  547. PROCEDURE IsEmptyStr (t: TypeIndex): BOOLEAN;
  548. (* TRUE for the empty string literal "" (compatible with CHAR too). *)
  549. PROCEDURE UStrCompat (l, r: TypeIndex): BOOLEAN;
  550. (* Both operands are UString literals (U"..."): `+` concatenates. *)
  551. PROCEDURE IsUCharArray (t: TypeIndex): BOOLEAN;
  552. (* A 1-D ARRAY OF UCHAR (a U"..." content-copy destination). *)
  553. PROCEDURE StrCompat (l, r: TypeIndex): BOOLEAN;
  554. (* Both operands are string-compatible (ClStr / 1-D ARRAY OF CHAR):
  555. `=`/`#` compare content via the shim. *)
  556. PROCEDURE EqCheck (l, r: TypeIndex): BOOLEAN;
  557. (* = # compatibility, also reused for CASE label matching. *)
  558. PROCEDURE InCheck (l, set: TypeIndex): BOOLEAN;
  559. (* Membership `l IN set`. *)
  560. PROCEDURE SetElemClassOk (c: INTEGER): BOOLEAN;
  561. (* TRUE for an element class acceptable in a set constructor. *)
  562. PROCEDURE StrLen (s: ARRAY OF CHAR): CARDINAL;
  563. PROCEDURE ConstInt (q: ARRAY OF CHAR; VAR v: INTEGER): BOOLEAN;
  564. (* Parses a folded integer immediate ("42", "-5", "16") as produced
  565. by NormInt/NegFold. FALSE for temporaries and non-numerics. *)
  566. END SymTab.