; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.

; Various data structures used by the compiler, module system, etc.

; Type system

(define-structure meta-types meta-types-interface
  (open scheme-level-2
	define-record-types tables bitwise
	features ;make-immutable!
	util low-exceptions)
  (files (bcomp mtype))
  (declare no-state no-syntax)
  (optimize auto-integrate))

; Bindings

(define-structure bindings bindings-interface
  (open scheme-level-2
	define-record-types
	meta-types
	locations)
  (files (bcomp binding))
  (declare no-state no-syntax)
  (optimize auto-integrate))

; Syntax objects

(define-structure syntax-object-utils syntax-object-utils-interface
  (open scheme-level-2
	low-exceptions
	nodes
	syntax-objects 
	(subset syntax-objects-internal (make-syntax-object)))
  (declare no-state no-syntax)
  (files (bcomp syntax-object-util)))
	
; Names & Transforms

(define-structures ((names names-interface)
		    (transforms transforms-interface))
  (open scheme-level-2
	define-record-types tables
	proposals
	low-exceptions
	meta-types	;sexp->type
	bindings	;same-denotation?
	features	;make-immutable! string-hash
	syntax-objects syntax-objects-internal
	compiler-envs)
  (files (bcomp name)
	 (bcomp transform))
  (declare no-state no-syntax)
  (optimize auto-integrate))

(define-structure name+phase-tables name+phase-tables-interface
  (open scheme-level-2
	util
	tables)
  (files (bcomp name-phase-table))
  (declare no-state no-syntax)
  (optimize auto-integrate))

(define-structure thingies thingies-interface
  (open scheme-level-2 define-record-types)
  (optimize auto-integrate)
  (declare no-state no-syntax)
  (files (bcomp thingie)))

; Nodes

(define-structure compiler-envs compiler-envs-interface
  (open scheme-level-2 define-record-types
	meta-types bindings
	(subset util (filter)))
  (files (bcomp cenv))
  (declare no-state no-syntax)
  (optimize auto-integrate))

(define-structure nodes nodes-interface
  (open scheme-level-2
	meta-types 
	names qualified-names
	packages packages-internal
	compiler-envs bindings transforms
	low-exceptions define-record-types tables
	util)
  (files (bcomp node)
	 (bcomp schemify))
  (declare no-state no-syntax)
  (optimize auto-integrate))

;--------------------------------
; Macros

(define-structure syntax-resolution syntax-resolution-interface
  (open scheme-level-2
	tables cells fluids
	syntax-objects syntax-objects-internal
	bindings
	compiler-envs
	packages
	(subset packages-internal (env-phase)))
  (files (bcomp syntax-resolve))
  (declare no-state no-syntax))
  
(define-structure syntax-expand syntax-expand-interface
  (open scheme-level-2 
	(subset util (receive fold fold->2))
	tables cells fluids
	syms
	transforms
	locations
	compiler-envs
	low-exceptions
	syntax-objects syntax-objects-internal
	syntax-object-utils
	syntax-resolution
	syntax-transformers
	(subset meta-types (syntax-type usual-variable-type))
	nodes
	packages
	(subset packages-internal (env-phase))
	bindings)
  (files (bcomp syntax-expand)
	 (bcomp syntax-parse)))

(define-structure usual-macros usual-macros-interface
  (open scheme-level-2
	packages 
	(subset packages-internal (package-usual-transforms))
	meta-types
	locations
	transforms
	low-exceptions
	(subset util (receive filter every first))
	syntax-rules-data
	syntax-objects syntax-objects-internal
	syntax-object-utils
	syntax-resolution
	(subset syntax-expand (syntax-local-introduce
			       syntax-local-value
			       syntax-local-bind-syntaxes
			       local-expand
			       syntax-local-make-definition-context))
	tables)
  (declare no-state no-syntax)
  (files (bcomp usual)
	 (bcomp syntax-rules-compiler)))

; Little utilities to be phased out by changing the format of lambda var lists
; in nodes.

(define-structure var-utilities (export n-ary?
					normalize-formals
					number-of-required-args)
  (open scheme-level-2)
  (declare no-state no-syntax)
  (files (bcomp var-util))) ; can't use (BEGIN ...) because this is flatloaded

;--------------------------------
; Byte-code compiler

; Lexical environment layout info for debugging

