diff options
author | Adrian Thurston <thurston@complang.org> | 2015-06-14 17:42:43 -0400 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2015-06-14 17:42:43 -0400 |
commit | 9e75a50f3e953acdc6de8d0b8644b58ae4f96826 (patch) | |
tree | 8fee144141c726844172d801245c8543d057d157 | |
parent | 79710f96389cefc498cf806cefb5e05f6fed38ea (diff) | |
download | colm-9e75a50f3e953acdc6de8d0b8644b58ae4f96826.tar.gz |
more cleanup in the list and map type resolve/declare
-rw-r--r-- | src/parsetree.h | 6 | ||||
-rw-r--r-- | src/resolve.cc | 153 |
2 files changed, 83 insertions, 76 deletions
diff --git a/src/parsetree.h b/src/parsetree.h index 38a0ff61..693260f0 100644 --- a/src/parsetree.h +++ b/src/parsetree.h @@ -2209,6 +2209,12 @@ struct TypeRef UniqueType *resolveType( Compiler *pd ); UniqueType *resolveTypeRef( Compiler *pd ); + bool uniqueGeneric( UniqueGeneric *&inMap, + Compiler *pd, const UniqueGeneric &searchKey ); + + StructEl *declareMapElStruct( Compiler *pd, TypeRef *keyType, TypeRef *valType ); + StructEl *declareListEl( Compiler *pd, TypeRef *valType ); + Type type; InputLoc loc; NamespaceQual *nspaceQual; diff --git a/src/resolve.cc b/src/resolve.cc index d55c575b..57482b3f 100644 --- a/src/resolve.cc +++ b/src/resolve.cc @@ -114,48 +114,60 @@ UniqueType *TypeRef::resolveTypeLiteral( Compiler *pd ) return 0; } -UniqueType *TypeRef::resolveTypeListEl( Compiler *pd ) +bool TypeRef::uniqueGeneric( UniqueGeneric *&inMap, Compiler *pd, + const UniqueGeneric &searchKey ) { - UniqueType *utValue = typeRef1->resolveType( pd ); - - UniqueGeneric searchKey( UniqueGeneric::ListEl, utValue ); - UniqueGeneric *inMap = pd->uniqueGenericMap.find( &searchKey ); + bool inserted = false; + inMap = pd->uniqueGenericMap.find( &searchKey ); if ( inMap == 0 ) { + inserted = true; inMap = new UniqueGeneric( searchKey ); pd->uniqueGenericMap.insert( inMap ); + } + return inserted; +} - static long vlistElId = 1; - String name( 32, "list_el_%d", vlistElId++ ); - ObjectDef *objectDef = ObjectDef::cons( ObjectDef::StructType, - name, pd->nextObjectId++ ); +StructEl *TypeRef::declareListEl( Compiler *pd, TypeRef *valType ) +{ + static long vlistElId = 1; + String name( 32, "list_el_%d", vlistElId++ ); + ObjectDef *objectDef = ObjectDef::cons( ObjectDef::StructType, + name, pd->nextObjectId++ ); - StructDef *structDef = new StructDef( loc, name, objectDef ); + StructDef *structDef = new StructDef( loc, name, objectDef ); - pd->rootNamespace->structDefList.append( structDef ); + pd->rootNamespace->structDefList.append( structDef ); - /* Value Element. */ - String id = "value"; - ObjectField *elValObjField = ObjectField::cons( internal, - ObjectField::StructFieldType, typeRef1, id ); + /* Value Element. */ + String id = "value"; + ObjectField *elValObjField = ObjectField::cons( internal, + ObjectField::StructFieldType, valType, id ); - objectDef->rootScope->insertField( elValObjField->name, elValObjField ); + objectDef->rootScope->insertField( elValObjField->name, elValObjField ); + /* Typeref for the struct. Used for pointers. */ + NamespaceQual *nspaceQual = NamespaceQual::cons( pd->rootNamespace ); + TypeRef *selfTypeRef = TypeRef::cons( InputLoc(), nspaceQual, name, RepeatNone ); - /* Typeref for the struct. Used for pointers. */ - NamespaceQual *nspaceQual = NamespaceQual::cons( pd->rootNamespace ); - TypeRef *selfTypeRef = TypeRef::cons( InputLoc(), nspaceQual, name, RepeatNone ); + /* Type ref for the list pointers psuedo type. */ + TypeRef *elTr = TypeRef::cons( InputLoc(), TypeRef::ListPtrs, 0, selfTypeRef, 0 ); - /* Type ref for the list pointers psuedo type. */ - TypeRef *elTr = TypeRef::cons( InputLoc(), TypeRef::ListPtrs, 0, selfTypeRef, 0 ); + ObjectField *of = ObjectField::cons( InputLoc(), + ObjectField::GenericElementType, elTr, name ); - ObjectField *of = ObjectField::cons( InputLoc(), - ObjectField::GenericElementType, elTr, name ); + objectDef->rootScope->insertField( of->name, of ); - objectDef->rootScope->insertField( of->name, of ); + return declareStruct( pd, pd->rootNamespace, name, structDef ); +} - StructEl *sel = declareStruct( pd, pd->rootNamespace, name, structDef ); - inMap->structEl = sel; - } +UniqueType *TypeRef::resolveTypeListEl( Compiler *pd ) +{ + TypeRef *valTr = typeRef1; + UniqueType *utValue = valTr->resolveType( pd ); + + UniqueGeneric *inMap = 0, searchKey( UniqueGeneric::ListEl, utValue ); + if ( uniqueGeneric( inMap, pd, searchKey ) ) + inMap->structEl = declareListEl( pd, valTr ); return pd->findUniqueType( TYPE_STRUCT, inMap->structEl ); } @@ -177,11 +189,8 @@ UniqueType *TypeRef::resolveTypeList( Compiler *pd ) if ( !listEl ) error( loc ) << "could not find list element in type ref" << endp; - UniqueGeneric searchKey( UniqueGeneric::List, utValue ); - UniqueGeneric *inMap = pd->uniqueGenericMap.find( &searchKey ); - if ( inMap == 0 ) { - inMap = new UniqueGeneric( searchKey ); - pd->uniqueGenericMap.insert( inMap ); + UniqueGeneric *inMap = 0, searchKey( UniqueGeneric::List, utValue ); + if ( uniqueGeneric( inMap, pd, searchKey ) ) { GenericType *generic = new GenericType( GEN_LIST, pd->nextGenericId++, typeRef1, 0, typeRef2, listEl ); @@ -197,52 +206,50 @@ UniqueType *TypeRef::resolveTypeList( Compiler *pd ) return pd->findUniqueType( TYPE_GENERIC, inMap->generic ); } -UniqueType *TypeRef::resolveTypeMapEl( Compiler *pd ) +StructEl *TypeRef::declareMapElStruct( Compiler *pd, TypeRef *keyType, TypeRef *valType ) { - TypeRef *keyType = typeRef1; - TypeRef *valType = typeRef2; + static long vlistElId = 1; + String name( 32, "map_el_%d", vlistElId++ ); + ObjectDef *objectDef = ObjectDef::cons( ObjectDef::StructType, + name, pd->nextObjectId++ ); - UniqueType *utKey = keyType->resolveType( pd ); - UniqueType *utValue = valType->resolveType( pd ); + StructDef *structDef = new StructDef( loc, name, objectDef ); - UniqueGeneric searchKey( UniqueGeneric::MapEl, utKey, utValue ); - UniqueGeneric *inMap = pd->uniqueGenericMap.find( &searchKey ); - if ( inMap == 0 ) { - inMap = new UniqueGeneric( searchKey ); - pd->uniqueGenericMap.insert( inMap ); + pd->rootNamespace->structDefList.append( structDef ); - static long vlistElId = 1; - String name( 32, "map_el_%d", vlistElId++ ); - ObjectDef *objectDef = ObjectDef::cons( ObjectDef::StructType, - name, pd->nextObjectId++ ); + /* Value Element. */ + String id = "value"; + ObjectField *elValObjField = ObjectField::cons( internal, + ObjectField::StructFieldType, valType, id ); - StructDef *structDef = new StructDef( loc, name, objectDef ); + objectDef->rootScope->insertField( elValObjField->name, elValObjField ); - pd->rootNamespace->structDefList.append( structDef ); + /* Typeref for the pointers. */ + NamespaceQual *nspaceQual = NamespaceQual::cons( pd->rootNamespace ); + TypeRef *selfTypeRef = TypeRef::cons( InputLoc(), nspaceQual, name, RepeatNone ); - /* Value Element. */ - String id = "value"; - ObjectField *elValObjField = ObjectField::cons( internal, - ObjectField::StructFieldType, valType, id ); + TypeRef *elTr = TypeRef::cons( InputLoc(), TypeRef::MapPtrs, 0, selfTypeRef, keyType ); - objectDef->rootScope->insertField( elValObjField->name, elValObjField ); + ObjectField *of = ObjectField::cons( InputLoc(), + ObjectField::GenericElementType, elTr, name ); - /* List element with the same name as containing context. */ - NamespaceQual *nspaceQual = NamespaceQual::cons( pd->rootNamespace ); - TypeRef *selfTypeRef = TypeRef::cons( InputLoc(), nspaceQual, name, RepeatNone ); - TypeRef *elTr = TypeRef::cons( InputLoc(), TypeRef::MapPtrs, 0, selfTypeRef, keyType ); + objectDef->rootScope->insertField( of->name, of ); - ObjectField *of = ObjectField::cons( InputLoc(), - ObjectField::GenericElementType, elTr, name ); + StructEl *sel = declareStruct( pd, pd->rootNamespace, name, structDef ); + return sel; +} - objectDef->rootScope->insertField( of->name, of ); +UniqueType *TypeRef::resolveTypeMapEl( Compiler *pd ) +{ + TypeRef *keyType = typeRef1; + TypeRef *valType = typeRef2; - /* Note this is recurse, all of above must complete. */ - //structDef->declare( pd, nspace ); + UniqueType *utKey = keyType->resolveType( pd ); + UniqueType *utValue = valType->resolveType( pd ); - StructEl *sel = declareStruct( pd, pd->rootNamespace, name, structDef ); - inMap->structEl = sel; - } + UniqueGeneric *inMap = 0, searchKey( UniqueGeneric::MapEl, utKey, utValue ); + if ( uniqueGeneric( inMap, pd, searchKey ) ) + inMap->structEl = declareMapElStruct( pd, keyType, valType ); return pd->findUniqueType( TYPE_STRUCT, inMap->structEl ); } @@ -266,11 +273,9 @@ UniqueType *TypeRef::resolveTypeMap( Compiler *pd ) if ( !mapEl ) error( loc ) << "could not find map element in type ref" << endp; - UniqueGeneric searchKey( UniqueGeneric::Map, utKey, utEl ); - UniqueGeneric *inMap = pd->uniqueGenericMap.find( &searchKey ); - if ( inMap == 0 ) { - inMap = new UniqueGeneric( searchKey ); - pd->uniqueGenericMap.insert( inMap ); + UniqueGeneric *inMap = 0, searchKey( UniqueGeneric::Map, utKey, utEl ); + + if ( uniqueGeneric( inMap, pd, searchKey ) ) { GenericType *generic = new GenericType( GEN_MAP, pd->nextGenericId++, typeRef2, typeRef1, typeRef3, mapEl ); @@ -292,12 +297,8 @@ UniqueType *TypeRef::resolveTypeParser( Compiler *pd ) UniqueType *utParse = typeRef1->resolveType( pd ); - UniqueGeneric searchKey( UniqueGeneric::Parser, utParse ); - UniqueGeneric *inMap = pd->uniqueGenericMap.find( &searchKey ); - if ( inMap == 0 ) { - inMap = new UniqueGeneric( searchKey ); - pd->uniqueGenericMap.insert( inMap ); - + UniqueGeneric *inMap = 0, searchKey( UniqueGeneric::Parser, utParse ); + if ( uniqueGeneric( inMap, pd, searchKey ) ) { GenericType *generic = new GenericType( GEN_PARSER, pd->nextGenericId++, typeRef1, 0, 0, 0 ); |