ClassCategoryReader.st
author Claus Gittinger <cg@exept.de>
Thu, 25 Apr 1996 19:02:53 +0200
changeset 1295 83f594f05c52
parent 1176 1c53fc7a69c0
child 1565 63876d73db7d
permissions -rw-r--r--
documentation

"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Object subclass:#ClassCategoryReader
	instanceVariableNames:'myClass myCategory privacy ignore primSpec'
	classVariableNames:'KeepSource SourceMode'
	poolDictionaries:''
	category:'Kernel-Support'
!

!ClassCategoryReader class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    a helper class for fileIn - keeps track of class and category to filein for.
    Instances of this are created by the #methodsFor: methods in Class, to
    read the next chunk(s) from a stream.

    ClassCategoryReaders allow different ways of keeping the sourceCode of its
    loaded code. This is controlled by the SourceMode class variable which may be:

        #discard         - forget the source code; no browsing/recompilation is possible

        #keep            - keep the source code as a string.
                           any saved image will later be fully independent of any
                           source files.

        #reference       - keep a reference to the loaded files basename
                           In order to be browsable, the original sourcefile should be
                           found along the sourcePath 
                           (i.e. typically a link in the source directory should be present)

        #absReference    - keep a reference to the loaded files absolute pathname
                           (this is ok in multiUser configurations, where all sourcefiles are mounted
                           on a common path; typically automounted systems)

        #sourceReference - append source to the `st.src' file,
                           and keep a reference to that file.
                           if the image is later moved to another location,
                           this file should be moved along with it.

    [author:]
        Claus Gittinger

    [see also:]
        Class PositionableStream
"
! !

!ClassCategoryReader class methodsFor:'initialization'!

initialize
    KeepSource := true.
    SourceMode := #keep.

    "
     ClassCategoryReader initialize
    "

    "Modified: 9.2.1996 / 17:22:57 / cg"
! !

!ClassCategoryReader class methodsFor:'instance creation'!

class:aClass category:aCategory
    "return a new ClassCategoryReader to read methods for aClass with
     methodCategory aCategory"

    ^ self new class:aClass category:aCategory
!

class:aClass primitiveSpec:which
    "return a ClassCategoryReader to read a primitiveSpec chunk"

    ^ self new class:aClass primitiveSpec:which
! !

!ClassCategoryReader class methodsFor:'defaults'!

keepSource
    ^ KeepSource

    "Created: 9.9.1995 / 15:22:27 / claus"
!

