xtc.type
Class C

java.lang.Object
  extended by xtc.type.C

public class C
extends java.lang.Object

Common type operations for the C language.

Version:
$Revision: 1.44 $
Author:
Robert Grimm

Field Summary
protected  java.math.BigInteger FACTOR_CHAR
          The factor for chars.
protected  java.math.BigInteger FACTOR_WIDE
          The factor for wide chars.
static boolean GCC_LEGACY_LAYOUT
          The flag for GCC's legacy structure layout.
static IntegerT IMPLICIT
          The canonical implicit int type.
protected static NumberT.Kind KIND_PTR_DIFF
          The integer kind of the pointer difference type.
protected static NumberT.Kind KIND_SIZEOF
          The integer kind of the sizeof type.
protected static NumberT.Kind KIND_WCHAR
          The number kind of the wchar_t type.
static IntegerT PTR_DIFF
          The canonical pointer difference type.
static IntegerT SIZEOF
          The canonical sizeof type.
static IntegerT WCHAR
          The canonical wide char type.
 
Constructor Summary
C()
          Create a new instance.
 
Method Summary
 Type compose(Type t1, Type t2, boolean pedantic)
          Compose the specified types.
protected  Type compose(Type t1, Type t2, boolean pedantic, boolean recursive)
          Compose the specified types.
protected  Type composeArrays(ArrayT a1, ArrayT a2)
          Compose the specified array types (C99 6.2.7).
protected  Type composeFunctions(FunctionT f1, FunctionT f2, boolean pedantic)
          Compose the specified function types (C99 6.2.7).
 Type convert(Type t1, Type t2)
          Perform the usual arithmetic conversions.
 boolean equal(Type t1, Type t2)
          Determine whether the specified types are equal to each other.
 Type fit(java.math.BigInteger number)
          Fit the specified number to the closest integer type, starting with int.
 boolean fits(java.math.BigInteger number, Type type)
          Determine whether the specified number fits the specified integer type.
protected  long getAligned(Type type)
          Get the specified type's alignment.
 long getAlignment(Type type)
          Get the specified type's alignment in bytes.
 long getAlignment(Type type, boolean natural)
          Get the specified type's alignment in bytes.
 Reference getConstRef(Type type)
          Get the specified type's constant reference.
 long getOffset(StructOrUnionT type, java.lang.String name)
          Get the specified member's offset for the specified structure or union.
 long getSize(Type type)
          Get the specified type's size in bytes.
 long getWidth(Type number)
          Get the specified number's size in bits.
 boolean hasConstRef(Type type)
          Determine whether this type has a constant reference.
 boolean hasQualifiers(Type type)
          Determine whether the specified type has any qualifiers.
 boolean hasQualifiers(Type type, Type template)
          Determine whether the specified type has at least the qualifiers of the specified template.
 boolean hasSameQualifiers(Type type, Type template)
          Determine whether the specified type has the same qualifiers as the specified template.
 boolean hasThreadLocals()
          Determine whether the current target supports thread-local storage.
 boolean hasTrailingArray(Type type)
          Determine whether the specified type has a trailing array.
 boolean isArithmetic(Type type)
          Determine whether the specified type is arithmetic.
 boolean isChar(Type type)
          Determine whether the specified type is a char.
 boolean isConstant(Type type)
          Determine whether the specified type is qualified as constant.
 boolean isIncomplete(Type type)
          Determine whether the specified type is incomplete.
 boolean isIntegral(Type type)
          Determine whether the specified type is integral.
 boolean isModifiable(Type type)
          Determine whether the specified type represents a modifiable lvalue.
protected  boolean isPacked(Type type)
          Determine whether the specified type has a GCC packed attribute.
 boolean isReal(Type type)
          Determine whether the specified type is real.
 boolean isScalar(Type type)
          Determine whether the specified type is scalar.
 boolean isString(Type type)
          Determine whether the specified type is a string.
 boolean isStringLiteral(Type type)
          Determine whether the specified type is a string literal.
 boolean isVariablyModified(Type type)
          Determine whether the specified type is variably modified.
 boolean isWideChar(Type type)
          Determine whether the specified type is a wide char.
 boolean isWideString(Type type)
          Determine whether the specified type is a wide string.
 long layout(StructT type, java.lang.String name)
          Lay out the specified structure.
 java.math.BigInteger mask(java.math.BigInteger number, Type type)
          Mask the specified number as a value of this integer type.
 Type pointerize(Type type)
          Pointerize the specified type.
 Type promote(Type type)
          Integer promote the specified type.
 Type promoteArgument(Type type)
          Argument promote this type.
 Type qualify(Type type, Type template)
          Qualify the specified type with the qualifiers of the specified template.
 Type reattribute(Type type, Type template)
          Reattribute the specified type with the specified template's GCC attributes.
 java.lang.String toDesignation(Type type)
          Get the specified type's designation.
 Type toRValue(Type type)
          Convert the specified type to an rvalue.
 Type typeCharacter(java.lang.String literal)
          Type the specified C character literal.
 Type typeFloat(java.lang.String literal)
          Type the specified floating point literal.
 Type typeInteger(java.lang.String literal)
          Type the specified integer literal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GCC_LEGACY_LAYOUT

