Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize first field in named union constructors #784

Merged
merged 2 commits into from
Jun 16, 2024

Conversation

lethosor
Copy link
Member

@lethosor lethosor commented Jun 15, 2024

This allows job_spec_data fields embedded in other types to be initialized to -1 as expected

[lua]# ~df.manager_order:new().specdata
<job_spec_data: 0x7fffac2e267c>
hist_figure_id           = -1
race                     = -1
improvement              = -1

Full diff of codegen output - extra eyes on this appreciated, as I haven't verified that all of the changes are reasonable:

diff -ur codegen-old/agreement_details.h codegen/agreement_details.h
--- codegen-old/agreement_details.h	2024-06-15 00:36:56.859387672 -0400
+++ codegen/agreement_details.h	2024-06-15 02:20:17.780758234 -0400
@@ -45,7 +45,10 @@
       df::agreement_details_data_plot_infiltration_coup* PlotInfiltrationCoup;
       df::agreement_details_data_plot_frame_treason* PlotFrameTreason;
       df::agreement_details_data_plot_induce_war* PlotInduceWar;
-      T_data(){memset(this, 0, sizeof(T_data));}
+      T_data() {
+        memset(this, 0, sizeof(*this));
+        JoinParty = 0;
+      }
     };
     T_data data;
     df::agreement_details_type type;
diff -ur codegen-old/army_controller.h codegen/army_controller.h
--- codegen-old/army_controller.h	2024-06-15 00:36:56.819387514 -0400
+++ codegen/army_controller.h	2024-06-15 02:20:17.768758179 -0400
@@ -102,7 +102,10 @@
       df::army_controller_goal_abduct_hfst* goal_abduct_hf;
       df::army_controller_goal_sabotage_entityst* goal_sabotage_entity;
       df::army_controller_goal_infiltrate_societyst* goal_infiltrate_society;
-      T_data(){memset(this, 0, sizeof(T_data));}
+      T_data() {
+        memset(this, 0, sizeof(*this));
+        goal_site_invasion = 0;
+      }
     };
     T_data data;
     df::army_controller_goal_type goal;
diff -ur codegen-old/building_civzonest.h codegen/building_civzonest.h
--- codegen-old/building_civzonest.h	2024-06-15 00:36:56.811387482 -0400
+++ codegen/building_civzonest.h	2024-06-15 02:20:17.780758234 -0400
@@ -70,7 +70,10 @@
       struct DFHACK_EXPORT T_whole {
         int32_t i1;
         int32_t i2;
-        T_whole(){memset(this, 0, sizeof(T_whole));}
+        T_whole() {
+          memset(this, 0, sizeof(*this));
+          i1 = 0;
+        }
       };
       T_whole whole;
       struct DFHACK_EXPORT T_gather {
@@ -95,7 +98,9 @@
         };
         T_flags flags;
         int32_t timer;
-        T_gather(){memset(this, 0, sizeof(T_gather));}
+        T_gather() {
+          memset(this, 0, sizeof(*this));
+        }
       };
       T_gather gather;
       union T_pen {
@@ -132,7 +137,10 @@
       struct DFHACK_EXPORT T_archery {
         int32_t dir_x;
         int32_t dir_y;
-        T_archery(){memset(this, 0, sizeof(T_archery));}
+        T_archery() {
+          memset(this, 0, sizeof(*this));
+          dir_x = 0;
+        }
       };
       T_archery archery;
       struct DFHACK_EXPORT T_pond {
@@ -154,10 +162,16 @@
         };
         T_flag flag;
         int16_t fill_timer;
-        T_pond(){memset(this, 0, sizeof(T_pond));}
+        T_pond() {
+          memset(this, 0, sizeof(*this));
+        }
       };
       T_pond pond;
-      T_zone_settings(){memset(this, 0, sizeof(T_zone_settings));}
+      T_zone_settings() {
+        memset(this, 0, sizeof(*this));
+        whole.i1 = 0;
+        whole.i2 = 0;
+      }
     };
     T_zone_settings zone_settings;
     std::vector<int32_t > home_general_hf;
diff -ur codegen-old/entity_event.h codegen/entity_event.h
--- codegen-old/entity_event.h	2024-06-15 00:36:56.827387546 -0400
+++ codegen/entity_event.h	2024-06-15 02:20:17.776758216 -0400
@@ -17,7 +17,10 @@
         int32_t site_id;
         int32_t attack_leader_hf;
         int32_t attack_commander_hf;
