Add forward declarations for functions

This patch adds a forward declaration for every user funciton.
This fixes VHDL compile problems if a function calls another
before it has been declared.
This commit is contained in:
Nick Gasson 2008-08-03 10:50:31 +01:00
parent c1b5424ca6
commit c8cbac58f5
3 changed files with 32 additions and 0 deletions

View File

@ -529,6 +529,11 @@ static int draw_function(ivl_scope_t scope, ivl_scope_t parent)
}
set_active_entity(NULL);
// Add a forward declaration too in case it is called by
// another function that has already been added
ent->get_arch()->get_scope()->add_forward_decl
(new vhdl_forward_fdecl(func));
ent->get_arch()->get_scope()->add_decl(func);
return 0;
}

View File

@ -48,6 +48,11 @@ void vhdl_scope::add_decl(vhdl_decl *decl)
decls_.push_back(decl);
}
void vhdl_scope::add_forward_decl(vhdl_decl *decl)
{
decls_.push_front(decl);
}
vhdl_decl *vhdl_scope::get_decl(const std::string &name) const
{
decl_list_t::const_iterator it;
@ -831,6 +836,16 @@ void vhdl_function::emit(std::ostream &of, int level) const
of << "end function;";
}
void vhdl_forward_fdecl::emit(std::ostream &of, int level) const
{
of << "function " << f_->get_name() << " (";
emit_children<vhdl_decl>(of, f_->scope_.get_decls(), level, ";");
of << ") ";
newline(of, level);
of << "return " << f_->type_->get_string() << ";";
newline(of, level);
}
void vhdl_param_decl::emit(std::ostream &of, int level) const
{
of << name_ << " : ";

View File

@ -608,6 +608,7 @@ public:
~vhdl_scope();
void add_decl(vhdl_decl *decl);
void add_forward_decl(vhdl_decl *decl);
vhdl_decl *get_decl(const std::string &name) const;
bool have_declared(const std::string &name) const;
bool contained_within(const vhdl_scope *other) const;
@ -647,6 +648,7 @@ protected:
class vhdl_function : public vhdl_decl, public vhdl_procedural {
friend class vhdl_forward_fdecl;
public:
vhdl_function(const char *name, vhdl_type *ret_type);
@ -657,6 +659,16 @@ private:
vhdl_scope variables_;
};
class vhdl_forward_fdecl : public vhdl_decl {
public:
vhdl_forward_fdecl(const vhdl_function *f)
: vhdl_decl((f->get_name() + "_Forward").c_str()), f_(f) {}
void emit(std::ostream &of, int level) const;
private:
const vhdl_function *f_;
};
class vhdl_process : public vhdl_conc_stmt, public vhdl_procedural {
public: