<commit_before>//===-- CanonicalType.h - C Language Family Type Representation -*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines the CanQual class template, which provides access to
//  canonical types.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_AST_CANONICAL_TYPE_H
#define LLVM_CLANG_AST_CANONICAL_TYPE_H

#include "clang/AST/Type.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/type_traits.h"
#include <iterator>

namespace clang {
  
template<typename T> class CanProxy;
template<typename T> struct CanProxyAdaptor;

//----------------------------------------------------------------------------//
// Canonical, qualified type template
//----------------------------------------------------------------------------//
  
/// \brief Represents a canonical, potentially-qualified type.
///
/// The CanQual template is a lightweight smart pointer that provides access
/// to the canonical representation of a type, where all typedefs and other
/// syntactic sugar has been eliminated. A CanQualType may also have various 
/// qualifiers (const, volatile, restrict) attached to it.
///
/// The template type parameter @p T is one of the Type classes (PointerType, 
/// BuiltinType, etc.). The type stored within @c CanQual<T> will be of that
/// type (or some subclass of that type). The typedef @c CanQualType is just
/// a shorthand for @c CanQual<Type>.
///
/// An instance of @c CanQual<T> can be implicitly converted to a 
/// @c CanQual<U> when T is derived from U, which essentially provides an
/// implicit upcast. For example, @c CanQual<LValueReferenceType> can be 
/// converted to @c CanQual<ReferenceType>. Note that any @c CanQual type can 
/// be implicitly converted to a QualType, but the reverse operation requires
/// a call to ASTContext::getCanonicalType().
/// 
/// 
template<typename T = Type>
class CanQual {
  /// \brief The actual, canonical type. 
  QualType Stored;
  
public:
  /// \brief Constructs a NULL canonical type.
  CanQual() : Stored() { }
  
  /// \brief Converting constructor that permits implicit upcasting of
  /// canonical type pointers.
  template<typename U>
  CanQual(const CanQual<U>& Other, 
          typename llvm::enable_if<llvm::is_base_of<T, U>, int>::type = 0);
  
  /// \brief Implicit conversion to the underlying pointer.
  ///
  /// Also provides the ability to use canonical types in a boolean context,
  /// e.g., 
  /// @code
  ///   if (CanQual<PointerType> Ptr = T->getAs<PointerType>()) { ... }
  /// @endcode
  operator const T*() const { return getTypePtr(); }
  
  /// \brief Retrieve the underlying type pointer, which refers to a 
  /// canonical type.
  T *getTypePtr() const { return cast_or_null<T>(Stored.getTypePtr()); }
  
  /// \brief Implicit conversion to a qualified type.
  operator QualType() const { return Stored; }
  
  /// \brief Retrieve a canonical type pointer with a different static type,
  /// upcasting or downcasting as needed.
  ///
  /// The getAs() function is typically used to try to downcast to a 
  /// more specific (canonical) type in the type system. For example:
  ///
  /// @code
  /// void f(CanQual<Type> T) {
  ///   if (CanQual<PointerType> Ptr = T->getAs<PointerType>()) {
  ///     // look at Ptr's pointee type
  ///   }
  /// }
  /// @endcode
  ///
  /// \returns A proxy pointer to the same type, but with the specified
  /// static type (@p U). If the dynamic type is not the specified static type
  /// or a derived class thereof, a NULL canonical type.
  template<typename U> CanProxy<U> getAs() const;
  
  /// \brief Overloaded arrow operator that produces a canonical type
  /// proxy.
  CanProxy<T> operator->() const;
  
  /// \brief Retrieve the const/volatile/restrict qualifiers.
  unsigned getCVRQualifiers() const { return Stored.getCVRQualifiers(); }
  
  /// \brief Set the const/volatile/restrict qualifiers
  void setCVRQualifiers(unsigned Quals) { Stored.setCVRQualifiers(Quals); }
  
  bool isConstQualified() const {
    return (getCVRQualifiers() & QualType::Const) ? true : false;
  }
  bool isVolatileQualified() const {
    return (getCVRQualifiers() & QualType::Volatile) ? true : false;
  }
  bool isRestrictQualified() const {
    return (getCVRQualifiers() & QualType::Restrict) ? true : false;
  }  
  
  /// \brief Retrieve the unqualified form of this type.
  CanQual<T> getUnqualifiedType() const;
  