-        T_invasion(){memset(this, 0, sizeof(T_invasion));}
+        T_invasion() {
+          memset(this, 0, sizeof(*this));
+          entity_id = -1;
+        }
       };
       T_invasion invasion;
       struct DFHACK_EXPORT T_abduction {
@@ -25,13 +28,19 @@
         int32_t site_id;
         int32_t abductor_id;
         int32_t event;
-        T_abduction(){memset(this, 0, sizeof(T_abduction));}
+        T_abduction() {
+          memset(this, 0, sizeof(*this));
+          histfig_id = -1;
+        }
       };
       T_abduction abduction;
       struct DFHACK_EXPORT T_incident {
         df::incident_type type;
         int32_t incident_id;
-        T_incident(){memset(this, 0, sizeof(T_incident));}
+        T_incident() {
+          memset(this, 0, sizeof(*this));
+          type = ENUM_FIRST_ITEM(incident_type);
+        }
       };
       T_incident incident;
       struct DFHACK_EXPORT T_occupation {
@@ -39,27 +48,39 @@
         int32_t entity_id;
         int32_t leader_hf;
         int32_t evicted_entity_id;
-        T_occupation(){memset(this, 0, sizeof(T_occupation));}
+        T_occupation() {
+          memset(this, 0, sizeof(*this));
+          site_id = -1;
+        }
       };
       T_occupation occupation;
       struct DFHACK_EXPORT T_beast {
         int32_t histfig_id;
         int32_t site_id;
         int32_t region_id;
-        T_beast(){memset(this, 0, sizeof(T_beast));}
+        T_beast() {
+          memset(this, 0, sizeof(*this));
+          histfig_id = -1;
+        }
       };
       T_beast beast;
       struct DFHACK_EXPORT T_group {
         int32_t entity_id;
         int32_t site_id;
-        T_group(){memset(this, 0, sizeof(T_group));}
+        T_group() {
+          memset(this, 0, sizeof(*this));
+          entity_id = -1;
+        }
       };
       T_group group;
       struct DFHACK_EXPORT T_harass {
         int32_t entity_id;
         int32_t site_id;
         int32_t army_leader_hf_id;
-        T_harass(){memset(this, 0, sizeof(T_harass));}
+        T_harass() {
+          memset(this, 0, sizeof(*this));
+          entity_id = -1;
+        }
       };
       T_harass harass;
       struct DFHACK_EXPORT T_flee {
@@ -67,14 +88,20 @@
         int32_t from_site_id;
         int32_t army_entity_id;
         int32_t army_leader_hf_id;
-        T_flee(){memset(this, 0, sizeof(T_flee));}
+        T_flee() {
+          memset(this, 0, sizeof(*this));
+          refugee_entity_id = -1;
+        }
       };
       T_flee flee;
       struct DFHACK_EXPORT T_abandon {
         int32_t entity_id;
         int32_t site_id;
         int32_t parent_entity_id;
-        T_abandon(){memset(this, 0, sizeof(T_abandon));}
+        T_abandon() {
+          memset(this, 0, sizeof(*this));
+          entity_id = -1;
+        }
       };
       T_abandon abandon;
       struct DFHACK_EXPORT T_reclaimed {
@@ -82,7 +109,10 @@
         int32_t site_id;
         int32_t reclaimer_entity_id;
         int32_t leader_hf;
-        T_reclaimed(){memset(this, 0, sizeof(T_reclaimed));}
+        T_reclaimed() {
+          memset(this, 0, sizeof(*this));
+          behalf_entity_id = -1;
+        }
       };
       T_reclaimed reclaimed;
       struct DFHACK_EXPORT T_founded {
@@ -90,7 +120,10 @@
         int32_t site_id;
         int32_t parent_entity_id;
         int32_t leader_hf;
-        T_founded(){memset(this, 0, sizeof(T_founded));}
+        T_founded() {
+          memset(this, 0, sizeof(*this));
+          entity_id = -1;
+        }
       };
       T_founded founded;
       struct DFHACK_EXPORT T_reclaiming {
@@ -98,7 +131,10 @@
         int32_t site_id;
         int32_t civ_leader_hf;
         int32_t civ_commander_hf;
-        T_reclaiming(){memset(this, 0, sizeof(T_reclaiming));}
+        T_reclaiming() {
+          memset(this, 0, sizeof(*this));
+          entity_id = -1;
+        }
       };
       T_reclaiming reclaiming;
       struct DFHACK_EXPORT T_founding {
@@ -106,26 +142,38 @@
         int32_t region_id;
         int32_t civ_leader_hf;
         int32_t civ_commander_hf;
-        T_founding(){memset(this, 0, sizeof(T_founding));}
+        T_founding() {
+          memset(this, 0, sizeof(*this));
+          entity_id = -1;
+        }
       };
       T_founding founding;
       struct DFHACK_EXPORT T_leave {
         int32_t entity_id;
         int32_t site_id;
-        T_leave(){memset(this, 0, sizeof(T_leave));}
+        T_leave() {
+          memset(this, 0, sizeof(*this));
+          entity_id = -1;
+        }
       };
       T_leave leave;
       struct DFHACK_EXPORT T_insurrection {
         int32_t site_id;
         int32_t entity_id;
-        T_insurrection(){memset(this, 0, sizeof(T_insurrection));}
+        T_insurrection() {
+          memset(this, 0, sizeof(*this));
+          site_id = -1;
+        }
       };
       T_insurrection insurrection;
       struct DFHACK_EXPORT T_insurrection_end {
         int32_t site_id;
         int32_t entity_id;
         df::insurrection_outcome result;
-        T_insurrection_end(){memset(this, 0, sizeof(T_insurrection_end));}
+        T_insurrection_end() {
+          memset(this, 0, sizeof(*this));
+          site_id = -1;
+        }
       };
       T_insurrection_end insurrection_end;
       struct DFHACK_EXPORT T_succession {
@@ -133,14 +181,20 @@
         int32_t former_histfig_id;
         int32_t entity_id;
         int32_t position_assignment_id;
-        T_succession(){memset(this, 0, sizeof(T_succession));}
+        T_succession() {
+          memset(this, 0, sizeof(*this));
+          histfig_id = -1;
+        }
       };
       T_succession succession;
       struct DFHACK_EXPORT T_claim {
         int32_t entity_id;
         int32_t site_id;
         int32_t histfig_id;
-        T_claim(){memset(this, 0, sizeof(T_claim));}
+        T_claim() {
+          memset(this, 0, sizeof(*this));
+          entity_id = -1;
+        }
       };
       T_claim claim;
       struct DFHACK_EXPORT T_accept_tribute_offer {
@@ -148,7 +202,10 @@
         int32_t histfig1_id;
         int32_t entity2_id;
         int32_t histfig2_id;
-        T_accept_tribute_offer(){memset(this, 0, sizeof(T_accept_tribute_offer));}
+        T_accept_tribute_offer() {
+          memset(this, 0, sizeof(*this));
+          entity1_id = -1;
+        }
       };
       T_accept_tribute_offer accept_tribute_offer;
       struct DFHACK_EXPORT T_refuse_tribute_offer {
@@ -156,7 +213,10 @@
         int32_t histfig1_id;
         int32_t entity2_id;
         int32_t histfig2_id;
-        T_refuse_tribute_offer(){memset(this, 0, sizeof(T_refuse_tribute_offer));}
+        T_refuse_tribute_offer() {
+          memset(this, 0, sizeof(*this));
+          entity1_id = -1;
+        }
       };
       T_refuse_tribute_offer refuse_tribute_offer;
       struct DFHACK_EXPORT T_accept_tribute_demand {
@@ -164,7 +224,10 @@
         int32_t histfig1_id;
         int32_t entity2_id;
         int32_t histfig2_id;
-        T_accept_tribute_demand(){memset(this, 0, sizeof(T_accept_tribute_demand));}
+        T_accept_tribute_demand() {
+          memset(this, 0, sizeof(*this));
+          entity1_id = -1;
+        }
       };
       T_accept_tribute_demand accept_tribute_demand;
       struct DFHACK_EXPORT T_refuse_tribute_demand {
@@ -172,7 +235,10 @@
         int32_t histfig1_id;
         int32_t entity2_id;
         int32_t histfig2_id;
-        T_refuse_tribute_demand(){memset(this, 0, sizeof(T_refuse_tribute_demand));}
+        T_refuse_tribute_demand() {
+          memset(this, 0, sizeof(*this));
+          entity1_id = -1;
+        }
       };
       T_refuse_tribute_demand refuse_tribute_demand;
       struct DFHACK_EXPORT T_accept_peace_offer {
@@ -180,7 +246,10 @@
         int32_t histfig1_id;
         int32_t entity2_id;
         int32_t histfig2_id;
-        T_accept_peace_offer(){memset(this, 0, sizeof(T_accept_peace_offer));}
+        T_accept_peace_offer() {
+          memset(this, 0, sizeof(*this));
+          entity1_id = -1;
+        }
       };
       T_accept_peace_offer accept_peace_offer;
       struct DFHACK_EXPORT T_refuse_peace_offer {
@@ -188,7 +257,10 @@
         int32_t histfig1_id;
         int32_t entity2_id;
         int32_t histfig2_id;
-        T_refuse_peace_offer(){memset(this, 0, sizeof(T_refuse_peace_offer));}
+        T_refuse_peace_offer() {
+          memset(this, 0, sizeof(*this));
+          entity1_id = -1;
+        }
       };
       T_refuse_peace_offer refuse_peace_offer;
       struct DFHACK_EXPORT T_cease_tribute_offer {
@@ -196,65 +268,101 @@
         int32_t histfig1_id;
         int32_t entity2_id;
         int32_t histfig2_id;
-        T_cease_tribute_offer(){memset(this, 0, sizeof(T_cease_tribute_offer));}
+        T_cease_tribute_offer() {
+          memset(this, 0, sizeof(*this));
+          entity1_id = -1;
+        }
       };
       T_cease_tribute_offer cease_tribute_offer;
       struct DFHACK_EXPORT T_artifact_in_site {
         int32_t artifact_id;
         int32_t site_id;
         int32_t structure_id;
-        T_artifact_in_site(){memset(this, 0, sizeof(T_artifact_in_site));}
+        T_artifact_in_site() {
+          memset(this, 0, sizeof(*this));
+          artifact_id = -1;
+        }
       };
       T_artifact_in_site artifact_in_site;
       struct DFHACK_EXPORT T_artifact_in_subregion {
         int32_t artifact_id;
         int32_t subregion_id;
-        T_artifact_in_subregion(){memset(this, 0, sizeof(T_artifact_in_subregion));}
+        T_artifact_in_subregion() {
+          memset(this, 0, sizeof(*this));
+          artifact_id = -1;
+        }
       };
       T_artifact_in_subregion artifact_in_subregion;
       struct DFHACK_EXPORT T_artifact_in_feature_layer {
         int32_t artifact_id;
         int32_t feature_layer_id;
-        T_artifact_in_feature_layer(){memset(this, 0, sizeof(T_artifact_in_feature_layer));}
+        T_artifact_in_feature_layer() {
+          memset(this, 0, sizeof(*this));
+          artifact_id = -1;
+        }
       };
       T_artifact_in_feature_layer artifact_in_feature_layer;
       struct DFHACK_EXPORT T_artifact_in_inventory {
         int32_t artifact_id;
         int32_t hist_figure_id;
-        T_artifact_in_inventory(){memset(this, 0, sizeof(T_artifact_in_inventory));}
+        T_artifact_in_inventory() {
+          memset(this, 0, sizeof(*this));
+          artifact_id = -1;
+        }
       };
       T_artifact_in_inventory artifact_in_inventory;
       struct DFHACK_EXPORT T_artifact_not_in_site {
         int32_t artifact_id;
         int32_t site_id;
         int32_t structure_id;
-        T_artifact_not_in_site(){memset(this, 0, sizeof(T_artifact_not_in_site));}
+        T_artifact_not_in_site() {
+          memset(this, 0, sizeof(*this));
+          artifact_id = -1;
+        }
       };
       T_artifact_not_in_site artifact_not_in_site;
       struct DFHACK_EXPORT T_artifact_not_in_subregion {
         int32_t artifact_id;
         int32_t subregion_id;
-        T_artifact_not_in_subregion(){memset(this, 0, sizeof(T_artifact_not_in_subregion));}
+        T_artifact_not_in_subregion() {
+          memset(this, 0, sizeof(*this));
+          artifact_id = -1;
+        }
       };
       T_artifact_not_in_subregion artifact_not_in_subregion;
       struct DFHACK_EXPORT T_artifact_not_in_feature_layer {
         int32_t artifact_id;
         int32_t feature_layer_id;
-        T_artifact_not_in_feature_layer(){memset(this, 0, sizeof(T_artifact_not_in_feature_layer));}
+        T_artifact_not_in_feature_layer() {
+          memset(this, 0, sizeof(*this));
+          artifact_id = -1;
+        }
       };
       T_artifact_not_in_feature_layer artifact_not_in_feature_layer;
       struct DFHACK_EXPORT T_artifact_not_in_inventory {
         int32_t artifact_id;
         int32_t hist_figure_id;
-        T_artifact_not_in_inventory(){memset(this, 0, sizeof(T_artifact_not_in_inventory));}
+        T_artifact_not_in_inventory() {
+          memset(this, 0, sizeof(*this));
+          artifact_id = -1;
+        }
       };
       T_artifact_not_in_inventory artifact_not_in_inventory;
       struct DFHACK_EXPORT T_artifact_destroyed {
         int32_t artifact_id;
-        T_artifact_destroyed(){memset(this, 0, sizeof(T_artifact_destroyed));}
+        T_artifact_destroyed() {
+          memset(this, 0, sizeof(*this));
+          artifact_id = -1;
+        }
       };
       T_artifact_destroyed artifact_destroyed;
