Recent

Author Topic: About FP types  (Read 7356 times)

Sergios

  • New Member
  • *
  • Posts: 10
About FP types
« on: September 28, 2014, 09:08:55 am »
Hi!
I have two questions:
1. What means directive "type" in type delaration section?
Quote from: systemh.inc
Type
...
  UTF8String = type ansistring;
2. Where can I find identifiers for Byte and Boolean?
Quote from: ref.pdf, page 14
Remark: Predefined types such as Byte, Boolean and constants such as maxint are not reserved words.
They are identifiers, declared in the system unit.
Windows 8.1 Pro, Lazarus 1.2.4, FPC 2.6.4

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: About FP types
« Reply #1 on: September 28, 2014, 10:06:20 am »
Hi!
I have two questions:
1. What means directive "type" in type delaration section?
Quote from: systemh.inc
Type
...
  UTF8String = type ansistring;

That means that utf8string is not assign compatible with ansistring any more so unless the rtl defines some kind of auto conversion routine for it then doing some thing like this
Code: [Select]
var
  vStr1 : AnsiString;
  vStr2 : Utf8string;
begin
  vstr1 := 'This is ansi';
  vstr2 := vstr1; //<--- incompatible type can't assign ansistring to utf8string
end;
will raise an exception.

2. Where can I find identifiers for Byte and Boolean?
Quote from: ref.pdf, page 14
Remark: Predefined types such as Byte, Boolean and constants such as maxint are not reserved words.
They are identifiers, declared in the system unit.

No idea never gone looking for them.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: About FP types
« Reply #2 on: September 28, 2014, 01:11:01 pm »
Quote from: ref.pdf, page 14
Remark: Predefined types such as Byte, Boolean and constants such as maxint are not reserved words.
They are identifiers, declared in the system unit.

Actually I think part of that remark may be incorrect.
Maxint is an alias for maxSmallint which is declared in systemh.inc.
I cannot find declarations for the two types byte and boolean in the system unit (however, I may have searched wrongly).
I think they are effectively part of the Pascal language (whether or not they are technically reserved words), and are defined/declared somewhere inside the compiler, rather than in the RTL. We would need a compiler guy to guide us on this.
Boolean behaves as if it were an enumerated type declared like this:
Code: [Select]
type
  boolean = (False, True);
« Last Edit: September 28, 2014, 01:21:57 pm by howardpc »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: About FP types
« Reply #3 on: September 28, 2014, 04:59:45 pm »
Quote
Maxint is an alias for maxSmallint which is declared in systemh.inc.
SysUtils override it to MaxLongInt AFAIK.
Quote
I think they are effectively part of the Pascal language (whether or not they are technically reserved words), and are defined/declared somewhere inside the compiler, rather than in the RTL
Correct, all base types are defined by the compiler. You can see a little comment in the type section of systemh.inc (shared by all supported targets). However, they are identifiers. You can perfectly override them.
Quote
Boolean behaves as if it were an enumerated type declared like this:
That's exactly how the standard defines Boolean. However, enum can't mark them as reserved words so they must be internally defined.

Sergios

  • New Member
  • *
  • Posts: 10
Re: About FP types
« Reply #4 on: October 10, 2014, 11:20:23 am »
I thank All for responses.
I have the other question now.
Are the following typecastings safe?:
  • PWideChar(varWideString)
  • WideString(varPWideChar)
  • PChar(varAnsiString)
  • AnsiString(varPChar)
Where var[Type] is a variable of type [Type] declared in var section of code.
Thanks in advance.
« Last Edit: October 10, 2014, 11:24:19 am by Sergios »
Windows 8.1 Pro, Lazarus 1.2.4, FPC 2.6.4

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: About FP types
« Reply #5 on: October 10, 2014, 12:12:55 pm »
Responding to your third question - it depends what you mean by safe.

PChar(anAnsitString) is always a valid cast in the sense that it will not raise an exception or seg fault, because of the way that the Pascal AnsiString type is defined.
However, AnsiStrings can contain embedded #0 characters (since their length is not determined by the position of #0), so such casts may not give you the results you expect. Here's a little example:

Code: [Select]
program Project1;

{$mode objfpc}{$H+}

var
  anS: AnsiString;
  aP: PChar;

begin
  anS:= 'first'#0'second';
  aP:=PChar(anS);
  WriteLn('Displaying AnsiString: "',anS,'"');
  WriteLn('Displaying PChar cast: "',aP, '"');
  ReadLn;
end.

It might be better to start a new thread for somewhat unrelated questions (this helps people searching for specific topics in future, since they don't have to wade through a long rambling conversation).
« Last Edit: October 10, 2014, 12:14:33 pm by howardpc »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: About FP types
« Reply #6 on: October 10, 2014, 01:05:32 pm »
Quote
Maxint is an alias for maxSmallint which is declared in systemh.inc.
SysUtils override it to MaxLongInt AFAIK.

No, since sysutils won't be guaranteed included in objfpc/delphi mode. The system unit addendum for those modes is objpas.

If you choose mode delphiunicode in 2.7.1 or use the modeswitch, unit uuchar gets additionally added. (which redefines char and pchar for D2009+ compat)

Note that starting with 2.6.x afaik also pascal boolean types exist in multiple sizes, with normal boolean being equal to boolean8.

 

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: About FP types
« Reply #7 on: October 11, 2014, 04:49:21 pm »
No, since sysutils won't be guaranteed included in objfpc/delphi mode. The system unit addendum for those modes is objpas.
Whoops, I wrote the wrong unit :-[

Sergios

  • New Member
  • *
  • Posts: 10
Re: About FP types
« Reply #8 on: October 12, 2014, 02:16:38 pm »
...it depends what you mean by safe.
Safe type conversion (typecasting), in my opinion, type conversion without any errors (including hidden errors, e.g. padding), and without loss of data.
Windows 8.1 Pro, Lazarus 1.2.4, FPC 2.6.4

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: About FP types
« Reply #9 on: October 12, 2014, 04:43:53 pm »
2. Where can I find identifiers for Byte and Boolean?
Quote from: ref.pdf, page 14
Remark: Predefined types such as Byte, Boolean and constants such as maxint are not reserved words.
They are identifiers, declared in the system unit.

Compiler built-in types can be seen in symdef, like:
Code: [Select]
    function torddef.GetTypeName : string;
      const
        names : array[tordtype] of string[20] = (
          'untyped',
          'Byte','Word','DWord','QWord',
          'ShortInt','SmallInt','LongInt','Int64',
          'Boolean','Boolean16','Boolean32','Boolean64',
          'ByteBool','WordBool','LongBool','QWordBool',
          'Char','WideChar','Currency');

Howardpc's example uses WriteLn, which the compiler translates into one or more calls to fpc_write_text_* procedures in ninl.pas
for ansi string parameters it calls fpc_write_text_AnsiStr and it uses the length provided by the AnsiString itself.
while for pchar parameters it calls fpc_write_text_PChar_As_Pointer and it finds the lengths based of 1st #0 char.
both are declared in compproc.inc.
In both cases data came from the same address. You can check with: PtrUInt(@anS[1]) = PtrUInt(aP)

Conversion from String to Pointer, as in 1 and 3, is safe and fast. Conversion from Pointer to String (2 and 4) is not as safe, because it involves memory allocation. Notice that fpc_PWideChar_To_WideStr and fpc_PChar_To_AnsiStr are used in this case and also declared in compproc.inc, astrings.inc, and wstrings.inc.

 

TinyPortal © 2005-2018