@@ -24,41 +24,39 @@ std::shared_ptr<TypeDef> IR::addTypeDef(std::string name,
2424void IR::addEnum (std::string name, const std::string &type,
2525 std::vector<Enumerator> enumerators,
2626 std::shared_ptr<Location> location) {
27- std::shared_ptr<Enum> e =
28- std::make_shared<Enum>( std:: move (name), type, std::move (enumerators));
27+ std::shared_ptr<Enum> e = std::make_shared<Enum>(
28+ std::move (name), type, std::move (enumerators), std::move (location ));
2929 enums.push_back (e);
3030 if (!e->isAnonymous ()) {
31- typeDefs.push_back (e->generateTypeDef (std::move (location) ));
31+ typeDefs.push_back (e->generateTypeDef ());
3232 }
3333}
3434
3535void IR::addStruct (std::string name, std::vector<Field *> fields,
3636 uint64_t typeSize, std::shared_ptr<Location> location) {
37- std::shared_ptr<Struct> s =
38- std::make_shared<Struct>( name, std::move (fields), typeSize);
37+ std::shared_ptr<Struct> s = std::make_shared<Struct>(
38+ name, std::move (fields), typeSize, std::move (location) );
3939 structs.push_back (s);
4040 std::shared_ptr<TypeDef> typeDef = getTypeDefWithName (" struct_" + name);
4141 if (typeDef) {
4242 /* the struct type used to be opaque type, typeDef contains nullptr */
4343 typeDef.get ()->setType (s);
44- typeDef.get ()->setLocation (location);
4544 } else {
46- typeDefs.push_back (s->generateTypeDef (std::move (location) ));
45+ typeDefs.push_back (s->generateTypeDef ());
4746 }
4847}
4948
5049void IR::addUnion (std::string name, std::vector<Field *> fields,
5150 uint64_t maxSize, std::shared_ptr<Location> location) {
52- std::shared_ptr<Union> u =
53- std::make_shared<Union>( name, std::move (fields), maxSize);
51+ std::shared_ptr<Union> u = std::make_shared<Union>(
52+ name, std::move (fields), maxSize, std::move (location) );
5453 unions.push_back (u);
5554 std::shared_ptr<TypeDef> typeDef = getTypeDefWithName (" union_" + name);
5655 if (typeDef) {
5756 /* the union type used to be opaque type, typeDef contains nullptr */
5857 typeDef.get ()->setType (u);
59- typeDef.get ()->setLocation (location);
6058 } else {
61- typeDefs.push_back (u->generateTypeDef (std::move (location) ));
59+ typeDefs.push_back (u->generateTypeDef ());
6260 }
6361}
6462
@@ -175,7 +173,7 @@ void IR::generate(const std::string &excludePrefix) {
175173 if (!generated) {
176174 setScalaNames ();
177175 filterDeclarations (excludePrefix);
178- removeUnusedExternalTypes ();
176+ removeUnusedExternalTypedefs ();
179177 generated = true ;
180178 }
181179}
@@ -364,14 +362,13 @@ IR::~IR() {
364362 varDefines.clear ();
365363}
366364
367- void IR::removeUnusedExternalTypes () {
365+ void IR::removeUnusedExternalTypedefs () {
368366 for (auto it = typeDefs.begin (); it != typeDefs.end ();) {
369367 std::shared_ptr<TypeDef> typeDef = *it;
370368 std::shared_ptr<Location> location = typeDef->getLocation ();
371369 auto *sourceLocation = dynamic_cast <SourceLocation *>(location.get ());
372370 if (sourceLocation && !sourceLocation->isMainFile ()) {
373371 if (!isTypeUsed (typeDef)) {
374- removeStructOrUnionOrEnum (typeDef->getType ());
375372 it = typeDefs.erase (it);
376373 } else {
377374 ++it;
@@ -380,6 +377,9 @@ void IR::removeUnusedExternalTypes() {
380377 ++it;
381378 }
382379 }
380+ removeUnusedExternalTypeAndTypedef (structs);
381+ removeUnusedExternalTypeAndTypedef (unions);
382+ removeUnusedExternalTypeAndTypedef (enums);
383383}
384384
385385template <typename T>
@@ -395,15 +395,22 @@ void IR::removeDeclaration(std::vector<std::shared_ptr<T>> &declarations,
395395 }
396396}
397397
398- void IR::removeStructOrUnionOrEnum (std::shared_ptr<Type> type) {
399- if (isInstanceOf<Struct>(type.get ())) {
400- auto *s = dynamic_cast <Struct *>(type.get ());
401- removeDeclaration (structs, s);
402- } else if (isInstanceOf<Union>(type.get ())) {
403- auto *u = dynamic_cast <Union *>(type.get ());
404- removeDeclaration (unions, u);
405- } else if (isInstanceOf<Enum>(type.get ())) {
406- auto *e = dynamic_cast <Enum *>(type.get ());
407- removeDeclaration (enums, e);
398+ template <typename T>
399+ void IR::removeUnusedExternalTypeAndTypedef (
400+ std::vector<std::shared_ptr<T>> &types) {
401+ for (auto it = types.begin (); it != types.end ();) {
402+ std::shared_ptr<T> t = *it;
403+ auto *sourceLocation =
404+ dynamic_cast <SourceLocation *>(t->getLocation ().get ());
405+ if (sourceLocation && !sourceLocation->isMainFile ()) {
406+ std::shared_ptr<TypeDef> typeDef =
407+ getTypeDefWithName (t->getTypeAlias ());
408+ if (!isTypeUsed (typeDef)) {
409+ it = types.erase (it);
410+ removeDeclaration (typeDefs, typeDef.get ());
411+ } else {
412+ ++it;
413+ }
414+ }
408415 }
409416}
0 commit comments