Press enter to see results or esc to cancel.

Adjusting Expression To Specified Type

This procedure converts expression between compatible types. For example, Integer values can be assigned to Real variables, only the expression and type need to be converted.

Procedure TExpression.AdjustExpressionToType (TypeDef: PTypeDefinition);
Var NewTypeDefinition: PTypeDefinition;
begin
  Case TypeDef^.BaseType of
    btString: begin
                ConvertCharToString;
                CopyCharArrayCompatibleWithStringToStackFrameAsString;
              end;
    btExtended: begin
                  ConvertIntegerToExtended;
                  ConvertRealToExtended;
                end;
    btReal: begin
              ConvertIntegerToReal;
              ConvertExtendedToReal;
            end;
    btBoolean: If not (it32Bytes in TypeDef^.DataType) then ConvertToBooleanByte;
    btObject: begin
                NewTypeDefinition := TypeDef;
                If TypeDefPtr^.BaseType <> btObject then Exit;
                If InObjectTypeDomain (PObjectTypeDefinition (TypeDefPtr), PObjectTypeDefinition (NewTypeDefinition)) then
                  TypeDefPtr := TypeDef;
              end;
    btPointer: begin
                 If ExtendedSyntax in ModuleCompilerSwitches then
                   begin
                     CheckForStringConversionToPChar (TypeDef);
                     CheckForConversionOfZeroBasedCharacterArrayToPChar (TypeDef);
                   end;
                 NewTypeDefinition := PointerFromOffsets (PPointerTypeDefinition (TypeDef)^.PointerBaseTypeOffset);
                 If NewTypeDefinition^.BaseType <> btObject then Exit;
                 If TypeDefPtr^.BaseType <> btPointer then Exit;
                 If InObjectTypeDomain (PObjectTypeDefinition (
                             PointerFromOffsets (PPointerTypeDefinition (TypeDefPtr)^.PointerBaseTypeOffset)),
                             PObjectTypeDefinition (NewTypeDefinition)) then
                   TypeDefPtr := TypeDef;
               end;
  end;
end;

This procedure checks if ExpressionType is compatible with (assignable to) ObjectTypeDefinition.

Function InObjectTypeDomain (ExpressionType, ObjectTypeDefinition: PObjectTypeDefinition): Boolean;
begin
  InObjectTypeDomain := False;
  If ExpressionType^.BaseType <> btObject then Exit;
  Repeat
    If ExpressionType = ObjectTypeDefinition then
      begin
        InObjectTypeDomain := True;
        Exit;
      end;
    If ExpressionType^.AncestorTypeOffset.TypeOffset = 0 then Exit;
    ExpressionType := PObjectTypeDefinition (PointerFromOffsets (ExpressionType^.AncestorTypeOffset));
  until False;
end;