SymTab.def 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. (* operator codes for RelCheck *)
  42. OpEq = 0; OpNeq1 = 1; OpNeq2 = 2;
  43. OpLt = 3; OpLe = 4; OpGt = 5; OpGe = 6;
  44. OpIn = 7;
  45. (* operator codes for AddOp / MulOp *)
  46. OpAdd = 0; OpSub = 1; OpOr = 2;
  47. OpTimes = 0; OpSlash = 1; OpDiv = 2; OpMod = 3; OpAnd = 4;
  48. TYPE
  49. Name = ARRAY [0 .. 63] OF CHAR;
  50. TypeIndex = INTEGER;
  51. (* ---------------- symbols and scopes ---------------- *)
  52. PROCEDURE Init;
  53. (* Clears the table and enters predefined identifiers
  54. (INTEGER, CARDINAL, SHORTINT, LONGINT, REAL, LONGREAL, CHAR,
  55. BOOLEAN, TRUE, FALSE, NIL). *)
  56. PROCEDURE Enter (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
  57. (* Enters name at the current scope level (type InvalidType).
  58. Returns FALSE on duplicate within the same level. *)
  59. PROCEDURE EnterPending (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
  60. (* Like Enter, but remembers the entry for a later FixPending call
  61. (used for VAR identifier lists whose type is parsed afterwards).
  62. Returns FALSE on duplicate (entry not recorded). *)
  63. PROCEDURE FixPending (t: TypeIndex);
  64. (* Assigns type t to all pending entries, clears the buffer. *)
  65. PROCEDURE PendCount (): CARDINAL;
  66. (* Number of entries currently waiting for FixPending. *)
  67. PROCEDURE PendName (i: CARDINAL; VAR n: Name);
  68. (* Name of the i-th pending entry (empty string if out of range).
  69. Used by the QBE backend to emit storage for VAR lists whose
  70. type is parsed after the identifiers. *)
  71. PROCEDURE FieldPending (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  72. (* Records a field name for record descriptor rec (type fixed later
  73. with FixPendingF). Returns FALSE on duplicate field. *)
  74. PROCEDURE FixPendingF (rec: TypeIndex; t: TypeIndex);
  75. (* Assigns type t to pending fields owned by rec. *)
  76. PROCEDURE Lookup (name: ARRAY OF CHAR): BOOLEAN;
  77. (* TRUE if name is visible (innermost scope wins). *)
  78. PROCEDURE SymType (name: ARRAY OF CHAR): TypeIndex;
  79. (* Type of innermost visible entry, InvalidType if absent. *)
  80. PROCEDURE SetSymType (name: ARRAY OF CHAR; t: TypeIndex);
  81. (* Sets type of innermost visible entry. *)
  82. PROCEDURE SymKind (name: ARRAY OF CHAR): INTEGER;
  83. (* Kind of innermost visible entry, -1 if absent. *)
  84. PROCEDURE Equal (a, b: ARRAY OF CHAR): BOOLEAN;
  85. PROCEDURE PushScope;
  86. PROCEDURE PopScope;
  87. (* WITH statement support: PushRecord pushes a record's fields. *)
  88. PROCEDURE PushRecord (t: TypeIndex): BOOLEAN;
  89. (* Pushes a scope containing t's fields (as KindField). FALSE if t
  90. is not a record type (or invalid). Caller must PopScope after. *)
  91. PROCEDURE PrintTable;
  92. (* ---------------- procedures (headings now, bodies step 4) ---------------- *)
  93. PROCEDURE EnterProc (name: ARRAY OF CHAR): BOOLEAN;
  94. (* Enters a KindProc heading and pushes its parameter scope.
  95. FALSE on duplicate (no scope pushed). Resets result to
  96. InvalidType (proper procedure), clears fwd, empties param chain. *)
  97. PROCEDURE ReenterProc (name: ARRAY OF CHAR): BOOLEAN;
  98. (* Resumes a FORWARD-marked procedure of this scope for its body
  99. heading: clears fwd, pushes a fresh parameter scope. FALSE
  100. when no such forward heading exists (then 200 applies). *)
  101. PROCEDURE EnterParam (name: ARRAY OF CHAR; isVar: BOOLEAN): BOOLEAN;
  102. (* Enters a KindParam in the heading scope and chains it into the
  103. current procedure's positional param list. FALSE on duplicate. *)
  104. PROCEDURE SetProcRes (t: TypeIndex);
  105. (* Sets the current procedure's result type (InvalidType = none). *)
  106. PROCEDURE ProcRes (name: ARRAY OF CHAR): TypeIndex;
  107. (* Result type of the named procedure, InvalidType if absent/none. *)
  108. PROCEDURE MarkFwd;
  109. (* Marks the current procedure FORWARD (body must follow). *)
  110. PROCEDURE MarkVirtual;
  111. (* Marks the current procedure VIRTUAL (dynamically dispatched;
  112. dispatch tables arrive with class lowering). *)
  113. PROCEDURE CloseProc;
  114. (* Pops the current procedure's scope and its proc-stack entry.
  115. Replaces bare PopScope at the end of headings (DefUnit),
  116. FORWARD clauses and bodies. *)
  117. PROCEDURE InProc (): BOOLEAN;
  118. (* TRUE inside a procedure heading/body (RETURN legal). *)
  119. PROCEDURE CurRes (): TypeIndex;
  120. (* Result type of the innermost open procedure,
  121. InvalidType outside procedures or for proper procedures. *)
  122. (* ---------------- classes (Clarion form, lowering deferred) ---------------- *)
  123. PROCEDURE SetParent (t, p: TypeIndex);
  124. (* Records p as the (single) parent class of t. Multiple parents
  125. are rejected in the grammar with 230. *)
  126. PROCEDURE PushClassScope (t: TypeIndex);
  127. (* Pushes the body scope of class t and records it on t. *)
  128. PROCEDURE MethodExists (t: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  129. (* TRUE if the class scope of t declares name as a procedure. *)
  130. PROCEDURE PushClassMembers (t: TypeIndex): BOOLEAN;
  131. (* Pushes a scope with t's fields (KindField) for method bodies in
  132. CLASS IMPLEMENTATION. FALSE if t is not a class. Method names
  133. are re-entered fresh by each body (copying them would collide);
  134. sibling-method references report 201 until step 4 calls. *)
  135. (* ---------------- type descriptors ---------------- *)
  136. PROCEDURE NewAlias (): TypeIndex;
  137. PROCEDURE NewSub (base: TypeIndex): TypeIndex;
  138. PROCEDURE NewSubR (lo, hi: INTEGER): TypeIndex;
  139. (* Integer subrange [lo..hi] (no base descriptor needed). Bounds
  140. accessors arrive with step 3; the backend cannot lower them yet. *)
  141. PROCEDURE NewEnum (): TypeIndex;
  142. PROCEDURE NewArray (elem: TypeIndex): TypeIndex;
  143. PROCEDURE NewRecord (): TypeIndex;
  144. PROCEDURE NewSet (base: TypeIndex): TypeIndex;
  145. PROCEDURE NewPtr (base: TypeIndex): TypeIndex;
  146. PROCEDURE NewStr (): TypeIndex;
  147. PROCEDURE NewClass (): TypeIndex;
  148. (* Fresh class descriptor (holds fields like a record; methods live
  149. in the class scope). Parent and scope via SetParent/PushClassScope. *)
  150. (* Fresh descriptors; base/elem may be InvalidType. InvalidType is
  151. returned when the table is full. *)
  152. PROCEDURE SetTarget (t, base: TypeIndex);
  153. (* Sets an alias target (TYPE declaration completion). *)
  154. PROCEDURE IntType (): TypeIndex;
  155. PROCEDURE RealType (): TypeIndex;
  156. PROCEDURE CharType (): TypeIndex;
  157. PROCEDURE BoolType (): TypeIndex;
  158. PROCEDURE ClassOf (t: TypeIndex): INTEGER;
  159. (* Resolves aliases; InvalidType maps to ClInvalid. *)
  160. PROCEDURE IsIntFamily (t: TypeIndex): BOOLEAN;
  161. (* INTEGER, CARDINAL or subrange thereof (Invalid suppresses). *)
  162. PROCEDURE SameType (a, b: TypeIndex): BOOLEAN;
  163. (* Same resolved descriptor (Invalid suppresses). *)
  164. PROCEDURE FieldExists (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  165. PROCEDURE FieldType (rec: TypeIndex; name: ARRAY OF CHAR): TypeIndex;
  166. PROCEDURE ArrayElem (t: TypeIndex): TypeIndex;
  167. PROCEDURE PtrBase (t: TypeIndex): TypeIndex;
  168. (* ---------------- predicates used by grammar checks ---------------- *)
  169. (* All return TRUE if either operand is InvalidType (no cascades). *)
  170. PROCEDURE Assignable (src, dst: TypeIndex): BOOLEAN;
  171. (* Assignment compatibility (210). *)
  172. PROCEDURE ArithCheck (l, r: TypeIndex; divmod: BOOLEAN;
  173. VAR res: TypeIndex): BOOLEAN;
  174. (* + - * / (divmod FALSE) or DIV MOD (TRUE); res is result type (211). *)
  175. PROCEDURE UnaryCheck (t: TypeIndex; VAR res: TypeIndex): BOOLEAN;
  176. (* Unary + - (part of 211). *)
  177. PROCEDURE BoolCheck (t: TypeIndex): BOOLEAN;
  178. (* BOOLEAN required: NOT/AND/OR operands (212), conditions (214). *)
  179. PROCEDURE RelCheck (l, r: TypeIndex; op: INTEGER): BOOLEAN;
  180. (* = # <> < <= > >= IN (213/222 chosen by caller via op). *)
  181. PROCEDURE EqCheck (l, r: TypeIndex): BOOLEAN;
  182. (* = # compatibility, also reused for CASE label matching. *)
  183. PROCEDURE InCheck (l, set: TypeIndex): BOOLEAN;
  184. PROCEDURE SetElemCheck (first, elem: TypeIndex): BOOLEAN;
  185. PROCEDURE SetFor (elem: TypeIndex): TypeIndex;
  186. (* Fresh SET OF elem descriptor for set literals. *)
  187. PROCEDURE StrLen (s: ARRAY OF CHAR): CARDINAL;
  188. PROCEDURE ConstInt (q: ARRAY OF CHAR; VAR v: INTEGER): BOOLEAN;
  189. (* Parses a folded integer immediate ("42", "-5", "16") as produced
  190. by NormInt/NegFold. FALSE for temporaries and non-numerics. *)
  191. END SymTab.