Skip to content

Commit

Permalink
irgen: Generate explicit instantiations (p4lang#4681)
Browse files Browse the repository at this point in the history
- explicit instantiations for all Vector and IndexedVector used in any
  .def file
  • Loading branch information
ChrisDodd authored May 30, 2024
1 parent 90e90e6 commit 675d5bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
38 changes: 30 additions & 8 deletions tools/ir-generator/irclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ void IrDefinitions::generate(std::ostream &t, std::ostream &out, std::ostream &i
}
impl << " };\n" << std::endl;

impl << "template class IR::Vector<IR::Node>;" << std::endl;
out << "extern template class IR::Vector<IR::Node>;" << std::endl;
impl << "template class IR::IndexedVector<IR::Node>;" << std::endl;
out << "extern template class IR::IndexedVector<IR::Node>;" << std::endl;
for (auto cls : *getClasses()) {
if (cls->needVector || cls->needIndexedVector) {
impl << "template class IR::Vector<IR::" << cls->containedIn << cls->name << ">;"
<< std::endl;
out << "extern template class IR::Vector<IR::" << cls->containedIn << cls->name << ">;"
<< std::endl;
}
if (cls->needIndexedVector) {
impl << "template class IR::IndexedVector<IR::" << cls->containedIn << cls->name << ">;"
<< std::endl;
out << "extern template class IR::IndexedVector<IR::" << cls->containedIn << cls->name
<< ">;" << std::endl;
}
}

for (auto e : elements) {
e->generate_hdr(out);
e->generate_impl(impl);
Expand Down Expand Up @@ -564,17 +583,10 @@ void IrEnumType::generate_hdr(std::ostream &out) const {

////////////////////////////////////////////////////////////////////////////////////

void IrField::generate(std::ostream &out, bool asField) const {
if (asField) {
out << IrClass::indent;
if (isStatic) out << "static ";
if (isConst) out << "const ";
}

void IrField::resolve() {
auto tmpl = dynamic_cast<const TemplateInstantiation *>(type);
const IrClass *cls = type->resolve(clss ? clss->containedIn : nullptr);
if (cls) {
// FIXME -- should be doing this in resolve and converting type to PointerType as needed
if (tmpl) {
if (cls->kind != NodeKind::Template)
throw Util::CompilationError("Template args with non-template class %1%", cls);
Expand Down Expand Up @@ -602,6 +614,16 @@ void IrField::generate(std::ostream &out, bool asField) const {
throw Util::CompilationError("No args for template %1%", cls);
}
}
}

void IrField::generate(std::ostream &out, bool asField) const {
if (asField) {
out << IrClass::indent;
if (isStatic) out << "static ";
if (isConst) out << "const ";
}

const IrClass *cls = type->resolve(clss ? clss->containedIn : nullptr);
if (cls != nullptr && !isInline) out << "const ";
out << type->toString();
if (cls != nullptr && !isInline) out << "*";
Expand Down
1 change: 1 addition & 0 deletions tools/ir-generator/irclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class IrField : public IrElement {
: IrField(Util::SourceInfo(), type, name, init, flags) {}
IrField(const Type *type, cstring name, int flags)
: IrField(Util::SourceInfo(), type, name, cstring(), flags) {}
void resolve() override;
void generate(std::ostream &out, bool asField) const;
void generate_hdr(std::ostream &out) const override { generate(out, true); }
void generate_impl(std::ostream &) const override;
Expand Down

0 comments on commit 675d5bc

Please sign in to comment.