(define-structures ((debug-data debug-data-interface)
		    (debug-data-internal debug-data-internal-interface))
  (open scheme-level-2 
	define-record-types
	tables
	fluids
	record-types		;for debug-flags randomness
	features)		;make-immutable!
  (files (bcomp ddata)
	 (bcomp state))
  (declare no-state no-syntax)
  (optimize auto-integrate))

; Determining stack usage.  No longer used.
;
;(define-structure stack-check (export maximum-stack-use)
;  (open scheme-level-2 architecture code-vectors low-exceptions)
;  (files (bcomp stack-check))
;  (optimize auto-integrate))

; Compiler back end

(define-structure segments segments-interface
  (open scheme-level-2 util tables low-exceptions fluids
	define-record-types
	bitwise vm-data
	code-vectors
	templates
	architecture
	features		;make-immutable!
	debug-data debug-data-internal
	frames)
  (files (bcomp segment))
  (declare no-state no-syntax)
  (optimize auto-integrate))

; Primops

(define-structure primops primops-interface
  (open scheme-level-2 tables define-record-types
	meta-types
	low-exceptions)
  (files (bcomp primop))
  (declare no-state no-syntax)
  (optimize auto-integrate))

; Type reconstruction.

(define-structure reconstruction (export node-type reconstruct-type)
  (open scheme-level-2 tables
	meta-types nodes names bindings
	primops
	var-utilities		;n-ary?
	util			;last
	low-exceptions)
  (declare no-state no-syntax)
  (files (bcomp recon)))

; The compiler itself.

(define-structures ((compiler compiler-interface)
		    (bc-generation bc-generation-interface))
  (open scheme-level-2 util low-exceptions
	features		;force-output
	enumerated		;enumerand->name
	ascii
	architecture
	meta-types names bindings
	transforms
	nodes var-utilities
	primops
	segments
	(subset templates (template?))
	debug-data-internal	; keep-source-code?
	flat-environments
	frames
	reconstruction)
  (files (bcomp comp-exp)
	 (bcomp comp-lambda)
	 (bcomp comp-prim)
	 (bcomp comp))
  (declare no-state no-syntax)
  (optimize auto-integrate))

(define-structure frames frames-interface
  (open scheme-level-2
	define-record-types
	names
	architecture			; two-byte-limit
	templates			; template-overhead
	debug-data-internal		; new-debug-data
	low-exceptions			; error
	thingies)
  (files (bcomp frame))
  (declare no-state no-syntax)
  (optimize auto-integrate))

;----------------
; Reading the forms in a file.
; This is used by scan-package and rts/eval.scm.

(define-structure reading-forms (export read-forms $note-file-package)
  (open scheme-level-2
	fluids filenames cells
	features		;current-noise-port force-output
	low-exceptions          ;error
	(subset packages-internal (package-reader))
	)
  (declare no-state no-syntax)
  (files (bcomp read-form)))

(define-structure vanilla-reader (export vanilla-read-forms)
  (open scheme-level-2
	(subset syntax-objects (in-package-context)))
  (declare no-state no-syntax)
  (files (bcomp vanilla-reader)))

;----------------
; Live-variable analysis for closures.

(define-structure flat-environments (export flatten-form)
  (open scheme-level-2 nodes low-exceptions
	optimizer primops
	util			;every
	var-utilities)
  (declare no-state no-syntax)
  (files (bcomp flatten)))

;----------------
; Module system

(define-structure interfaces interfaces-interface
  (open scheme-level-2
	define-record-types
	name+phase-tables
	util			;filter every receive symbol-append
	low-exceptions		;error
	weak			;populations
	meta-types)
  (files (bcomp interface))
  (declare no-state no-syntax)
  (optimize auto-integrate))

(define-structures ((packages packages-interface)
		    (packages-internal packages-internal-interface))
  (open scheme-level-2
	define-record-types tables fluids low-exceptions cells
	util features locations weak
        meta-types interfaces
	names transforms
	vanilla-reader
	syntax-objects syntax-objects-internal
	bindings
	name+phase-tables
	compiler-envs
	templates)
  (files (bcomp package))
  (declare no-state no-syntax)
  (optimize auto-integrate))

