Made the code more constness-correct with 'Type' usage.

This commit is contained in:
acqn 2021-04-05 16:40:32 +08:00 committed by Oliver Schmidt
parent cb64aaf20c
commit 9cea9ce5e2
15 changed files with 130 additions and 91 deletions

View File

@ -136,7 +136,7 @@ static int CopyStruct (ExprDesc* LExpr, ExprDesc* RExpr)
void Assignment (ExprDesc* Expr)
/* Parse an assignment */
{
Type* ltype = Expr->Type;
const Type* ltype = Expr->Type;
ExprDesc Expr2;
ED_Init (&Expr2);

View File

@ -59,18 +59,18 @@
/* Predefined type strings */
Type type_char[] = { TYPE(T_CHAR), TYPE(T_END) };
Type type_schar[] = { TYPE(T_SCHAR), TYPE(T_END) };
Type type_uchar[] = { TYPE(T_UCHAR), TYPE(T_END) };
Type type_int[] = { TYPE(T_INT), TYPE(T_END) };
Type type_uint[] = { TYPE(T_UINT), TYPE(T_END) };
Type type_long[] = { TYPE(T_LONG), TYPE(T_END) };
Type type_ulong[] = { TYPE(T_ULONG), TYPE(T_END) };
Type type_bool[] = { TYPE(T_INT), TYPE(T_END) };
Type type_void[] = { TYPE(T_VOID), TYPE(T_END) };
Type type_size_t[] = { TYPE(T_SIZE_T), TYPE(T_END) };
Type type_float[] = { TYPE(T_FLOAT), TYPE(T_END) };
Type type_double[] = { TYPE(T_DOUBLE), TYPE(T_END) };
const Type type_char[] = { TYPE(T_CHAR), TYPE(T_END) };
const Type type_schar[] = { TYPE(T_SCHAR), TYPE(T_END) };
const Type type_uchar[] = { TYPE(T_UCHAR), TYPE(T_END) };
const Type type_int[] = { TYPE(T_INT), TYPE(T_END) };
const Type type_uint[] = { TYPE(T_UINT), TYPE(T_END) };
const Type type_long[] = { TYPE(T_LONG), TYPE(T_END) };
const Type type_ulong[] = { TYPE(T_ULONG), TYPE(T_END) };
const Type type_bool[] = { TYPE(T_INT), TYPE(T_END) };
const Type type_void[] = { TYPE(T_VOID), TYPE(T_END) };
const Type type_size_t[] = { TYPE(T_SIZE_T), TYPE(T_END) };
const Type type_float[] = { TYPE(T_FLOAT), TYPE(T_END) };
const Type type_double[] = { TYPE(T_DOUBLE), TYPE(T_END) };
@ -559,7 +559,8 @@ static unsigned TypeOfBySize (const Type* Type)
}
Type* PointerTo (const Type* T)
Type* NewPointerTo (const Type* T)
/* Return a type string that is "pointer to T". The type string is allocated
** on the heap and may be freed after use.
*/
@ -590,7 +591,7 @@ void PrintType (FILE* F, const Type* T)
void PrintFuncSig (FILE* F, const char* Name, Type* T)
void PrintFuncSig (FILE* F, const char* Name, const Type* T)
/* Print a function signature */
{
StrBuf Buf = AUTO_STRBUF_INITIALIZER;
@ -713,7 +714,6 @@ TypeCode GetUnderlyingTypeCode (const Type* Type)
return IS_Get (&SignedChars) ? T_SCHAR : T_UCHAR;
} else if (IsTypeEnum (Type)) {
/* This should not happen, but just in case */
if (Type->A.S == 0) {
Internal ("Enum tag type error in GetUnderlyingTypeCode");
@ -934,7 +934,7 @@ unsigned FuncTypeOf (const Type* T)
Type* Indirect (Type* T)
const Type* Indirect (const Type* T)
/* Do one indirection for the given type, that is, return the type where the
** given type points to.
*/
@ -948,11 +948,25 @@ Type* Indirect (Type* T)
Type* ArrayToPtr (Type* T)
Type* IndirectModifiable (Type* T)
/* Do one indirection for the given type, that is, return the type where the
** given type points to.
*/
{
/* We are expecting a pointer expression */
CHECK (IsClassPtr (T));
/* Skip the pointer or array token itself */
return T + 1;
}
Type* ArrayToPtr (const Type* T)
/* Convert an array to a pointer to it's first element */
{
/* Return pointer to first element */
return PointerTo (GetElementType (T));
return NewPointerTo (GetElementType (T));
}
@ -1129,7 +1143,7 @@ void SetFuncDesc (Type* T, FuncDesc* F)
Type* GetFuncReturn (Type* T)
const Type* GetFuncReturn (const Type* T)
/* Return a pointer to the return type of a function or pointer-to-function type */
{
if (UnqualifiedType (T->C) == T_PTR) {
@ -1146,10 +1160,27 @@ Type* GetFuncReturn (Type* T)
FuncDesc* GetFuncDefinitionDesc (Type* T)
Type* GetFuncReturnModifiable (Type* T)
/* Return a non-const pointer to the return type of a function or pointer-to-function type */
{
if (UnqualifiedType (T->C) == T_PTR) {
/* Pointer to function */
++T;
}
/* Be sure it's a function type */
CHECK (IsClassFunc (T));
/* Return a pointer to the return type */
return T + 1;
}
const FuncDesc* GetFuncDefinitionDesc (const Type* T)
/* Get the function descriptor of the function definition */
{
FuncDesc* D;
const FuncDesc* D;
/* Be sure it's a function type */
CHECK (IsClassFunc (T));
@ -1182,7 +1213,7 @@ void SetElementCount (Type* T, long Count)
Type* GetElementType (Type* T)
const Type* GetElementType (const Type* T)
/* Return the element type of the given array type. */
{
CHECK (IsTypeArray (T));
@ -1191,7 +1222,7 @@ Type* GetElementType (Type* T)
Type* GetBaseElementType (Type* T)
const Type* GetBaseElementType (const Type* T)
/* Return the base element type of a given type. If T is not an array, this
** will return. Otherwise it will return the base element type, which means
** the element type that is not an array.
@ -1229,7 +1260,7 @@ void SetESUSymEntry (Type* T, SymEntry* S)
Type* IntPromotion (Type* T)
const Type* IntPromotion (const Type* T)
/* Apply the integer promotions to T and return the result. The returned type
** string may be T if there is no need to change it.
*/
@ -1268,14 +1299,14 @@ Type* IntPromotion (Type* T)
Type* PtrConversion (Type* T)
const Type* PtrConversion (const Type* T)
/* If the type is a function, convert it to pointer to function. If the
** expression is an array, convert it to pointer to first element. Otherwise
** return T.
*/
{
if (IsTypeFunc (T)) {
return PointerTo (T);
return NewPointerTo (T);
} else if (IsTypeArray (T)) {
return ArrayToPtr (T);
} else {

View File

@ -198,18 +198,18 @@ struct Type {
#define PTR_BITS (8 * SIZEOF_PTR)
/* Predefined type strings */
extern Type type_char[];
extern Type type_schar[];
extern Type type_uchar[];
extern Type type_int[];
extern Type type_uint[];
extern Type type_long[];
extern Type type_ulong[];
extern Type type_bool[];
extern Type type_void[];
extern Type type_size_t[];
extern Type type_float[];
extern Type type_double[];
extern const Type type_char[];
extern const Type type_schar[];
extern const Type type_uchar[];
extern const Type type_int[];
extern const Type type_uint[];
extern const Type type_long[];
extern const Type type_ulong[];
extern const Type type_bool[];
extern const Type type_void[];
extern const Type type_size_t[];
extern const Type type_float[];
extern const Type type_double[];
/* Forward for the SymEntry struct */
struct SymEntry;
@ -280,7 +280,7 @@ unsigned long GetIntegerTypeMax (const Type* Type);
** The type must have a known size.
*/
Type* PointerTo (const Type* T);
Type* NewPointerTo (const Type* T);
/* Return a type string that is "pointer to T". The type string is allocated
** on the heap and may be freed after use.
*/
@ -288,7 +288,7 @@ Type* PointerTo (const Type* T);
void PrintType (FILE* F, const Type* T);
/* Print fulle name of the type */
void PrintFuncSig (FILE* F, const char* Name, Type* T);
void PrintFuncSig (FILE* F, const char* Name, const Type* T);
/* Print a function signature */
void PrintRawType (FILE* F, const Type* T);
@ -348,12 +348,17 @@ unsigned TypeOf (const Type* T);
unsigned FuncTypeOf (const Type* T);
/* Get the code generator flag for calling the function */
Type* Indirect (Type* T);
const Type* Indirect (const Type* T);
/* Do one indirection for the given type, that is, return the type where the
** given type points to.
*/
Type* ArrayToPtr (Type* T);
Type* IndirectModifiable (Type* T);
/* Do one indirection for the given type, that is, return the type where the
** given type points to.
*/
Type* ArrayToPtr (const Type* T);
/* Convert an array to a pointer to it's first element */
#if defined(HAVE_INLINE)
@ -845,10 +850,13 @@ FuncDesc* GetFuncDesc (const Type* T) attribute ((const));
void SetFuncDesc (Type* T, FuncDesc* F);
/* Set the FuncDesc pointer in a function or pointer-to-function type */
Type* GetFuncReturn (Type* T) attribute ((const));
const Type* GetFuncReturn (const Type* T) attribute ((const));
/* Return a pointer to the return type of a function or pointer-to-function type */
FuncDesc* GetFuncDefinitionDesc (struct Type* T);
Type* GetFuncReturnModifiable (Type* T) attribute ((const));
/* Return a non-const pointer to the return type of a function or pointer-to-function type */
const FuncDesc* GetFuncDefinitionDesc (const Type* T) attribute ((const));
/* Get the function descriptor of the function definition */
long GetElementCount (const Type* T);
@ -861,10 +869,10 @@ void SetElementCount (Type* T, long Count);
** array type).
*/
Type* GetElementType (Type* T);
const Type* GetElementType (const Type* T);
/* Return the element type of the given array type. */
Type* GetBaseElementType (Type* T);
const Type* GetBaseElementType (const Type* T);
/* Return the base element type of a given type. If T is not an array, this
** will return. Otherwise it will return the base element type, which means
** the element type that is not an array.
@ -876,12 +884,12 @@ struct SymEntry* GetESUSymEntry (const Type* T) attribute ((const));
void SetESUSymEntry (Type* T, struct SymEntry* S);
/* Set the SymEntry pointer for an enum/struct/union type */
Type* IntPromotion (Type* T);
const Type* IntPromotion (const Type* T);
/* Apply the integer promotions to T and return the result. The returned type
** string may be T if there is no need to change it.
*/
Type* PtrConversion (Type* T);
const Type* PtrConversion (const Type* T);
/* If the type is a function, convert it to pointer to function. If the
** expression is an array, convert it to pointer to first element. Otherwise
** return T.

View File

@ -1510,7 +1510,7 @@ static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers,
static Type* ParamTypeCvt (Type* T)
static const Type* ParamTypeCvt (Type* T)
/* If T is an array or a function, convert it to a pointer else do nothing.
** Return the resulting type.
*/
@ -1520,7 +1520,7 @@ static Type* ParamTypeCvt (Type* T)
if (IsTypeArray (T)) {
Tmp = ArrayToPtr (T);
} else if (IsTypeFunc (T)) {
Tmp = PointerTo (T);
Tmp = NewPointerTo (T);
}
if (Tmp != 0) {
@ -2017,7 +2017,7 @@ void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) {
/* A function. Check the return type */
Type* RetType = GetFuncReturn (D->Type);
Type* RetType = GetFuncReturnModifiable (D->Type);
/* Functions may not return functions or arrays */
if (IsTypeFunc (RetType)) {
@ -2343,7 +2343,7 @@ static unsigned ParseArrayInit (Type* T, int* Braces, int AllowFlexibleMembers)
int HasCurly = 0;
/* Get the array data */
Type* ElementType = GetElementType (T);
Type* ElementType = IndirectModifiable (T);
unsigned ElementSize = SizeOf (ElementType);
long ElementCount = GetElementCount (T);

View File

@ -150,7 +150,7 @@ void MarkedExprWithCheck (void (*Func) (ExprDesc*), ExprDesc* Expr)
static Type* ArithmeticConvert (Type* lhst, Type* rhst)
static const Type* ArithmeticConvert (const Type* lhst, const Type* rhst)
/* Perform the usual arithmetic conversions for binary operators. */
{
/* https://port70.net/~nsz/c/c89/c89-draft.html#3.2.1.5
@ -209,7 +209,7 @@ static Type* ArithmeticConvert (Type* lhst, Type* rhst)
static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
static unsigned typeadjust (ExprDesc* lhs, const ExprDesc* rhs, int NoPush)
/* Adjust the two values for a binary operation. lhs is expected on stack or
** to be constant, rhs is expected to be in the primary register or constant.
** The function will put the type of the result into lhs and return the
@ -223,8 +223,8 @@ static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush)
unsigned flags;
/* Get the type strings */
Type* lhst = lhs->Type;
Type* rhst = rhs->Type;
const Type* lhst = lhs->Type;
const Type* rhst = rhs->Type;
/* Generate type adjustment code if needed */
ltype = TypeOf (lhst);
@ -865,7 +865,7 @@ static void FunctionCall (ExprDesc* Expr)
int PtrOffs = 0; /* Offset of function pointer on stack */
int IsFastcall = 0; /* True if we are fast-calling the function */
int PtrOnStack = 0; /* True if a pointer copy is on stack */
Type* ReturnType;
const Type* ReturnType;
/* Skip the left paren */
NextToken ();
@ -1121,7 +1121,7 @@ static void Primary (ExprDesc* E)
/* output its label */
E->Flags = E_RTYPE_RVAL | E_LOC_CODE | E_ADDRESS_OF;
E->Name = Entry->V.L.Label;
E->Type = PointerTo (type_void);
E->Type = NewPointerTo (type_void);
NextToken ();
} else {
Error ("Computed gotos are a C extension, not supported with this --standard");
@ -2052,7 +2052,7 @@ void hie10 (ExprDesc* Expr)
/* The & operator yields an rvalue address */
ED_AddrExpr (Expr);
}
Expr->Type = PointerTo (Expr->Type);
Expr->Type = NewPointerTo (Expr->Type);
break;
case TOK_SIZEOF:
@ -2380,7 +2380,7 @@ static void hie_compare (const GenDesc* Ops, /* List of generators */
/* If lhs is a function, convert it to pointer to function */
if (IsTypeFunc (Expr->Type)) {
Expr->Type = PointerTo (Expr->Type);
Expr->Type = NewPointerTo (Expr->Type);
}
/* Get the lhs on stack */
@ -2402,7 +2402,7 @@ static void hie_compare (const GenDesc* Ops, /* List of generators */
/* If rhs is a function, convert it to pointer to function */
if (IsTypeFunc (Expr2.Type)) {
Expr2.Type = PointerTo (Expr2.Type);
Expr2.Type = NewPointerTo (Expr2.Type);
}
/* Check for a numeric constant expression */
@ -2792,8 +2792,8 @@ static void parseadd (ExprDesc* Expr, int DoArrayRef)
ExprDesc Expr2;
unsigned flags; /* Operation flags */
CodeMark Mark; /* Remember code position */
Type* lhst; /* Type of left hand side */
Type* rhst; /* Type of right hand side */
const Type* lhst; /* Type of left hand side */
const Type* rhst; /* Type of right hand side */
int lscale;
int rscale;
int AddDone; /* No need to generate runtime code */
@ -3189,8 +3189,8 @@ static void parsesub (ExprDesc* Expr)
{
ExprDesc Expr2;
unsigned flags; /* Operation flags */
Type* lhst; /* Type of left hand side */
Type* rhst; /* Type of right hand side */
const Type* lhst; /* Type of left hand side */
const Type* rhst; /* Type of right hand side */
CodeMark Mark1; /* Save position of output queue */
CodeMark Mark2; /* Another position in the queue */
int rscale; /* Scale factor for pointer arithmetics */
@ -4102,10 +4102,10 @@ static void hieQuest (ExprDesc* Expr)
** appropriately qualified void.
*/
if (IsTypeVoid (Indirect (Expr2.Type))) {
ResultType = PointerTo (Indirect (Expr2.Type));
ResultType = NewPointerTo (Indirect (Expr2.Type));
ResultType[1].C |= GetQualifier (Indirect (Expr3.Type));
} else if (IsTypeVoid (Indirect (Expr3.Type))) {
ResultType = PointerTo (Indirect (Expr3.Type));
ResultType = NewPointerTo (Indirect (Expr3.Type));
ResultType[1].C |= GetQualifier (Indirect (Expr2.Type));
} else {
/* Must point to compatible types */
@ -4113,7 +4113,7 @@ static void hieQuest (ExprDesc* Expr)
TypeCompatibilityDiagnostic (Expr2.Type, Expr3.Type,
1, "Incompatible pointer types in ternary: '%s' and '%s'");
/* Avoid further errors */
ResultType = PointerTo (type_void);
ResultType = NewPointerTo (type_void);
} else {
/* Result has the composite type */
ResultType = TypeDup (Expr2.Type);

View File

@ -228,7 +228,7 @@ int ED_GetStackOffs (const ExprDesc* Expr, int Offs)
ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type)
ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, const Type* Type)
/* Replace Expr with an absolute const with the given value and type */
{
Expr->Sym = 0;
@ -595,10 +595,10 @@ void PrintExprDesc (FILE* F, ExprDesc* E)
Type* ReplaceType (ExprDesc* Expr, const Type* NewType)
const Type* ReplaceType (ExprDesc* Expr, const Type* NewType)
/* Replace the type of Expr by a copy of Newtype and return the old type string */
{
Type* OldType = Expr->Type;
const Type* OldType = Expr->Type;
Expr->Type = TypeDup (NewType);
return OldType;
}

View File

@ -199,7 +199,7 @@ struct Literal;
typedef struct ExprDesc ExprDesc;
struct ExprDesc {
struct SymEntry* Sym; /* Symbol table entry if known */
Type* Type; /* Type array of expression */
const Type* Type; /* Type array of expression */
unsigned Flags;
uintptr_t Name; /* Name pointer or label number */
long IVal; /* Integer value if expression constant */
@ -544,7 +544,7 @@ int ED_GetStackOffs (const ExprDesc* Expr, int Offs);
** an additional offset in Offs.
*/
ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, Type* Type);
ExprDesc* ED_MakeConstAbs (ExprDesc* Expr, long Value, const Type* Type);
/* Replace Expr with an absolute const with the given value and type */
ExprDesc* ED_MakeConstAbsInt (ExprDesc* Expr, long Value);
@ -685,7 +685,7 @@ int ED_IsBool (const ExprDesc* Expr);
void PrintExprDesc (FILE* F, ExprDesc* Expr);
/* Print an ExprDesc */
Type* ReplaceType (ExprDesc* Expr, const Type* NewType);
const Type* ReplaceType (ExprDesc* Expr, const Type* NewType);
/* Replace the type of Expr by a copy of Newtype and return the old type string */

View File

@ -191,7 +191,7 @@ unsigned F_GetParamSize (const Function* F)
Type* F_GetReturnType (Function* F)
const Type* F_GetReturnType (Function* F)
/* Get the return type for the function */
{
return F->ReturnType;

View File

@ -54,7 +54,7 @@ typedef enum {
/* Structure that holds all data needed for function activation */
struct Function {
struct SymEntry* FuncEntry; /* Symbol table entry */
Type* ReturnType; /* Function return type */
const Type* ReturnType; /* Function return type */
FuncDesc* Desc; /* Function descriptor */
int Reserved; /* Reserved local space */
unsigned RetLab; /* Return code label */
@ -96,7 +96,7 @@ unsigned F_GetParamCount (const Function* F);
unsigned F_GetParamSize (const Function* F);
/* Return the parameter size for the current function */
Type* F_GetReturnType (Function* F);
const Type* F_GetReturnType (Function* F);
/* Get the return type for the function */
int F_HasVoidReturn (const Function* F);

View File

@ -205,7 +205,7 @@ struct Token {
struct Literal* SVal; /* String literal is any */
ident Ident; /* Identifier if IDENT */
LineInfo* LI; /* Source line where the token comes from */
Type* Type; /* Type if integer or float constant */
const Type* Type; /* Type if integer or float constant */
};
extern Token CurTok; /* The current token */

View File

@ -64,8 +64,8 @@ void ShiftExpr (struct ExprDesc* Expr)
CodeMark Mark1;
CodeMark Mark2;
token_t Tok; /* The operator token */
Type* EffType; /* Effective lhs type */
Type* ResultType; /* Type of the result */
const Type* EffType; /* Effective lhs type */
const Type* ResultType; /* Type of the result */
unsigned ExprBits; /* Bits of the lhs operand */
unsigned GenFlags; /* Generator flags */
unsigned ltype;
@ -193,7 +193,7 @@ void ShiftExpr (struct ExprDesc* Expr)
ED_IsLocQuasiConst (Expr) &&
Expr2.IVal >= 8) {
Type* OldType;
const Type* OldType;
/* Increase the address by one and decrease the shift count */
++Expr->IVal;

View File

@ -311,7 +311,7 @@ const char* GetSymTypeName (const Type* T)
void ChangeSymType (SymEntry* Entry, Type* T)
void ChangeSymType (SymEntry* Entry, const Type* T)
/* Change the type of the given symbol */
{
TypeFree (Entry->Type);

View File

@ -325,7 +325,7 @@ const char* GetSymTypeName (const Type* T);
** Note: This may use a static buffer that could be overwritten by other calls.
*/
void ChangeSymType (SymEntry* Entry, Type* T);
void ChangeSymType (SymEntry* Entry, const Type* T);
/* Change the type of the given symbol */
void ChangeAsmName (SymEntry* Entry, const char* NewAsmName);

View File

@ -67,8 +67,8 @@ static int EqualFuncParams (const FuncDesc* F1, const FuncDesc* F2)
while (Sym1 && (Sym1->Flags & SC_PARAM) && Sym2 && (Sym2->Flags & SC_PARAM)) {
/* Get the symbol types */
Type* Type1 = Sym1->Type;
Type* Type2 = Sym2->Type;
const Type* Type1 = Sym1->Type;
const Type* Type2 = Sym2->Type;
/* If either of both functions is old style, apply the default
** promotions to the parameter type.

View File

@ -58,9 +58,9 @@
static void DoConversion (ExprDesc* Expr, const Type* NewType)
/* Emit code to convert the given expression to a new type. */
{
Type* OldType;
unsigned OldBits;
unsigned NewBits;
const Type* OldType;
unsigned OldBits;
unsigned NewBits;
/* Remember the old type */
@ -371,8 +371,8 @@ static void ComposeFuncParamList (const FuncDesc* F1, const FuncDesc* F2)
while (Sym1 && (Sym1->Flags & SC_PARAM) && Sym2 && (Sym2->Flags & SC_PARAM)) {
/* Get the symbol types */
Type* Type1 = Sym1->Type;
Type* Type2 = Sym2->Type;
const Type* Type1 = Sym1->Type;
const Type* Type2 = Sym2->Type;
/* If either of both functions is old style, apply the default
** promotions to the parameter type.