objpas.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 by the Free Pascal development team
  5. This unit makes Free Pascal as much as possible Delphi compatible
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit objpas;
  13. {$I-,S-}
  14. interface
  15. {*****************************************************************************
  16. Basic Types/constants
  17. *****************************************************************************}
  18. const
  19. // vmtSelfPtr = -36; { not implemented yet }
  20. vmtIntfTable = -32;
  21. vmtAutoTable = -28;
  22. vmtInitTable = -24;
  23. vmtTypeInfo = -20;
  24. vmtFieldTable = -16;
  25. vmtMethodTable = -12;
  26. vmtDynamicTable = -8;
  27. vmtClassName = -4;
  28. vmtInstanceSize = 0;
  29. vmtParent = 8;
  30. vmtDestroy = 12;
  31. vmtNewInstance = 16;
  32. vmtFreeInstance = 20;
  33. vmtSafeCallException = 24;
  34. vmtDefaultHandler = 28;
  35. vmtAfterConstruction = 32;
  36. vmtBeforeDestruction = 36;
  37. type
  38. { first, in object pascal, the types must be redefined }
  39. smallint = system.integer;
  40. integer = system.longint;
  41. { some pointer definitions }
  42. pshortstring = ^shortstring;
  43. plongstring = ^longstring;
  44. pansistring = ^ansistring;
  45. pwidestring = ^widestring;
  46. // pstring = pansistring;
  47. pextended = ^extended;
  48. ppointer = ^pointer;
  49. { now the let's declare the base classes for the class object }
  50. { model }
  51. tobject = class;
  52. tclass = class of tobject;
  53. pclass = ^tclass;
  54. tobject = class
  55. { please don't change the order of virtual methods, because }
  56. { their vmt offsets are used by some assembler code which uses }
  57. { hard coded addresses (FK) }
  58. constructor create;
  59. { the virtual procedures must be in THAT order }
  60. destructor destroy;virtual;
  61. class function newinstance : tobject;virtual;
  62. procedure freeinstance;virtual;
  63. function safecallexception(exceptobject : tobject;
  64. exceptaddr : pointer) : integer;virtual;
  65. procedure defaulthandler(var message);virtual;
  66. procedure free;
  67. class function initinstance(instance : pointer) : tobject;
  68. procedure cleanupinstance;
  69. function classtype : tclass;
  70. class function classinfo : pointer;
  71. class function classname : shortstring;
  72. class function classnameis(const name : string) : boolean;
  73. class function classparent : tclass;
  74. class function instancesize : longint;
  75. class function inheritsfrom(aclass : tclass) : boolean;
  76. { message handling routines }
  77. procedure dispatch(var message);
  78. class function methodaddress(const name : shortstring) : pointer;
  79. class function methodname(address : pointer) : shortstring;
  80. function fieldaddress(const name : shortstring) : pointer;
  81. { new since Delphi 4 }
  82. procedure AfterConstruction;virtual;
  83. procedure BeforeDestruction;virtual;
  84. { interface functions, I don't know if we need this }
  85. {
  86. function getinterface(const iid : tguid;out obj) : boolean;
  87. class function getinterfaceentry(const iid : tguid) : pinterfaceentry;
  88. class function getinterfacetable : pinterfacetable;
  89. }
  90. end;
  91. TExceptProc = Procedure (Obj : TObject; Addr: Pointer);
  92. Const
  93. ExceptProc : Pointer {TExceptProc} = Nil;
  94. {*****************************************************************************
  95. Variant Type
  96. *****************************************************************************}
  97. Const
  98. varEmpty = $0000;
  99. varNull = $0001;
  100. varSmallint = $0002;
  101. varInteger = $0003;
  102. varSingle = $0004;
  103. varDouble = $0005;
  104. varCurrency = $0006;
  105. varDate = $0007;
  106. varOleStr = $0008;
  107. varDispatch = $0009;
  108. varError = $000A;
  109. varBoolean = $000B;
  110. varVariant = $000C;
  111. varUnknown = $000D;
  112. varByte = $0011;
  113. varString = $0100;
  114. varAny = $0101;
  115. varTypeMask = $0FFF;
  116. varArray = $2000;
  117. varByRef = $4000;
  118. vtInteger = 0;
  119. vtBoolean = 1;
  120. vtChar = 2;
  121. vtExtended = 3;
  122. vtString = 4;
  123. vtPointer = 5;
  124. vtPChar = 6;
  125. vtObject = 7;
  126. vtClass = 8;
  127. vtWideChar = 9;
  128. vtPWideChar = 10;
  129. vtAnsiString = 11;
  130. vtCurrency = 12;
  131. vtVariant = 13;
  132. vtInterface = 14;
  133. vtWideString = 15;
  134. vtInt64 = 16;
  135. Type
  136. PVarRec = ^TVarRec;
  137. TVarRec = record
  138. case VType : Longint of
  139. vtInteger : (VInteger: Integer);
  140. vtBoolean : (VBoolean: Boolean);
  141. vtChar : (VChar: Char);
  142. vtExtended : (VExtended: PExtended);
  143. vtString : (VString: PShortString);
  144. vtPointer : (VPointer: Pointer);
  145. vtPChar : (VPChar: PChar);
  146. vtObject : (VObject: TObject);
  147. vtClass : (VClass: TClass);
  148. // vtWideChar : (VWideChar: WideChar);
  149. // vtPWideChar : (VPWideChar: PWideChar);
  150. vtAnsiString : (VAnsiString: Pointer);
  151. // vtCurrency : (VCurrency: PCurrency);
  152. // vtVariant : (VVariant: PVariant);
  153. // vtInterface : (VInterface: Pointer);
  154. vtWideString : (VWideString: Pointer);
  155. // vtInt64 : (VInt64: PInt64);
  156. end;
  157. implementation
  158. Procedure HandleError (Errno : longint);external name 'FPC_HANDLEERROR';
  159. {****************************************************************************
  160. Internal Routines called from the Compiler
  161. ****************************************************************************}
  162. procedure finalize(data,typeinfo : pointer);external name 'FPC_FINALIZE';
  163. { the reverse order of the parameters make code generation easier }
  164. function int_do_is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'FPC_DO_IS'];
  165. begin
  166. int_do_is:=aobject.inheritsfrom(aclass);
  167. end;
  168. { the reverse order of the parameters make code generation easier }
  169. procedure int_do_as(aclass : tclass;aobject : tobject);[public,alias: 'FPC_DO_AS'];
  170. begin
  171. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  172. handleerror(219);
  173. end;
  174. {****************************************************************************
  175. TOBJECT
  176. ****************************************************************************}
  177. constructor TObject.Create;
  178. begin
  179. end;
  180. destructor TObject.Destroy;
  181. begin
  182. end;
  183. procedure TObject.Free;
  184. begin
  185. // the call via self avoids a warning
  186. if self<>nil then
  187. self.destroy;
  188. end;
  189. class function TObject.InstanceSize : LongInt;
  190. type
  191. plongint = ^longint;
  192. begin
  193. { type of self is class of tobject => it points to the vmt }
  194. { the size is saved at offset 0 }
  195. InstanceSize:=plongint(self)^;
  196. end;
  197. class function TObject.InitInstance(instance : pointer) : tobject;
  198. begin
  199. fillchar(instance^,self.instancesize,0);
  200. { insert VMT pointer into the new created memory area }
  201. { (in class methods self contains the VMT!) }
  202. ppointer(instance)^:=pointer(self);
  203. InitInstance:=TObject(Instance);
  204. end;
  205. class function TObject.ClassParent : tclass;
  206. begin
  207. { type of self is class of tobject => it points to the vmt }
  208. { the parent vmt is saved at offset vmtParent }
  209. classparent:=(pclass(self)+vmtParent)^;
  210. end;
  211. class function TObject.NewInstance : tobject;
  212. var
  213. p : pointer;
  214. begin
  215. getmem(p,instancesize);
  216. InitInstance(p);
  217. NewInstance:=TObject(p);
  218. end;
  219. procedure TObject.FreeInstance;
  220. var
  221. p : Pointer;
  222. begin
  223. CleanupInstance;
  224. { self is a register, so we can't pass it call by reference }
  225. p:=Pointer(Self);
  226. FreeMem(p,InstanceSize);
  227. end;
  228. function TObject.ClassType : TClass;
  229. begin
  230. ClassType:=TClass(Pointer(Self)^)
  231. end;
  232. class function TObject.MethodAddress(const name : shortstring) : pointer;
  233. begin
  234. methodaddress:=nil;
  235. end;
  236. class function TObject.MethodName(address : pointer) : shortstring;
  237. begin
  238. methodname:='';
  239. end;
  240. function TObject.FieldAddress(const name : shortstring) : pointer;
  241. begin
  242. fieldaddress:=nil;
  243. end;
  244. function TObject.SafeCallException(exceptobject : tobject;
  245. exceptaddr : pointer) : integer;
  246. begin
  247. safecallexception:=0;
  248. end;
  249. class function TObject.ClassInfo : pointer;
  250. begin
  251. ClassInfo:=(PPointer(self)+vmtTypeInfo)^;
  252. end;
  253. class function TObject.ClassName : ShortString;
  254. begin
  255. ClassName:=PShortString((PPointer(Self)+vmtClassName)^)^;
  256. end;
  257. class function TObject.ClassNameIs(const name : string) : boolean;
  258. begin
  259. ClassNameIs:=ClassName=name;
  260. end;
  261. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  262. var
  263. c : tclass;
  264. begin
  265. c:=self;
  266. while assigned(c) do
  267. begin
  268. if c=aclass then
  269. begin
  270. InheritsFrom:=true;
  271. exit;
  272. end;
  273. c:=c.ClassParent;
  274. end;
  275. InheritsFrom:=false;
  276. end;
  277. procedure TObject.Dispatch(var message);
  278. begin
  279. end;
  280. procedure TObject.DefaultHandler(var message);
  281. begin
  282. end;
  283. procedure TObject.CleanupInstance;
  284. var
  285. vmt : tclass;
  286. begin
  287. vmt:=ClassType;
  288. while vmt<>nil do
  289. begin
  290. if Assigned(Pointer((Pointer(vmt)+vmtInitTable)^)) then
  291. Finalize(Pointer(Self),Pointer((Pointer(vmt)+vmtInitTable)^));
  292. vmt:=vmt.ClassParent;
  293. end;
  294. end;
  295. procedure TObject.AfterConstruction;
  296. begin
  297. end;
  298. procedure TObject.BeforeDestruction;
  299. begin
  300. end;
  301. {****************************************************************************
  302. Exception Support
  303. ****************************************************************************}
  304. {$i except.inc}
  305. {****************************************************************************
  306. Initialize
  307. ****************************************************************************}
  308. begin
  309. InitExceptions;
  310. end.
  311. {
  312. $Log$
  313. Revision 1.18 1998-10-12 12:42:58 florian
  314. * as operator runtime error can be now caught by an errorproc
  315. Revision 1.17 1998/10/05 12:32:53 peter
  316. + assert() support
  317. Revision 1.16 1998/10/03 15:07:16 florian
  318. + TObject.AfterConstruction and TObject.BeforeDestruction of Delphi 4
  319. Revision 1.15 1998/09/24 16:13:48 michael
  320. Changes in exception and open array handling
  321. Revision 1.14 1998/09/23 12:40:43 michael
  322. Fixed TVarRec again. Should be OK now
  323. Revision 1.13 1998/09/23 12:18:32 michael
  324. + added VType in TVArRec
  325. Revision 1.12 1998/09/23 10:00:47 peter
  326. * tvarrec should be 8 bytes
  327. Revision 1.11 1998/09/22 15:30:07 peter
  328. * array of const update
  329. Revision 1.9 1998/09/16 13:08:19 michael
  330. Added AbstractErrorHandler
  331. Revision 1.8 1998/09/06 21:27:31 florian
  332. + method tobject.classinfo added
  333. Revision 1.7 1998/09/04 08:49:06 peter
  334. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  335. Revision 1.6 1998/08/23 20:58:52 florian
  336. + rtti for objects and classes
  337. + TObject.GetClassName implemented
  338. Revision 1.5 1998/07/30 16:10:11 michael
  339. + Added support for ExceptProc+
  340. Revision 1.4 1998/07/29 15:44:33 michael
  341. included sysutils and math.pp as target. They compile now.
  342. Revision 1.3 1998/07/29 10:09:28 michael
  343. + put in exception support
  344. Revision 1.2 1998/03/25 23:40:24 florian
  345. + stuff from old objpash.inc and objpas.inc merged in
  346. }