(define-structures ((dynamic-linker dynamic-linker-interface)
		    (undefined undefined-interface))
  (open scheme-level-2
	util
	fluids
	low-exceptions
	meta-types
	interfaces
	names qualified-names 
	syntax-objects syntax-objects-internal
	bindings
	packages packages-internal
	compiler-envs
	tables
	locations
	templates thingies
	usual-macros)
  (declare no-state no-syntax)
  (files (bcomp package-undef)))

(define-structure qualified-names qualified-names-interface
  (open scheme-level-2
	low-exceptions
	locations
	names transforms
	syntax-objects syntax-objects-internal
	packages packages-internal
	compiler-envs
	bindings)
  (files (bcomp qualified))
  (declare no-state no-syntax)
  (optimize auto-integrate))

(define-structure scan-package scan-package-interface
  (open scheme-level-2 util
	packages packages-internal
	locations
	meta-types bindings
	compiler-envs
	reading-forms
	filenames
	low-exceptions
	features		;current-noise-port force-output
	)
  (declare no-state no-syntax)
  (files (bcomp scan-package)))

(define-structure optimizer optimizer-interface
  (open scheme-level-2
	low-exceptions tables
	util)
  (files (bcomp optimize)))

(define-structure compile-packages compile-packages-interface
  (open scheme-level-2 util tables
	syntax-expand
	packages
	packages-internal	;package-name
	optimizer
	compiler
	primops			;walk-primops
	compiler-envs
	nodes
	scan-package
	usual-macros		;for usual-transforms
	transforms		;for usual-transforms
	locations
	meta-types		;for usual-transforms and define-all-operators
	(subset weak (walk-population)))
  (declare no-state no-syntax)
  (files (bcomp comp-package)))

;----------------
; DEFINE-STRUCTURE and friends

(define-structure defpackage defpackage-interface
  (open scheme-level-2
	packages 
	(subset packages-internal (set-package-reader!))
	usual-macros types
	interfaces
	source-file-names	;%file-name%
	low-exceptions		;error
	tables)
  (for-syntax (open scheme))
  (declare no-state)
  (files (bcomp module-language)
	 (bcomp config)))

(define-structure types types-interface  ;Typing language
  (open scheme-level-2 meta-types syntax-expand loopholes)
  (files (bcomp type))
  (declare no-state no-syntax)
  ;; (optimize auto-integrate)  - doesn't work
  )

(define-structure module-system (compound-interface defpackage-interface
						    types-interface)
  (declare no-state)
  (open defpackage types))

(define-structure copy-packages copy-packages-interface
  (open scheme-level-2
	low-exceptions
	packages packages-internal
	dynamic-linker
	qualified-names
	names bindings
	transforms
	usual-macros
	inline)
  (declare no-state no-syntax)
  (files (bcomp copy-package)))

;----------------
; Code analysis and inlining

(define-structure usages usages-interface
  (open scheme-level-2
	meta-types names nodes
	packages
	packages-internal	;package-refine-type!
	reconstruction
	var-utilities
	define-record-types
	util low-exceptions tables strong)
  (declare no-state no-syntax)
  (files (opt usage)
         (opt sort)))

(define-structure analysis (export analyze-forms)
  (open scheme-level-2
	meta-types bindings nodes primops
	packages-internal	;package-add-static!
	inline
	usages
	reconstruction
	var-utilities
	transforms
	syntax-expand               ;static-value
	packages
	low-exceptions
	features		;force-output
	optimizer		;set-optimizer!
	util)
  (declare no-state no-syntax)
  (files (opt analyze)))

(define-structure inline inline-interface
  (open scheme-level-2 util var-utilities
	syms
	meta-types names qualified-names bindings locations nodes
	compiler-envs
	transforms
	packages
	usages
	low-exceptions)
  (declare no-state no-syntax)
  (files (opt inline)))

(define-structure strong (export strongly-connected-components)
  (open scheme-level-2 define-record-types low-exceptions)
  (declare no-state no-syntax)
  (files (big strong))) ;!


;----------------
; Two basic structures needed to support the compiler.

(define-structure tables general-tables-interface
  (open scheme-level-1
	define-record-types
	low-exceptions
	features)    ; string-hash, make-immutable!
  (declare no-state no-syntax)
  (files (big general-table))
  (optimize auto-integrate))

(define-structure filenames filenames-interface
  (open scheme-level-1 low-exceptions fluids cells)
  (declare no-state no-syntax)
  (files (big filename)))
