1 //===-- DWARFDeclContext.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DWARFDeclContext.h"
10 
11 using namespace lldb_private::dwarf;
12 
13 const char *DWARFDeclContext::GetQualifiedName() const {
14   if (m_qualified_name.empty()) {
15     // The declaration context array for a class named "foo" in namespace
16     // "a::b::c" will be something like:
17     //  [0] DW_TAG_class_type "foo"
18     //  [1] DW_TAG_namespace "c"
19     //  [2] DW_TAG_namespace "b"
20     //  [3] DW_TAG_namespace "a"
21     if (!m_entries.empty()) {
22       if (m_entries.size() == 1) {
23         if (m_entries[0].name) {
24           m_qualified_name.append("::");
25           m_qualified_name.append(m_entries[0].name);
26         }
27       } else {
28         collection::const_reverse_iterator pos;
29         collection::const_reverse_iterator begin = m_entries.rbegin();
30         collection::const_reverse_iterator end = m_entries.rend();
31         for (pos = begin; pos != end; ++pos) {
32           if (pos != begin)
33             m_qualified_name.append("::");
34           if (pos->name == nullptr) {
35             if (pos->tag == DW_TAG_namespace)
36               m_qualified_name.append("(anonymous namespace)");
37             else if (pos->tag == DW_TAG_class_type)
38               m_qualified_name.append("(anonymous class)");
39             else if (pos->tag == DW_TAG_structure_type)
40               m_qualified_name.append("(anonymous struct)");
41             else if (pos->tag == DW_TAG_union_type)
42               m_qualified_name.append("(anonymous union)");
43             else
44               m_qualified_name.append("(anonymous)");
45           } else
46             m_qualified_name.append(pos->name);
47         }
48       }
49     }
50   }
51   if (m_qualified_name.empty())
52     return nullptr;
53   return m_qualified_name.c_str();
54 }
55 
56 bool DWARFDeclContext::operator==(const DWARFDeclContext &rhs) const {
57   if (m_entries.size() != rhs.m_entries.size())
58     return false;
59 
60   collection::const_iterator pos;
61   collection::const_iterator begin = m_entries.begin();
62   collection::const_iterator end = m_entries.end();
63 
64   collection::const_iterator rhs_pos;
65   collection::const_iterator rhs_begin = rhs.m_entries.begin();
66   // The two entry arrays have the same size
67 
68   // First compare the tags before we do expensive name compares
69   for (pos = begin, rhs_pos = rhs_begin; pos != end; ++pos, ++rhs_pos) {
70     if (pos->tag != rhs_pos->tag) {
71       // Check for DW_TAG_structure_type and DW_TAG_class_type as they are
72       // often used interchangeably in GCC
73       if (pos->tag == DW_TAG_structure_type &&
74           rhs_pos->tag == DW_TAG_class_type)
75         continue;
76       if (pos->tag == DW_TAG_class_type &&
77           rhs_pos->tag == DW_TAG_structure_type)
78         continue;
79       return false;
80     }
81   }
82   // The tags all match, now compare the names
83   for (pos = begin, rhs_pos = rhs_begin; pos != end; ++pos, ++rhs_pos) {
84     if (!pos->NameMatches(*rhs_pos))
85       return false;
86   }
87   // All tags and names match
88   return true;
89 }
90