blockrtl.pp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 20134 by Jonas Maebe,
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit blockrtl;
  12. { for tmethod }
  13. {$mode objfpc}
  14. {$packrecords c}
  15. interface
  16. {$IFDEF FPC_DOTTEDUNITS}
  17. uses
  18. System.InitC,
  19. System.CTypes;
  20. {$ELSE FPC_DOTTEDUNITS}
  21. uses
  22. initc,
  23. ctypes;
  24. {$ENDIF FPC_DOTTEDUNITS}
  25. { blocks helpers }
  26. function _Block_copy(const aBlock: pointer): pointer; cdecl; external;
  27. procedure _Block_release(const aBlock: pointer); cdecl; external;
  28. procedure _Block_object_assign(dst, src: pointer; const flags: cint); cdecl; external;
  29. procedure _Block_object_dispose(const aBlock: pointer; const flags: cint); cdecl; external;
  30. { blocks isa pointers }
  31. var
  32. _NSConcreteGlobalBlock: array[0..31] of pointer; cvar; external;
  33. _NSConcreteStackBlock: array[0..31] of pointer; cvar; external;
  34. { from Clang source, distributed under the University of Illinois Open Source
  35. License }
  36. { BlockByrefFlags }
  37. const
  38. BLOCK_BYREF_HAS_COPY_DISPOSE = 1 shl 25;
  39. BLOCK_BYREF_LAYOUT_MASK = $f shl 28;
  40. BLOCK_BYREF_LAYOUT_EXTENDED = 1 shl 28;
  41. BLOCK_BYREF_LAYOUT_NON_OBJECT = 2 shl 28;
  42. BLOCK_BYREF_LAYOUT_STRONG = 3 shl 28;
  43. BLOCK_BYREF_LAYOUT_WEAK = 4 shl 28;
  44. BLOCK_BYREF_LAYOUT_UNRETAINED = 5 shl 28;
  45. { BlockLiteralFlags }
  46. const
  47. BLOCK_HAS_COPY_DISPOSE = 1 shl 25;
  48. BLOCK_HAS_CXX_OBJ = 1 shl 26;
  49. BLOCK_IS_GLOBAL = 1 shl 28;
  50. BLOCK_USE_STRET = 1 shl 29;
  51. BLOCK_HAS_SIGNATURE = 1 shl 30;
  52. BLOCK_HAS_EXTENDED_LAYOUT = 1 shl 31;
  53. { BlockFieldFlag_t }
  54. const
  55. BLOCK_FIELD_IS_OBJECT = $03; { id, NSObject, __attribute__((NSObject)), block, ... }
  56. BLOCK_FIELD_IS_BLOCK = $07; { a block variable }
  57. BLOCK_FIELD_IS_BYREF = $08; { the on stack structure holding the __block variable }
  58. BLOCK_FIELD_IS_WEAK = $10; { declared __weak, only used in byref copy helpers }
  59. BLOCK_FIELD_IS_ARC = $40; { field has ARC-specific semantics }
  60. BLOCK_BYREF_CALLER = 128; { called from __block (byref) copy/dispose support routines }
  61. BLOCK_BYREF_CURRENT_MAX = 256;
  62. type
  63. FPC_Block_literal_base = record
  64. isa: pointer; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
  65. flags: cint;
  66. reserved: cint;
  67. { actually a function pointer, will be cast by the compiler }
  68. invoke: pointer;
  69. descriptor: pointer;
  70. end;
  71. { descriptor for a simple block (no copy/release) }
  72. FPC_Block_descriptor_simple = record
  73. reserved: culong;
  74. Block_size: culong;
  75. { signatures are only for the "ABI.2010.3.16" version, but that's all we support
  76. because otherwise the callback has to be a C-style variadic function, which
  77. we cannot (yet?) generate on the callee side}
  78. signature: pAnsichar;
  79. end;
  80. { descriptor for a simple block (no copy/release) }
  81. FPC_Block_descriptor_complex = record
  82. reserved: culong;
  83. Block_size: culong;
  84. copy_helper: procedure(dst, src: pointer); cdecl;
  85. dispose_helper: procedure(block: pointer); cdecl;
  86. { signatures are only for the "ABI.2010.3.16" version, but that's all we support
  87. because otherwise the callback has to be a C-style variadic function, which
  88. we cannot (yet?) generate on the callee side}
  89. signature: pansichar;
  90. end;
  91. { for global procedures }
  92. FPC_Block_literal_static = record
  93. base: FPC_Block_literal_base;
  94. { no local state }
  95. end;
  96. { for simple procedure variable (just an address) }
  97. FPC_Block_literal_simple_procvar = record
  98. base: FPC_Block_literal_base;
  99. { the procvar to call }
  100. pv: pointer;
  101. end;
  102. { for procedure of object, and in the future also nested procedures and
  103. maybe Objective-C methods }
  104. FPC_Block_literal_complex_procvar = record
  105. base: FPC_Block_literal_base;
  106. { the procvar to call }
  107. pv: tmethod;
  108. end;
  109. PFPC_Block_literal_complex_procvar = ^FPC_Block_literal_complex_procvar;
  110. implementation
  111. end.