• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "linker/module_merger.h"
16 #include "repr/abi_diff_helpers.h"
17 #include "repr/ir_representation_internal.h"
18 
19 #include <cassert>
20 
21 #include <llvm/Support/raw_ostream.h>
22 
23 
24 namespace header_checker {
25 namespace linker {
26 
27 
MergeBuiltinType(const repr::BuiltinTypeIR * builtin_type,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)28 MergeStatus ModuleMerger::MergeBuiltinType(
29     const repr::BuiltinTypeIR *builtin_type, const repr::ModuleIR &addend,
30     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
31   std::string linker_set_key = builtin_type->GetLinkerSetKey();
32   auto builtin_it = module_->builtin_types_.find(linker_set_key);
33   if (builtin_it != module_->builtin_types_.end()) {
34     return MergeStatus(false, builtin_it->second.GetSelfType());
35   }
36 
37   // Add this builtin type to the parent graph's builtin_types_ map.
38   const std::string &type_id = builtin_type->GetSelfType();
39   auto p = module_->builtin_types_.emplace(linker_set_key, *builtin_type);
40   module_->type_graph_.emplace(type_id, &p.first->second);
41 
42   MergeStatus merge_status(true, type_id);
43   local_to_global_type_id_map->emplace(type_id, merge_status);
44   return merge_status;
45 }
46 
47 
LookupUserDefinedType(const repr::TypeIR * ud_type,const repr::ModuleIR & addend,const std::string & ud_type_unique_id_and_source,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map_)48 MergeStatus ModuleMerger::LookupUserDefinedType(
49     const repr::TypeIR *ud_type, const repr::ModuleIR &addend,
50     const std::string &ud_type_unique_id_and_source,
51     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map_) {
52   auto it = module_->odr_list_map_.find(ud_type_unique_id_and_source);
53   if (it == module_->odr_list_map_.end()) {
54     // Calling this an ODR violation even though it means no UD with the same
55     // name + source combination was seen in the parent graph. The type-id
56     // passed does not matter since was_newly_added_ is true, the type will get
57     // allocated a new type id.
58     return MergeStatus(true, "");
59   }
60 
61   // Initialize type comparator (which will compare the referenced types
62   // recursively).
63   std::set<std::string> type_cache;
64   repr::DiffPolicyOptions diff_policy_options(false);
65   repr::AbiDiffHelper diff_helper(module_->type_graph_, addend.type_graph_,
66                                   diff_policy_options, &type_cache, nullptr);
67 
68   // Compare each user-defined type with the latest input user-defined type.
69   // If there is a match, re-use the existing user-defined type.
70   for (auto &definition : it->second) {
71     const repr::TypeIR *contender_ud = definition.type_ir_;
72     repr::DiffStatus result = diff_helper.CompareAndDumpTypeDiff(
73         contender_ud->GetSelfType(), ud_type->GetSelfType());
74     if (result == repr::DiffStatus::no_diff) {
75       local_to_global_type_id_map_->emplace(
76           ud_type->GetSelfType(),
77           MergeStatus(false, contender_ud->GetSelfType()));
78       return MergeStatus(false, contender_ud->GetSelfType());
79     }
80   }
81 
82 #ifdef DEBUG
83   llvm::errs() << "ODR violation detected for: " << ud_type->GetName() << "\n";
84 #endif
85   return MergeStatus(true, it->second.begin()->type_ir_->GetSelfType());
86 }
87 
88 
LookupType(const repr::TypeIR * addend_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)89 MergeStatus ModuleMerger::LookupType(
90     const repr::TypeIR *addend_node, const repr::ModuleIR &addend,
91     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
92   std::string unique_type_id;
93   switch (addend_node->GetKind()) {
94     case repr::RecordTypeKind:
95       unique_type_id = repr::GetODRListMapKey(
96           static_cast<const repr::RecordTypeIR *>(addend_node));
97       break;
98     case repr::EnumTypeKind:
99       unique_type_id = repr::GetODRListMapKey(
100           static_cast<const repr::EnumTypeIR *>(addend_node));
101       break;
102     case repr::FunctionTypeKind:
103       unique_type_id = repr::GetODRListMapKey(
104           static_cast<const repr::FunctionTypeIR *>(addend_node));
105       break;
106     default:
107       // Other kinds (e.g. PointerTypeKind, QualifiedTypeKind, ArrayTypeKind,
108       // LvalueReferenceTypeKind, RvalueReferenceTypeKind, or BuiltinTypeKind)
109       // should be proactively added by returning MergeStatus with
110       // was_newly_added_ = true.
111       return MergeStatus(true, "type-hidden");
112   }
113 
114   return LookupUserDefinedType(
115       addend_node, addend, unique_type_id, local_to_global_type_id_map);
116 }
117 
118 
119 // This method merges the type referenced by 'references_type' into the parent
120 // graph. It also corrects the referenced_type field in the references_type
121 // object passed and returns the merge status of the *referenced type*.
MergeReferencingTypeInternal(const repr::ModuleIR & addend,repr::ReferencesOtherType * references_type,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)122 MergeStatus ModuleMerger::MergeReferencingTypeInternal(
123     const repr::ModuleIR &addend, repr::ReferencesOtherType *references_type,
124     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
125   // First look in the local_to_global_type_id_map for the referenced type's
126   // id.
127   const std::string &referenced_type_id = references_type->GetReferencedType();
128   auto local_to_global_it = local_to_global_type_id_map->find(
129       referenced_type_id);
130   if (local_to_global_it != local_to_global_type_id_map->end()) {
131     // The type was already added to the parent graph. So change the
132     // referenced type to the global type id.
133     references_type->SetReferencedType(local_to_global_it->second.type_id_);
134     return local_to_global_it->second;
135   }
136 
137   // If that did not go through, look at the addend's type_map_ and get the
138   // TypeIR* and call MergeType on it.
139   auto local_type_it = addend.type_graph_.find(referenced_type_id);
140   if (local_type_it != addend.type_graph_.end()) {
141     // We don't care about merge_status.was_newly_added since we wouldn't have
142     // gotten this far if we weren't adding this.
143     MergeStatus merge_status =
144         MergeType(local_type_it->second, addend, local_to_global_type_id_map);
145     const std::string &global_type_id = merge_status.type_id_;
146     references_type->SetReferencedType(global_type_id);
147     return merge_status;
148   }
149 
150   // If the referenced type was hidden, create the name reference type in the
151   // parent module and keep the referenced type_id as-is.
152   return MergeStatus(true, referenced_type_id);
153 }
154 
155 
MergeRecordFields(const repr::ModuleIR & addend,repr::RecordTypeIR * added_node,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)156 void ModuleMerger::MergeRecordFields(
157     const repr::ModuleIR &addend, repr::RecordTypeIR *added_node,
158     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
159   for (auto &field : added_node->GetFields()) {
160     MergeReferencingTypeInternal(addend, &field, local_to_global_type_id_map);
161   }
162 }
163 
164 
MergeRecordCXXBases(const repr::ModuleIR & addend,repr::RecordTypeIR * added_node,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)165 void ModuleMerger::MergeRecordCXXBases(
166     const repr::ModuleIR &addend, repr::RecordTypeIR *added_node,
167     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
168   for (auto &base : added_node->GetBases()) {
169     MergeReferencingTypeInternal(addend, &base, local_to_global_type_id_map);
170   }
171 }
172 
173 
MergeRecordTemplateElements(const repr::ModuleIR & addend,repr::RecordTypeIR * added_node,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)174 void ModuleMerger::MergeRecordTemplateElements(
175     const repr::ModuleIR &addend, repr::RecordTypeIR *added_node,
176     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
177   for (auto &template_element : added_node->GetTemplateElements()) {
178     MergeReferencingTypeInternal(
179         addend, &template_element, local_to_global_type_id_map);
180   }
181 }
182 
183 
MergeRecordDependencies(const repr::ModuleIR & addend,repr::RecordTypeIR * added_node,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)184 void ModuleMerger::MergeRecordDependencies(
185     const repr::ModuleIR &addend, repr::RecordTypeIR *added_node,
186     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
187   // First call MergeType on all its fields.
188   MergeRecordFields(addend, added_node, local_to_global_type_id_map);
189 
190   // Call MergeType on CXXBases of the record.
191   MergeRecordCXXBases(addend, added_node, local_to_global_type_id_map);
192 
193   MergeRecordTemplateElements(addend, added_node, local_to_global_type_id_map);
194 }
195 
196 
197 template <typename T>
198 std::pair<MergeStatus, typename repr::AbiElementMap<T>::iterator>
UpdateUDTypeAccounting(const T * addend_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map,repr::AbiElementMap<T> * specific_type_map)199 ModuleMerger::UpdateUDTypeAccounting(
200     const T *addend_node, const repr::ModuleIR &addend,
201     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map,
202     repr::AbiElementMap<T> *specific_type_map) {
203   const std::string addend_compilation_unit_path =
204       addend.GetCompilationUnitPath(addend_node);
205   assert(addend_compilation_unit_path != "");
206   std::string added_type_id = addend_node->GetSelfType();
207   auto type_id_it = module_->type_graph_.find(added_type_id);
208   if (type_id_it != module_->type_graph_.end()) {
209     added_type_id = added_type_id + "#ODR:" + addend_compilation_unit_path;
210   }
211 
212   // Add the ud-type with type-id to the type_graph_, since if there are generic
213   // reference types which refer to the record being added, they'll need to find
214   // it's id in the map.
215   // Add ud-type to the parent graph.
216   T added_type_ir = *addend_node;
217   added_type_ir.SetSelfType(added_type_id);
218   added_type_ir.SetReferencedType(added_type_id);
219   auto it = AddToMapAndTypeGraph(std::move(added_type_ir), specific_type_map,
220                                  &module_->type_graph_);
221   // Add to facilitate ODR checking.
222   const std::string &key = GetODRListMapKey(&(it->second));
223   MergeStatus type_merge_status = MergeStatus(true, added_type_id);
224   module_->AddToODRListMap(key, &(it->second), addend_compilation_unit_path);
225   local_to_global_type_id_map->emplace(addend_node->GetSelfType(),
226                                        type_merge_status);
227   return {type_merge_status, it};
228 }
229 
230 
231 // This method is necessarily going to have a was_newly_merged_ = true in its
232 // MergeStatus return. So it necessarily merges a new RecordType.
MergeRecordAndDependencies(const repr::RecordTypeIR * addend_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)233 MergeStatus ModuleMerger::MergeRecordAndDependencies(
234     const repr::RecordTypeIR *addend_node, const repr::ModuleIR &addend,
235     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
236   auto p = UpdateUDTypeAccounting(
237       addend_node, addend, local_to_global_type_id_map,
238       &module_->record_types_);
239   MergeRecordDependencies(addend, &p.second->second,
240                           local_to_global_type_id_map);
241   return p.first;
242 }
243 
244 
MergeEnumDependencies(const repr::ModuleIR & addend,repr::EnumTypeIR * added_node,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)245 void ModuleMerger::MergeEnumDependencies(
246     const repr::ModuleIR &addend, repr::EnumTypeIR *added_node,
247     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
248   const std::string underlying_type_id = added_node->GetUnderlyingType();
249   // Get the underlying type, it nessarily has to be present in the addend's
250   // type graph since builtin types can't be hidden. Call MergeType on it and
251   // change the underlying type to that.
252   auto it = addend.type_graph_.find(underlying_type_id);
253   if (it == addend.type_graph_.end()) {
254     llvm::errs() << "Enum underlying types should not be hidden\n";
255     ::exit(1);
256   }
257   MergeStatus merge_status = MergeType(
258       it->second, addend, local_to_global_type_id_map);
259   added_node->SetUnderlyingType(merge_status.type_id_);
260 }
261 
262 
263 // This method is necessarily going to have a was_newly_merged_ = true in its
264 // MergeStatus return. So it necessarily merges a new EnumType.
MergeEnumType(const repr::EnumTypeIR * addend_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)265 MergeStatus ModuleMerger::MergeEnumType(
266     const repr::EnumTypeIR *addend_node, const repr::ModuleIR &addend,
267     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
268   auto p = UpdateUDTypeAccounting(
269       addend_node, addend, local_to_global_type_id_map, &module_->enum_types_);
270   MergeEnumDependencies(addend, &p.second->second, local_to_global_type_id_map);
271   return p.first;
272 }
273 
274 
MergeFunctionType(const repr::FunctionTypeIR * addend_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)275 MergeStatus ModuleMerger::MergeFunctionType(
276     const repr::FunctionTypeIR *addend_node, const repr::ModuleIR &addend,
277     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
278   auto p = UpdateUDTypeAccounting(
279       addend_node, addend, local_to_global_type_id_map,
280       &module_->function_types_);
281   MergeCFunctionLikeDeps(addend, &p.second->second,
282                          local_to_global_type_id_map);
283   return p.first;
284 }
285 
286 
287 template <typename T>
MergeReferencingTypeInternalAndUpdateParent(const repr::ModuleIR & addend,const T * addend_node,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map,repr::AbiElementMap<T> * parent_map,const std::string & updated_self_type_id)288 MergeStatus ModuleMerger::MergeReferencingTypeInternalAndUpdateParent(
289     const repr::ModuleIR &addend, const T *addend_node,
290     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map,
291     repr::AbiElementMap<T> *parent_map,
292     const std::string &updated_self_type_id) {
293   MergeStatus merge_status;
294 
295   // Create copy of addend_node
296   T added_node = *addend_node;
297   added_node.SetSelfType(updated_self_type_id);
298 
299   // The merge status returned is the merge status of the referenced type.
300   merge_status = MergeReferencingTypeInternal(addend, &added_node,
301                                               local_to_global_type_id_map);
302   if (merge_status.was_newly_added_) {
303     // Emplace to map (type-referenced -> Referencing type)
304     AddToMapAndTypeGraph(std::move(added_node), parent_map,
305                          &module_->type_graph_);
306     return MergeStatus(true, updated_self_type_id);
307   }
308 
309   // Try finding the referenced_type is referred to by any referencing type
310   // of the same kind in the parent graph. It is safe to call this on the
311   // added_node, since the referenced_type in the added_node would have been
312   // modified by the MergeReferencingTypeInternal call.
313   auto it = parent_map->find(GetReferencedTypeMapKey(added_node));
314   if (it == parent_map->end()) {
315     // There was no counterpart found for the added_node's type Kind referencing
316     // the referenced type, so we added it to the parent and also updated the
317     // local_to_global_type_id_map's global_id value.
318     AddToMapAndTypeGraph(std::move(added_node), parent_map,
319                          &module_->type_graph_);
320 
321     merge_status = MergeStatus(true, updated_self_type_id);
322     return merge_status;
323   }
324 
325   // Update local_to_global_type_id map's MergeStatus.was_newly_added value for
326   // this key with false since this was node was not newly added.
327   // We never remove anything from the local_to_global_type_id_map, what's
328   // the point ? Since you store the decision of whether the type was newly
329   // added or not. It's global type id is the type-id of the element found
330   // in the parent map which refers to the added_node's modified
331   // referenced_type.
332   merge_status = MergeStatus(false, it->second.GetSelfType());
333   (*local_to_global_type_id_map)[addend_node->GetSelfType()] = merge_status;
334 
335   return merge_status;
336 }
337 
338 
IsReferencingType(repr::LinkableMessageKind kind)339 static bool IsReferencingType(repr::LinkableMessageKind kind) {
340   switch (kind) {
341     case repr::PointerTypeKind:
342     case repr::QualifiedTypeKind:
343     case repr::ArrayTypeKind:
344     case repr::LvalueReferenceTypeKind:
345     case repr::RvalueReferenceTypeKind:
346       return true;
347     case repr::RecordTypeKind:
348     case repr::EnumTypeKind:
349     case repr::BuiltinTypeKind:
350     case repr::FunctionTypeKind:
351     case repr::FunctionKind:
352     case repr::GlobalVarKind:
353       return false;
354   }
355 }
356 
357 // Trace the referenced type until reaching a RecordTypeIR, EnumTypeIR,
358 // FunctionTypeIR, or BuiltinTypeIR. Return nullptr if the referenced type is
359 // undefined or built-in.
DereferenceType(const repr::ModuleIR & module,const repr::TypeIR * type_ir)360 static const repr::TypeIR *DereferenceType(const repr::ModuleIR &module,
361                                            const repr::TypeIR *type_ir) {
362   auto &type_graph = module.GetTypeGraph();
363   while (IsReferencingType(type_ir->GetKind())) {
364     auto it = type_graph.find(type_ir->GetReferencedType());
365     // The referenced type is undefined in the module.
366     if (it == type_graph.end()) {
367       return nullptr;
368     }
369     type_ir = it->second;
370   }
371   return type_ir;
372 }
373 
374 
375 // This method creates a new node for the addend node in the graph if MergeType
376 // on the reference returned a MergeStatus with was_newly_added_ = true.
MergeReferencingType(const repr::ModuleIR & addend,const repr::TypeIR * addend_node,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)377 MergeStatus ModuleMerger::MergeReferencingType(
378     const repr::ModuleIR &addend, const repr::TypeIR *addend_node,
379     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
380   // First add the type 'pro-actively'. We need to do this since we'll need to
381   // fill in 'referenced-type' fields in all this type's descendants and
382   // descendants which are compound types (records), can refer to this type.
383   std::string added_type_id = addend_node->GetSelfType();
384   auto type_id_it = module_->type_graph_.find(added_type_id);
385   if (type_id_it != module_->type_graph_.end()) {
386     const repr::TypeIR *final_referenced_type =
387         DereferenceType(addend, addend_node);
388     if (final_referenced_type != nullptr) {
389       std::string compilation_unit_path =
390           addend.GetCompilationUnitPath(final_referenced_type);
391       // The path is empty for built-in types.
392       if (compilation_unit_path != "") {
393         added_type_id = added_type_id + "#ODR:" + compilation_unit_path;
394       }
395     }
396   }
397 
398   // Add the added record type to the local_to_global_type_id_map.
399   local_to_global_type_id_map->emplace(addend_node->GetSelfType(),
400                                        MergeStatus(true, added_type_id));
401 
402   // Merge the type.
403   switch (addend_node->GetKind()) {
404     case repr::PointerTypeKind:
405       return MergeReferencingTypeInternalAndUpdateParent(
406           addend, static_cast<const repr::PointerTypeIR *>(addend_node),
407           local_to_global_type_id_map, &module_->pointer_types_,
408           added_type_id);
409     case repr::QualifiedTypeKind:
410       return MergeReferencingTypeInternalAndUpdateParent(
411           addend, static_cast<const repr::QualifiedTypeIR *>(addend_node),
412           local_to_global_type_id_map, &module_->qualified_types_,
413           added_type_id);
414     case repr::ArrayTypeKind:
415       return MergeReferencingTypeInternalAndUpdateParent(
416           addend, static_cast<const repr::ArrayTypeIR *>(addend_node),
417           local_to_global_type_id_map, &module_->array_types_,
418           added_type_id);
419     case repr::LvalueReferenceTypeKind:
420       return MergeReferencingTypeInternalAndUpdateParent(
421           addend, static_cast<const repr::LvalueReferenceTypeIR *>(addend_node),
422           local_to_global_type_id_map, &module_->lvalue_reference_types_,
423           added_type_id);
424     case repr::RvalueReferenceTypeKind:
425       return MergeReferencingTypeInternalAndUpdateParent(
426           addend, static_cast<const repr::RvalueReferenceTypeIR *>(addend_node),
427           local_to_global_type_id_map, &module_->rvalue_reference_types_,
428           added_type_id);
429     default:
430       // Only referencing types
431       assert(0);
432   }
433 }
434 
435 
MergeTypeInternal(const repr::TypeIR * addend_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)436 MergeStatus ModuleMerger::MergeTypeInternal(
437     const repr::TypeIR *addend_node, const repr::ModuleIR &addend,
438     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
439   switch (addend_node->GetKind()) {
440     case repr::BuiltinTypeKind:
441       return MergeBuiltinType(
442           static_cast<const repr::BuiltinTypeIR *>(addend_node), addend,
443           local_to_global_type_id_map);
444     case repr::RecordTypeKind:
445       return MergeRecordAndDependencies(
446           static_cast<const repr::RecordTypeIR *>(addend_node), addend,
447           local_to_global_type_id_map);
448     case repr::EnumTypeKind:
449       return MergeEnumType(static_cast<const repr::EnumTypeIR *>(addend_node),
450                            addend, local_to_global_type_id_map);
451     case repr::FunctionTypeKind:
452       return MergeFunctionType(
453           static_cast<const repr::FunctionTypeIR *>(addend_node), addend,
454           local_to_global_type_id_map);
455     default:
456       return MergeReferencingType(addend, addend_node,
457                                   local_to_global_type_id_map);
458   }
459   assert(0);
460 }
461 
462 
MergeType(const repr::TypeIR * addend_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)463 MergeStatus ModuleMerger::MergeType(
464     const repr::TypeIR *addend_node, const repr::ModuleIR &addend,
465     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
466   // Check if the addend type is already in the parent graph. Since we're
467   // going to traverse all the dependencies add whichever ones are not in the
468   // parent graph. This does not add the node itself though.
469   auto type_it = local_to_global_type_id_map->find(addend_node->GetSelfType());
470   if (type_it != local_to_global_type_id_map->end()) {
471     return type_it->second;
472   }
473 
474   MergeStatus merge_status = LookupType(
475       addend_node, addend, local_to_global_type_id_map);
476   if (!merge_status.was_newly_added_) {
477     return merge_status;
478   }
479   merge_status = MergeTypeInternal(
480       addend_node, addend, local_to_global_type_id_map);
481   return merge_status;
482 }
483 
484 
MergeCFunctionLikeDeps(const repr::ModuleIR & addend,repr::CFunctionLikeIR * cfunction_like_ir,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)485 void ModuleMerger::MergeCFunctionLikeDeps(
486     const repr::ModuleIR &addend, repr::CFunctionLikeIR *cfunction_like_ir,
487     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
488   // Merge the return type.
489   auto ret_type_it =
490       addend.type_graph_.find(cfunction_like_ir->GetReturnType());
491   if (ret_type_it != addend.type_graph_.end()) {
492     // Merge the type if we can find another type in the parent module.
493     MergeStatus ret_merge_status = MergeType(ret_type_it->second, addend,
494                                              local_to_global_type_id_map);
495     cfunction_like_ir->SetReturnType(ret_merge_status.type_id_);
496   }
497 
498   // Merge the argument types.
499   for (auto &param : cfunction_like_ir->GetParameters()) {
500     MergeReferencingTypeInternal(addend, &param, local_to_global_type_id_map);
501   }
502 }
503 
504 
MergeFunctionDeps(repr::FunctionIR * added_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)505 void ModuleMerger::MergeFunctionDeps(
506     repr::FunctionIR *added_node, const repr::ModuleIR &addend,
507     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
508   MergeCFunctionLikeDeps(addend, added_node, local_to_global_type_id_map);
509 
510   // Merge the template arguments.
511   for (auto &template_element : added_node->GetTemplateElements()) {
512     MergeReferencingTypeInternal(addend, &template_element,
513                                  local_to_global_type_id_map);
514   }
515 }
516 
517 
518 template <typename T>
519 static bool
IsLinkableMessagePresent(const repr::LinkableMessageIR * lm,const repr::AbiElementMap<T> & message_map)520 IsLinkableMessagePresent(const repr::LinkableMessageIR *lm,
521                          const repr::AbiElementMap<T> &message_map) {
522   return (message_map.find(lm->GetLinkerSetKey()) != message_map.end());
523 }
524 
525 
MergeFunction(const repr::FunctionIR * addend_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)526 void ModuleMerger::MergeFunction(
527     const repr::FunctionIR *addend_node, const repr::ModuleIR &addend,
528     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
529   const std::string &function_linkage_name = addend_node->GetLinkerSetKey();
530   if (IsLinkableMessagePresent(addend_node, module_->functions_)) {
531     // The functions and all of its dependencies have already been added.
532     // No two globally visible functions can have the same symbol name.
533     return;
534   }
535   repr::FunctionIR function_ir = *addend_node;
536   MergeFunctionDeps(&function_ir, addend, local_to_global_type_id_map);
537   // Add it to the parent's function map.
538   module_->functions_.emplace(function_linkage_name, std::move(function_ir));
539 }
540 
541 
MergeGlobalVariable(const repr::GlobalVarIR * addend_node,const repr::ModuleIR & addend,repr::AbiElementMap<MergeStatus> * local_to_global_type_id_map)542 void ModuleMerger::MergeGlobalVariable(
543     const repr::GlobalVarIR *addend_node, const repr::ModuleIR &addend,
544     repr::AbiElementMap<MergeStatus> *local_to_global_type_id_map) {
545   const std::string &global_variable_linkage_name =
546       addend_node->GetLinkerSetKey();
547   if (IsLinkableMessagePresent(addend_node, module_->global_variables_)) {
548     // The global variable and all of its dependencies have already been added.
549     return;
550   }
551   repr::GlobalVarIR global_variable_ir = *addend_node;
552   MergeReferencingTypeInternal(addend, &global_variable_ir,
553                                local_to_global_type_id_map);
554   module_->global_variables_.emplace(
555       global_variable_linkage_name, std::move(global_variable_ir));
556 }
557 
558 
MergeGraphs(const repr::ModuleIR & addend)559 void ModuleMerger::MergeGraphs(const repr::ModuleIR &addend) {
560   // Iterate through nodes of addend reader and merge them.
561   // Keep a merged types cache since if a type is merged, so will all of its
562   // dependencies which weren't already merged.
563   repr::AbiElementMap<MergeStatus> merged_types_cache;
564 
565   for (auto &&type_ir : addend.type_graph_) {
566     MergeType(type_ir.second, addend, &merged_types_cache);
567   }
568 
569   for (auto &&function_ir : addend.functions_) {
570     MergeFunction(&function_ir.second, addend, &merged_types_cache);
571   }
572 
573   for (auto &&global_var_ir : addend.global_variables_) {
574     MergeGlobalVariable(&global_var_ir.second, addend, &merged_types_cache);
575   }
576 }
577 
578 
579 }  // namespace linker
580 }  // namespace header_checker
581 
582