objpas.pp 15 KB

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