public static final boolean GCC_LEGACY_LAYOUT
The flag for GCC's legacy structure layout.


IMPLICIT

public static final IntegerT IMPLICIT
The canonical implicit int type. In K&R C, a missing type specifier is treated as an int. We preserve knowledge about this lack of type specifier through this type, which has a Constants.ATT_IMPLICIT attribute.


KIND_SIZEOF

protected static final NumberT.Kind KIND_SIZEOF
The integer kind of the sizeof type.


SIZEOF

public static final IntegerT SIZEOF
The canonical sizeof type.


KIND_PTR_DIFF

protected static final NumberT.Kind KIND_PTR_DIFF
The integer kind of the pointer difference type.


PTR_DIFF

public static final IntegerT PTR_DIFF
The canonical pointer difference type.


KIND_WCHAR

protected static final NumberT.Kind KIND_WCHAR
The number kind of the wchar_t type.


WCHAR

public static final IntegerT WCHAR
The canonical wide char type.


FACTOR_CHAR

protected final java.math.BigInteger FACTOR_CHAR
The factor for chars.


FACTOR_WIDE

protected final java.math.BigInteger FACTOR_WIDE
The factor for wide chars.

Constructor Detail

C

public C()
Create a new instance.

Method Detail

isChar

public boolean isChar(Type type)
Determine whether the specified type is a char.

Parameters:
type - The type.
Returns:
true if the type is a char.

isWideChar

public boolean isWideChar(Type type)
Determine whether the specified type is a wide char.

Parameters:
type - The type.
Returns:
true if the type is a wide char.

isString

public boolean isString(Type type)
Determine whether the specified type is a string.

Parameters:
type - The type.
Returns:
true if the type is a string.

isWideString

public boolean isWideString(Type type)
Determine whether the specified type is a wide string.

Parameters:
type - The type.
Returns:
true if the type is a wide string.

isStringLiteral

public boolean isStringLiteral(Type type)
Determine whether the specified type is a string literal.

Parameters:
type - The type.
Returns:
true if the type is a string literal.

isIntegral

public boolean isIntegral(Type type)
Determine whether the specified type is integral.

Parameters:
type - The type.
Returns:
true if the specified type is integral.

isReal

public boolean isReal(Type type)
Determine whether the specified type is real.

Parameters:
type - The type.
Returns:
true if the specified type is real.

isArithmetic

public boolean isArithmetic(Type type)
Determine whether the specified type is arithmetic.

Parameters:
type - The type.
Returns:
true if the specified type is arithmetic.

isScalar

public boolean isScalar(Type type)
Determine whether the specified type is scalar.

Parameters:
type - The type.
Returns:
true if the specified type is scalar.

isIncomplete

public boolean isIncomplete(Type type)
Determine whether the specified type is incomplete.

Per C99 6.2.5, a type is incomplete if it does not contain sufficient information for determining its size. However, per C99 6.7.2.1, a struct type with more than one named member may have an incomplete type as its last member, while still being considered complete.

Parameters:
type - The type.
Returns:
true if the type is incomplete.

hasTrailingArray

public boolean hasTrailingArray(Type type)
Determine whether the specified type has a trailing array. This method checks whether the specified type is either a struct type with an incomplete array as its last member or a union containing such a member.

Parameters:
type - The type.
Returns:
true if the type has a trailing array.

isVariablyModified

public boolean isVariablyModified(Type type)
Determine whether the specified type is variably modified. Consistent with C99 6.7.5-3, this method checks whether the type contains a variable length array type.

Parameters:
type - The type.
Returns:
true if the type is variably modified.

isConstant

public boolean isConstant(Type type)
Determine whether the specified type is qualified as constant. Consistent with C99 6.3.2.1, this method checks whether the type has a Constants.ATT_CONSTANT attribute and, if it is an array, struct, or union, whether any member or element of the type has that attribute.

Parameters:
type - The type.
Returns:
true if the specified type is constant qualified.

isModifiable

