-- SourceBcd.mesa
-- last edited by JGS on 17-Sep-82 14:04:28
-- last edited by Satterthwaite, December 30, 1982 9:56 am


DIRECTORY
  BcdDefs: TYPE USING [CTIndex, MTIndex, NameRecord],
  BcdOps: TYPE USING [BcdBase],
  BcdUtilDefs: TYPE USING [BcdBasePtr, BcdLimitPtr],
  HashTypes: TYPE USING [HTIndex],
  String: TYPE USING [SubString],
  Table: TYPE USING [Base, Limit];

SourceBcd: DEFINITIONS = {

  BadSourceBcd: ERROR;  -- obsolete, already repackaged, or compiled for Alto

 -- ************************* BCD Table Information *************************

  bcdHeader: READONLY BcdOps.BcdBase;

  bcdBases: READONLY BcdUtilDefs.BcdBasePtr;
  bcdLimits: READONLY BcdUtilDefs.BcdLimitPtr;

  moduleCount: READONLY CARDINAL;

  ModuleNum: TYPE = CARDINAL;
  nullModuleNum: ModuleNum = CARDINAL.LAST;


  -- Interface
  Load, Unload: PROC;

  EnumerateConfigs: PROC [
    userProc: PROC [BcdDefs.CTIndex] RETURNS [stop: BOOL]];
  EnumerateModules: PROC [
    userProc: PROC [BcdDefs.MTIndex] RETURNS [stop: BOOL]];

  IsTableCompiled: PROC [mti: BcdDefs.MTIndex] RETURNS [BOOL];

  ModuleNumForMti: PROC [      -- map i-th module index to i
    mti: BcdDefs.MTIndex] RETURNS [ModuleNum];

  SubStringForName: PROC [ss: String.SubString, name: BcdDefs.NameRecord];

  EqualIdAndName: PROC [id: HashTypes.HTIndex, name: BcdDefs.NameRecord] 
    RETURNS [equal: BOOL];
    


 -- *************************** Configuration Tree ***************************


  CTreeIndex: TYPE = Table.Base RELATIVE POINTER[0..Table.Limit) TO ConfigTreeNode;
  nullCTreeIndex: CTreeIndex = CTreeIndex.LAST;
    
  ConfigTreeNode: TYPE;

  ComponentKind: TYPE = {instance, prototype};
 
  BcdTableLoc: TYPE = RECORD [
    SELECT kind: * FROM
      config => [cti: BcdDefs.CTIndex],
      module => [mti: BcdDefs.MTIndex],
      ENDCASE];

 -- Attributes
  
  Father: PROC [self: CTreeIndex] RETURNS [CTreeIndex];
  EnumerateSons: PROC [
    self: CTreeIndex, userProc: PROC [CTreeIndex] RETURNS [stop: BOOL]];
  
  Kind: PROC [self: CTreeIndex] RETURNS [ComponentKind];
  Name: PROC [self: CTreeIndex, kind: ComponentKind] RETURNS [BcdDefs.NameRecord];
  SharedProtoName: PROC [self: CTreeIndex] RETURNS [BOOL];
  Link: PROC [self: CTreeIndex, kind: ComponentKind] RETURNS [CTreeIndex];
  Prev: PROC [self: CTreeIndex, kind: ComponentKind] RETURNS [CTreeIndex];
  
  Index: PROC [self: CTreeIndex] RETURNS [BcdTableLoc];

 -- Tree Building
 
  BuildConfigTree: PROC RETURNS [root: CTreeIndex];
  DestroyConfigTree: PROC [root: CTreeIndex];

 -- Tree Traversal
 
  EnumerateModulesInConfig: PROC [
    configTreeNode: CTreeIndex, 
    kind: ComponentKind,  -- if prototype, no duplications appear in output 
    userProc: PROC [mti: BcdDefs.MTIndex] RETURNS [stop: BOOL]];

  FindModuleOrConfig: PROC [
      kind: ComponentKind, 
      ResetIdStream: PROC,
      FirstQualId, NextQualId: PROC RETURNS [id: HashTypes.HTIndex]] 
    RETURNS [component: CTreeIndex];

  IsModuleInConfig: PROC [
      kind: ComponentKind, 
      mti: BcdDefs.MTIndex,
      configTreeNode: CTreeIndex] 
    RETURNS [BOOL];

  -- find first node with given instance/prototype id   
  -- (others with same id are found along first node's prev chain)   
  LookupId: PROC [id: HashTypes.HTIndex, kind: ComponentKind] 
    RETURNS [firstTreeLoc: CTreeIndex];

  LookupName: PROC [name: BcdDefs.NameRecord, kind: ComponentKind] 
    RETURNS [firstTreeLoc: CTreeIndex];

  LookupSS: PROC [idSS: String.SubString, kind: ComponentKind] 
    RETURNS [firstTreeLoc: CTreeIndex];

  }.