objpas.pp 12 KB

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