  CanQual<T> getQualifiedType(unsigned TQs) const {
    return CanQual<T>::CreateUnsafe(QualType(getTypePtr(), TQs));
  }
  
  /// \brief Determines whether this canonical type is more qualified than 
  /// the @p Other canonical type.
  bool isMoreQualifiedThan(CanQual<T> Other) const {
    return Stored.isMoreQualifiedThan(Other.Stored);
  }
  
  /// \brief Determines whether this canonical type is at least as qualified as
  /// the @p Other canonical type.
  bool isAtLeastAsQualifiedAs(CanQual<T> Other) const {
    return Stored.isAtLeastAsQualifiedAs(Other.Stored);
  }
  
  /// \brief If the canonical type is a reference type, returns the type that
  /// it refers to; otherwise, 
  CanQual<Type> getNonReferenceType() const;
  
  /// \brief Retrieve the internal representation of this canonical type.
  void *getAsOpaquePtr() const { return Stored.getAsOpaquePtr(); }
  
  /// \brief Construct a canonical type from its internal representation.
  static CanQual<T> getFromOpaquePtr(void *Ptr);
  
  /// \brief Builds a canonical type from a QualType.
  ///
  /// This routine is inherently unsafe, because it requires the user to 
  /// ensure that the given type is a canonical type with the correct 
  // (dynamic) type. 
  static CanQual<T> CreateUnsafe(QualType Other);
};

template<typename T, typename U>
inline bool operator==(CanQual<T> x, CanQual<U> y) {
  return x.getAsOpaquePtr() == y.getAsOpaquePtr();
}

template<typename T, typename U>
inline bool operator!=(CanQual<T> x, CanQual<U> y) {
  return x.getAsOpaquePtr() != y.getAsOpaquePtr();
}

/// \brief Represents a canonical, potentially-qualified type.
typedef CanQual<Type> CanQualType;

//----------------------------------------------------------------------------//
// Internal proxy classes used by canonical types<commit_msg>Fix a comment that trailed off<commit_after>//===-- CanonicalType.h - C Language Family Type Representation -*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines the CanQual class template, which provides access to
//  canonical types.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_AST_CANONICAL_TYPE_H
#define LLVM_CLANG_AST_CANONICAL_TYPE_H

#include "clang/AST/Type.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/type_traits.h"
#include <iterator>

namespace clang {
  
template<typename T> class CanProxy;
template<typename T> struct CanProxyAdaptor;

//----------------------------------------------------------------------------//
// Canonical, qualified type template
//----------------------------------------------------------------------------//
  
/// \brief Represents a canonical, potentially-qualified type.
///
/// The CanQual template is a lightweight smart pointer that provides access
/// to the canonical representation of a type, where all typedefs and other
/// syntactic sugar has been eliminated. A CanQualType may also have various 
/// qualifiers (const, volatile, restrict) attached to it.
///
/// The template type parameter @p T is one of the Type classes (PointerType, 
/// BuiltinType, etc.). The type stored within @c CanQual<T> will be of that
/// type (or some subclass of that type). The typedef @c CanQualType is just
/// a shorthand for @c CanQual<Type>.
///
/// An instance of @c CanQual<T> can be implicitly converted to a 
/// @c CanQual<U> when T is derived from U, which essentially provides an
/// implicit upcast. For example, @c CanQual<LValueReferenceType> can be 
/// converted to @c CanQual<ReferenceType>. Note that any @c CanQual type can 
/// be implicitly converted to a QualType, but the reverse operation requires
/// a call to ASTContext::getCanonicalType().
/// 
/// 
template<typename T = Type>
class CanQual {
  /// \brief The actual, canonical type. 
  QualType Stored;
  
public:
  /// \brief Constructs a NULL canonical type.
  CanQual() : Stored() { }
  
  /// \brief Converting constructor that permits implicit upcasting of
  /// canonical type pointers.
  template<typename U>
  CanQual(const CanQual<U>& Other, 
          typename llvm::enable_if<llvm::is_base_of<T, U>, int>::type = 0);
  
  /// \brief Implicit conversion to the underlying pointer.
  ///
  /// Also provides the ability to use canonical types in a boolean context,
  /// e.g., 
  /// @code
  ///   if (CanQual<PointerType> Ptr = T->getAs<PointerType>()) { ... }
  /// @endcode
  operator const T*() const { return getTypePtr(); }
  