-      T_data(){memset(this, 0, sizeof(T_data));}
+      T_data() {
+        memset(this, 0, sizeof(*this));
+        invasion.entity_id = -1;
+        invasion.site_id = -1;
+        invasion.attack_leader_hf = -1;
+        invasion.attack_commander_hf = -1;
+      }
     };
     T_data data;
     int32_t valid_year; /*!< often the same as the other year/tick. Start/stop time? */
diff -ur codegen-old/historical_figure_relationships.h codegen/historical_figure_relationships.h
--- codegen-old/historical_figure_relationships.h	2024-06-15 00:36:56.859387672 -0400
+++ codegen/historical_figure_relationships.h	2024-06-15 02:20:17.768758179 -0400
@@ -93,7 +93,10 @@
           struct DFHACK_EXPORT T_Sabotage_Actor {
             int32_t target_civ;
             int32_t target_site;
-            T_Sabotage_Actor(){memset(this, 0, sizeof(T_Sabotage_Actor));}
+            T_Sabotage_Actor() {
+              memset(this, 0, sizeof(*this));
+              target_civ = -1;
+            }
           };
           T_Sabotage_Actor* Sabotage_Actor;
           std::vector<int32_t >* Undead_World_Conquest;
@@ -123,10 +126,15 @@
             T_flags flags;
             int32_t last_entered_year;
             int32_t last_entered_season_count;