keepSource:aBoolean
    KeepSource := aBoolean.
    SourceMode := (aBoolean ifTrue:[#keep] ifFalse:[#reference])

    "Created: 9.9.1995 / 15:22:26 / claus"
    "Modified: 9.2.1996 / 17:23:04 / cg"
!

sourceMode 
    "return the sourceMode, which controls how sources are to be handled.
     Read #documentation for more info"

    ^ SourceMode

    "Created: 9.2.1996 / 17:22:58 / cg"
!

sourceMode:aSymbol 
    "set the sourceMode, which controls how sources are to be handled.
     Read #documentation for more info"

    SourceMode := aSymbol

    "Created: 9.2.1996 / 17:23:00 / cg"
! !

!ClassCategoryReader methodsFor:'fileIn'!

fileInFrom:aStream
    "read method-chunks from the input stream, aStream; compile them
     and add the methods to the class defined by the class-instance var"

    self fileInFrom:aStream notifying:nil passChunk:false
!

fileInFrom:aStream notifying:requestor passChunk:passChunk
    "read method-chunks from the input stream, aStream; compile them
     and add the methods to the class defined by the class-instance var;
     errors and notifications are passed to requestor"

    |aString done method compiler canMakeSourceRef sourceFile pos nm src s|

    Smalltalk silentLoading ifFalse:[
        myClass isNil ifTrue:[
            nm := '** UndefinedClass **'
        ] ifFalse:[
            nm := myClass name
        ].
        Transcript show:'  '; show:nm; show:' -> '; show:myCategory.
        ignore ifTrue:[
            Transcript show:' (** ignored **)'.
        ] ifFalse:[
            privacy notNil ifTrue:[
                Transcript show:' (** '; show:privacy; show:' **)'
            ]
        ].
        Transcript cr.
    ].

    canMakeSourceRef := false.

    "/
    "/ KeepSource controls if
    "/ the sourceString should be kept or
    "/ a reference to the fileStream should be placed into the
    "/ method intead.
    "/
    ((SourceMode ~~ #keep) 
    and:[SourceMode ~~ #discard]) ifTrue:[
        aStream isFileStream ifTrue:[
            sourceFile := aStream pathName.
            "/
            "/ only do it, if the sourceFiles name
            "/ ends with '.st'
            "/ this prevents methods to reference the changes file.
            "/
            (sourceFile endsWith:'.st') ifTrue:[
                canMakeSourceRef := true.

                SourceMode == #absReference ifFalse:[
                    SourceMode == #sourceReference ifTrue:[
                        sourceFile := 'st.src'.
                    ] ifFalse:[
                        sourceFile := aStream pathName asFilename baseName
                    ]
                ]
            ]
        ]
    ].

    done := false.
    [done] whileFalse:[
        done := aStream atEnd.
        done ifFalse:[
            canMakeSourceRef ifTrue:[
                pos := aStream position
            ].
            aString := aStream nextChunk.
            done := aString isNil or:[aString isEmpty].
            done ifFalse:[
                primSpec notNil ifTrue:[
                    ignore ifFalse:[
                        myClass perform:primSpec with:aString.
                        "
                         ignore rest
                        "
                        ignore := true
                    ]
                ] ifFalse:[
                    passChunk ifTrue:[
                        requestor source:aString
                    ].

                    compiler := myClass compilerClass.

                    "/
                    "/ kludge - for now;
                    "/ have to make ST/X's compiler protocol be compatible to ST-80's
                    "/ for other compilers to work ... (TGEN for example)
                    "/
                    (compiler respondsTo:#compile:forClass:inCategory:notifying:install:skipIfSame:)
                    ifTrue:[
                        "/ ST/X's compiler
                        method :=compiler
                                     compile:aString
                                     forClass:myClass
                                     inCategory:myCategory
                                     notifying:requestor
                                     install:true
                                     skipIfSame:true.

                        (method notNil and:[method ~~ #Error]) ifTrue:[
                            canMakeSourceRef ifTrue:[
                                (src := method getSource) = aString ifTrue:[
                                    "/ it was not corrected ...

                                    SourceMode == #sourceReference ifTrue:[
                                        s := sourceFile asFilename appendingWriteStream.
                                        s setToEnd.     
                                        pos := s position.
                                        s nextChunkPut:src.
                                        s close.
                                    ].
                                    method sourceFilename:sourceFile position:pos 
                                ]
                            ] ifFalse:[
                                SourceMode == #discard ifTrue:[
                                    method sourceFilename:nil position:nil
                                ]
                            ]
                        ]
                    ] ifFalse:[
                        "/ some generated (TGEN) compiler
                        method := compiler new
                                      compile:aString 
                                      in:myClass 
                                      notifying:requestor 
                                      ifFail:nil
                    ].

                    ignore ifTrue:[
                        method setToIgnored
                    ] ifFalse:[
                        privacy notNil ifTrue:[
                            privacy == #private ifTrue:[
                                method setToPrivate
                            ] ifFalse:[
                                privacy == #protected ifTrue:[
                                    method setToProtected
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]

    "Modified: 9.9.1995 / 15:29:08 / claus"
    "Modified: 10.2.1996 / 13:24:04 / cg"
! !

!ClassCategoryReader methodsFor:'private'!

class:aClass category:aCategory
    "set the instance variables"

    myClass := aClass.
    myCategory := aCategory.
    ignore := false
!

class:aClass primitiveSpec:which
    "set the instance variables"

    myClass := aClass.
    primSpec := which.
    ignore := false
! !

!ClassCategoryReader methodsFor:'special'!

ignoredProtocol 
    ignore := true

    "Created: 10.2.1996 / 12:54:15 / cg"
!

privateProtocol
    privacy := #private
!

protectedProtocol
    privacy := #protected
! !

!ClassCategoryReader class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/ClassCategoryReader.st,v 1.30 1996-04-25 16:57:06 cg Exp $'
! !
ClassCategoryReader initialize!