Permalink
Browse files

Reworking symbols

  • Loading branch information...
1 parent 174b0f5 commit d15f5ee83fcfdf510eda8d910d6440fdc45c9f2a @mwolf76 committed Apr 8, 2013
Showing with 90 additions and 34 deletions.
  1. +0 −1 src/model/model.hh
  2. +8 −0 src/model/resolver.cc
  3. +3 −6 src/parser/grammars/smv.g
  4. +1 −0 src/symb/symbol.cc
  5. +29 −12 src/symb/symbol.hh
  6. +10 −4 src/type/type_mgr.cc
  7. +39 −11 src/type/type_mgr.hh
View
@@ -163,7 +163,6 @@ public:
const Type_ptr type() const
{ return f_type; }
-
};
class Constant : public IConstant {
View
@@ -83,6 +83,14 @@ ISymbol_ptr Resolver::fetch_symbol(const Expr_ptr ctx, const Expr_ptr symb)
}
}
+ { /* enum types */
+ const Enums& enums = TypeMgr::INSTANCE().enums();
+ Enums::const_iterator eiter = enums.find(key);
+ if (eiter != enums.end()) {
+ return (*eiter).second;
+ }
+ }
+
{ /* enum literals */
const Literals& lits = TypeMgr::INSTANCE().literals();
Literals::const_iterator liter = lits.find(key);
@@ -276,11 +276,8 @@ fsm_enum_decl_clause
@init {
ExprSet lits;
}
- : local=identifier ':' fsm_enum_type_lits[lits]
- {
- tm.define_enum( em.make_dot($smv::module->expr(),
- local), lits);
- }
+ : expr=identifier ':' fsm_enum_type_lits[lits]
+ { tm.add_enum( $smv::module->expr(), expr, lits ); }
;
fsm_enum_type_lits [ExprSet& lits]
@@ -718,7 +715,7 @@ type_name returns [Type_ptr res]
// reserved for ENUMs
| expr=identifier
- { $res = tm.find_enum( em.make_dot( $smv::module->expr(), expr)); }
+ { $res = tm.find_enum( $smv::module->expr(), expr ); }
;
literal returns [Expr_ptr res]
View
@@ -96,3 +96,4 @@ ILiteral& ISymbol::as_literal(void) const
assert (res);
return (*res);
}
+
View
@@ -37,6 +37,10 @@ class ILiteral;
typedef ILiteral* ILiteral_ptr;
typedef unordered_map<FQExpr, ILiteral_ptr, FQExprHash, FQExprEq> Literals;
+class IEnum;
+typedef IEnum* IEnum_ptr;
+typedef unordered_map<FQExpr, IEnum_ptr, FQExprHash, FQExprEq> Enums;
+
class IConstant;
typedef IConstant* IConstant_ptr;
typedef unordered_map<FQExpr, IConstant_ptr, FQExprHash, FQExprEq> Constants;
@@ -53,7 +57,16 @@ class IDefine;
typedef IDefine* IDefine_ptr;
typedef unordered_map<FQExpr, IDefine_ptr, FQExprHash, FQExprEq> Defines;
-// -- primary decls --------------------------------------------------------------
+class ITyped : IObject {
+public:
+ virtual const Type_ptr type() const =0;
+};
+
+class IValue : IObject {
+public:
+ virtual value_t value() const =0;
+};
+
class ISymbol : IObject {
public:
virtual const Expr_ptr ctx() const =0;
@@ -75,17 +88,21 @@ public:
IDefine& as_define() const;
};
-class ILiteral : public ISymbol {
-public:
- virtual value_t value() const =0;
- virtual const Type_ptr type() const =0;
-};
-
-class IConstant : public ISymbol {
-public:
- virtual value_t value() const =0;
- virtual const Type_ptr type() const =0;
-};
+class IEnum
+ : public ITyped
+ , public ISymbol
+{};
+
+class ILiteral
+ : public ISymbol
+ , public ITyped
+{};
+
+class IConstant
+ : public ISymbol
+ , public ITyped
+ , public IValue
+{};
class IVariable : public ISymbol {
public:
View
@@ -101,35 +101,41 @@ const Type_ptr TypeMgr::find_signed_array(unsigned digits, unsigned size)
return res;
}
-void TypeMgr::define_enum(Expr_ptr fullname, ExprSet& lits)
+void TypeMgr::add_enum(Expr_ptr ctx, Expr_ptr name, ExprSet& lits)
{
/*
IMPORTANT: lits ordering has to be canonical for enum types to
work as expected! Otherwise same set of lits with different
ordering could be mistakingly seen as a different type.
*/
+ Expr_ptr fullname = ExprMgr::INSTANCE().make_dot( ctx, name );
if (NULL != lookup_type(fullname)) {
assert(0); // tODO: better error handling
}
EnumType_ptr tp = new EnumType( *this, lits );
+ // Define the ENUM
+ IEnum_ptr enm = new Enum(ctx, name, tp);
+ f_enums.insert( make_pair<FQExpr,
+ IEnum_ptr>( FQExpr( ctx, name), enm));
+
// Literals are all maintained together by the type mgr. This
// greatly simplifies the resolver.
for (ExprSet::iterator eye = lits.begin(); eye != lits.end(); ++ eye) {
f_lits.insert( make_pair<FQExpr,
ILiteral_ptr>(FQExpr( fullname, *eye),
- new Literal(fullname, *eye, tp)));
+ new Literal(enm, *eye)));
}
// new type, needs to be registered before returning
register_type(fullname, tp);
}
-const Type_ptr TypeMgr::find_enum(Expr_ptr fullname)
+const Type_ptr TypeMgr::find_enum(Expr_ptr ctx, Expr_ptr name)
{
- Type_ptr res = lookup_type(fullname);
+ Type_ptr res = lookup_type(ExprMgr::INSTANCE().make_dot(ctx, name));
assert( NULL != res ); // TODO error handling
return res;
View
@@ -64,36 +64,63 @@ typedef pair<TypeMap::iterator, bool> TypeHit;
*/
typedef class TypeMgr* TypeMgr_ptr;
-class Literal : public ILiteral {
+class Enum : public IEnum {
const Expr_ptr f_ctx;
const Expr_ptr f_name;
const EnumType_ptr f_type;
public:
- Literal(const Expr_ptr ctx, const Expr_ptr name, EnumType_ptr type)
+ Enum(const Expr_ptr ctx, const Expr_ptr name, EnumType_ptr type)
: f_ctx(ctx)
, f_name(name)
, f_type(type)
{}
+ virtual const Expr_ptr ctx() const
+ { return f_ctx; }
+
+ virtual const Expr_ptr name() const
+ { return f_name; }
+
+ virtual const Expr_ptr expr() const
+ { return ExprMgr::INSTANCE().make_dot( ctx(), name() ); }
+
virtual const Type_ptr type() const
{ return f_type; }
+};
+
+class Literal : public ILiteral {
+ const IEnum_ptr f_owner;
+ const Expr_ptr f_name;
+
+public:
+ Literal(IEnum_ptr owner, const Expr_ptr name)
+ : f_owner(owner)
+ , f_name(name)
+ {}
+
+ virtual const Expr_ptr ctx() const
+ { return f_owner->ctx(); }
virtual const Expr_ptr name() const
{ return f_name; }
- virtual const Expr_ptr expr() const
- { return ExprMgr::INSTANCE().make_dot( f_ctx, f_name ); }
+ virtual const Type_ptr type() const
+ { return f_owner->type(); }
- virtual const Expr_ptr ctx() const
- { return f_ctx; }
+ virtual const Expr_ptr expr() const
+ { return ExprMgr::INSTANCE().make_dot( ctx(), name() ); }
- // delegate to owner (which is an Enum)
virtual value_t value() const
- { return f_type->value( expr() ); }
+ {
+ return dynamic_cast<EnumType_ptr> (type())
+ ->value(expr());
+ }
};
class TypeMgr {
+ /* Shared enums and literals */
+ Enums f_enums;
Literals f_lits;
public:
@@ -130,10 +157,11 @@ public:
const Type_ptr find_default_unsigned_fixed();
/* Remark: unambiguous enums resolution requires DOT fullnames */
- void define_enum(Expr_ptr fullname, ExprSet& lits);
- const Type_ptr find_enum(Expr_ptr fullname);
+ void add_enum(Expr_ptr ctx, Expr_ptr name, ExprSet& lits);
+ const Type_ptr find_enum(Expr_ptr ctx, Expr_ptr name);
- /* Literals are const-like objects managed by the TypeMgr */
+ const Enums& enums() const
+ { return f_enums; }
const Literals& literals() const
{ return f_lits; }

0 comments on commit d15f5ee

Please sign in to comment.