Literals.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2008 Roberto Raggi <[email protected]>
  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. #include "Literals.h"
  21. #include "NameVisitor.h"
  22. #include "Matcher.h"
  23. #include <cstring>
  24. #include <algorithm>
  25. #include <iostream>
  26. using namespace CPlusPlus;
  27. ////////////////////////////////////////////////////////////////////////////////
  28. Literal::Literal(const char *chars, unsigned size)
  29. : _next(0), _index(0)
  30. {
  31. _chars = new char[size + 1];
  32. std::strncpy(_chars, chars, size);
  33. _chars[size] = '\0';
  34. _size = size;
  35. _hashCode = hashCode(_chars, _size);
  36. }
  37. Literal::~Literal()
  38. { delete[] _chars; }
  39. bool Literal::equalTo(const Literal *other) const
  40. {
  41. if (! other)
  42. return false;
  43. else if (this == other)
  44. return true;
  45. else if (hashCode() != other->hashCode())
  46. return false;
  47. else if (size() != other->size())
  48. return false;
  49. return ! std::strcmp(chars(), other->chars());
  50. }
  51. unsigned Literal::hashCode(const char *chars, unsigned size)
  52. {
  53. /* Hash taken from QtCore's qHash for strings, which in turn has the note:
  54. These functions are based on Peter J. Weinberger's hash function
  55. (from the Dragon Book). The constant 24 in the original function
  56. was replaced with 23 to produce fewer collisions on input such as
  57. "a", "aa", "aaa", "aaaa", ...
  58. */
  59. unsigned h = 0;
  60. while (size--) {
  61. h = (h << 4) + *chars++;
  62. h ^= (h & 0xf0000000) >> 23;
  63. h &= 0x0fffffff;
  64. }
  65. return h;
  66. }
  67. ////////////////////////////////////////////////////////////////////////////////
  68. NumericLiteral::NumericLiteral(const char *chars, unsigned size)
  69. : Literal(chars, size), _flags(0)
  70. {
  71. f._type = NumericLiteralIsInt;
  72. if (size > 1 && chars[0] == '0' && (chars[1] == 'x' || chars[1] == 'X')) {
  73. f._isHex = true;
  74. } else {
  75. const char *begin = chars;
  76. const char *end = begin + size;
  77. bool done = false;
  78. const char *it = end - 1;
  79. for (; it != begin - 1 && ! done; --it) {
  80. switch (*it) {
  81. case 'l': case 'L': // long suffix
  82. case 'u': case 'U': // unsigned suffix
  83. case 'f': case 'F': // floating suffix
  84. break;
  85. default:
  86. done = true;
  87. break;
  88. } // switch
  89. }
  90. for (const char *dot = it; it != begin - 1; --it) {
  91. if (*dot == '.')
  92. f._type = NumericLiteralIsDouble;
  93. }
  94. for (++it; it != end; ++it) {
  95. if (*it == 'l' || *it == 'L') {
  96. if (f._type == NumericLiteralIsDouble) {
  97. f._type = NumericLiteralIsLongDouble;
  98. } else if (it + 1 != end && (it[1] == 'l' || it[1] == 'L')) {
  99. ++it;
  100. f._type = NumericLiteralIsLongLong;
  101. } else {
  102. f._type = NumericLiteralIsLong;
  103. }
  104. } else if (*it == 'f' || *it == 'F') {
  105. f._type = NumericLiteralIsFloat;
  106. } else if (*it == 'u' || *it == 'U') {
  107. f._isUnsigned = true;
  108. }
  109. }
  110. }
  111. }
  112. ////////////////////////////////////////////////////////////////////////////////
  113. void Identifier::accept0(NameVisitor *visitor) const
  114. { visitor->visit(this); }
  115. bool Identifier::match0(const Name *otherName, Matcher *matcher) const
  116. {
  117. if (const Identifier *id = otherName->asNameId())
  118. return matcher->match(this, id);
  119. return false;
  120. }