Skip to content

Commit

Permalink
Remove special behaviour of ENUMs inside EXTERN blocks
Browse files Browse the repository at this point in the history
This behaviour was added in 468769a, but I think it's better to keep Enums
behave the same no matter whether inside Extern blocks or not, just like
any other constructs.
  • Loading branch information
dkl committed Jul 18, 2013
1 parent 9825ffd commit e438f7f
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 104 deletions.
1 change: 1 addition & 0 deletions changelog.txt
@@ -1,6 +1,7 @@
Version 0.91.0:

[changed]
- Enums declared inside Extern blocks now behave like ones declared outside: The Enum's constants don't collide with previously declared global constants, instead they can be accessed through "EnumName.MyConstant".

[added]
- Updated header for libzip 0.11.1
Expand Down
1 change: 0 additions & 1 deletion src/compiler/error.bas
Expand Up @@ -321,7 +321,6 @@ declare function hMakeParamDesc _
@"Illegal outside a FUNCTION block", _
@"Ambiguous symbol access, explicit scope resolution required", _
@"An ENUM, TYPE or UNION cannot be empty", _
@"ENUM's declared inside EXTERN .. END EXTERN blocks don't open new scopes", _
@"STATIC used on non-member procedure", _
@"CONST used on non-member procedure", _
@"ABSTRACT used on non-member procedure", _
Expand Down
1 change: 0 additions & 1 deletion src/compiler/error.bi
Expand Up @@ -243,7 +243,6 @@ enum FB_ERRMSG
FB_ERRMSG_ILLEGALOUTSIDEAFUNCTION
FB_ERRMSG_AMBIGUOUSSYMBOLACCESS
FB_ERRMSG_NOELEMENTSDEFINED
FB_ERRMSG_NONSCOPEDENUM
FB_ERRMSG_STATICNONMEMBERPROC
FB_ERRMSG_CONSTNONMEMBERPROC
FB_ERRMSG_ABSTRACTNONMEMBERPROC
Expand Down
36 changes: 10 additions & 26 deletions src/compiler/parser-decl-enum.bas
Expand Up @@ -180,29 +180,19 @@ sub cEnumDecl( byval attrib as integer )
isexplicit = TRUE
end if

''
'' Normally, Enums are namespaces containing their constants, and a
'' Using will automatically be done below to import the constants into
'' the parent namespace unless the Enum was declared Explicit.
'' Enums are namespaces containing their constants, and a Using will
'' automatically be done below to import the constants into the parent
'' namespace unless the Enum was declared Explicit.
''
'' This way, an Enum's constants can be accessed via "constid" or
'' "enumid.constid", and an Explicit Enum's constants can only be
'' accessed via the latter.
''
'' Non-Explicit Enums inside Extern blocks have special behaviour
'' though, they're not treated as separate namespaces. Instead, the
'' constants are added to the parent namespace directly. Access to them
'' via "enumid.constid" is disallowed (FB_ERRMSG_NONSCOPEDENUM).
''

dim as integer use_hashtb = (parser.mangling = FB_MANGLING_BASIC)
use_hashtb or= isexplicit

e = symbAddEnum( @id, palias, attrib, use_hashtb )
e = symbAddEnum( @id, palias, attrib )
if( e = NULL ) then
errReportEx( FB_ERRMSG_DUPDEFINITION, id )
'' error recovery: create a fake symbol
e = symbAddEnum( symbUniqueLabel( ), NULL, FB_SYMBATTRIB_NONE, use_hashtb )
e = symbAddEnum( symbUniqueLabel( ), NULL, FB_SYMBATTRIB_NONE )
end if

'' Comment? SttSeparator
Expand All @@ -217,18 +207,14 @@ sub cEnumDecl( byval attrib as integer )
hSkipUntil( INVALID, TRUE )
end if

'' if in BASIC mangling mode, start a new scope
if( use_hashtb ) then
symbNestBegin( e, FALSE )
end if
'' Start a new scope
symbNestBegin( e, FALSE )

'' EnumBody
cEnumBody( e, attrib )

'' close scope
if( use_hashtb ) then
symbNestEnd( FALSE )
end if
symbNestEnd( FALSE )

'' END ENUM
if( lexGetToken( ) <> FB_TK_END ) then
Expand All @@ -245,11 +231,9 @@ sub cEnumDecl( byval attrib as integer )
else
lexSkipToken( )

'' Do an implicit 'USING enum', unless it's an EXPLICIT enum
if( isexplicit = FALSE ) then
'' if in BASIC mangling mode, do an implicit 'USING enum'
if( use_hashtb ) then
symbNamespaceImport( e )
end if
symbNamespaceImport( e )
end if
end if
end if
Expand Down
16 changes: 0 additions & 16 deletions src/compiler/parser-identifier.bas
Expand Up @@ -252,15 +252,6 @@ function cIdentifier _
exit do
end if

if( symbIsEnum( sym ) ) then
if( symbEnumHasHashTb( sym ) = FALSE ) then
if( (options and FB_IDOPT_SHOWERROR) <> 0 ) then
errReport( FB_ERRMSG_NONSCOPEDENUM )
end if
exit do
end if
end if

'' skip id
lexSkipToken( LEXCHECK_NOPERIOD )

Expand Down Expand Up @@ -423,13 +414,6 @@ function cParentId _
exit do
end if

