/*
Компилятор языка программирования
Объектно-ориентированный продвинутый векторный транслятор
Copyright © 2021, 2024 Малик Разработчик
Это свободная программа: вы можете перераспространять ее и/или изменять
ее на условиях Стандартной общественной лицензии GNU в том виде,
в каком она была опубликована Фондом свободного программного обеспечения;
либо версии 3 лицензии, либо (по вашему выбору) любой более поздней версии.
Эта программа распространяется в надежде, что она будет полезной,
но БЕЗО ВСЯКИХ ГАРАНТИЙ; даже без неявной гарантии ТОВАРНОГО ВИДА
или ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Подробнее см. в Стандартной
общественной лицензии GNU.
Вы должны были получить копию Стандартной общественной лицензии GNU
вместе с этой программой. Если это не так, см.
<https://www.gnu.org/licenses/>.
*/
package ru.malik.elaborarer.avtoo.lang;
import avt.lang.array.*;
public class Field(AllocatableMember, Cloneable, Measureable, ObjectArray, TypedItem, TypedMember, ValuedItem, Fieldoid, AVTOOConstants)
{
public static final int NOT_COMPUTED = -1;
public static final int COMPUTING = 0;
public static final int COMPUTED = 1;
private int fldState;
private Constant fldValue;
private String fldFasmStructName;
private final boolean fldHasStructOffset;
private final int fldCapacity;
private final Type fldType;
public (AllocatableFactory parentItem, Type type, int capacity, String specialSimpleName, int declarationPosition, int documentationPosition):
super(parentItem, PUBLIC, specialSimpleName, declarationPosition, documentationPosition) {
fldState = COMPUTED;
fldCapacity = capacity;
fldType = type;
}
public (ClassType parentType, int structOffset, Type type, int capacity, String specialSimpleName, int declarationPosition, int documentationPosition):
super(parentType, PUBLIC, specialSimpleName, declarationPosition, documentationPosition) {
super.computeStructOffset(structOffset);
fldState = COMPUTED;
fldHasStructOffset = true;
fldCapacity = capacity;
fldType = type;
}
public (ClassType parentType, int attributes, Type type, String specialSimpleName, int declarationPosition, int documentationPosition):
super(parentType, attributes, specialSimpleName, declarationPosition, documentationPosition) {
fldState = NOT_COMPUTED;
fldType = type;
}
public String toString() { return Fieldoid.super.toString(); }
public void openComputing() {
if(fldState != NOT_COMPUTED)
{
throw new IllegalFieldStateException(package.getResourceString("field.not-computed"));
}
fldState = COMPUTING;
}
public void closeComputing() { fldState = COMPUTED; }
public void setComputedValue(Constant value) {
if(fldState != COMPUTING)
{
throw new IllegalFieldStateException(package.getResourceString("field.computing"));
}
fldValue = value;
}
public final boolean isConstant() { return (attributes & (ATTR_STATIC | ATTR_FINAL)) == (ATTR_STATIC | ATTR_FINAL) && fldValue != null; }
public final Type type { read = fldType }
public final Constant value { read = fldValue }
public final int capacity { read = fldCapacity }
public final int state { read = fldState }
public final String fasmStructName { read = fldFasmStructName }
protected void afterConstruction() {
super.afterConstruction();
fldFasmStructName = composeFasmStructName();
}
protected int computeStructOffset(int currentOffset) {
Type type = fldType;
if(type == null)
{
throw new IllegalMemberStateException(package.getResourceString("member.type.not-specified"));
}
if(type instanceof ArrayType)
{
type = ((ArrayType) type).componentType;
}
int capacity = fldCapacity;
return (fldHasStructOffset ? super.structOffset : super.computeStructOffset(currentOffset)) + type.fieldWidth * (capacity <= 0 ? 1 : capacity);
}
protected int computeInstanceOffset(int currentOffset, FieldsMap fieldsMap) {
Type type = fldType;
if(type == null)
{
throw new IllegalMemberStateException(package.getResourceString("member.type.not-specified"));
}
int fieldWidth = type.fieldWidth;
int allocatedOffset = fieldsMap == null ? (
currentOffset + (-currentOffset & (fieldWidth <= 4 ? 0x03 : fieldWidth <= 8 ? 0x07 : 0x0f))
) : fieldWidth <= 4 ? (
fieldsMap.allocateDwordAlignedField(fieldWidth)
) : fieldWidth <= 8 ? (
fieldsMap.allocateQwordAlignedField(fieldWidth)
) : (
fieldsMap.allocateXwordAlignedField(fieldWidth)
);
return super.computeInstanceOffset(allocatedOffset, null) + fieldWidth;
}
protected String composeRecompilableName() {
String name = specialSimpleName;
return name.isEmpty() ? null : name;
}
protected String composeFasmSimpleName() {
String name = specialSimpleName;
return name.isEmpty() ? null : name;
}
protected String composeFasmFullName() { return Fieldoid.super.composeFasmFullName(); }
protected String composeFasmStructName() {
String name = fasmFullName;
return name == null ? null : name + ".struct";
}
}