1 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #include "Literals.h"
22 #include "Matcher.h"
23 #include "Name.h"
24 #include "Names.h"
25 #include "NameVisitor.h"
26 
27 #include <cstring>
28 #include <string_view>
29 
30 using namespace CPlusPlus;
31 
Name()32 Name::Name()
33 { }
34 
~Name()35 Name::~Name()
36 { }
37 
isNameId() const38 bool Name::isNameId() const
39 { return asNameId() != nullptr; }
40 
isAnonymousNameId() const41 bool Name::isAnonymousNameId() const
42 { return asAnonymousNameId() != nullptr; }
43 
isTemplateNameId() const44 bool Name::isTemplateNameId() const
45 { return asTemplateNameId() != nullptr; }
46 
isDestructorNameId() const47 bool Name::isDestructorNameId() const
48 { return asDestructorNameId() != nullptr; }
49 
isOperatorNameId() const50 bool Name::isOperatorNameId() const
51 { return asOperatorNameId() != nullptr; }
52 
isConversionNameId() const53 bool Name::isConversionNameId() const
54 { return asConversionNameId() != nullptr; }
55 
isQualifiedNameId() const56 bool Name::isQualifiedNameId() const
57 { return asQualifiedNameId() != nullptr; }
58 
isSelectorNameId() const59 bool Name::isSelectorNameId() const
60 { return asSelectorNameId() != nullptr; }
61 
accept(NameVisitor * visitor) const62 void Name::accept(NameVisitor *visitor) const
63 {
64     if (visitor->preVisit(this))
65         accept0(visitor);
66     visitor->postVisit(this);
67 }
68 
accept(const Name * name,NameVisitor * visitor)69 void Name::accept(const Name *name, NameVisitor *visitor)
70 {
71     if (! name)
72         return;
73     name->accept(visitor);
74 }
75 
match(const Name * other,Matcher * matcher) const76 bool Name::match(const Name *other, Matcher *matcher) const
77 {
78     return Matcher::match(this, other, matcher);
79 }
80 
operator ()(const Name * name,const Name * other) const81 bool Name::Equals::operator()(const Name *name, const Name *other) const
82 {
83     if (name == other)
84         return true;
85     if (name == nullptr || other == nullptr)
86         return false;
87 
88     const Identifier *id = name->identifier();
89     const Identifier *otherId = other->identifier();
90 
91     if (id == otherId)
92         return true;
93     if (id == nullptr || otherId == nullptr)
94         return false;
95 
96     return std::strcmp(id->chars(), otherId->chars()) == 0;
97 }
98 
operator ()(const Name * name) const99 size_t Name::Hash::operator()(const Name *name) const
100 {
101     if (name == nullptr)
102         return 0;
103 
104     const Identifier *id = name->identifier();
105 
106     if (id == nullptr)
107         return 0;
108 
109     return std::hash<std::string_view>()(std::string_view(id->chars()));
110 }
111