if( symbIsEnum( sym ) ) then
if( symbEnumHasHashTb( sym ) = FALSE ) then
errReport( FB_ERRMSG_NONSCOPEDENUM )
exit do
end if
end if

'' skip id
lexSkipToken( LEXCHECK_NOPERIOD )

Expand Down
15 changes: 3 additions & 12 deletions src/compiler/symb-enum.bas
Expand Up @@ -15,8 +15,7 @@ function symbAddEnum _
( _
byval id as zstring ptr, _
byval id_alias as zstring ptr, _
byval attrib as integer, _
byval use_hashtb as integer _
byval attrib as integer _
) as FBSYMBOL ptr

dim as FBSYMBOL ptr e = any
Expand All @@ -41,15 +40,7 @@ function symbAddEnum _

'' init tables
symbSymbTbInit( e->enum_.ns.symtb, e )

'' Create a new hash if in BASIC mangling mode or if Explicit, otherwise
'' the hashtb will be unused and there's no point allocating one.
'' (check via symbEnumHasHashTb() later)
if( use_hashtb ) then
symbHashTbInit( e->enum_.ns.hashtb, e, FB_INITFIELDNODES )
else
symbHashTbInit( e->enum_.ns.hashtb, e, 0 )
end if
symbHashTbInit( e->enum_.ns.hashtb, e, FB_INITFIELDNODES )

'' unused (while mixins aren't supported)
e->enum_.ns.ext = NULL
Expand Down Expand Up @@ -97,7 +88,7 @@ function symbAddEnumElement _
end function

sub symbDelEnum( byval s as FBSYMBOL ptr )
symbDelNamespaceMembers( s, symbEnumHasHashTb( s ) )
symbDelNamespaceMembers( s, TRUE )
symbFreeSymbol( s )
end sub

Expand Down
4 changes: 0 additions & 4 deletions src/compiler/symb.bas
Expand Up @@ -1001,10 +1001,6 @@ function symbLookupAt _

assert( symbIsStruct( ns ) or symbIsNamespace( ns ) or symbIsEnum( ns ) )

if( symbIsEnum( ns ) and (not symbEnumHasHashTb( ns )) ) then
exit function
end if

if( preserve_case = FALSE ) then
hUcase( *id, sname )
id = @sname
Expand Down
47 changes: 6 additions & 41 deletions src/compiler/symb.bi
Expand Up @@ -1143,8 +1143,7 @@ declare function symbAddEnum _
( _
byval id as zstring ptr, _
byval id_alias as zstring ptr, _
byval attrib as integer, _
byval use_hashtb as integer _
byval attrib as integer _
) as FBSYMBOL ptr

declare function symbAddEnumElement _
Expand Down Expand Up @@ -1807,60 +1806,27 @@ declare function symbGetUDTBaseLevel _
'' macros
''

'':::::
#macro symbHashTbInit _
( _
_hashtb, _
_owner, _
_nodes _
)

#macro symbHashTbInit( _hashtb, _owner, _nodes )
_hashtb.owner = _owner
_hashtb.prev = NULL
_hashtb.next = NULL

if( (_nodes) <> 0 ) then
hashInit( @_hashtb.tb, _nodes )
else
clear( _hashtb.tb, 0, sizeof( _hashtb.tb ) )
end if

hashInit( @_hashtb.tb, _nodes )
#endmacro

'':::::
#macro symbSymbTbInit _
( _
_symtb, _
_owner _
)

#macro symbSymbTbInit( _symtb, _owner )
_symtb.owner = _owner
_symtb.head = NULL
_symtb.tail = NULL

#endmacro

'':::::
#macro symbHashListMove _
( _
hashtb _
)

#macro symbHashListMove( hashtb )
symbHashListDel( hashtb )
symbHashListAdd( hashtb )

#endmacro

'':::::
#macro symbHashListMoveBefore _
( _
lasttb, _
hashtb _
)

#macro symbHashListMoveBefore( lasttb, hashtb )
symbHashListDel( hashtb )
symbHashListAddBefore( lasttb, hashtb )

#endmacro

#define symbGetGlobalNamespc( ) symb.globnspc
Expand Down Expand Up @@ -2174,7 +2140,6 @@ declare function symbGetUDTBaseLevel _

#define symbGetEnumSymbTbHead(s) s->enum_.ns.symtb.head
#define symbGetEnumElements(s) s->enum_.elements
#define symbEnumHasHashTb( s ) ((s)->enum_.ns.hashtb.tb.nodes <> 0)

#define symbGetScope(s) s->scope

Expand Down
6 changes: 3 additions & 3 deletions tests/namespace/enum.bas
Expand Up @@ -56,7 +56,7 @@ end enum
extern "c++"
enum Enum3
Enum3Const1 = 1
''GlobalConst3
GlobalConst3
end enum

enum Enum4 explicit
Expand All @@ -81,8 +81,8 @@ private sub test5 cdecl( )

CU_ASSERT( Enum3Const1 = 1 )
CU_ASSERT( GlobalConst3 = 333 )
''CU_ASSERT( Enum3.Enum3Const1 = 1 )
''CU_ASSERT( Enum3.GlobalConst3 = 2 )
CU_ASSERT( Enum3.Enum3Const1 = 1 )
CU_ASSERT( Enum3.GlobalConst3 = 2 )

CU_ASSERT( GlobalConst4 = 444 )
CU_ASSERT( Enum4.Enum4Const1 = 1 )
Expand Down

0 comments on commit e438f7f

Please sign in to comment.