freebsd-nq/include/clang/AST/CharUnits.h

150 lines
4.9 KiB
C
Raw Normal View History

2010-01-01 10:34:51 +00:00
//===--- CharUnits.h - Character units for sizes and offsets ----*- 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 CharUnits class
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_AST_CHARUNITS_H
#define LLVM_CLANG_AST_CHARUNITS_H
#include "llvm/System/DataTypes.h"
namespace clang {
/// CharUnits - This is an opaque type for sizes expressed in character units.
/// Instances of this type represent a quantity as a multiple of the size
/// of the standard C type, char, on the target architecture. As an opaque
/// type, CharUnits protects you from accidentally combining operations on
/// quantities in bit units and character units.
///
/// It should be noted that characters and bytes are distinct concepts. Bytes
/// refer to addressable units of data storage on the target machine, and
/// characters are members of a set of elements used for the organization,
/// control, or representation of data. According to C99, bytes are allowed
/// to exceed characters in size, although currently, clang only supports
/// architectures where the two are the same size.
///
/// For portability, never assume that a target character is 8 bits wide. Use
/// CharUnit values whereever you calculate sizes, offsets, or alignments
/// in character units.
class CharUnits {
public:
2010-01-15 15:39:40 +00:00
typedef int64_t QuantityType;
2010-01-01 10:34:51 +00:00
private:
2010-01-15 15:39:40 +00:00
QuantityType Quantity;
2010-01-01 10:34:51 +00:00
2010-01-15 15:39:40 +00:00
explicit CharUnits(QuantityType C) : Quantity(C) {}
2010-01-01 10:34:51 +00:00
public:
/// CharUnits - A default constructor.
CharUnits() : Quantity(0) {}
/// Zero - Construct a CharUnits quantity of zero.
static CharUnits Zero() {
return CharUnits(0);
}
/// One - Construct a CharUnits quantity of one.
static CharUnits One() {
return CharUnits(1);
}
2010-01-15 15:39:40 +00:00
/// fromQuantity - Construct a CharUnits quantity from a raw integer type.
static CharUnits fromQuantity(QuantityType Quantity) {
2010-01-01 10:34:51 +00:00
return CharUnits(Quantity);
}
// Compound assignment.
CharUnits& operator+= (const CharUnits &Other) {
Quantity += Other.Quantity;
return *this;
}
CharUnits& operator-= (const CharUnits &Other) {
Quantity -= Other.Quantity;
return *this;
}
// Comparison operators.
bool operator== (const CharUnits &Other) const {
return Quantity == Other.Quantity;
}
bool operator!= (const CharUnits &Other) const {
return Quantity != Other.Quantity;
}
// Relational operators.
bool operator< (const CharUnits &Other) const {
return Quantity < Other.Quantity;
}
bool operator<= (const CharUnits &Other) const {
return Quantity <= Other.Quantity;
}
bool operator> (const CharUnits &Other) const {
return Quantity > Other.Quantity;
}
bool operator>= (const CharUnits &Other) const {
return Quantity >= Other.Quantity;
}
// Other predicates.
/// isZero - Test whether the quantity equals zero.
bool isZero() const { return Quantity == 0; }
/// isOne - Test whether the quantity equals one.
bool isOne() const { return Quantity == 1; }
2010-01-15 15:39:40 +00:00
/// isPositive - Test whether the quantity is greater than zero.
2010-01-01 10:34:51 +00:00
bool isPositive() const { return Quantity > 0; }
/// isNegative - Test whether the quantity is less than zero.
bool isNegative() const { return Quantity < 0; }
// Arithmetic operators.
2010-01-15 15:39:40 +00:00
CharUnits operator* (QuantityType N) const {
2010-01-01 10:34:51 +00:00
return CharUnits(Quantity * N);
}
2010-01-15 15:39:40 +00:00
CharUnits operator/ (QuantityType N) const {
2010-01-01 10:34:51 +00:00
return CharUnits(Quantity / N);
}
2010-01-15 15:39:40 +00:00
QuantityType operator/ (const CharUnits &Other) const {
2010-01-01 10:34:51 +00:00
return Quantity / Other.Quantity;
}
2010-01-15 15:39:40 +00:00
CharUnits operator% (QuantityType N) const {
2010-01-01 10:34:51 +00:00
return CharUnits(Quantity % N);
}
2010-01-15 15:39:40 +00:00
QuantityType operator% (const CharUnits &Other) const {
2010-01-01 10:34:51 +00:00
return Quantity % Other.Quantity;
}
CharUnits operator+ (const CharUnits &Other) const {
return CharUnits(Quantity + Other.Quantity);
}
CharUnits operator- (const CharUnits &Other) const {
return CharUnits(Quantity - Other.Quantity);
}
// Conversions.
2010-01-15 15:39:40 +00:00
/// getQuantity - Get the raw integer representation of this quantity.
QuantityType getQuantity() const { return Quantity; }
2010-01-01 10:34:51 +00:00
}; // class CharUnit
} // namespace clang
2010-01-15 15:39:40 +00:00
inline clang::CharUnits operator* (clang::CharUnits::QuantityType Scale,
2010-01-01 10:34:51 +00:00
const clang::CharUnits &CU) {
return CU * Scale;
}
#endif // LLVM_CLANG_AST_CHARUNITS_H