public boolean isModifiable(Type type)
Determine whether the specified type represents a modifiable lvalue. Consistent with C99 6.3.2.1, this method checks that the type represents an lvalue, is not incomplete, and is not qualified as constant. For struct and union types, this method also checks that no member is qualified as constant.

Parameters:
type - The type.
Returns:
true if the type represents a modifiable lvalue.

hasConstRef

public boolean hasConstRef(Type type)
Determine whether this type has a constant reference. This method takes the pointer decay of arrays and functions into account and returns true either if the specified type has a constant reference or if this type is an array or function and has a constant reference.

Parameters:
type - The type.
Returns:
true if the type has a constant reference.

getConstRef

public Reference getConstRef(Type type)
Get the specified type's constant reference.

Parameters:
type - The type.
Returns:
The constant reference.
Throws:
java.lang.IllegalArgumentException - Signals that the type does not have a constant reference.

hasQualifiers

public boolean hasQualifiers(Type type)
Determine whether the specified type has any qualifiers.

Parameters:
type - The type.
Returns:
true if the type has any qualifiers.

hasQualifiers

public boolean hasQualifiers(Type type,
                             Type template)
Determine whether the specified type has at least the qualifiers of the specified template.

Parameters:
type - The type.
template - The template.
Returns:
true if the type has at least the template's qualifiers.

hasSameQualifiers

public boolean hasSameQualifiers(Type type,
                                 Type template)
Determine whether the specified type has the same qualifiers as the specified template.

Parameters:
type - The type.
template - The template.
Returns:
true if the type has the same qualifiers as the template.

qualify

public Type qualify(Type type,
                    Type template)
Qualify the specified type with the qualifiers of the specified template. If the template has any qualifiers, this method first annotates the specified type first.

Parameters:
type - The type.
template - The template.
Returns:
The qualified type.

reattribute

public Type reattribute(Type type,
                        Type template)
Reattribute the specified type with the specified template's GCC attributes.

Parameters:
type - The type.
template - The template.
Returns:
The reattributed type.

toRValue

public Type toRValue(Type type)
Convert the specified type to an rvalue. If the specified type has a shape, this method returns the resolved type, annotated with any qualifiers and constant value. Otherwise, it returns the specified type.

Parameters:
type - The type.
Returns:
The type as an rvalue.

hasThreadLocals

public boolean hasThreadLocals()
Determine whether the current target supports thread-local storage.

Returns:
true if the current target supports thread-local storage.

getAlignment

public long getAlignment(Type type)
Get the specified type's alignment in bytes.

Parameters:
type - The type.
Returns:
The type's alignment.
Throws:
java.lang.IllegalArgumentException - Signals that the type does not have a static alignment.

getAlignment

public long getAlignment(Type type,
                         boolean natural)
Get the specified type's alignment in bytes. A type's natural alignment is the type's alignment outside arrays, structures, and unions.

Parameters:
type - The type.
natural - The flag for determining the natural alignment.
Returns:
The type's alignment.
Throws:
java.lang.IllegalArgumentException - Signals that the type does not have a static alignment.

getSize

public long getSize(Type type)
Get the specified type's size in bytes.

Parameters:
type - The type.
Returns:
The type's size.
Throws:
java.lang.IllegalArgumentException - Signals that the type does not have a static size.

getOffset

public long getOffset(StructOrUnionT type,
                      java.lang.String name)
Get the specified member's offset for the specified structure or union.

Parameters:
type - The structure or union.
name - The member's name.
Returns:
The offset or -1 if the structure or union does not have a member with the specified name or the member is a bit-field.

layout

public long layout(StructT type,
                   java.lang.String name)
Lay out the specified structure.

Parameters:
type - The structure.
name - The member name or null for the entire structure.
Returns:
The offset/size or -1 if the structure does not have a member with the specified name or the member is a bit-field.

isPacked

protected boolean isPacked(Type type)
Determine whether the specified type has a GCC packed attribute.

Parameters:
type - The type.
Returns:
true if the type has a packed attribute.

getAligned

protected long getAligned(Type type)
Get the specified type's alignment. If the specified type has a GCC aligned attribute, this method returns the corresponding alignment. Otherwise, it returns -1.

Parameters:
type - The type.
Returns:
The alignment or -1 if the type does not have an aligned attribute.

getWidth

public long getWidth(Type number)
Get the specified number's size in bits.

Parameters:
number - The number.
Returns:
The size in bits.

fit

public Type fit(java.math.BigInteger number)
Fit the specified number to the closest integer type, starting with int.

Parameters:
number - The number.
Returns:
The closest containing integer type or ErrorT.TYPE if the number is too large.

fits

public boolean fits(java.math.BigInteger number,
                    Type type)
Determine whether the specified number fits the specified integer type.