  /// \brief Retrieve the underlying type pointer, which refers to a 
  /// canonical type.
  T *getTypePtr() const { return cast_or_null<T>(Stored.getTypePtr()); }
  
  /// \brief Implicit conversion to a qualified type.
  operator QualType() const { return Stored; }
  
  /// \brief Retrieve a canonical type pointer with a different static type,
  /// upcasting or downcasting as needed.
  ///
  /// The getAs() function is typically used to try to downcast to a 
  /// more specific (canonical) type in the type system. For example:
  ///
  /// @code
  /// void f(CanQual<Type> T) {
  ///   if (CanQual<PointerType> Ptr = T->getAs<PointerType>()) {
  ///     // look at Ptr's pointee type
  ///   }
  /// }
  /// @endcode
  ///
  /// \returns A proxy pointer to the same type, but with the specified
  /// static type (@p U). If the dynamic type is not the specified static type
  /// or a derived class thereof, a NULL canonical type.
  template<typename U> CanProxy<U> getAs() const;
  
  /// \brief Overloaded arrow operator that produces a canonical type
  /// proxy.
  CanProxy<T> operator->() const;
  
  /// \brief Retrieve the const/volatile/restrict qualifiers.
  unsigned getCVRQualifiers() const { return Stored.getCVRQualifiers(); }
  
  /// \brief Set the const/volatile/restrict qualifiers
  void setCVRQualifiers(unsigned Quals) { Stored.setCVRQualifiers(Quals); }
  
  bool isConstQualified() const {
    return (getCVRQualifiers() & QualType::Const) ? true : false;
  }
  bool isVolatileQualified() const {
    return (getCVRQualifiers() & QualType::Volatile) ? true : false;
  }
  bool isRestrictQualified() const {
    return (getCVRQualifiers() & QualType::Restrict) ? true : false;
  }  
  
  /// \brief Retrieve the unqualified form of this type.
  CanQual<T> getUnqualifiedType() const;
  
  CanQual<T> getQualifiedType(unsigned TQs) const {
    return CanQual<T>::CreateUnsafe(QualType(getTypePtr(), TQs));
  }
  
  /// \brief Determines whether this canonical type is more qualified than 
  /// the @p Other canonical type.
  bool isMoreQualifiedThan(CanQual<T> Other) const {
    return Stored.isMoreQualifiedThan(Other.Stored);
  }
  
  /// \brief Determines whether this canonical type is at least as qualified as
  /// the @p Other canonical type.
  bool isAtLeastAsQualifiedAs(CanQual<T> Other) const {
    return Stored.isAtLeastAsQualifiedAs(Other.Stored);
  }
  
  /// \brief If the canonical type is a reference type, returns the type that
  /// it refers to; otherwise, returns the type itself.
  CanQual<Type> getNonReferenceType() const;
  
  /// \brief Retrieve the internal representation of this canonical type.
  void *getAsOpaquePtr() const { return Stored.getAsOpaquePtr(); }
  
  /// \brief Construct a canonical type from its internal representation.
  static CanQual<T> getFromOpaquePtr(void *Ptr);
  
  /// \brief Builds a canonical type from a QualType.
  ///
  /// This routine is inherently unsafe, because it requires the user to 
  /// ensure that the given type is a canonical type with the correct 
  // (dynamic) type. 
  static CanQual<T> CreateUnsafe(QualType Other);
};

template<typename T, typename U>
inline bool operator==(CanQual<T> x, CanQual<U> y) {
  return x.getAsOpaquePtr() == y.getAsOpaquePtr();
}

template<typename T, typename U>
inline bool operator!=(CanQual<T> x, CanQual<U> y) {
  return x.getAsOpaquePtr() != y.getAsOpaquePtr();
}

/// \brief Represents a canonical, potentially-qualified type.
typedef CanQual<Type> CanQualType;

//----------------------------------------------------------------------------//
// Internal proxy classes used by canonical types
{'fraction_non_alphanumeric': 0.17095658855167115, 'fraction_numerical': 0.0001536688436419516, 'mean_word_length': 2.538879825992387, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1862675', 'n_tokens_mistral': 3727, 'n_tokens_neox': 3657, 'n_words': 1314}