-            T_Infiltrate_Society(){memset(this, 0, sizeof(T_Infiltrate_Society));}
+            T_Infiltrate_Society() {
+              memset(this, 0, sizeof(*this));
+            }
           };
           T_Infiltrate_Society* Infiltrate_Society;
-          T_plot_data(){memset(this, 0, sizeof(T_plot_data));}
+          T_plot_data() {
+            memset(this, 0, sizeof(*this));
+            Sabotage_Actor = 0;
+          }
         };
         T_plot_data plot_data;
         int32_t delegated_plot_id;
@@ -214,14 +222,20 @@
             int32_t plot_id;
             int32_t hire_hfid;
             int32_t rate;
-            T_hire_plot_actor(){memset(this, 0, sizeof(T_hire_plot_actor));}
+            T_hire_plot_actor() {
+              memset(this, 0, sizeof(*this));
+              plot_origin_hfid = -1;
+            }
           };
           T_hire_plot_actor* hire_plot_actor;
           struct DFHACK_EXPORT T_satisfy_agreement {
             int32_t agreement_id;
             int32_t plot_origin_hfid;
             int32_t plot_id;
-            T_satisfy_agreement(){memset(this, 0, sizeof(T_satisfy_agreement));}
+            T_satisfy_agreement() {
+              memset(this, 0, sizeof(*this));
+              agreement_id = 0;
+            }
           };
           T_satisfy_agreement* satisfy_agreement;
           struct DFHACK_EXPORT T_send_message {
@@ -229,7 +243,9 @@
             int32_t recipient_hfid;
             int32_t plot_origin_hfid;
             int32_t plot_id;
-            T_send_message(){memset(this, 0, sizeof(T_send_message));}
+            T_send_message() {
+              memset(this, 0, sizeof(*this));
+            }
           };
           T_send_message* send_message;
           struct DFHACK_EXPORT T_delegate_plot {
@@ -237,10 +253,16 @@
             int32_t plot_id;
             int32_t delegate_hfid;
             int32_t rate;
-            T_delegate_plot(){memset(this, 0, sizeof(T_delegate_plot));}
+            T_delegate_plot() {
+              memset(this, 0, sizeof(*this));
+              plot_origin_hfid = -1;
+            }
           };
           T_delegate_plot* delegate_plot;
-          T_data(){memset(this, 0, sizeof(T_data));}
+          T_data() {
+            memset(this, 0, sizeof(*this));
+            hire_plot_actor = 0;
+          }
         };
         T_data data;
         static struct_identity _identity;
diff -ur codegen-old/history_event_body_abusedst.h codegen/history_event_body_abusedst.h
--- codegen-old/history_event_body_abusedst.h	2024-06-15 00:36:56.867387705 -0400
+++ codegen/history_event_body_abusedst.h	2024-06-15 02:20:17.800758326 -0400
@@ -35,7 +35,10 @@
         int32_t item_subtype;
         int32_t mat_type;
         int32_t mat_index;
-        T_Impaled(){memset(this, 0, sizeof(T_Impaled));}
+        T_Impaled() {
+          memset(this, 0, sizeof(*this));
+          item_type = ENUM_FIRST_ITEM(item_type);
+        }
       };
       T_Impaled Impaled;
       struct DFHACK_EXPORT T_Piled {
@@ -45,27 +48,45 @@
           GruesomeSculpture, // 2, 0x2
         };
         T_pile_type pile_type;
