-- file P3.Mesa
-- last modified by Satterthwaite, December 17, 1979  1:44 PM

DIRECTORY
  Symbols: FROM "symbols"
    USING [
      HTIndex, SEIndex, ISEIndex, CSEIndex, RecordSEIndex,
      CTXIndex, IncludedCTXIndex, BTIndex, ContextLevel],
  Table: FROM "table" USING [Notifier],
  Tree: FROM "tree" USING [Index, Link, Map, Scan];

P3: DEFINITIONS =
  BEGIN
  OPEN Symbols;

  Mark: BOOLEAN = TRUE;		-- mark in tree as set by pass 3

  CircuitCheck: TYPE = SIGNAL [loopNode: Tree.Index] RETURNS [BOOLEAN];
  CircuitSignal: TYPE = SIGNAL [loopNode: Tree.Index];

 -- attributes

  Attr: TYPE = RECORD [
    noAssign: BOOLEAN,
    noXfer: BOOLEAN,
    const: BOOLEAN];

  EmptyAttr: Attr = [noAssign:FALSE, noXfer:FALSE, const:FALSE];
  FullAttr: Attr = [noAssign:TRUE, noXfer:TRUE, const:TRUE];
  VoidAttr: Attr = [noAssign:TRUE, noXfer:TRUE, const:FALSE];

 -- parameter use bookkeeping (for subst by name)

  NPUse: TYPE = {none, ref, set, refset, unsafe};	-- (name) param use
  NPMap: TYPE = ARRAY NPUse OF ARRAY NPUse OF NPUse;

  SetNP: ARRAY NPUse OF NPUse = [set, refset, set, refset, unsafe];

  MergeNP: NPMap =
    [[  none,    ref,    set, refset, unsafe],
     [   ref,    ref, unsafe, unsafe, unsafe],
     [   set, unsafe,    set, unsafe, unsafe],
     [refset, unsafe, unsafe, unsafe, unsafe],
     [unsafe, unsafe, unsafe, unsafe, unsafe]];

  SequenceNP: NPMap =
    [[  none,    ref,    set, refset, unsafe],
     [   ref,    ref, refset, refset, unsafe],
     [   set, unsafe,    set, unsafe, unsafe],
     [refset, unsafe, refset, unsafe, unsafe],
     [unsafe, unsafe, unsafe, unsafe, unsafe]];

  BoundNP: NPMap =
    [[  none,    ref,    set, refset, unsafe],
     [   ref,    ref, unsafe, refset, unsafe],
     [   set, unsafe,    set, refset, unsafe],
     [refset, refset, refset, refset, unsafe],
     [unsafe, unsafe, unsafe, unsafe, unsafe]];


 -- state info

  BodyData: TYPE = RECORD[	-- shared communication record
    level: Symbols.ContextLevel,	-- current level
    bodyNode: Tree.Index,		-- current body
    inputRecord: RecordSEIndex,		-- input record for current body
    returnRecord: RecordSEIndex,	-- return record for current body
    entry: BOOLEAN,			-- set for entry procedures

    reachable: BOOLEAN,
    labelList: Tree.Link,		-- list of accessible labels
    loopDepth: CARDINAL,		-- depth of loop nesting

    catchDepth: CARDINAL,		-- depth of catch phrase nesting
    unwindEnabled: BOOLEAN,		-- set iff in scope of unwind
    resumeFlag: BOOLEAN,		-- set iff a resume is legal
    resumeRecord: RecordSEIndex];	-- for current catch phrase

 -- notifiers

  DeclNotify, IdNotify, MiscNotify, StmtNotify: Table.Notifier;
  ExpANotify, ExpBNotify: Table.Notifier;

 -- exported by Pass3B

  Directory: Tree.Scan;
  ModulePrefix: Tree.Scan;

 -- exported by Pass3D

  Bundling: PROCEDURE [CSEIndex] RETURNS [CARDINAL];
  CanonicalType: PROCEDURE [CSEIndex] RETURNS [CSEIndex];
  DeclList: Tree.Scan;
  DefaultInit: PROCEDURE [SEIndex] RETURNS [Tree.Link];
  IdentifiedType: PROCEDURE [CSEIndex] RETURNS [BOOLEAN];
  MakeLongType: PROCEDURE [SEIndex, CSEIndex] RETURNS [CSEIndex];
  MakePointerType: PROCEDURE [cType: SEIndex, hint: CSEIndex, readOnly: BOOLEAN←FALSE]
    RETURNS [CSEIndex];
  OrderedType: PROCEDURE [SEIndex] RETURNS [BOOLEAN];
  ResolveType: PROCEDURE [ISEIndex];
  ResolveValue: PROCEDURE [ISEIndex];
  TargetType: PROCEDURE [CSEIndex] RETURNS [CSEIndex];
  TypeExp: Tree.Map;
  TypeForTree: PROCEDURE [Tree.Link] RETURNS [SEIndex];
  Unbundle: PROCEDURE [RecordSEIndex] RETURNS [CSEIndex];
  VoidItem: PROCEDURE [Tree.Link] RETURNS [BOOLEAN];
  Voidable: PROCEDURE [SEIndex] RETURNS [BOOLEAN];

  CheckTypeLoop: CircuitCheck;

 -- exported by Pass3I

  IdInit: PROCEDURE;
  IdFinish: Tree.Scan;

  ArrangeKeys: PROCEDURE [
	expList: Tree.Link,
	ctx: CTXIndex,
	startSei, endSei: ISEIndex,
	omittedKey: PROCEDURE [ISEIndex] RETURNS [Tree.Link]]
    RETURNS [CARDINAL];
  BaseTree: PROCEDURE [Tree.Link, CSEIndex] RETURNS [Tree.Link];
  BumpCount: PROCEDURE [ISEIndex];
  CheckDisjoint: PROCEDURE [ctx1, ctx2: CTXIndex];
  ClearRefStack: PROCEDURE;
  CloseBase: PROCEDURE [Tree.Link, HTIndex];
  CompleteRecord: PROCEDURE [RecordSEIndex];
  DefinedId: PROCEDURE [HTIndex, CSEIndex] RETURNS [BOOLEAN, ISEIndex];
  EnterIdList: PROCEDURE [IncludedCTXIndex, Tree.Link];
  FieldId: PROCEDURE [HTIndex, RecordSEIndex] RETURNS [CARDINAL, ISEIndex];
  FindSe: PROCEDURE [HTIndex] RETURNS [symbol: ISEIndex, base: Tree.Link, indirect: BOOLEAN];
  Id: PROCEDURE [HTIndex] RETURNS [Tree.Link];
  MakeIdTable: PROCEDURE [CARDINAL];
  OpenBase: PROCEDURE [Tree.Link, HTIndex] RETURNS [Tree.Link];
  OpenPointer: PROCEDURE [Tree.Link, CSEIndex] RETURNS [Tree.Link, CSEIndex];
  PopCtx: PROCEDURE;
  PushCtx: PROCEDURE [CTXIndex];
  PushHtCtx: PROCEDURE [HTIndex, Tree.Link, BOOLEAN];
  PushRecordCtx: PROCEDURE [RecordSEIndex, Tree.Link, BOOLEAN]; 
  RecordLhs: PROCEDURE [ISEIndex];
  RecordMention: PROCEDURE [ISEIndex];
  SealRefStack: PROCEDURE;
  SearchCtxList: PROCEDURE [HTIndex, CTXIndex] RETURNS [BOOLEAN, ISEIndex];
  TopCtx: PROCEDURE RETURNS [CTXIndex];
  UnsealRefStack: PROCEDURE;
  UpdateTreeAttr: PROCEDURE [Tree.Link] RETURNS [attr: Attr];

 -- exported by Pass3M
  
  CatchPhrase: PROCEDURE [Tree.Link] RETURNS [unwindCaught: BOOLEAN];
  CopyLock: PROCEDURE RETURNS [Tree.Link];
  FindLockParams: PROCEDURE RETURNS [formal, actual: ISEIndex];
  LockVar: PROCEDURE [Tree.Link] RETURNS [Tree.Link];
  MakeFrameRecord: PROCEDURE [Tree.Link] RETURNS [CSEIndex];
  MiscStmt: PROCEDURE [Tree.Index] RETURNS [Tree.Link];
  MiscXfer: PROCEDURE [Tree.Index, CSEIndex] RETURNS [Tree.Link];
  XferForFrame: PROCEDURE [CTXIndex] RETURNS [CSEIndex];

  InsertCatchLabel: SIGNAL [catchSeen, exit: BOOLEAN];

 -- exported by Pass3S
  
  currentArgCtx: CTXIndex;
  pathNP: NPUse;

  BodyList: PROCEDURE [firstBti: BTIndex];
  BumpArgRefs: PROCEDURE [record: RecordSEIndex, write: BOOLEAN];
  Case: PROCEDURE [Tree.Index, Tree.Map];
  CheckLocals: PROCEDURE [Tree.Link] RETURNS [localsOnly: BOOLEAN];
  Stmt: Tree.Map;

 -- exported by Pass3V

  DiscriminatedType: PROCEDURE [CSEIndex, Tree.Link] RETURNS [CSEIndex];
  Discrimination: PROCEDURE [Tree.Index, Tree.Map];
  SelectVariantType: PROCEDURE [SEIndex, HTIndex] RETURNS [ISEIndex];
  VariantUnionType: PROCEDURE [SEIndex] RETURNS [CSEIndex];

 -- exported by Pass3Xa

  phraseNP: NPUse;

  All: PROCEDURE [Tree.Index, CSEIndex];
  Apply: PROCEDURE [Tree.Index, CSEIndex, BOOLEAN];
  Assignment: PROCEDURE [Tree.Index];
  Dot: PROCEDURE [Tree.Index];
  Extract: PROCEDURE [Tree.Index];
  LongPath: PROCEDURE [Tree.Link] RETURNS [BOOLEAN];
  MatchFields: PROCEDURE [RecordSEIndex, Tree.Link, BOOLEAN] RETURNS [Tree.Link];
  OperandInline: PROCEDURE [Tree.Link] RETURNS [BOOLEAN];
  OperandInternal: PROCEDURE [Tree.Link] RETURNS [BOOLEAN];
  OperandLhs: PROCEDURE [Tree.Link] RETURNS [BOOLEAN];
  UpArrow: PROCEDURE [Tree.Index];

 -- exported by Pass3Xb

  ExpInit: PROCEDURE;

  Exp: PROCEDURE [Tree.Link, CSEIndex] RETURNS [Tree.Link];
  Interval: PROCEDURE [Tree.Link, CSEIndex, BOOLEAN];
  ForceType: PROCEDURE [Tree.Link, CSEIndex] RETURNS [Tree.Link];
  OperandType: PROCEDURE [Tree.Link] RETURNS [CSEIndex];
  Range: PROCEDURE [Tree.Link, CSEIndex] RETURNS [Tree.Link];
  RAttr: PROCEDURE RETURNS [Attr];
  Rhs: PROCEDURE [Tree.Link, CSEIndex] RETURNS [Tree.Link];
  RPop: PROCEDURE;
  RPush: PROCEDURE [CSEIndex, Attr];
  RType: PROCEDURE RETURNS [CSEIndex];
  VoidExp: Tree.Map;

 -- exported by Pass3Xc

  Addr: PROCEDURE [Tree.Index, CSEIndex];
  DescOp: PROCEDURE [Tree.Index, CSEIndex];
  Span: PROCEDURE [CSEIndex] RETURNS [first, last: ISEIndex];

 -- exported by Pass3P

  Postlude: PROCEDURE;

 -- the global frames

  Pass3B, Pass3D, Pass3I, Pass3M, Pass3S, Pass3V, Pass3P: PROGRAM;
  Pass3Xa, Pass3Xb, Pass3Xc: PROGRAM;

  END.