1 /*
2  * Copyright (c) 2011-2013 Stephen Williams (steve@icarus.com)
3  * Copyright CERN 2012-2015 / Stephen Williams (steve@icarus.com),
4  * Copyright CERN 2016
5  * @author Maciej Suminski (maciej.suminski@cern.ch)
6  *
7  *    This source code is free software; you can redistribute it
8  *    and/or modify it in source code form under the terms of the GNU
9  *    General Public License as published by the Free Software
10  *    Foundation; either version 2 of the License, or (at your option)
11  *    any later version.
12  *
13  *    This program is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU General Public License for more details.
17  *
18  *    You should have received a copy of the GNU General Public License
19  *    along with this program; if not, write to the Free Software
20  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 # include  "expression.h"
24 # include  "subprogram.h"
25 # include  "parse_types.h"
26 # include  "scope.h"
27 # include  "library.h"
28 # include  <iostream>
29 # include  <typeinfo>
30 # include  <cstring>
31 # include  <ivl_assert.h>
32 # include  <cassert>
33 
34 using namespace std;
35 
Expression()36 Expression::Expression()
37 : type_(0)
38 {
39 }
40 
~Expression()41 Expression::~Expression()
42 {
43 }
44 
set_type(const VType * typ)45 void Expression::set_type(const VType*typ)
46 {
47       assert(type_==0 || type_==typ);
48       type_ = typ;
49 }
50 
symbolic_compare(const Expression *) const51 bool Expression::symbolic_compare(const Expression*) const
52 {
53       cerr << get_fileline() << ": internal error: "
54 	   << "symbolic_compare() method not implemented "
55 	   << "for " << typeid(*this).name() << endl;
56       return false;
57 }
58 
ExpAttribute(perm_string nam,list<Expression * > * args)59 ExpAttribute::ExpAttribute(perm_string nam, list<Expression*>*args)
60 : name_(nam), args_(args)
61 {
62 }
63 
~ExpAttribute()64 ExpAttribute::~ExpAttribute()
65 {
66       if(args_) {
67 	    for(list<Expression*>::iterator it = args_->begin();
68                     it != args_->end(); ++it) {
69 		delete *it;
70             }
71       }
72 
73       delete args_;
74 }
75 
clone_args() const76 list<Expression*>*ExpAttribute::clone_args() const {
77       list<Expression*>*new_args = NULL;
78 
79       if(args_) {
80 	    for(list<Expression*>::iterator it = args_->begin();
81                     it != args_->end(); ++it) {
82 		new_args->push_back((*it)->clone());
83             }
84       }
85 
86       return new_args;
87 }
88 
visit_args(ExprVisitor & func)89 void ExpAttribute::visit_args(ExprVisitor& func)
90 {
91       func.down();
92       func(this);
93 
94       if(args_) {
95           for(list<Expression*>::iterator it = args_->begin();
96                     it != args_->end(); ++it) {
97               (*it)->visit(func);
98           }
99       }
100 
101       func.up();
102 }
103 
ExpObjAttribute(ExpName * base,perm_string name,list<Expression * > * args)104 ExpObjAttribute::ExpObjAttribute(ExpName*base, perm_string name, list<Expression*>*args)
105 : ExpAttribute(name, args), base_(base)
106 {
107 }
108 
~ExpObjAttribute()109 ExpObjAttribute::~ExpObjAttribute()
110 {
111     delete base_;
112 }
113 
clone() const114 Expression*ExpObjAttribute::clone() const
115 {
116       return new ExpObjAttribute(static_cast<ExpName*>(base_->clone()),
117                                  name_, clone_args());
118 }
119 
visit(ExprVisitor & func)120 void ExpObjAttribute::visit(ExprVisitor&func)
121 {
122       func.down();
123       func(this);
124       visit_args(func);
125       base_->visit(func);
126       func.up();
127 }
128 
ExpTypeAttribute(const VType * base,perm_string name,list<Expression * > * args)129 ExpTypeAttribute::ExpTypeAttribute(const VType*base, perm_string name, list<Expression*>*args)
130 : ExpAttribute(name, args), base_(base)
131 {
132 }
133 
clone() const134 Expression*ExpTypeAttribute::clone() const
135 {
136       return new ExpTypeAttribute(base_, name_, clone_args());
137 }
138 
visit(ExprVisitor & func)139 void ExpTypeAttribute::visit(ExprVisitor&func)
140 {
141       func.down();
142       func(this);
143       visit_args(func);
144       func.up();
145 }
146 
147 const perm_string ExpAttribute::LEFT = perm_string::literal("left");
148 const perm_string ExpAttribute::RIGHT = perm_string::literal("right");
149 
ExpBinary(Expression * op1,Expression * op2)150 ExpBinary::ExpBinary(Expression*op1, Expression*op2)
151 : operand1_(op1), operand2_(op2)
152 {
153 }
154 
~ExpBinary()155 ExpBinary::~ExpBinary()
156 {
157       delete operand1_;
158       delete operand2_;
159 }
160 
eval_operand1(Entity * ent,ScopeBase * scope,int64_t & val) const161 bool ExpBinary::eval_operand1(Entity*ent, ScopeBase*scope, int64_t&val) const
162 {
163       return operand1_->evaluate(ent, scope, val);
164 }
165 
eval_operand2(Entity * ent,ScopeBase * scope,int64_t & val) const166 bool ExpBinary::eval_operand2(Entity*ent, ScopeBase*scope, int64_t&val) const
167 {
168       return operand2_->evaluate(ent, scope, val);
169 }
170 
visit(ExprVisitor & func)171 void ExpBinary::visit(ExprVisitor&func)
172 {
173       func.down();
174       func(this);
175       operand1_->visit(func);
176       operand2_->visit(func);
177       func.up();
178 }
179 
ExpUnary(Expression * op1)180 ExpUnary::ExpUnary(Expression*op1)
181 : operand1_(op1)
182 {
183 }
184 
~ExpUnary()185 ExpUnary::~ExpUnary()
186 {
187       delete operand1_;
188 }
189 
visit(ExprVisitor & func)190 void ExpUnary::visit(ExprVisitor&func)
191 {
192     func.down();
193     func(this);
194     operand1_->visit(func);
195     func.up();
196 }
197 
ExpAggregate(std::list<element_t * > * el)198 ExpAggregate::ExpAggregate(std::list<element_t*>*el)
199 : elements_(el? el->size() : 0)
200 {
201       assert(el);
202       size_t idx = 0;
203       while (! el->empty()) {
204 	    assert(idx < elements_.size());
205 	    elements_[idx++] = el->front();
206 	    el->pop_front();
207       }
208       delete el;
209 }
210 
~ExpAggregate()211 ExpAggregate::~ExpAggregate()
212 {
213       for(std::vector<element_t*>::iterator it = elements_.begin();
214               it != elements_.end(); ++it) {
215         delete *it;
216       }
217 
218       for(std::vector<choice_element>::iterator it = aggregate_.begin();
219               it != aggregate_.end(); ++it) {
220         delete it->choice;
221         if(!it->alias_flag) delete it->expr;
222       }
223 }
224 
clone() const225 Expression* ExpAggregate::clone() const
226 {
227       std::list<element_t*>*new_elements = NULL;
228 
229       if(!elements_.empty()) {
230           new_elements = new std::list<element_t*>();
231           for(std::vector<element_t*>::const_iterator it = elements_.begin();
232                   it != elements_.end(); ++it) {
233               new_elements->push_back(new element_t(**it));
234           }
235       }
236 
237       assert(aggregate_.empty());   // cloning should not happen after elab
238 
239       return new ExpAggregate(new_elements);
240 }
241 
visit(ExprVisitor & func)242 void ExpAggregate::visit(ExprVisitor&func)
243 {
244     func.down();
245     func(this);
246 
247     for(std::vector<element_t*>::iterator it = elements_.begin();
248             it != elements_.end(); ++it) {
249         (*it)->extract_expression()->visit(func);
250     }
251 
252     for(std::vector<choice_element>::iterator it = aggregate_.begin();
253             it != aggregate_.end(); ++it) {
254         if(Expression*choice_expr = it->choice->simple_expression(false))
255             choice_expr->visit(func);
256 
257         it->expr->visit(func);
258     }
259 
260     func.up();
261 }
262 
choice_t(Expression * exp)263 ExpAggregate::choice_t::choice_t(Expression*exp)
264 : expr_(exp)
265 {
266 }
267 
choice_t()268 ExpAggregate::choice_t::choice_t()
269 {
270 }
271 
choice_t(ExpRange * rang)272 ExpAggregate::choice_t::choice_t(ExpRange*rang)
273 : range_(rang)
274 {
275 }
276 
choice_t(const choice_t & other)277 ExpAggregate::choice_t::choice_t(const choice_t&other)
278 {
279     if(Expression*e = other.expr_.get())
280         expr_.reset(e->clone());
281 
282     if(other.range_.get())
283         range_.reset(static_cast<ExpRange*>(other.range_.get()->clone()));
284 }
285 
~choice_t()286 ExpAggregate::choice_t::~choice_t()
287 {
288 }
289 
others() const290 bool ExpAggregate::choice_t::others() const
291 {
292       return expr_.get() == 0 && range_.get() == 0;
293 }
294 
simple_expression(bool detach_flag)295 Expression*ExpAggregate::choice_t::simple_expression(bool detach_flag)
296 {
297       Expression*res = detach_flag? expr_.release() : expr_.get();
298       return res;
299 }
300 
range_expressions(void)301 ExpRange*ExpAggregate::choice_t::range_expressions(void)
302 {
303       return range_.get();
304 }
305 
element_t(list<choice_t * > * fields,Expression * val)306 ExpAggregate::element_t::element_t(list<choice_t*>*fields, Expression*val)
307 : fields_(fields? fields->size() : 0), val_(val)
308 {
309       if (fields) {
310 	    size_t idx = 0;
311 	    while (! fields->empty()) {
312 		  assert(idx < fields_.size());
313 		  fields_[idx++] = fields->front();
314 		  fields->pop_front();
315 	    }
316       }
317 }
318 
element_t(const ExpAggregate::element_t & other)319 ExpAggregate::element_t::element_t(const ExpAggregate::element_t&other)
320 {
321       fields_.reserve(other.fields_.size());
322 
323       for(std::vector<choice_t*>::const_iterator it = other.fields_.begin();
324               it != other.fields_.end(); ++it) {
325           fields_.push_back(*it);
326       }
327 
328       val_ = other.val_->clone();
329 }
330 
~element_t()331 ExpAggregate::element_t::~element_t()
332 {
333       for (size_t idx = 0 ; idx < fields_.size() ; idx += 1)
334 	    delete fields_[idx];
335 
336       delete val_;
337 }
338 
ExpArithmetic(ExpArithmetic::fun_t op,Expression * op1,Expression * op2)339 ExpArithmetic::ExpArithmetic(ExpArithmetic::fun_t op, Expression*op1, Expression*op2)
340 : ExpBinary(op1, op2), fun_(op)
341 {
342 	// The xCONCAT type is not actually used.
343       assert(op != xCONCAT);
344 }
345 
~ExpArithmetic()346 ExpArithmetic::~ExpArithmetic()
347 {
348 }
349 
350 /*
351  *  Store bitstrings in little-endian order.
352  */
ExpBitstring(const char * val)353 ExpBitstring::ExpBitstring(const char*val)
354 : value_(strlen(val))
355 {
356       for (size_t idx = value_.size() ; idx > 0 ; idx -= 1)
357 	    value_[idx-1] = *val++;
358 }
359 
~ExpBitstring()360 ExpBitstring::~ExpBitstring()
361 {
362 }
363 
ExpCharacter(char val)364 ExpCharacter::ExpCharacter(char val)
365 : value_(val)
366 {
367 }
368 
~ExpCharacter()369 ExpCharacter::~ExpCharacter()
370 {
371 }
372 
ExpConcat(Expression * op1,Expression * op2)373 ExpConcat::ExpConcat(Expression*op1, Expression*op2)
374 : operand1_(op1), operand2_(op2)
375 {
376 }
377 
~ExpConcat()378 ExpConcat::~ExpConcat()
379 {
380       delete operand1_;
381       delete operand2_;
382 }
383 
visit(ExprVisitor & func)384 void ExpConcat::visit(ExprVisitor&func)
385 {
386       func.down();
387       func(this);
388       operand1_->visit(func);
389       operand2_->visit(func);
390       func.up();
391 }
392 
ExpConditional(Expression * co,list<Expression * > * tru,list<ExpConditional::case_t * > * options)393 ExpConditional::ExpConditional(Expression*co, list<Expression*>*tru,
394 			       list<ExpConditional::case_t*>*options)
395 {
396       if(co && tru) options_.push_back(new case_t(co, tru));
397       if(options) options_.splice(options_.end(), *options);
398 }
399 
~ExpConditional()400 ExpConditional::~ExpConditional()
401 {
402       while (!options_.empty()) {
403 	    case_t*tmp = options_.front();
404 	    options_.pop_front();
405 	    delete tmp;
406       }
407 }
408 
clone() const409 Expression*ExpConditional::clone() const
410 {
411       std::list<case_t*>*new_options = NULL;
412       if(!options_.empty()) {
413           new_options = new std::list<case_t*>();
414 
415           for(std::list<case_t*>::const_iterator it = options_.begin();
416                   it != options_.end(); ++it) {
417               new_options->push_back(new case_t(**it));
418           }
419       }
420 
421       return new ExpConditional(NULL, NULL, new_options);
422 }
423 
visit(ExprVisitor & func)424 void ExpConditional::visit(ExprVisitor&func)
425 {
426       func.down();
427       func(this);
428 
429       for(std::list<case_t*>::iterator it = options_.begin();
430               it != options_.end(); ++it)
431           (*it)->visit(func);
432 
433       func.up();
434 }
435 
case_t(Expression * cond,std::list<Expression * > * tru)436 ExpConditional::case_t::case_t(Expression*cond, std::list<Expression*>*tru)
437 : cond_(cond)
438 {
439       if (tru) true_clause_.splice(true_clause_.end(), *tru);
440 }
441 
case_t(const case_t & other)442 ExpConditional::case_t::case_t(const case_t&other)
443 : LineInfo(other)
444 {
445       cond_ = other.cond_->clone();
446       for(std::list<Expression*>::const_iterator it = other.true_clause_.begin();
447             it != other.true_clause_.end(); ++it) {
448           true_clause_.push_back((*it)->clone());
449       }
450 }
451 
~case_t()452 ExpConditional::case_t::~case_t()
453 {
454       delete cond_;
455       while (! true_clause_.empty()) {
456 	    Expression*tmp = true_clause_.front();
457 	    true_clause_.pop_front();
458 	    delete tmp;
459       }
460 }
461 
ExpSelected(Expression * selector,std::list<case_t * > * options)462 ExpSelected::ExpSelected(Expression*selector, std::list<case_t*>*options)
463 : ExpConditional(NULL, NULL, options), selector_(selector)
464 {
465     // Currently condition field contains only value,
466     // so substitute it with a comparison to create a valid condition
467     for(std::list<case_t*>::iterator it = options_.begin();
468             it != options_.end(); ++it) {
469         Expression*cond = (*it)->condition();
470 
471         if(cond)
472             (*it)->set_condition(new ExpRelation(ExpRelation::EQ, selector_->clone(), cond));
473     }
474 }
475 
~ExpSelected()476 ExpSelected::~ExpSelected()
477 {
478 }
479 
clone() const480 Expression*ExpSelected::clone() const
481 {
482       std::list<case_t*>*new_options = NULL;
483       if(!options_.empty()) {
484           new_options = new std::list<case_t*>();
485 
486           for(std::list<case_t*>::const_iterator it = options_.begin();
487                   it != options_.end(); ++it) {
488               new_options->push_back(new case_t(**it));
489           }
490       }
491 
492       return new ExpSelected(selector_->clone(), new_options);
493 }
494 
visit(ExprVisitor & func)495 void ExpConditional::case_t::visit(ExprVisitor&func)
496 {
497       func.down();
498       if(cond_)
499           cond_->visit(func);
500 
501       for(std::list<Expression*>::iterator it = true_clause_.begin();
502               it != true_clause_.end(); ++it)
503           (*it)->visit(func);
504       func.up();
505 }
506 
ExpEdge(ExpEdge::fun_t typ,Expression * op)507 ExpEdge::ExpEdge(ExpEdge::fun_t typ, Expression*op)
508 : ExpUnary(op), fun_(typ)
509 {
510 }
511 
~ExpEdge()512 ExpEdge::~ExpEdge()
513 {
514 }
515 
ExpFunc(perm_string nn)516 ExpFunc::ExpFunc(perm_string nn)
517 : name_(nn), def_(0)
518 {
519 }
520 
ExpFunc(perm_string nn,list<Expression * > * args)521 ExpFunc::ExpFunc(perm_string nn, list<Expression*>*args)
522 : name_(nn), argv_(args->size()), def_(0)
523 {
524       for (size_t idx = 0; idx < argv_.size() ; idx += 1) {
525 	    ivl_assert(*this, !args->empty());
526 	    argv_[idx] = args->front();
527 	    args->pop_front();
528       }
529       ivl_assert(*this, args->empty());
530 }
531 
~ExpFunc()532 ExpFunc::~ExpFunc()
533 {
534       for (size_t idx = 0 ; idx < argv_.size() ; idx += 1)
535 	    delete argv_[idx];
536 }
537 
clone() const538 Expression*ExpFunc::clone() const {
539     std::list<Expression*>*new_args = NULL;
540 
541     if(!argv_.empty()) {
542         new_args = new std::list<Expression*>();
543         for(std::vector<Expression*>::const_iterator it = argv_.begin();
544                 it != argv_.end(); ++it)
545             new_args->push_back((*it)->clone());
546     }
547 
548     ExpFunc*f = new ExpFunc(name_, new_args);
549     f->def_ = def_;
550 
551     return f;
552 }
553 
visit(ExprVisitor & func)554 void ExpFunc::visit(ExprVisitor&func)
555 {
556     func.down();
557     func(this);
558 
559     if(!argv_.empty()) {
560         for(std::vector<Expression*>::iterator it = argv_.begin();
561                 it != argv_.end(); ++it)
562             (*it)->visit(func);
563     }
564 
565     func.up();
566 }
567 
func_ret_type() const568 const VType* ExpFunc::func_ret_type() const
569 {
570     return def_ ? def_->peek_return_type() : NULL;
571 }
572 
match_signature(Entity * ent,ScopeBase * scope) const573 SubprogramHeader*ExpFunc::match_signature(Entity*ent, ScopeBase*scope) const
574 {
575     SubprogramHeader*prog = NULL;
576     list<const VType*> arg_types;
577 
578       // Create a list of argument types to find a matching subprogram
579     for(vector<Expression*>::const_iterator it = argv_.begin();
580             it != argv_.end(); ++it) {
581         arg_types.push_back((*it)->probe_type(ent, scope));
582     }
583 
584     prog = scope->match_subprogram(name_, &arg_types);
585 
586     if(!prog)
587         prog = library_match_subprogram(name_, &arg_types);
588 
589     if(!prog) {
590         cerr << get_fileline() << ": sorry: could not find function ";
591         emit_subprogram_sig(cerr, name_, arg_types);
592         cerr << endl;
593         ivl_assert(*this, false);
594     }
595 
596     return prog;
597 }
598 
ExpInteger(int64_t val)599 ExpInteger::ExpInteger(int64_t val)
600 : value_(val)
601 {
602 }
603 
~ExpInteger()604 ExpInteger::~ExpInteger()
605 {
606 }
607 
evaluate(Entity *,ScopeBase *,int64_t & val) const608 bool ExpInteger::evaluate(Entity*, ScopeBase*, int64_t&val) const
609 {
610       val = value_;
611       return true;
612 }
613 
ExpReal(double val)614 ExpReal::ExpReal(double val)
615 : value_(val)
616 {
617 }
618 
~ExpReal()619 ExpReal::~ExpReal()
620 {
621 }
622 
ExpLogical(ExpLogical::fun_t ty,Expression * op1,Expression * op2)623 ExpLogical::ExpLogical(ExpLogical::fun_t ty, Expression*op1, Expression*op2)
624 : ExpBinary(op1, op2), fun_(ty)
625 {
626 }
627 
~ExpLogical()628 ExpLogical::~ExpLogical()
629 {
630 }
631 
ExpName(perm_string nn)632 ExpName::ExpName(perm_string nn)
633 : name_(nn), indices_(NULL)
634 {
635 }
636 
ExpName(perm_string nn,list<Expression * > * indices)637 ExpName::ExpName(perm_string nn, list<Expression*>*indices)
638 : name_(nn), indices_(indices)
639 {
640 }
641 
ExpName(ExpName * prefix,perm_string nn,std::list<Expression * > * indices)642 ExpName::ExpName(ExpName*prefix, perm_string nn, std::list<Expression*>*indices)
643 : prefix_(prefix), name_(nn), indices_(indices)
644 {
645 }
646 
~ExpName()647 ExpName::~ExpName()
648 {
649     if(indices_) {
650         for(list<Expression*>::iterator it = indices_->begin();
651                 it != indices_->end(); ++it) {
652             delete *it;
653         }
654 
655         delete indices_;
656     }
657 }
658 
clone() const659 Expression*ExpName::clone() const {
660     list<Expression*>*new_indices = NULL;
661 
662     if(indices_) {
663         new_indices = new list<Expression*>();
664 
665         for(list<Expression*>::const_iterator it = indices_->begin();
666                 it != indices_->end(); ++it) {
667             new_indices->push_back((*it)->clone());
668         }
669     }
670 
671     return new ExpName(static_cast<ExpName*>(safe_clone(prefix_.get())),
672             name_, new_indices);
673 }
674 
add_index(std::list<Expression * > * idx)675 void ExpName::add_index(std::list<Expression*>*idx)
676 {
677       if(!indices_)
678           indices_ = new list<Expression*>();
679 
680       indices_->splice(indices_->end(), *idx);
681 }
682 
symbolic_compare(const Expression * that) const683 bool ExpName::symbolic_compare(const Expression*that) const
684 {
685       const ExpName*that_name = dynamic_cast<const ExpName*> (that);
686       if (that_name == 0)
687 	    return false;
688 
689       if (name_ != that_name->name_)
690 	    return false;
691 
692       if (that_name->indices_ && !indices_)
693 	    return false;
694       if (indices_ && !that_name->indices_)
695 	    return false;
696 
697       if (indices_) {
698 	    assert(that_name->indices_);
699 
700             if(indices_->size() != that_name->indices_->size())
701                 return false;
702 
703             list<Expression*>::const_iterator it, jt;
704             it = indices_->begin();
705             jt = that_name->indices_->begin();
706 
707             for(unsigned int i = 0; i < indices_->size(); ++i) {
708                 if(!(*it)->symbolic_compare(*jt))
709                     return false;
710 
711                 ++it;
712                 ++jt;
713             }
714       }
715 
716       return true;
717 }
718 
index(unsigned int number) const719 Expression*ExpName::index(unsigned int number) const
720 {
721     if(!indices_)
722         return NULL;
723 
724     if(number >= indices_->size())
725         return NULL;
726 
727     if(number == 0)
728         return indices_->front();
729 
730     list<Expression*>::const_iterator it = indices_->begin();
731     advance(it, number);
732 
733     return *it;
734 }
735 
visit(ExprVisitor & func)736 void ExpName::visit(ExprVisitor&func)
737 {
738       func.down();
739       func(this);
740 
741       if(prefix_.get())
742           prefix_.get()->visit(func);
743 
744       if(indices_) {
745           for(list<Expression*>::const_iterator it = indices_->begin();
746                   it != indices_->end(); ++it) {
747               (*it)->visit(func);
748           }
749       }
750 
751       func.up();
752 }
753 
emit(ostream & out,Entity * ent,ScopeBase * scope) const754 int ExpName::index_t::emit(ostream&out, Entity*ent, ScopeBase*scope) const
755 {
756       int errors = 0;
757 
758       out << "(";
759 
760       if(idx_ && size_) {
761         errors += idx_->emit(out, ent, scope);
762         out << "*";
763         errors += size_->emit(out, ent, scope);
764       }
765 
766       if(offset_) {
767         if(idx_ && size_)
768           out << "+";
769         errors += offset_->emit(out, ent, scope);
770       }
771 
772       out << ")";
773       return errors;
774 }
775 
ExpRelation(ExpRelation::fun_t ty,Expression * op1,Expression * op2)776 ExpRelation::ExpRelation(ExpRelation::fun_t ty, Expression*op1, Expression*op2)
777 : ExpBinary(op1, op2), fun_(ty)
778 {
779 }
780 
~ExpRelation()781 ExpRelation::~ExpRelation()
782 {
783 }
784 
ExpScopedName(perm_string scope,ExpName * exp)785 ExpScopedName::ExpScopedName(perm_string scope, ExpName*exp)
786 : scope_name_(scope), scope_(NULL), name_(exp)
787 {
788 }
789 
~ExpScopedName()790 ExpScopedName::~ExpScopedName()
791 {
792     delete name_;
793 }
794 
visit(ExprVisitor & func)795 void ExpScopedName::visit(ExprVisitor&func)
796 {
797     func.down();
798     func(this);
799     name_->visit(func);
800     func.up();
801 }
802 
get_scope(const ScopeBase * scope)803 ScopeBase*ExpScopedName::get_scope(const ScopeBase*scope)
804 {
805     if(!scope_)
806         scope_ = scope->find_scope(scope_name_);
807 
808     return scope_;
809 }
810 
get_scope(const ScopeBase * scope) const811 ScopeBase*ExpScopedName::get_scope(const ScopeBase*scope) const
812 {
813     return scope_ ? scope_ : scope->find_scope(scope_name_);
814 }
815 
ExpShift(ExpShift::shift_t op,Expression * op1,Expression * op2)816 ExpShift::ExpShift(ExpShift::shift_t op, Expression*op1, Expression*op2)
817 : ExpBinary(op1, op2), shift_(op)
818 {
819 }
820 
ExpString(const char * value)821 ExpString::ExpString(const char* value)
822 : value_(value)
823 {
824 }
825 
~ExpString()826 ExpString::~ExpString()
827 {
828 }
829 
ExpUAbs(Expression * op1)830 ExpUAbs::ExpUAbs(Expression*op1)
831 : ExpUnary(op1)
832 {
833 }
834 
~ExpUAbs()835 ExpUAbs::~ExpUAbs()
836 {
837 }
838 
ExpUNot(Expression * op1)839 ExpUNot::ExpUNot(Expression*op1)
840 : ExpUnary(op1)
841 {
842 }
843 
~ExpUNot()844 ExpUNot::~ExpUNot()
845 {
846 }
847 
ExpUMinus(Expression * op1)848 ExpUMinus::ExpUMinus(Expression*op1)
849 : ExpUnary(op1)
850 {
851 }
852 
~ExpUMinus()853 ExpUMinus::~ExpUMinus()
854 {
855 }
856 
ExpCast(Expression * base,const VType * type)857 ExpCast::ExpCast(Expression*base, const VType*type) :
858     base_(base), type_(type)
859 {
860 }
861 
~ExpCast()862 ExpCast::~ExpCast()
863 {
864 }
865 
visit(ExprVisitor & func)866 void ExpCast::visit(ExprVisitor&func)
867 {
868     func.down();
869     func(this);
870     base_->visit(func);
871     func.up();
872 }
873 
ExpNew(Expression * size)874 ExpNew::ExpNew(Expression*size) :
875     size_(size)
876 {
877 }
878 
~ExpNew()879 ExpNew::~ExpNew()
880 {
881     delete size_;
882 }
883 
visit(ExprVisitor & func)884 void ExpNew::visit(ExprVisitor&func)
885 {
886     func.down();
887     func(this);
888     size_->visit(func);
889     func.up();
890 }
891 
ExpTime(uint64_t amount,timeunit_t unit)892 ExpTime::ExpTime(uint64_t amount, timeunit_t unit)
893 : amount_(amount), unit_(unit)
894 {
895 }
896 
to_fs() const897 double ExpTime::to_fs() const
898 {
899     double val = amount_;
900 
901     switch(unit_) {
902         case FS: break;
903         case PS: val *= 1e3; break;
904         case NS: val *= 1e6; break;
905         case US: val *= 1e9; break;
906         case MS: val *= 1e12; break;
907         case S:  val *= 1e15; break;
908         default: ivl_assert(*this, false); break;
909     }
910 
911     return val;
912 }
913 
ExpRange(Expression * left_idx,Expression * right_idx,range_dir_t dir)914 ExpRange::ExpRange(Expression*left_idx, Expression*right_idx, range_dir_t dir)
915 : left_(left_idx), right_(right_idx), direction_(dir), range_expr_(false),
916     range_base_(NULL)
917 {
918 }
919 
ExpRange(ExpName * base,bool reverse_range)920 ExpRange::ExpRange(ExpName*base, bool reverse_range)
921 : left_(NULL), right_(NULL), direction_(AUTO), range_expr_(true),
922     range_base_(base), range_reverse_(reverse_range)
923 {
924 }
925 
~ExpRange()926 ExpRange::~ExpRange()
927 {
928     delete left_;
929     delete right_;
930     delete range_base_;
931 }
932 
clone() const933 Expression*ExpRange::clone() const
934 {
935     if(range_expr_)
936         return new ExpRange(static_cast<ExpName*>(range_base_->clone()), range_reverse_);
937     else
938         return new ExpRange(left_->clone(), right_->clone(), direction_);
939 }
940 
msb()941 Expression* ExpRange::msb()
942 {
943     ivl_assert(*this, direction() != AUTO);
944 
945     switch(direction()) {
946         case DOWNTO: return left_;
947         case TO: return right_;
948         default: return NULL;
949     }
950 
951     return NULL;
952 }
953 
lsb()954 Expression* ExpRange::lsb()
955 {
956     ivl_assert(*this, direction() != AUTO);
957 
958     switch(direction()) {
959         case DOWNTO: return right_;
960         case TO: return left_;
961         default: return NULL;
962     }
963 
964     return NULL;
965 }
966 
left()967 Expression*ExpRange::left()
968 {
969     if(range_expr_ && !left_)
970         // TODO check if it is an object or type
971         left_ = new ExpObjAttribute(static_cast<ExpName*>(range_base_->clone()),
972                                     ExpAttribute::LEFT, NULL);
973 
974     return left_;
975 }
976 
right()977 Expression*ExpRange::right()
978 {
979     if(range_expr_ && !right_)
980         // TODO check if it is an object or type
981         right_ = new ExpObjAttribute(static_cast<ExpName*>(range_base_->clone()),
982                                     ExpAttribute::RIGHT, NULL);
983     return right_;
984 }
985 
ExpDelay(Expression * expr,Expression * delay)986 ExpDelay::ExpDelay(Expression*expr, Expression*delay)
987 : expr_(expr), delay_(delay)
988 {
989 }
990 
~ExpDelay()991 ExpDelay::~ExpDelay()
992 {
993     delete expr_;
994     delete delay_;
995 }
996 
visit(ExprVisitor & func)997 void ExpDelay::visit(ExprVisitor&func)
998 {
999     func.down();
1000     func(this);
1001     expr_->visit(func);
1002     delay_->visit(func);
1003     func.up();
1004 }
1005