-        T_Piled(){memset(this, 0, sizeof(T_Piled));}
+        T_Piled() {
+          memset(this, 0, sizeof(*this));
+          pile_type = GrislyMound;
+        }
       };
       T_Piled Piled;
       struct DFHACK_EXPORT T_Flayed {
         int32_t structure;
-        T_Flayed(){memset(this, 0, sizeof(T_Flayed));}
+        T_Flayed() {
+          memset(this, 0, sizeof(*this));
+          structure = -1;
+        }
       };
       T_Flayed Flayed;
       struct DFHACK_EXPORT T_Hung {
         int32_t tree;
         int32_t mat_type; /*!< rope */
         int32_t mat_index; /*!< rope */
-        T_Hung(){memset(this, 0, sizeof(T_Hung));}
+        T_Hung() {
+          memset(this, 0, sizeof(*this));
+          tree = -1;
+        }
       };
       T_Hung Hung;
       struct DFHACK_EXPORT T_Animated {
         int32_t interaction;
-        T_Animated(){memset(this, 0, sizeof(T_Animated));}
+        T_Animated() {
+          memset(this, 0, sizeof(*this));
+          interaction = -1;
+        }
       };
       T_Animated Animated;
-      T_abuse_data(){memset(this, 0, sizeof(T_abuse_data));}
+      T_abuse_data() {
+        memset(this, 0, sizeof(*this));
+        Impaled.item_type = ENUM_FIRST_ITEM(item_type);
+        Impaled.item_subtype = -1;
+        Impaled.mat_type = -1;
+        Impaled.mat_index = 0;
+      }
     };
     T_abuse_data abuse_data;
     static virtual_identity _identity;
diff -ur codegen-old/history_event_circumstance_info.h codegen/history_event_circumstance_info.h
--- codegen-old/history_event_circumstance_info.h	2024-06-15 00:36:56.827387546 -0400
+++ codegen/history_event_circumstance_info.h	2024-06-15 02:20:17.776758216 -0400
@@ -18,7 +18,10 @@
       int32_t Murdered;
       int32_t HistEventCollection;
       int32_t AfterAbducting;
-      T_data(){memset(this, 0, sizeof(T_data));}
+      T_data() {
+        memset(this, 0, sizeof(*this));
+        Death = -1;
+      }
     };
     T_data data;
     static struct_identity _identity;
diff -ur codegen-old/history_event_reason_info.h codegen/history_event_reason_info.h
--- codegen-old/history_event_reason_info.h	2024-06-15 00:36:56.879387752 -0400
+++ codegen/history_event_reason_info.h	2024-06-15 02:20:17.792758290 -0400
@@ -15,7 +15,10 @@
       int32_t sanctify_hf;
       int32_t artifact_is_heirloom_of_family_hfid;
       int32_t artifact_is_symbol_of_entity_position;
-      T_data(){memset(this, 0, sizeof(T_data));}
+      T_data() {
+        memset(this, 0, sizeof(*this));
+        glorify_hf = -1;
+      }
     };
     T_data data;
     static struct_identity _identity;
diff -ur codegen-old/incident.h codegen/incident.h
--- codegen-old/incident.h	2024-06-15 00:36:56.807387467 -0400
+++ codegen/incident.h	2024-06-15 02:20:17.768758179 -0400
@@ -76,7 +76,10 @@
       df::incident_data_writing* Writing;
       df::incident_data_identity* SelfID;
       df::incident_data_refused_identify* RefusedID;
-      T_data(){memset(this, 0, sizeof(T_data));}
+      T_data() {
+        memset(this, 0, sizeof(*this));
+        Performance = 0;
+      }
     };
     T_data data;
     static struct_identity _identity;
diff -ur codegen-old/job_spec_data.h codegen/job_spec_data.h
--- codegen-old/job_spec_data.h	2024-06-15 00:39:07.847910614 -0400
+++ codegen/job_spec_data.h	2024-06-15 02:20:17.764758162 -0400
@@ -12,7 +12,10 @@
     int32_t hist_figure_id;
     int32_t race;
     df::improvement_type improvement;
-    job_spec_data(){memset(this, 0, sizeof(job_spec_data));}
+    job_spec_data() {
+      memset(this, 0, sizeof(*this));
+      hist_figure_id = -1;
+    }
   };
   template<> struct DFHACK_EXPORT identity_traits<job_spec_data> {
     static union_identity identity;
diff -ur codegen-old/job_spec_flags.h codegen/job_spec_flags.h
--- codegen-old/job_spec_flags.h	2024-06-15 00:36:56.839387593 -0400
+++ codegen/job_spec_flags.h	2024-06-15 02:20:17.796758308 -0400
@@ -160,7 +160,10 @@
     T_link_building_to_trigger_flags link_building_to_trigger_flags;
     df::tile_occupancy carve_track_flags;
     df::stockpile_group_set encrust_flags;
-    job_spec_flags(){memset(this, 0, sizeof(job_spec_flags));}
+    job_spec_flags() {
+      memset(this, 0, sizeof(*this));
+      whole = 0;
+    }
   };
   template<> struct DFHACK_EXPORT bitfield_traits<job_spec_flags::T_construct_building_flags> {
     typedef uint32_t base_type;
diff -ur codegen-old/knowledge_scholar_category_flag.h codegen/knowledge_scholar_category_flag.h
--- codegen-old/knowledge_scholar_category_flag.h	2024-06-15 00:36:56.847387625 -0400
+++ codegen/knowledge_scholar_category_flag.h	2024-06-15 02:20:17.796758308 -0400
@@ -62,7 +62,9 @@
       df::knowledge_scholar_flags_12 flags_12;
       df::knowledge_scholar_flags_13 flags_13;
       uint32_t whole;
-      T_flag_data(){memset(this, 0, sizeof(T_flag_data));}
+      T_flag_data() {
+        memset(this, 0, sizeof(*this));
+      }
     };
     T_flag_data flag_data;
     static struct_identity _identity;
diff -ur codegen-old/large_integer.h codegen/large_integer.h
--- codegen-old/large_integer.h	2024-06-15 00:36:56.867387705 -0400
+++ codegen/large_integer.h	2024-06-15 02:20:17.772758198 -0400
@@ -14,11 +14,18 @@
     struct DFHACK_EXPORT T_u {
       long low_part;
       long high_part;
-      T_u(){memset(this, 0, sizeof(T_u));}
+      T_u() {
+        memset(this, 0, sizeof(*this));
+        low_part = 0;
+      }
     };
     T_u u;
     int64_t quad_part;
