[cfe-commits] r91683 - /cfe/trunk/include/clang/AST/CharUnits.h
Ken Dyck
ken.dyck at onsemi.com
Fri Dec 18 05:39:47 PST 2009
Author: kjdyck
Date: Fri Dec 18 07:39:46 2009
New Revision: 91683
URL: http://llvm.org/viewvc/llvm-project?rev=91683&view=rev
Log:
Initial implementation of CharUnits, an opaque value class for representing
sizes, offsets, and alignments in character units.
Added:
cfe/trunk/include/clang/AST/CharUnits.h
Added: cfe/trunk/include/clang/AST/CharUnits.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/CharUnits.h?rev=91683&view=auto
==============================================================================
--- cfe/trunk/include/clang/AST/CharUnits.h (added)
+++ cfe/trunk/include/clang/AST/CharUnits.h Fri Dec 18 07:39:46 2009
@@ -0,0 +1,141 @@
+//===--- 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/ADT/StringExtras.h"
+
+#include <stdint.h>
+#include <string>
+
+namespace clang {
+ // An opaque type for sizes expressed in character units
+ class CharUnits {
+ public:
+ typedef int64_t RawType;
+
+ private:
+ RawType Quantity;
+
+ explicit CharUnits(RawType C) : Quantity(C) {}
+
+ public:
+
+ /// 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);
+ }
+
+ /// fromRaw - Construct a CharUnits quantity from a raw integer type.
+ static CharUnits fromRaw(RawType Quantity) {
+ 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; }
+
+ /// isPositive - Test whether the quanity is greater than zero.
+ bool isPositive() const { return Quantity > 0; }
+
+ /// isNegative - Test whether the quantity is less than zero.
+ bool isNegative() const { return Quantity < 0; }
+
+ // arithmetic operators
+ CharUnits operator* (RawType N) const {
+ return CharUnits(Quantity * N);
+ }
+ CharUnits operator/ (RawType N) const {
+ return CharUnits(Quantity / N);
+ }
+ RawType operator/ (const CharUnits &Other) const {
+ return Quantity / Other.Quantity;
+ }
+ CharUnits operator% (RawType N) const {
+ return CharUnits(Quantity % N);
+ }
+ RawType operator% (const CharUnits &Other) const {
+ 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
+
+ /// toString - Convert to a string.
+ std::string toString() const {
+ return llvm::itostr(Quantity);
+ }
+
+ /// getRaw - Get the raw integer representation of this quantity.
+ RawType getRaw() const { return Quantity; }
+
+
+ }; // class CharUnit
+} // namespace clang
+
+inline clang::CharUnits operator* (clang::CharUnits::RawType Scale,
+ const clang::CharUnits &CU) {
+ return CU * Scale;
+}
+
+#endif // LLVM_CLANG_AST_CHARUNITS_H
More information about the cfe-commits
mailing list