Skip to content

Commit db243d9

Browse files
committed
codegen: store prev defined containers as a class field
1 parent 6aead62 commit db243d9

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

oi/CodeGen.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,10 @@ void getContainerSizeFuncDecl(const Container& c, std::string& code) {
385385
code += fmt.str();
386386
}
387387

388-
void getContainerSizeFuncDef(const Container& c, std::string& code) {
389-
// TODO deduplicate containers in a better way (this isn't robust to vector
390-
// reallocs):
391-
// - implement hash for ContainerInfo
392-
// - use ref<ContainerInfo>
393-
static std::unordered_set<const ContainerInfo*> usedContainers{};
394-
if (!usedContainers.insert(&c.containerInfo_).second) {
388+
void getContainerSizeFuncDef(std::unordered_set<const ContainerInfo*>& used,
389+
const Container& c,
390+
std::string& code) {
391+
if (!used.insert(&c.containerInfo_).second) {
395392
return;
396393
}
397394

@@ -410,15 +407,17 @@ void addGetSizeFuncDecls(const TypeGraph& typeGraph, std::string& code) {
410407
}
411408
}
412409

413-
void addGetSizeFuncDefs(const TypeGraph& typeGraph,
414-
SymbolService& symbols,
415-
bool polymorphicInheritance,
416-
std::string& code) {
410+
void addGetSizeFuncDefs(
411+
const TypeGraph& typeGraph,
412+
SymbolService& symbols,
413+
std::unordered_set<const ContainerInfo*>& definedContainers,
414+
bool polymorphicInheritance,
415+
std::string& code) {
417416
for (const Type& t : typeGraph.finalTypes) {
418417
if (const auto* c = dynamic_cast<const Class*>(&t)) {
419418
getClassSizeFuncDef(*c, symbols, polymorphicInheritance, code);
420419
} else if (const auto* con = dynamic_cast<const Container*>(&t)) {
421-
getContainerSizeFuncDef(*con, code);
420+
getContainerSizeFuncDef(definedContainers, *con, code);
422421
}
423422
}
424423
}
@@ -525,9 +524,10 @@ class TypeHandler<DB, %1%> {
525524
.str();
526525
}
527526

528-
void getContainerTypeHandler(const Container& c, std::string& code) {
529-
static std::unordered_set<const ContainerInfo*> usedContainers{};
530-
if (!usedContainers.insert(&c.containerInfo_).second) {
527+
void getContainerTypeHandler(std::unordered_set<const ContainerInfo*>& used,
528+
const Container& c,
529+
std::string& code) {
530+
if (!used.insert(&c.containerInfo_).second) {
531531
return;
532532
}
533533

@@ -543,12 +543,15 @@ void getContainerTypeHandler(const Container& c, std::string& code) {
543543
code += fmt.str();
544544
}
545545

546-
void addTypeHandlers(const TypeGraph& typeGraph, std::string& code) {
546+
void addTypeHandlers(
547+
std::unordered_set<const ContainerInfo*>& definedContainers,
548+
const TypeGraph& typeGraph,
549+
std::string& code) {
547550
for (const Type& t : typeGraph.finalTypes) {
548551
if (const auto* c = dynamic_cast<const Class*>(&t)) {
549552
getClassTypeHandler(*c, code);
550553
} else if (const auto* con = dynamic_cast<const Container*>(&t)) {
551-
getContainerTypeHandler(*con, code);
554+
getContainerTypeHandler(definedContainers, *con, code);
552555
}
553556
}
554557
}
@@ -664,13 +667,13 @@ void CodeGen::generate(
664667

665668
if (config_.features[Feature::TypedDataSegment]) {
666669
addStandardTypeHandlers(code);
667-
addTypeHandlers(typeGraph, code);
670+
addTypeHandlers(definedContainers_, typeGraph, code);
668671
} else {
669672
addStandardGetSizeFuncDecls(code);
670673
addGetSizeFuncDecls(typeGraph, code);
671674

672675
addStandardGetSizeFuncDefs(code);
673-
addGetSizeFuncDefs(typeGraph, symbols_,
676+
addGetSizeFuncDefs(typeGraph, symbols_, definedContainers_,
674677
config_.features[Feature::PolymorphicInheritance], code);
675678
}
676679

oi/CodeGen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <filesystem>
1919
#include <functional>
2020
#include <string>
21+
#include <unordered_set>
2122

2223
#include "ContainerInfo.h"
2324
#include "OICodeGen.h"
@@ -56,4 +57,5 @@ class CodeGen {
5657
OICodeGen::Config config_;
5758
SymbolService& symbols_;
5859
std::vector<ContainerInfo> containerInfos_;
60+
std::unordered_set<const ContainerInfo*> definedContainers_;
5961
};

0 commit comments

Comments
 (0)