-    large_integer(){memset(this, 0, sizeof(large_integer));}
+    large_integer() {
+      memset(this, 0, sizeof(*this));
+      low_part = 0;
+      high_part = 0;
+    }
   };
   template<> struct DFHACK_EXPORT identity_traits<large_integer::T_u> {
     static struct_identity identity;
diff -ur codegen-old/lookinfost.h codegen/lookinfost.h
--- codegen-old/lookinfost.h	2024-06-15 00:36:56.847387625 -0400
+++ codegen/lookinfost.h	2024-06-15 02:20:17.780758234 -0400
@@ -33,22 +33,34 @@
     union DFHACK_EXPORT T_data {
       struct DFHACK_EXPORT T_item {
         int32_t item_id;
-        T_item(){memset(this, 0, sizeof(T_item));}
+        T_item() {
+          memset(this, 0, sizeof(*this));
+          item_id = -1;
+        }
       };
       T_item item;
       struct DFHACK_EXPORT T_floor {
         int32_t empty;
-        T_floor(){memset(this, 0, sizeof(T_floor));}
+        T_floor() {
+          memset(this, 0, sizeof(*this));
+          empty = 0;
+        }
       };
       T_floor floor;
       struct DFHACK_EXPORT T_unit {
         int32_t unit_id;
-        T_unit(){memset(this, 0, sizeof(T_unit));}
+        T_unit() {
+          memset(this, 0, sizeof(*this));
+          unit_id = -1;
+        }
       };
       T_unit unit;
       struct DFHACK_EXPORT T_building {
         int32_t bld_id;
-        T_building(){memset(this, 0, sizeof(T_building));}
+        T_building() {
+          memset(this, 0, sizeof(*this));
+          bld_id = -1;
+        }
       };
       T_building building;
       struct DFHACK_EXPORT T_vermin {
@@ -57,7 +69,10 @@
         int32_t item_id;
         uint32_t flag;
         int32_t number;
-        T_vermin(){memset(this, 0, sizeof(T_vermin));}
+        T_vermin() {
+          memset(this, 0, sizeof(*this));
+          race = -1;
+        }
       };
       T_vermin vermin;
       struct DFHACK_EXPORT T_flow {
@@ -79,12 +94,18 @@
           T_flag(uint32_t whole_ = 0) : whole(whole_) {};
         };
         T_flag flag;
-        T_flow(){memset(this, 0, sizeof(T_flow));}
+        T_flow() {
+          memset(this, 0, sizeof(*this));
+          type = 0;
+        }
       };
       T_flow flow;
       struct DFHACK_EXPORT T_campfire {
         int32_t empty;
-        T_campfire(){memset(this, 0, sizeof(T_campfire));}
+        T_campfire() {
+          memset(this, 0, sizeof(*this));
+          empty = 0;
+        }
       };
       T_campfire campfire;
       struct DFHACK_EXPORT T_spatter {
@@ -94,17 +115,26 @@
         int32_t matg;
         int32_t matstate;
         int16_t extend;
-        T_spatter(){memset(this, 0, sizeof(T_spatter));}
+        T_spatter() {
+          memset(this, 0, sizeof(*this));
+          i_type = ENUM_FIRST_ITEM(item_type);
+        }
       };
       T_spatter spatter;
       struct DFHACK_EXPORT T_building_item_adv {
         int32_t item_id;
-        T_building_item_adv(){memset(this, 0, sizeof(T_building_item_adv));}
+        T_building_item_adv() {
+          memset(this, 0, sizeof(*this));
+          item_id = -1;
+        }
       };
       T_building_item_adv building_item_adv;
       struct DFHACK_EXPORT T_fire {
         int32_t empty;
-        T_fire(){memset(this, 0, sizeof(T_fire));}
+        T_fire() {
+          memset(this, 0, sizeof(*this));
+          empty = 0;
+        }
       };
       T_fire fire;
       struct DFHACK_EXPORT T_liquid_water {
@@ -126,7 +156,9 @@
         };
         T_flag flag;
         int16_t amount;
-        T_liquid_water(){memset(this, 0, sizeof(T_liquid_water));}
+        T_liquid_water() {
+          memset(this, 0, sizeof(*this));
+        }
       };
       T_liquid_water liquid_water;
       struct DFHACK_EXPORT T_liquid_magma {
@@ -145,7 +177,9 @@
         };
         T_flag flag;
         int16_t amount;
-        T_liquid_magma(){memset(this, 0, sizeof(T_liquid_magma));}
+        T_liquid_magma() {
+          memset(this, 0, sizeof(*this));
+        }
       };
       T_liquid_magma liquid_magma;
       struct DFHACK_EXPORT T_spoor {
@@ -154,10 +188,15 @@
         int32_t id1;
         int32_t id2;
         int32_t id3;
-        T_spoor(){memset(this, 0, sizeof(T_spoor));}
+        T_spoor() {
+          memset(this, 0, sizeof(*this));
+        }
       };
       T_spoor spoor;
-      T_data(){memset(this, 0, sizeof(T_data));}
+      T_data() {
+        memset(this, 0, sizeof(*this));
+        item.item_id = -1;
+      }
     };
     T_data data;
     df::coord pos;
diff -ur codegen-old/messagest.h codegen/messagest.h
--- codegen-old/messagest.h	2024-06-15 00:36:56.811387482 -0400
+++ codegen/messagest.h	2024-06-15 02:20:17.792758290 -0400
@@ -41,7 +41,10 @@
         int32_t artifact_id;
         int32_t return_site;
         int32_t return_bld;
