summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2015-06-14 16:28:29 -0400
committerAdrian Thurston <thurston@complang.org>2015-06-14 16:28:29 -0400
commit79710f96389cefc498cf806cefb5e05f6fed38ea (patch)
tree6fce42d8f219db18914c20aedbfb03283e417234
parent324f1fc08d2876f8084f226a54bb06e0e817a791 (diff)
downloadcolm-79710f96389cefc498cf806cefb5e05f6fed38ea.tar.gz
cleanup in the list and map types
-rw-r--r--src/declare.cc5
-rw-r--r--src/parser.cc10
-rw-r--r--src/parsetree.cc2
-rw-r--r--src/parsetree.h23
-rw-r--r--src/resolve.cc85
-rw-r--r--src/synthesis.cc16
6 files changed, 42 insertions, 99 deletions
diff --git a/src/declare.cc b/src/declare.cc
index dc3f4385..bce148bc 100644
--- a/src/declare.cc
+++ b/src/declare.cc
@@ -250,11 +250,6 @@ StructEl *declareStruct( Compiler *pd, Namespace *inNspace,
pd->structEls.append( structEl );
structDef->structEl = structEl;
- if ( structDef->listEl )
- structEl->listEl = true;
- if ( structDef->mapEl )
- structEl->mapEl = true;
-
if ( inNspace ) {
TypeMapEl *typeMapEl = new TypeMapEl( TypeMapEl::StructType, data, structEl );
inNspace->typeMap.insert( typeMapEl );
diff --git a/src/parser.cc b/src/parser.cc
index dc4d4576..37ba9893 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -480,14 +480,13 @@ LangStmt *BaseParser::globalDef( ObjectField *objField, LangExpr *expr,
{
LangStmt *stmt = 0;
- StructDef *context = 0;
+ StructDef *structDef = 0;
ObjectDef *object = 0;
if ( curStruct() == 0 )
object = pd->globalObjectDef;
else {
- context = curStruct();
- objField->context = context;
- object = context->objectDef;
+ structDef = curStruct();
+ object = structDef->objectDef;
}
if ( object->rootScope->checkRedecl( objField->name ) != 0 )
@@ -497,7 +496,7 @@ LangStmt *BaseParser::globalDef( ObjectField *objField, LangExpr *expr,
if ( expr != 0 ) {
LangVarRef *varRef = LangVarRef::cons( objField->loc,
- context, curScope, objField->name );
+ structDef, curScope, objField->name );
stmt = LangStmt::cons( objField->loc,
assignType, varRef, expr );
@@ -988,7 +987,6 @@ void BaseParser::structVarDef( const InputLoc &loc, ObjectField *objField )
error(loc) << "internal error: no context stack items found" << endp;
StructDef *structDef = curStruct();
- objField->context = structDef;
object = structDef->objectDef;
if ( object->rootScope->checkRedecl( objField->name ) != 0 )
diff --git a/src/parsetree.cc b/src/parsetree.cc
index ca7e9b83..9d3b3d0a 100644
--- a/src/parsetree.cc
+++ b/src/parsetree.cc
@@ -173,13 +173,11 @@ int CmpUniqueGeneric::compare( const UniqueGeneric &ut1, const UniqueGeneric &ut
else {
switch ( ut1.type ) {
case UniqueGeneric::List:
- case UniqueGeneric::ListPtrs:
case UniqueGeneric::ListEl:
case UniqueGeneric::Parser:
break;
case UniqueGeneric::Map:
- case UniqueGeneric::MapPtrs:
case UniqueGeneric::MapEl:
if ( ut1.key < ut2.key )
return -1;
diff --git a/src/parsetree.h b/src/parsetree.h
index d4e52fd1..38a0ff61 100644
--- a/src/parsetree.h
+++ b/src/parsetree.h
@@ -554,17 +554,13 @@ struct StructDef
loc(loc),
name(name),
objectDef(objectDef),
- structEl(0),
- listEl(false),
- mapEl(false)
+ structEl(0)
{}
InputLoc loc;
String name;
ObjectDef *objectDef;
StructEl *structEl;
- bool listEl;
- bool mapEl;
StructDef *prev, *next;
};
@@ -575,16 +571,12 @@ struct StructEl
:
name(name),
structDef(structDef),
- id(-1),
- listEl(false),
- mapEl(false)
+ id(-1)
{}
String name;
StructDef *structDef;
int id;
- bool listEl;
- bool mapEl;
StructEl *prev, *next;
};
@@ -1952,7 +1944,9 @@ struct CmpUniqueRepeat
typedef AvlBasic< UniqueRepeat, CmpUniqueRepeat > UniqueRepeatMap;
/*
- * Unique Generics
+ * Unique generics. Allows us to do singleton declarations of generic types and
+ * supporting structures. For example, the list type, but also the list element
+ * struct created for the list type.
*/
struct UniqueGeneric
@@ -1961,10 +1955,8 @@ struct UniqueGeneric
enum Type
{
List,
- ListPtrs,
ListEl,
Map,
- MapPtrs,
MapEl,
Parser
};
@@ -2210,10 +2202,8 @@ struct TypeRef
UniqueType *resolveTypeName( Compiler *pd );
UniqueType *resolveTypeLiteral( Compiler *pd );
UniqueType *resolveTypeList( Compiler *pd );
- UniqueType *resolveTypeListPtrs( Compiler *pd );
UniqueType *resolveTypeListEl( Compiler *pd );
UniqueType *resolveTypeMap( Compiler *pd );
- UniqueType *resolveTypeMapPtrs( Compiler *pd );
UniqueType *resolveTypeMapEl( Compiler *pd );
UniqueType *resolveTypeParser( Compiler *pd );
UniqueType *resolveType( Compiler *pd );
@@ -2355,7 +2345,6 @@ struct ObjectField
ObjectField()
:
typeRef(0),
- context(0),
scope(0),
offset(0),
beenReferenced(false),
@@ -2431,7 +2420,6 @@ struct ObjectField
Type type;
TypeRef *typeRef;
String name;
- StructDef *context;
NameScope *scope;
long offset;
bool beenReferenced;
@@ -2572,6 +2560,7 @@ struct ObjectDef
void insertField( NameScope *inScope, const String &name, ObjectField *value );
void resolve( Compiler *pd );
ObjectField *findFieldNum( long offset );
+ ObjectField *findFieldType( Compiler *pd, UniqueType *ut );
long size() { return nextOffset; }
long sizeTrees() { return firstNonTree; }
diff --git a/src/resolve.cc b/src/resolve.cc
index e7214ff0..d55c575b 100644
--- a/src/resolve.cc
+++ b/src/resolve.cc
@@ -114,11 +114,6 @@ UniqueType *TypeRef::resolveTypeLiteral( Compiler *pd )
return 0;
}
-UniqueType *TypeRef::resolveTypeListPtrs( Compiler *pd )
-{
- return pd->findUniqueType( TYPE_LIST_PTRS );
-}
-
UniqueType *TypeRef::resolveTypeListEl( Compiler *pd )
{
UniqueType *utValue = typeRef1->resolveType( pd );
@@ -138,39 +133,26 @@ UniqueType *TypeRef::resolveTypeListEl( Compiler *pd )
pd->rootNamespace->structDefList.append( structDef );
- /* Make the new namespace. */
- Namespace *nspace = new Namespace( loc, name,
- pd->namespaceList.length(), pd->rootNamespace );
-
- pd->rootNamespace->childNamespaces.append( nspace );
-
- pd->namespaceList.append( nspace );
-
-
/* Value Element. */
String id = "value";
ObjectField *elValObjField = ObjectField::cons( internal,
ObjectField::StructFieldType, typeRef1, id );
- elValObjField->context = structDef;
objectDef->rootScope->insertField( elValObjField->name, elValObjField );
- elValObjField->context->listEl = true;
- /* List element with the same name as containing context. */
- NamespaceQual *nspaceQual = NamespaceQual::cons( nspace );
- TypeRef *objTr = TypeRef::cons( InputLoc(), nspaceQual, name, RepeatNone );
- TypeRef *elTr = TypeRef::cons( InputLoc(), TypeRef::ListPtrs, 0, objTr, 0 );
+ /* 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 );
ObjectField *of = ObjectField::cons( InputLoc(),
ObjectField::GenericElementType, elTr, name );
- of->context = structDef;
objectDef->rootScope->insertField( of->name, of );
- /* Note this is recurse, all of above must complete. */
- //structDef->declare( pd, nspace );
-
StructEl *sel = declareStruct( pd, pd->rootNamespace, name, structDef );
inMap->structEl = sel;
}
@@ -187,17 +169,10 @@ UniqueType *TypeRef::resolveTypeList( Compiler *pd )
if ( utValue->typeId != TYPE_STRUCT )
error( loc ) << "only structs can be list elements" << endp;
- /* Find the offset of the list element. */
- int off = 0;
- ObjectField *listEl = 0;
- FieldList *fieldList = utValue->structEl->structDef->objectDef->fieldList;
- for ( FieldList::Iter f = *fieldList; f.lte(); f++, off++ ) {
- UniqueType *fUT = f->value->typeRef->resolveType( pd );
- if ( fUT->typeId == TYPE_LIST_PTRS ) {
- listEl = f->value;
- break;
- }
- }
+ /* Find the list element. */
+ ObjectDef *elObjDef = utValue->structEl->structDef->objectDef;
+ UniqueType *ptrsUt = pd->findUniqueType( TYPE_LIST_PTRS );
+ ObjectField *listEl = elObjDef->findFieldType( pd, ptrsUt );
if ( !listEl )
error( loc ) << "could not find list element in type ref" << endp;
@@ -222,11 +197,6 @@ UniqueType *TypeRef::resolveTypeList( Compiler *pd )
return pd->findUniqueType( TYPE_GENERIC, inMap->generic );
}
-UniqueType *TypeRef::resolveTypeMapPtrs( Compiler *pd )
-{
- return pd->findUniqueType( TYPE_MAP_PTRS );
-}
-
UniqueType *TypeRef::resolveTypeMapEl( Compiler *pd )
{
TypeRef *keyType = typeRef1;
@@ -250,34 +220,21 @@ UniqueType *TypeRef::resolveTypeMapEl( Compiler *pd )
pd->rootNamespace->structDefList.append( structDef );
- /* Make the new namespace. */
- Namespace *nspace = new Namespace( loc, name,
- pd->namespaceList.length(), pd->rootNamespace );
-
- pd->rootNamespace->childNamespaces.append( nspace );
-
- pd->namespaceList.append( nspace );
-
-
/* Value Element. */
String id = "value";
ObjectField *elValObjField = ObjectField::cons( internal,
ObjectField::StructFieldType, valType, id );
- elValObjField->context = structDef;
objectDef->rootScope->insertField( elValObjField->name, elValObjField );
- elValObjField->context->mapEl = true;
-
/* List element with the same name as containing context. */
- NamespaceQual *nspaceQual = NamespaceQual::cons( nspace );
- TypeRef *objTr = TypeRef::cons( InputLoc(), nspaceQual, name, RepeatNone );
- TypeRef *elTr = TypeRef::cons( InputLoc(), TypeRef::MapPtrs, 0, objTr, keyType );
+ NamespaceQual *nspaceQual = NamespaceQual::cons( pd->rootNamespace );
+ TypeRef *selfTypeRef = TypeRef::cons( InputLoc(), nspaceQual, name, RepeatNone );
+ TypeRef *elTr = TypeRef::cons( InputLoc(), TypeRef::MapPtrs, 0, selfTypeRef, keyType );
ObjectField *of = ObjectField::cons( InputLoc(),
ObjectField::GenericElementType, elTr, name );
- of->context = structDef;
objectDef->rootScope->insertField( of->name, of );
/* Note this is recurse, all of above must complete. */
@@ -302,15 +259,9 @@ UniqueType *TypeRef::resolveTypeMap( Compiler *pd )
error( loc ) << "only structs can be map elements" << endp;
/* Find the list element. */
- ObjectField *mapEl = 0;
- FieldList *fieldList = utEl->structEl->structDef->objectDef->fieldList;
- for ( FieldList::Iter f = *fieldList; f.lte(); f++ ) {
- UniqueType *fUT = f->value->typeRef->resolveType( pd );
- if ( fUT->typeId == TYPE_MAP_PTRS ) {
- mapEl = f->value;
- break;
- }
- }
+ ObjectDef *elObjDef = utEl->structEl->structDef->objectDef;
+ UniqueType *ptrsUt = pd->findUniqueType( TYPE_MAP_PTRS );
+ ObjectField *mapEl = elObjDef->findFieldType( pd, ptrsUt );
if ( !mapEl )
error( loc ) << "could not find map element in type ref" << endp;
@@ -469,7 +420,7 @@ UniqueType *TypeRef::resolveType( Compiler *pd )
uniqueType = resolveTypeList( pd );
break;
case ListPtrs:
- uniqueType = resolveTypeListPtrs( pd );
+ uniqueType = pd->findUniqueType( TYPE_LIST_PTRS );
break;
case ListEl:
uniqueType = resolveTypeListEl( pd );
@@ -479,7 +430,7 @@ UniqueType *TypeRef::resolveType( Compiler *pd )
uniqueType = resolveTypeMap( pd );
break;
case MapPtrs:
- uniqueType = resolveTypeMapPtrs( pd );
+ uniqueType = pd->findUniqueType( TYPE_MAP_PTRS );
break;
case MapEl:
uniqueType = resolveTypeMapEl( pd );
diff --git a/src/synthesis.cc b/src/synthesis.cc
index f85bc3d9..558ea6b6 100644
--- a/src/synthesis.cc
+++ b/src/synthesis.cc
@@ -302,6 +302,18 @@ ObjectField *ObjectDef::findFieldNum( long offset )
return field->value;
}
+/* Finds the first field by type. */
+ObjectField *ObjectDef::findFieldType( Compiler *pd, UniqueType *ut )
+{
+ for ( FieldList::Iter f = *fieldList; f.lte(); f++ ) {
+ UniqueType *fUT = f->value->typeRef->resolveType( pd );
+ if ( fUT == ut )
+ return f->value;
+ }
+ return 0;
+}
+
+
long sizeOfField( UniqueType *fieldUT )
{
long size = 0;
@@ -834,14 +846,14 @@ IterImpl *LangVarRef::chooseTriterCall( Compiler *pd,
UniqueType *exprUT = (*pe)->expr->evaluate( pd, unused );
if ( exprUT->typeId == TYPE_GENERIC && exprUT->generic->typeId == GEN_LIST ) {
- if ( searchUT->structEl != 0 && searchUT->structEl->listEl )
+ if ( searchUT == exprUT->generic->elUt )
iterImpl = new IterImpl( IterImpl::ListEl );
else
iterImpl = new IterImpl( IterImpl::ListVal );
}
if ( exprUT->typeId == TYPE_GENERIC && exprUT->generic->typeId == GEN_MAP ) {
- if ( searchUT->structEl != 0 && searchUT->structEl->mapEl )
+ if ( searchUT == exprUT->generic->elUt )
iterImpl = new IterImpl( IterImpl::MapEl );
else
iterImpl = new IterImpl( IterImpl::MapVal );