Parameters:
number - The number.
type - The integer type.
Returns:
true if the number fits the type.

mask

public java.math.BigInteger mask(java.math.BigInteger number,
                                 Type type)
Mask the specified number as a value of this integer type.

Parameters:
number - The number.
type - The type.
Returns:
The number masked as a value of this type.

toDesignation

public java.lang.String toDesignation(Type type)
Get the specified type's designation.

Parameters:
type - The type.
Returns:
The designation.

promote

public Type promote(Type type)
Integer promote the specified type. This method resolves the type and, if the type is integral, then performs C's integer promotion (C99 6.3.1.1). Additionally, it normalizes implicit and signed int types to int types.

Parameters:
type - The type.
Returns:
The integer promoted type.

promoteArgument

public Type promoteArgument(Type type)
Argument promote this type. This method resolves the type and, if the type is an integral type or a float, then performs C's default argument promotions (C99 6.5.2.2).

Parameters:
type - The type.
Returns:
The argument promoted type.

pointerize

public Type pointerize(Type type)
Pointerize the specified type. This method resolves the type and, if the type is an array or function, then performs C's pointer decay (C99 6.3.2.1).

Parameters:
type - The type.
Returns:
The pointerized type.

convert

public Type convert(Type t1,
                    Type t2)
Perform the usual arithmetic conversions. Per C99 6.3.1.8, this method performs the usual arithmetic conversions for the specified two types and returns the type of the corresponding result.

Parameters:
t1 - The first type.
t2 - The second type.
Returns:
The converted type.
Throws:
java.lang.IllegalArgumentException - Signals that either type is not arithmetic.

compose

public Type compose(Type t1,
                    Type t2,
                    boolean pedantic)
Compose the specified types. This method determines whether the two types are compatible while also constructing a composite type as specified in C99 6.2.7. If the types are compatible, the resulting type is wrapped exactly as the first type. If the types are not compatible, the resulting type is ErrorT.TYPE.

Note that if both types are derived types, this method ensures that any referenced types have the same qualifiers. However, it does not ensure that the two types have the same qualifiers. As a result, two types t1 and t2 are compatible if:

 C.hasSameQualfiers(t1, t2) && (! C.compose(t1, t2).isError())
 

Further note that the composed type does not preserve any annotations or wraped types and thus needs to be annotated with the two type's qualifiers etc.

Parameters:
t1 - The first type.
t2 - The second type.
pedantic - The flag for pedantic composition.
Returns:
The composed type.
See Also:
equal(Type,Type)

compose

protected Type compose(Type t1,
                       Type t2,
                       boolean pedantic,
                       boolean recursive)
Compose the specified types.

Parameters:
t1 - The first type.
t2 - The second type.
pedantic - The flag for pedantic composition.
recursive - The flag for recursive invocations.
Returns:
The composed type.

composeArrays

protected Type composeArrays(ArrayT a1,
                             ArrayT a2)
Compose the specified array types (C99 6.2.7).

Parameters:
a1 - The first array.
a2 - The second array.
Returns:
The composed type.

composeFunctions

protected Type composeFunctions(FunctionT f1,
                                FunctionT f2,
                                boolean pedantic)
Compose the specified function types (C99 6.2.7). Note that this method ignores any exceptions, which are not part of the C language anyway.

Parameters:
f1 - The first function.
f2 - The second function.
pedantic - The flag for pedantic composition.
Returns:
The composed type.

equal

public boolean equal(Type t1,
                     Type t2)
Determine whether the specified types are equal to each other. Calling this method on types t1 and t2 is equivalent to:
 C.hasSameQualifiers(t1, t2) && (! C.compose(t1, t2).isError())
 

Parameters:
t1 - The first type.
t2 - The second type.
Returns:
true if the types are equal.

typeCharacter

public Type typeCharacter(java.lang.String literal)
Type the specified C character literal. This method determines the type for the specified character literal, which may be a wide character literal, and returns that type. The type is annotated with the literal's constant value, even if the value does not fit the type.

Parameters:
literal - The literal.
Returns:
The corresponding constant valued type.

typeInteger

public Type typeInteger(java.lang.String literal)
Type the specified integer literal. This method returns the type for the specified C integer literal wrapped in the literal's constant value. If the specified literal does not fit any type, this method returns the largest appropriate type.

Parameters:
literal - The literal.
Returns:
The corresponding type and constant value.

typeFloat

public Type typeFloat(java.lang.String literal)
Type the specified floating point literal. This method returns the type for the specified C floating point literal wrapped in the literal's constant value.

Parameters:
literal - The literal.
Returns:
The corresponding type.


Copyright © 2012. All Rights Reserved.