-        T_quest_retrieve_artifact(){memset(this, 0, sizeof(T_quest_retrieve_artifact));}
+        T_quest_retrieve_artifact() {
+          memset(this, 0, sizeof(*this));
+          giver_civ = -1;
+        }
       };
       T_quest_retrieve_artifact quest_retrieve_artifact;
       struct DFHACK_EXPORT T_hire_plot_actor {
@@ -49,14 +52,20 @@
         int32_t plot_id;
         int32_t hire_hf;
         int32_t rate;
-        T_hire_plot_actor(){memset(this, 0, sizeof(T_hire_plot_actor));}
+        T_hire_plot_actor() {
+          memset(this, 0, sizeof(*this));
+          plot_origin_hf = -1;
+        }
       };
       T_hire_plot_actor hire_plot_actor;
       struct DFHACK_EXPORT T_hiring_proposition {
         int32_t plot_origin_hf;
         int32_t plot_id;
         int32_t rate;
-        T_hiring_proposition(){memset(this, 0, sizeof(T_hiring_proposition));}
+        T_hiring_proposition() {
+          memset(this, 0, sizeof(*this));
+          plot_origin_hf = -1;
+        }
       };
       T_hiring_proposition hiring_proposition;
       struct DFHACK_EXPORT T_delegate_plot {
@@ -64,14 +73,20 @@
         int32_t plot_id;
         int32_t delegate_hf;
         int32_t rate;
-        T_delegate_plot(){memset(this, 0, sizeof(T_delegate_plot));}
+        T_delegate_plot() {
+          memset(this, 0, sizeof(*this));
+          plot_origin_hf = -1;
+        }
       };
       T_delegate_plot delegate_plot;
       struct DFHACK_EXPORT T_delegate_plot_proposition {
         int32_t plot_origin_hf;
         int32_t plot_id;
         int32_t rate;
-        T_delegate_plot_proposition(){memset(this, 0, sizeof(T_delegate_plot_proposition));}
+        T_delegate_plot_proposition() {
+          memset(this, 0, sizeof(*this));
+          plot_origin_hf = -1;
+        }
       };
       T_delegate_plot_proposition delegate_plot_proposition;
       struct DFHACK_EXPORT T_order_to_perform_action {
@@ -83,10 +98,20 @@
         T_action action;
         int32_t action_id;
         uint32_t action_transfer_flag;
-        T_order_to_perform_action(){memset(this, 0, sizeof(T_order_to_perform_action));}
+        T_order_to_perform_action() {
+          memset(this, 0, sizeof(*this));
+          commander_hf = -1;
+        }
       };
       T_order_to_perform_action order_to_perform_action;
-      T_plot_data(){memset(this, 0, sizeof(T_plot_data));}
+      T_plot_data() {
+        memset(this, 0, sizeof(*this));
+        quest_retrieve_artifact.giver_civ = -1;
+        quest_retrieve_artifact.giver_hf = -1;
+        quest_retrieve_artifact.artifact_id = -1;
+        quest_retrieve_artifact.return_site = -1;
+        quest_retrieve_artifact.return_bld = -1;
+      }
     };
     T_plot_data plot_data;
     int32_t last_holder_hf;
diff -ur codegen-old/poetic_form_subject_target.h codegen/poetic_form_subject_target.h
--- codegen-old/poetic_form_subject_target.h	2024-06-15 00:36:56.819387514 -0400
+++ codegen/poetic_form_subject_target.h	2024-06-15 02:20:17.772758198 -0400
@@ -11,15 +11,24 @@
   union DFHACK_EXPORT poetic_form_subject_target {
     struct DFHACK_EXPORT T_Histfig {
       int32_t subject_histfig;
-      T_Histfig(){memset(this, 0, sizeof(T_Histfig));}
+      T_Histfig() {
+        memset(this, 0, sizeof(*this));
+        subject_histfig = -1;
+      }
     };
     T_Histfig Histfig;
     struct DFHACK_EXPORT T_Concept {
       int32_t subject_topic;
-      T_Concept(){memset(this, 0, sizeof(T_Concept));}
+      T_Concept() {
+        memset(this, 0, sizeof(*this));
+        subject_topic = ENUM_FIRST_ITEM(sphere_type);
+      }
     };
     T_Concept Concept;
-    poetic_form_subject_target(){memset(this, 0, sizeof(poetic_form_subject_target));}
+    poetic_form_subject_target() {
+      memset(this, 0, sizeof(*this));
+      Histfig.subject_histfig = -1;
+    }
   };
   template<> struct DFHACK_EXPORT identity_traits<poetic_form_subject_target::T_Histfig> {
     static struct_identity identity;
diff -ur codegen-old/religious_practice_data.h codegen/religious_practice_data.h
--- codegen-old/religious_practice_data.h	2024-06-15 00:36:56.831387563 -0400
+++ codegen/religious_practice_data.h	2024-06-15 02:20:17.784758253 -0400
@@ -10,7 +10,10 @@
     int32_t practice_id;
     int32_t Deity;
     int32_t Religion;
-    religious_practice_data(){memset(this, 0, sizeof(religious_practice_data));}
+    religious_practice_data() {
+      memset(this, 0, sizeof(*this));
+      practice_id = 0;
+    }
   };
   template<> struct DFHACK_EXPORT identity_traits<religious_practice_data> {
     static union_identity identity;
diff -ur codegen-old/specific_ref.h codegen/specific_ref.h
--- codegen-old/specific_ref.h	2024-06-15 00:36:56.855387658 -0400
+++ codegen/specific_ref.h	2024-06-15 02:20:17.776758216 -0400
@@ -32,10 +32,16 @@
       struct DFHACK_EXPORT T_wrestle {
         df::unit* unit;
         df::unit_item_wrestle* item;
-        T_wrestle(){memset(this, 0, sizeof(T_wrestle));}
+        T_wrestle() {
+          memset(this, 0, sizeof(*this));
+          unit = 0;
+        }
       };
       T_wrestle wrestle;
-      T_data(){memset(this, 0, sizeof(T_data));}
+      T_data() {
+        memset(this, 0, sizeof(*this));
+        object = 0;
+      }
     };
     T_data data;
     static struct_identity _identity;
