LabelScope.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2008 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef LabelScope_h
  29. #define LabelScope_h
  30. #include <wtf/PassRefPtr.h>
  31. #include "Label.h"
  32. namespace JSC {
  33. class Identifier;
  34. class LabelScope {
  35. public:
  36. enum Type { Loop, Switch, NamedLabel };
  37. LabelScope(Type type, const Identifier* name, int scopeDepth, PassRefPtr<Label> breakTarget, PassRefPtr<Label> continueTarget)
  38. : m_refCount(0)
  39. , m_type(type)
  40. , m_name(name)
  41. , m_scopeDepth(scopeDepth)
  42. , m_breakTarget(breakTarget)
  43. , m_continueTarget(continueTarget)
  44. {
  45. }
  46. int refCount() const { return m_refCount; }
  47. Label* breakTarget() const { return m_breakTarget.get(); }
  48. Label* continueTarget() const { return m_continueTarget.get(); }
  49. Type type() const { return m_type; }
  50. const Identifier* name() const { return m_name; }
  51. int scopeDepth() const { return m_scopeDepth; }
  52. private:
  53. friend class LabelScopePtr;
  54. void ref() { ++m_refCount; }
  55. void deref()
  56. {
  57. --m_refCount;
  58. ASSERT(m_refCount >= 0);
  59. }
  60. int m_refCount;
  61. Type m_type;
  62. const Identifier* m_name;
  63. int m_scopeDepth;
  64. RefPtr<Label> m_breakTarget;
  65. RefPtr<Label> m_continueTarget;
  66. };
  67. typedef Vector<LabelScope, 8> LabelScopeStore;
  68. class LabelScopePtr {
  69. public:
  70. LabelScopePtr()
  71. : m_owner(0)
  72. , m_index(0)
  73. {
  74. }
  75. LabelScopePtr(LabelScopeStore* owner, size_t index)
  76. : m_owner(owner)
  77. , m_index(index)
  78. {
  79. m_owner->at(index).ref();
  80. }
  81. LabelScopePtr(const LabelScopePtr& other)
  82. : m_owner(other.m_owner)
  83. , m_index(other.m_index)
  84. {
  85. if (m_owner)
  86. m_owner->at(m_index).ref();
  87. }
  88. const LabelScopePtr& operator=(const LabelScopePtr& other)
  89. {
  90. if (other.m_owner)
  91. other.m_owner->at(other.m_index).ref();
  92. if (m_owner)
  93. m_owner->at(m_index).deref();
  94. m_owner = other.m_owner;
  95. m_index = other.m_index;
  96. return *this;
  97. }
  98. ~LabelScopePtr()
  99. {
  100. if (m_owner)
  101. m_owner->at(m_index).deref();
  102. }
  103. LabelScope& operator*() { ASSERT(m_owner); return m_owner->at(m_index); }
  104. LabelScope* operator->() { ASSERT(m_owner); return &m_owner->at(m_index); }
  105. const LabelScope& operator*() const { ASSERT(m_owner); return m_owner->at(m_index); }
  106. const LabelScope* operator->() const { ASSERT(m_owner); return &m_owner->at(m_index); }
  107. private:
  108. LabelScopeStore* m_owner;
  109. size_t m_index;
  110. };
  111. } // namespace JSC
  112. #endif // LabelScope_h