objpas.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. {****************************************************************************
  159. Internal Routines called from the Compiler
  160. ****************************************************************************}
  161. procedure finalize(data,typeinfo : pointer);external name 'FPC_FINALIZE';
  162. { the reverse order of the parameters make code generation easier }
  163. function int_do_is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'FPC_DO_IS'];
  164. begin
  165. int_do_is:=aobject.inheritsfrom(aclass);
  166. end;
  167. { the reverse order of the parameters make code generation easier }
  168. procedure int_do_as(aclass : tclass;aobject : tobject);[public,alias: 'FPC_DO_AS'];
  169. begin
  170. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  171. runerror(219);
  172. end;
  173. {****************************************************************************
  174. TOBJECT
  175. ****************************************************************************}
  176. constructor TObject.Create;
  177. begin
  178. end;
  179. destructor TObject.Destroy;
  180. begin
  181. end;
  182. procedure TObject.Free;
  183. begin
  184. // the call via self avoids a warning
  185. if self<>nil then
  186. self.destroy;
  187. end;
  188. class function TObject.InstanceSize : LongInt;
  189. type
  190. plongint = ^longint;
  191. begin
  192. { type of self is class of tobject => it points to the vmt }
  193. { the size is saved at offset 0 }
  194. InstanceSize:=plongint(self)^;
  195. end;
  196. class function TObject.InitInstance(instance : pointer) : tobject;
  197. begin
  198. fillchar(instance^,self.instancesize,0);
  199. { insert VMT pointer into the new created memory area }
  200. { (in class methods self contains the VMT!) }
  201. ppointer(instance)^:=pointer(self);
  202. InitInstance:=TObject(Instance);
  203. end;
  204. class function TObject.ClassParent : tclass;
  205. begin
  206. { type of self is class of tobject => it points to the vmt }
  207. { the parent vmt is saved at offset vmtParent }
  208. classparent:=(pclass(self)+vmtParent)^;
  209. end;
  210. class function TObject.NewInstance : tobject;
  211. var
  212. p : pointer;
  213. begin
  214. getmem(p,instancesize);
  215. InitInstance(p);
  216. NewInstance:=TObject(p);
  217. end;
  218. procedure TObject.FreeInstance;
  219. var
  220. p : Pointer;
  221. begin
  222. CleanupInstance;
  223. { self is a register, so we can't pass it call by reference }
  224. p:=Pointer(Self);
  225. FreeMem(p,InstanceSize);
  226. end;
  227. function TObject.ClassType : TClass;
  228. begin
  229. ClassType:=TClass(Pointer(Self)^)
  230. end;
  231. class function TObject.MethodAddress(const name : shortstring) : pointer;
  232. begin
  233. methodaddress:=nil;
  234. end;
  235. class function TObject.MethodName(address : pointer) : shortstring;
  236. begin
  237. methodname:='';
  238. end;
  239. function TObject.FieldAddress(const name : shortstring) : pointer;
  240. begin
  241. fieldaddress:=nil;
  242. end;
  243. function TObject.SafeCallException(exceptobject : tobject;
  244. exceptaddr : pointer) : integer;
  245. begin
  246. safecallexception:=0;
  247. end;
  248. class function TObject.ClassInfo : pointer;
  249. begin
  250. ClassInfo:=(PPointer(self)+vmtTypeInfo)^;
  251. end;
  252. class function TObject.ClassName : ShortString;
  253. begin
  254. ClassName:=PShortString((PPointer(Self)+vmtClassName)^)^;
  255. end;
  256. class function TObject.ClassNameIs(const name : string) : boolean;
  257. begin
  258. ClassNameIs:=ClassName=name;
  259. end;
  260. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  261. var
  262. c : tclass;
  263. begin
  264. c:=self;
  265. while assigned(c) do
  266. begin
  267. if c=aclass then
  268. begin
  269. InheritsFrom:=true;
  270. exit;
  271. end;
  272. c:=c.ClassParent;
  273. end;
  274. InheritsFrom:=false;
  275. end;
  276. procedure TObject.Dispatch(var message);
  277. begin
  278. end;
  279. procedure TObject.DefaultHandler(var message);
  280. begin
  281. end;
  282. procedure TObject.CleanupInstance;
  283. var
  284. vmt : tclass;
  285. begin
  286. vmt:=ClassType;
  287. while vmt<>nil do
  288. begin
  289. if Assigned(Pointer((Pointer(vmt)+vmtInitTable)^)) then
  290. Finalize(Pointer(Self),Pointer((Pointer(vmt)+vmtInitTable)^));
  291. vmt:=vmt.ClassParent;
  292. end;
  293. end;
  294. procedure TObject.AfterConstruction;
  295. begin
  296. end;
  297. procedure TObject.BeforeDestruction;
  298. begin
  299. end;
  300. {****************************************************************************
  301. Exception Support
  302. ****************************************************************************}
  303. {$i except.inc}
  304. {****************************************************************************
  305. Initialize
  306. ****************************************************************************}
  307. begin
  308. InitExceptions;
  309. end.
  310. {
  311. $Log$
  312. Revision 1.17 1998-10-05 12:32:53 peter
  313. + assert() support
  314. Revision 1.16 1998/10/03 15:07:16 florian
  315. + TObject.AfterConstruction and TObject.BeforeDestruction of Delphi 4
  316. Revision 1.15 1998/09/24 16:13:48 michael
  317. Changes in exception and open array handling
  318. Revision 1.14 1998/09/23 12:40:43 michael
  319. Fixed TVarRec again. Should be OK now
  320. Revision 1.13 1998/09/23 12:18:32 michael
  321. + added VType in TVArRec
  322. Revision 1.12 1998/09/23 10:00:47 peter
  323. * tvarrec should be 8 bytes
  324. Revision 1.11 1998/09/22 15:30:07 peter
  325. * array of const update
  326. Revision 1.9 1998/09/16 13:08:19 michael
  327. Added AbstractErrorHandler
  328. Revision 1.8 1998/09/06 21:27:31 florian
  329. + method tobject.classinfo added
  330. Revision 1.7 1998/09/04 08:49:06 peter
  331. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  332. Revision 1.6 1998/08/23 20:58:52 florian
  333. + rtti for objects and classes
  334. + TObject.GetClassName implemented
  335. Revision 1.5 1998/07/30 16:10:11 michael
  336. + Added support for ExceptProc+
  337. Revision 1.4 1998/07/29 15:44:33 michael
  338. included sysutils and math.pp as target. They compile now.
  339. Revision 1.3 1998/07/29 10:09:28 michael
  340. + put in exception support
  341. Revision 1.2 1998/03/25 23:40:24 florian
  342. + stuff from old objpash.inc and objpas.inc merged in
  343. }