diff -ur codegen-old/unit_action.h codegen/unit_action.h
--- codegen-old/unit_action.h	2024-06-15 00:36:56.815387498 -0400
+++ codegen/unit_action.h	2024-06-15 02:20:17.772758198 -0400
@@ -61,7 +61,12 @@
       df::unit_action_data_dismount dismount;
       df::unit_action_data_lead_animal leadanimal;
       df::unit_action_data_stop_lead_animal stopleadanimal;
-      T_data(){memset(this, 0, sizeof(T_data));}
+      T_data() {
+        memset(this, 0, sizeof(*this));
+        for (int anon_1 = 0; anon_1 < 24; anon_1++) {
+          raw_data[anon_1] = 0;
+        }
+      }
     };
     T_data data;
     static struct_identity _identity;
diff -ur codegen-old/unit_ghost_info.h codegen/unit_ghost_info.h
--- codegen-old/unit_ghost_info.h	2024-06-15 00:36:56.867387705 -0400
+++ codegen/unit_ghost_info.h	2024-06-15 02:20:17.800758326 -0400
@@ -18,7 +18,10 @@
       int32_t unit;
       int32_t item;
       int32_t building;
-      T_target(){memset(this, 0, sizeof(T_target));}
+      T_target() {
+        memset(this, 0, sizeof(*this));
+        unit = -1;
+      }
     };
     T_target target;
     df::coord misplace_pos;
diff -ur codegen-old/unit_health_info.h codegen/unit_health_info.h
--- codegen-old/unit_health_info.h	2024-06-15 00:36:56.859387672 -0400
+++ codegen/unit_health_info.h	2024-06-15 02:20:17.772758198 -0400
@@ -31,7 +31,10 @@
           int32_t mat_type;
           int32_t mat_index;
           int32_t item_id;
-          T_crutch(){memset(this, 0, sizeof(T_crutch));}
+          T_crutch() {
+            memset(this, 0, sizeof(*this));
+            item_type = -1;
+          }
         };
         T_crutch crutch;
         int32_t bed_id;
@@ -40,17 +43,30 @@
           int32_t mat_index;
           int32_t body_part_id;
           int32_t item_id;
-          T_bandage(){memset(this, 0, sizeof(T_bandage));}
+          T_bandage() {
+            memset(this, 0, sizeof(*this));
+            mat_type = -1;
+          }
         };
         T_bandage bandage;
         struct DFHACK_EXPORT T_surgery {
           df::job_subtype_surgery subtype;
           int32_t body_part_id;
           int32_t amputated_part_id;
-          T_surgery(){memset(this, 0, sizeof(T_surgery));}
+          T_surgery() {
+            memset(this, 0, sizeof(*this));
+            subtype = ENUM_FIRST_ITEM(job_subtype_surgery);
+          }
         };
         T_surgery surgery;
-        T_info(){memset(this, 0, sizeof(T_info));}
+        T_info() {
+          memset(this, 0, sizeof(*this));
+          crutch.item_type = -1;
+          crutch.item_subtype = -1;
+          crutch.mat_type = -1;
+          crutch.mat_index = -1;
+          crutch.item_id = -1;
+        }
       };
       T_info info;
       int32_t year;

This allows job_spec_data fields embedded in other types to be initialized to -1 as expected
@@ -677,7 +677,13 @@ sub emit_struct_fields($$;%) {
} 'fields-' . $fields_group;

# Needed for unions with fields with non-default ctors (e.g. bitfields)
emit "$name(){memset(this, 0, sizeof($name));}";
emit_block {
local $in_union = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this variable unused?

Copy link
Member

@ab9rf ab9rf Jun 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's used to tell render_field_init that it's rendering fields from a union, and is necessary here to get the behavior we want.

Remember that in Perl, local variables are dynamically, not lexically, scoped (see What is the difference between my and local in Perl?).

Copy link
Member Author

@lethosor lethosor Jun 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's read here in render_field_init but is not set there:

local $in_union = $in_union || $is_union;

Probably an oversimplification: I've been thinking of local vars as additional context args that get pushed on the stack and get restored when they go "out of scope". Inspecting $is_union outside of this emit_block will show it still set to 0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they're actually pushed into the current block's dynamics symbol table, which remains visible during a subroutine call, so any subroutine called will also see them (unless shadowed). lexically scoped my locals, however, are only visible within the lexical block (and any enclosed blocks) in which they're created.

it's not so much that they get "restored" but rather that defining a symbol within a block shadows any definition that might exist in a block further up. when the block ends, its symbol tables are disposed (each block effectively has two, one for dynamic scope and one for lexical scope), and any prior definition that was being shadowed is thereby unshadowed and thus once again visible. every block carries with it a pointer to the lexical context in which it was defined (a closure), and the current dynamic symbol context is used to initialize a new dynamic symbol context whenever execution enters a new block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank for the explanation. my perl fu is low

@ab9rf
Copy link
Member

ab9rf commented Jun 15, 2024

This looks good to me in general, but I notice that some of the fields are being initialized to -1 and I don't understand why this happens. Why is, for example, hist_figure_id being initialized to -1 in job_spec_data. While I agree that this is correct (or at least reasonable, I haven't checked to see what DF's own constructor does in this situation and is in fact what DF does), I don't understand what in either codegen or the XML is causing that to happen.

@lethosor
Copy link
Member Author

lethosor commented Jun 15, 2024

Why is, for example, hist_figure_id being initialized to -1 in job_spec_data

That's handled here in render_field_init:

unless (defined $cur_init_value) {
my $signed_ref =
!is_attr_true($field,'ld:unsigned') &&
($field->getAttribute('ref-target') || $field->getAttribute('refers-to'));
$cur_init_value = ($signed_ref ? '-1' : 0);
}

Copy link
Member

@ab9rf ab9rf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. i've reviewed both the diff provided as well as run a local build with this, and it appears to work as intended.

@lethosor lethosor merged commit 5718c3d into DFHack:master Jun 16, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants