pdecobj.pas 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does object types for Free Pascal
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecobj;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,symconst,symtype,symdef;
  23. { parses a object declaration }
  24. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  25. { parses a (class) method declaration }
  26. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  27. function class_constructor_head(astruct: tabstractrecorddef):tprocdef;
  28. function class_destructor_head(astruct: tabstractrecorddef):tprocdef;
  29. function constructor_head:tprocdef;
  30. function destructor_head:tprocdef;
  31. procedure struct_property_dec(is_classproperty:boolean);
  32. implementation
  33. uses
  34. sysutils,cutils,
  35. globals,verbose,systems,tokens,
  36. symbase,symsym,symtable,symcreat,defcmp,
  37. node,nld,nmem,ncon,ncnv,ncal,
  38. fmodule,scanner,
  39. pbase,pexpr,pdecsub,pdecvar,ptype,pdecl,ppu,
  40. {$ifdef jvm}
  41. pjvm,
  42. {$endif}
  43. parabase
  44. ;
  45. const
  46. { Please leave this here, this module should NOT use
  47. these variables.
  48. Declaring it as string here results in an error when compiling (PFV) }
  49. current_procinfo = 'error';
  50. var
  51. current_objectdef : tobjectdef absolute current_structdef;
  52. procedure constr_destr_finish_head(pd: tprocdef; const astruct: tabstractrecorddef);
  53. begin
  54. case astruct.typ of
  55. recorddef:
  56. parse_record_proc_directives(pd);
  57. objectdef:
  58. parse_object_proc_directives(pd);
  59. else
  60. internalerror(2011040502);
  61. end;
  62. handle_calling_convention(pd);
  63. { add definition to procsym }
  64. proc_add_definition(pd);
  65. { add procdef options to objectdef options }
  66. if (po_virtualmethod in pd.procoptions) then
  67. include(astruct.objectoptions,oo_has_virtual);
  68. maybe_parse_hint_directives(pd);
  69. end;
  70. function class_constructor_head(astruct: tabstractrecorddef):tprocdef;
  71. var
  72. pd : tprocdef;
  73. begin
  74. result:=nil;
  75. consume(_CONSTRUCTOR);
  76. { must be at same level as in implementation }
  77. parse_proc_head(current_structdef,potype_class_constructor,pd);
  78. if not assigned(pd) then
  79. begin
  80. consume(_SEMICOLON);
  81. exit;
  82. end;
  83. pd.calcparas;
  84. if (pd.maxparacount>0) then
  85. Message(parser_e_no_paras_for_class_constructor);
  86. consume(_SEMICOLON);
  87. include(astruct.objectoptions,oo_has_class_constructor);
  88. current_module.flags:=current_module.flags or uf_classinits;
  89. { no return value }
  90. pd.returndef:=voidtype;
  91. constr_destr_finish_head(pd,astruct);
  92. result:=pd;
  93. end;
  94. function constructor_head:tprocdef;
  95. var
  96. pd : tprocdef;
  97. begin
  98. result:=nil;
  99. consume(_CONSTRUCTOR);
  100. { must be at same level as in implementation }
  101. parse_proc_head(current_structdef,potype_constructor,pd);
  102. if not assigned(pd) then
  103. begin
  104. consume(_SEMICOLON);
  105. exit;
  106. end;
  107. if (cs_constructor_name in current_settings.globalswitches) and
  108. (pd.procsym.name<>'INIT') then
  109. Message(parser_e_constructorname_must_be_init);
  110. consume(_SEMICOLON);
  111. include(current_structdef.objectoptions,oo_has_constructor);
  112. { Set return type, class and record constructors return the
  113. created instance, object constructors return boolean }
  114. if is_class(pd.struct) or
  115. is_record(pd.struct) or
  116. is_javaclass(pd.struct) then
  117. pd.returndef:=pd.struct
  118. else
  119. {$ifdef CPU64bitaddr}
  120. pd.returndef:=bool64type;
  121. {$else CPU64bitaddr}
  122. pd.returndef:=bool32type;
  123. {$endif CPU64bitaddr}
  124. constr_destr_finish_head(pd,pd.struct);
  125. result:=pd;
  126. end;
  127. procedure struct_property_dec(is_classproperty:boolean);
  128. var
  129. p : tpropertysym;
  130. begin
  131. { check for a class, record or helper }
  132. if not((is_class_or_interface_or_dispinterface(current_structdef) or is_record(current_structdef) or
  133. is_objectpascal_helper(current_structdef) or is_java_class_or_interface(current_structdef)) or
  134. (not(m_tp7 in current_settings.modeswitches) and (is_object(current_structdef)))) then
  135. Message(parser_e_syntax_error);
  136. consume(_PROPERTY);
  137. p:=read_property_dec(is_classproperty,current_structdef);
  138. consume(_SEMICOLON);
  139. if try_to_consume(_DEFAULT) then
  140. begin
  141. if oo_has_default_property in current_structdef.objectoptions then
  142. message(parser_e_only_one_default_property);
  143. include(current_structdef.objectoptions,oo_has_default_property);
  144. include(p.propoptions,ppo_defaultproperty);
  145. if not(ppo_hasparameters in p.propoptions) then
  146. message(parser_e_property_need_paras);
  147. if (token=_COLON) then
  148. begin
  149. Message(parser_e_field_not_allowed_here);
  150. consume_all_until(_SEMICOLON);
  151. end;
  152. consume(_SEMICOLON);
  153. end;
  154. { parse possible enumerator modifier }
  155. if try_to_consume(_ENUMERATOR) then
  156. begin
  157. if (token = _ID) then
  158. begin
  159. if pattern='CURRENT' then
  160. begin
  161. if oo_has_enumerator_current in current_structdef.objectoptions then
  162. message(parser_e_only_one_enumerator_current);
  163. if not p.propaccesslist[palt_read].empty then
  164. begin
  165. include(current_structdef.objectoptions,oo_has_enumerator_current);
  166. include(p.propoptions,ppo_enumerator_current);
  167. end
  168. else
  169. Message(parser_e_enumerator_current_is_not_valid) // property has no reader
  170. end
  171. else
  172. Message1(parser_e_invalid_enumerator_identifier, pattern);
  173. consume(token);
  174. end
  175. else
  176. Message(parser_e_enumerator_identifier_required);
  177. consume(_SEMICOLON);
  178. end;
  179. { hint directives, these can be separated by semicolons here,
  180. that needs to be handled here with a loop (PFV) }
  181. while try_consume_hintdirective(p.symoptions,p.deprecatedmsg) do
  182. Consume(_SEMICOLON);
  183. end;
  184. function class_destructor_head(astruct: tabstractrecorddef):tprocdef;
  185. var
  186. pd : tprocdef;
  187. begin
  188. result:=nil;
  189. consume(_DESTRUCTOR);
  190. parse_proc_head(current_structdef,potype_class_destructor,pd);
  191. if not assigned(pd) then
  192. begin
  193. consume(_SEMICOLON);
  194. exit;
  195. end;
  196. pd.calcparas;
  197. if (pd.maxparacount>0) then
  198. Message(parser_e_no_paras_for_class_destructor);
  199. consume(_SEMICOLON);
  200. include(astruct.objectoptions,oo_has_class_destructor);
  201. current_module.flags:=current_module.flags or uf_classinits;
  202. { no return value }
  203. pd.returndef:=voidtype;
  204. constr_destr_finish_head(pd,astruct);
  205. result:=pd;
  206. end;
  207. function destructor_head:tprocdef;
  208. var
  209. pd : tprocdef;
  210. begin
  211. result:=nil;
  212. consume(_DESTRUCTOR);
  213. parse_proc_head(current_structdef,potype_destructor,pd);
  214. if not assigned(pd) then
  215. begin
  216. consume(_SEMICOLON);
  217. exit;
  218. end;
  219. if (cs_constructor_name in current_settings.globalswitches) and
  220. (pd.procsym.name<>'DONE') then
  221. Message(parser_e_destructorname_must_be_done);
  222. pd.calcparas;
  223. if not(pd.maxparacount=0) and
  224. (m_fpc in current_settings.modeswitches) then
  225. Message(parser_e_no_paras_for_destructor);
  226. consume(_SEMICOLON);
  227. include(current_structdef.objectoptions,oo_has_destructor);
  228. { no return value }
  229. pd.returndef:=voidtype;
  230. constr_destr_finish_head(pd,pd.struct);
  231. result:=pd;
  232. end;
  233. procedure setinterfacemethodoptions;
  234. var
  235. i : longint;
  236. def : tdef;
  237. begin
  238. include(current_structdef.objectoptions,oo_has_virtual);
  239. for i:=0 to current_structdef.symtable.DefList.count-1 do
  240. begin
  241. def:=tdef(current_structdef.symtable.DefList[i]);
  242. if assigned(def) and
  243. (def.typ=procdef) then
  244. begin
  245. include(tprocdef(def).procoptions,po_virtualmethod);
  246. tprocdef(def).forwarddef:=false;
  247. end;
  248. end;
  249. end;
  250. procedure setobjcclassmethodoptions;
  251. var
  252. i : longint;
  253. def : tdef;
  254. begin
  255. for i:=0 to current_structdef.symtable.DefList.count-1 do
  256. begin
  257. def:=tdef(current_structdef.symtable.DefList[i]);
  258. if assigned(def) and
  259. (def.typ=procdef) then
  260. begin
  261. include(tprocdef(def).procoptions,po_virtualmethod);
  262. end;
  263. end;
  264. end;
  265. procedure handleImplementedInterface(intfdef : tobjectdef);
  266. begin
  267. if not is_interface(intfdef) then
  268. begin
  269. Message1(type_e_interface_type_expected,intfdef.typename);
  270. exit;
  271. end;
  272. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  273. Message1(sym_e_duplicate_id,intfdef.objname^)
  274. else
  275. begin
  276. { allocate and prepare the GUID only if the class
  277. implements some interfaces. }
  278. if current_objectdef.ImplementedInterfaces.count = 0 then
  279. current_objectdef.prepareguid;
  280. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  281. end;
  282. end;
  283. procedure handleImplementedProtocolOrJavaIntf(intfdef : tobjectdef);
  284. begin
  285. intfdef:=find_real_class_definition(intfdef,false);
  286. case current_objectdef.objecttype of
  287. odt_objcclass,
  288. odt_objccategory,
  289. odt_objcprotocol:
  290. if not is_objcprotocol(intfdef) then
  291. begin
  292. Message1(type_e_protocol_type_expected,intfdef.typename);
  293. exit;
  294. end;
  295. odt_javaclass,
  296. odt_interfacejava:
  297. if not is_javainterface(intfdef) then
  298. begin
  299. Message1(type_e_interface_type_expected,intfdef.typename);
  300. exit
  301. end;
  302. else
  303. internalerror(2011010807);
  304. end;
  305. if ([oo_is_forward,oo_is_formal] * intfdef.objectoptions <> []) then
  306. begin
  307. Message1(parser_e_forward_intf_declaration_must_be_resolved,intfdef.objrealname^);
  308. exit;
  309. end;
  310. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  311. Message1(sym_e_duplicate_id,intfdef.objname^)
  312. else
  313. begin
  314. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  315. end;
  316. end;
  317. procedure readImplementedInterfacesAndProtocols(intf: boolean);
  318. var
  319. hdef : tdef;
  320. begin
  321. while try_to_consume(_COMMA) do
  322. begin
  323. { use single_type instead of id_type for specialize support }
  324. single_type(hdef,[stoAllowSpecialization,stoParseClassParent]);
  325. if (hdef.typ<>objectdef) then
  326. begin
  327. if intf then
  328. Message1(type_e_interface_type_expected,hdef.typename)
  329. else
  330. Message1(type_e_protocol_type_expected,hdef.typename);
  331. continue;
  332. end;
  333. if intf then
  334. handleImplementedInterface(tobjectdef(hdef))
  335. else
  336. handleImplementedProtocolOrJavaIntf(tobjectdef(hdef));
  337. end;
  338. end;
  339. procedure readinterfaceiid;
  340. var
  341. p : tnode;
  342. valid : boolean;
  343. begin
  344. p:=comp_expr(true,false);
  345. if p.nodetype=stringconstn then
  346. begin
  347. stringdispose(current_objectdef.iidstr);
  348. current_objectdef.iidstr:=stringdup(strpas(tstringconstnode(p).value_str));
  349. valid:=string2guid(current_objectdef.iidstr^,current_objectdef.iidguid^);
  350. if (current_objectdef.objecttype in [odt_interfacecom,odt_dispinterface]) and
  351. not valid then
  352. Message(parser_e_improper_guid_syntax);
  353. include(current_structdef.objectoptions,oo_has_valid_guid);
  354. end
  355. else
  356. Message(parser_e_illegal_expression);
  357. p.free;
  358. end;
  359. procedure get_cpp_or_java_class_external_status(od: tobjectdef);
  360. var
  361. hs: string;
  362. begin
  363. { C++ classes can be external -> all methods inside are external
  364. (defined at the class level instead of per method, so that you cannot
  365. define some methods as external and some not)
  366. }
  367. if try_to_consume(_EXTERNAL) then
  368. begin
  369. hs:='';
  370. if token in [_CSTRING,_CWSTRING,_CCHAR,_CWCHAR] then
  371. begin
  372. { Always add library prefix and suffix to create an uniform name }
  373. hs:=get_stringconst;
  374. if ExtractFileExt(hs)='' then
  375. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  376. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  377. hs:=target_info.sharedlibprefix+hs;
  378. end
  379. else if assigned(current_module.namespace) then
  380. begin
  381. { import_lib is used to specify the package name for the JVM
  382. target (= namespace) }
  383. if (target_info.system=system_jvm_java32) and
  384. assigned(current_module.namespace) then
  385. hs:=current_module.namespace^;
  386. { not sure how to deal with cppclass here, since namespaces
  387. mean something different there }
  388. end;
  389. if hs<>'' then
  390. begin
  391. { the JVM expects java/lang/Object rather than java.lang.Object }
  392. if target_info.system=system_jvm_java32 then
  393. Replace(hs,'.','/');
  394. od.import_lib:=stringdup(hs);
  395. end;
  396. include(od.objectoptions, oo_is_external);
  397. { check if we shall use another name for the class }
  398. if try_to_consume(_NAME) then
  399. od.objextname:=stringdup(get_stringconst)
  400. else
  401. od.objextname:=stringdup(od.objrealname^);
  402. include(od.objectoptions,oo_is_external);
  403. end
  404. else
  405. begin
  406. od.objextname:=stringdup(od.objrealname^);
  407. { ToDo for cpp: read/set the namespace of the class (influences the mangled name)
  408. (notice that for the JVM target, there is no difference between
  409. the namespace and import_lib) }
  410. if (target_info.system=system_jvm_java32) and
  411. assigned(current_module.namespace) then
  412. begin
  413. od.import_lib:=stringdup(current_module.namespace^);
  414. Replace(od.import_lib^,'.','/');
  415. end;
  416. end;
  417. end;
  418. procedure get_objc_class_or_protocol_external_status(od: tobjectdef);
  419. begin
  420. { Objective-C classes can be external -> all messages inside are
  421. external (defined at the class level instead of per method, so
  422. that you cannot define some methods as external and some not)
  423. }
  424. if try_to_consume(_EXTERNAL) then
  425. begin
  426. if try_to_consume(_NAME) then
  427. od.objextname:=stringdup(get_stringconst)
  428. else
  429. { the external name doesn't matter for formally declared
  430. classes, and allowing to specify one would mean that we would
  431. have to check it for consistency with the actual definition
  432. later on }
  433. od.objextname:=stringdup(od.objrealname^);
  434. include(od.objectoptions,oo_is_external);
  435. end
  436. else
  437. od.objextname:=stringdup(od.objrealname^);
  438. end;
  439. procedure parse_object_options;
  440. var
  441. gotexternal: boolean;
  442. begin
  443. case current_objectdef.objecttype of
  444. odt_object,odt_class,
  445. odt_javaclass:
  446. begin
  447. gotexternal:=false;
  448. while true do
  449. begin
  450. if try_to_consume(_ABSTRACT) then
  451. include(current_structdef.objectoptions,oo_is_abstract)
  452. else
  453. if try_to_consume(_SEALED) then
  454. include(current_structdef.objectoptions,oo_is_sealed)
  455. else if (current_objectdef.objecttype=odt_javaclass) and
  456. (token=_ID) and
  457. (idtoken=_EXTERNAL) then
  458. begin
  459. get_cpp_or_java_class_external_status(current_objectdef);
  460. gotexternal:=true;
  461. end
  462. else
  463. break;
  464. end;
  465. if [oo_is_abstract, oo_is_sealed] <= current_structdef.objectoptions then
  466. Message(parser_e_abstract_and_sealed_conflict);
  467. { set default external name in case of no external directive }
  468. if (current_objectdef.objecttype=odt_javaclass) and
  469. not gotexternal then
  470. get_cpp_or_java_class_external_status(current_objectdef)
  471. end;
  472. odt_cppclass,
  473. odt_interfacejava:
  474. get_cpp_or_java_class_external_status(current_objectdef);
  475. odt_objcclass,odt_objcprotocol,odt_objccategory:
  476. get_objc_class_or_protocol_external_status(current_objectdef);
  477. odt_helper: ; // nothing
  478. end;
  479. end;
  480. procedure parse_parent_classes;
  481. var
  482. intfchildof,
  483. childof : tobjectdef;
  484. hdef : tdef;
  485. hasparentdefined : boolean;
  486. begin
  487. childof:=nil;
  488. intfchildof:=nil;
  489. hasparentdefined:=false;
  490. { reads the parent class }
  491. if (token=_LKLAMMER) or
  492. is_objccategory(current_structdef) then
  493. begin
  494. consume(_LKLAMMER);
  495. { use single_type instead of id_type for specialize support }
  496. single_type(hdef,[stoAllowSpecialization, stoParseClassParent]);
  497. if (not assigned(hdef)) or
  498. (hdef.typ<>objectdef) then
  499. begin
  500. if assigned(hdef) then
  501. Message1(type_e_class_type_expected,hdef.typename)
  502. else if is_objccategory(current_structdef) then
  503. { a category must specify the class to extend }
  504. Message(type_e_objcclass_type_expected);
  505. end
  506. else
  507. begin
  508. childof:=tobjectdef(hdef);
  509. { a mix of class, interfaces, objects and cppclasses
  510. isn't allowed }
  511. case current_objectdef.objecttype of
  512. odt_class,
  513. odt_javaclass:
  514. if (childof.objecttype<>current_objectdef.objecttype) then
  515. begin
  516. if (is_interface(childof) and
  517. is_class(current_objectdef)) or
  518. (is_javainterface(childof) and
  519. is_javaclass(current_objectdef)) then
  520. begin
  521. { we insert the interface after the child
  522. is set, see below
  523. }
  524. intfchildof:=childof;
  525. childof:=class_tobject;
  526. end
  527. else
  528. Message(parser_e_mix_of_classes_and_objects);
  529. end
  530. else
  531. if oo_is_sealed in childof.objectoptions then
  532. Message1(parser_e_sealed_descendant,childof.typename)
  533. else
  534. childof:=find_real_class_definition(childof,true);
  535. odt_interfacecorba,
  536. odt_interfacecom:
  537. begin
  538. if not(is_interface(childof)) then
  539. Message(parser_e_mix_of_classes_and_objects);
  540. current_objectdef.objecttype:=childof.objecttype;
  541. end;
  542. odt_cppclass:
  543. if not(is_cppclass(childof)) then
  544. Message(parser_e_mix_of_classes_and_objects);
  545. odt_objcclass:
  546. if not(is_objcclass(childof) or
  547. is_objccategory(childof)) then
  548. begin
  549. if is_objcprotocol(childof) then
  550. begin
  551. if not(oo_is_classhelper in current_structdef.objectoptions) then
  552. begin
  553. intfchildof:=childof;
  554. childof:=nil;
  555. CGMessage(parser_h_no_objc_parent);
  556. end
  557. else
  558. { a category must specify the class to extend }
  559. CGMessage(type_e_objcclass_type_expected);
  560. end
  561. else
  562. Message(parser_e_mix_of_classes_and_objects);
  563. end
  564. else
  565. childof:=find_real_class_definition(childof,true);
  566. odt_objcprotocol:
  567. begin
  568. if not(is_objcprotocol(childof)) then
  569. Message(parser_e_mix_of_classes_and_objects);
  570. intfchildof:=childof;
  571. childof:=nil;
  572. end;
  573. odt_interfacejava:
  574. begin
  575. if not(is_javainterface(childof)) then
  576. Message(parser_e_mix_of_classes_and_objects);
  577. intfchildof:=find_real_class_definition(childof,true);
  578. childof:=nil;
  579. end;
  580. odt_object:
  581. if not(is_object(childof)) then
  582. Message(parser_e_mix_of_classes_and_objects)
  583. else
  584. if oo_is_sealed in childof.objectoptions then
  585. Message1(parser_e_sealed_descendant,childof.typename);
  586. odt_dispinterface:
  587. Message(parser_e_dispinterface_cant_have_parent);
  588. odt_helper:
  589. if not is_objectpascal_helper(childof) then
  590. begin
  591. Message(type_e_helper_type_expected);
  592. childof:=nil;
  593. end;
  594. end;
  595. end;
  596. hasparentdefined:=true;
  597. end;
  598. { if no parent class, then a class get tobject as parent }
  599. if not assigned(childof) then
  600. begin
  601. case current_objectdef.objecttype of
  602. odt_class:
  603. if current_objectdef<>class_tobject then
  604. childof:=class_tobject;
  605. odt_interfacecom:
  606. if current_objectdef<>interface_iunknown then
  607. childof:=interface_iunknown;
  608. odt_dispinterface:
  609. childof:=interface_idispatch;
  610. odt_objcclass:
  611. CGMessage(parser_h_no_objc_parent);
  612. odt_javaclass:
  613. { inherit from TObject by default for compatibility }
  614. if current_objectdef<>java_jlobject then
  615. childof:=class_tobject;
  616. end;
  617. end;
  618. if assigned(childof) then
  619. begin
  620. { Forbid not completly defined objects to be used as parents. This will
  621. also prevent circular loops of classes, because we set the forward flag
  622. at the start of the new definition and will reset it below after the
  623. parent has been set }
  624. if (oo_is_forward in childof.objectoptions) then
  625. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  626. else if not(oo_is_formal in childof.objectoptions) then
  627. current_objectdef.set_parent(childof)
  628. else
  629. Message1(sym_e_formal_class_not_resolved,childof.objrealname^);
  630. end;
  631. { remove forward flag, is resolved }
  632. exclude(current_structdef.objectoptions,oo_is_forward);
  633. if hasparentdefined then
  634. begin
  635. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  636. begin
  637. if assigned(intfchildof) then
  638. if current_objectdef.objecttype=odt_class then
  639. handleImplementedInterface(intfchildof)
  640. else
  641. handleImplementedProtocolOrJavaIntf(intfchildof);
  642. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  643. end;
  644. consume(_RKLAMMER);
  645. end;
  646. end;
  647. procedure parse_extended_type(helpertype:thelpertype);
  648. var
  649. hdef: tdef;
  650. begin
  651. if not is_objectpascal_helper(current_structdef) then
  652. Internalerror(2011021103);
  653. if helpertype=ht_none then
  654. Internalerror(2011021001);
  655. consume(_FOR);
  656. single_type(hdef,[stoParseClassParent]);
  657. if (not assigned(hdef)) or
  658. not (hdef.typ in [objectdef,recorddef]) then
  659. begin
  660. if helpertype=ht_class then
  661. Message1(type_e_class_type_expected,hdef.typename)
  662. else
  663. if helpertype=ht_record then
  664. Message1(type_e_record_type_expected,hdef.typename);
  665. end
  666. else
  667. begin
  668. case helpertype of
  669. ht_class:
  670. begin
  671. if not is_class(hdef) then
  672. Message1(type_e_class_type_expected,hdef.typename);
  673. { a class helper must extend the same class or a subclass
  674. of the class extended by the parent class helper }
  675. if assigned(current_objectdef.childof) then
  676. begin
  677. if not is_class(current_objectdef.childof.extendeddef) then
  678. Internalerror(2011021101);
  679. if not hdef.is_related(current_objectdef.childof.extendeddef) then
  680. Message1(type_e_class_helper_must_extend_subclass,current_objectdef.childof.extendeddef.typename);
  681. end;
  682. end;
  683. ht_record:
  684. begin
  685. if not is_record(hdef) then
  686. Message1(type_e_record_type_expected,hdef.typename);
  687. { a record helper must extend the same record as the
  688. parent helper }
  689. if assigned(current_objectdef.childof) then
  690. begin
  691. if not is_record(current_objectdef.childof.extendeddef) then
  692. Internalerror(2011021102);
  693. if hdef<>current_objectdef.childof.extendeddef then
  694. Message1(type_e_record_helper_must_extend_same_record,current_objectdef.childof.extendeddef.typename);
  695. end;
  696. end;
  697. end;
  698. current_objectdef.extendeddef:=tabstractrecorddef(hdef);
  699. end;
  700. end;
  701. procedure parse_guid;
  702. begin
  703. { read GUID }
  704. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  705. try_to_consume(_LECKKLAMMER) then
  706. begin
  707. readinterfaceiid;
  708. consume(_RECKKLAMMER);
  709. end
  710. else if (current_objectdef.objecttype=odt_dispinterface) then
  711. message(parser_e_dispinterface_needs_a_guid);
  712. end;
  713. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean): tprocdef;
  714. procedure chkobjc(pd: tprocdef);
  715. begin
  716. if is_objc_class_or_protocol(pd.struct) then
  717. begin
  718. include(pd.procoptions,po_objc);
  719. end;
  720. end;
  721. procedure chkjava(pd: tprocdef);
  722. begin
  723. if is_java_class_or_interface(pd.struct) then
  724. begin
  725. { mark all non-virtual instance methods as "virtual; final;",
  726. because
  727. a) that's the only way to guarantee "non-virtual" behaviour
  728. (other than making them class methods with an explicit self
  729. pointer, but that causes problems with interface mappings
  730. and procvars)
  731. b) if we don't mark them virtual, they don't get added to the
  732. vmt and we can't check whether child classes try to override
  733. them
  734. }
  735. if is_javaclass(pd.struct) and
  736. not(po_virtualmethod in pd.procoptions) and
  737. not(po_classmethod in pd.procoptions) then
  738. begin
  739. include(pd.procoptions,po_virtualmethod);
  740. include(pd.procoptions,po_finalmethod);
  741. include(pd.procoptions,po_java_nonvirtual);
  742. end;
  743. end;
  744. end;
  745. procedure chkcpp(pd:tprocdef);
  746. begin
  747. { nothing currently }
  748. end;
  749. var
  750. oldparse_only: boolean;
  751. begin
  752. case token of
  753. _PROCEDURE,
  754. _FUNCTION:
  755. begin
  756. if (astruct.symtable.currentvisibility=vis_published) and
  757. not(oo_can_have_published in astruct.objectoptions) then
  758. Message(parser_e_cant_have_published);
  759. oldparse_only:=parse_only;
  760. parse_only:=true;
  761. result:=parse_proc_dec(is_classdef,astruct);
  762. { this is for error recovery as well as forward }
  763. { interface mappings, i.e. mapping to a method }
  764. { which isn't declared yet }
  765. if assigned(result) then
  766. begin
  767. parse_object_proc_directives(result);
  768. { check if dispid is set }
  769. if is_dispinterface(result.struct) and not (po_dispid in result.procoptions) then
  770. begin
  771. result.dispid:=tobjectdef(result.struct).get_next_dispid;
  772. include(result.procoptions, po_dispid);
  773. end;
  774. { all Macintosh Object Pascal methods are virtual. }
  775. { this can't be a class method, because macpas mode }
  776. { has no m_class }
  777. if (m_mac in current_settings.modeswitches) then
  778. include(result.procoptions,po_virtualmethod);
  779. { for record helpers only static class methods are allowed }
  780. if is_objectpascal_helper(astruct) and
  781. is_record(tobjectdef(astruct).extendeddef) and
  782. is_classdef and not (po_staticmethod in result.procoptions) then
  783. MessagePos(result.fileinfo,parser_e_class_methods_only_static_in_records);
  784. handle_calling_convention(result);
  785. { add definition to procsym }
  786. proc_add_definition(result);
  787. { add procdef options to objectdef options }
  788. if (po_msgint in result.procoptions) then
  789. include(astruct.objectoptions,oo_has_msgint);
  790. if (po_msgstr in result.procoptions) then
  791. include(astruct.objectoptions,oo_has_msgstr);
  792. if (po_virtualmethod in result.procoptions) then
  793. include(astruct.objectoptions,oo_has_virtual);
  794. chkcpp(result);
  795. chkobjc(result);
  796. chkjava(result);
  797. end;
  798. maybe_parse_hint_directives(result);
  799. parse_only:=oldparse_only;
  800. end;
  801. _CONSTRUCTOR :
  802. begin
  803. if (astruct.symtable.currentvisibility=vis_published) and
  804. not(oo_can_have_published in astruct.objectoptions) then
  805. Message(parser_e_cant_have_published);
  806. if not is_classdef and not(astruct.symtable.currentvisibility in [vis_public,vis_published]) then
  807. Message(parser_w_constructor_should_be_public);
  808. if is_interface(astruct) then
  809. Message(parser_e_no_con_des_in_interfaces);
  810. { Objective-C does not know the concept of a constructor }
  811. if is_objc_class_or_protocol(astruct) then
  812. Message(parser_e_objc_no_constructor_destructor);
  813. if is_objectpascal_helper(astruct) then
  814. if is_classdef then
  815. { class constructors are not allowed in class helpers }
  816. Message(parser_e_no_class_constructor_in_helpers)
  817. else if is_record(tobjectdef(astruct).extendeddef) then
  818. { as long as constructors aren't allowed in records they
  819. aren't allowed in helpers either }
  820. Message(parser_e_no_constructor_in_records);
  821. { only 1 class constructor is allowed }
  822. if is_classdef and (oo_has_class_constructor in astruct.objectoptions) then
  823. Message1(parser_e_only_one_class_constructor_allowed, astruct.objrealname^);
  824. oldparse_only:=parse_only;
  825. parse_only:=true;
  826. if is_classdef then
  827. result:=class_constructor_head(current_structdef)
  828. else
  829. result:=constructor_head;
  830. chkcpp(result);
  831. parse_only:=oldparse_only;
  832. end;
  833. _DESTRUCTOR :
  834. begin
  835. if (astruct.symtable.currentvisibility=vis_published) and
  836. not(oo_can_have_published in astruct.objectoptions) then
  837. Message(parser_e_cant_have_published);
  838. if not is_classdef then
  839. if (oo_has_destructor in astruct.objectoptions) then
  840. Message(parser_n_only_one_destructor);
  841. if is_interface(astruct) then
  842. Message(parser_e_no_con_des_in_interfaces);
  843. { (class) destructors are not allowed in class helpers }
  844. if is_objectpascal_helper(astruct) then
  845. Message(parser_e_no_destructor_in_records);
  846. if not is_classdef and (astruct.symtable.currentvisibility<>vis_public) then
  847. Message(parser_w_destructor_should_be_public);
  848. { Objective-C does not know the concept of a destructor }
  849. if is_objc_class_or_protocol(astruct) then
  850. Message(parser_e_objc_no_constructor_destructor);
  851. { only 1 class destructor is allowed }
  852. if is_classdef and (oo_has_class_destructor in astruct.objectoptions) then
  853. Message1(parser_e_only_one_class_destructor_allowed, astruct.objrealname^);
  854. oldparse_only:=parse_only;
  855. parse_only:=true;
  856. if is_classdef then
  857. result:=class_destructor_head(current_structdef)
  858. else
  859. result:=destructor_head;
  860. chkcpp(result);
  861. parse_only:=oldparse_only;
  862. end;
  863. else
  864. internalerror(2011032102);
  865. end;
  866. end;
  867. procedure parse_object_members;
  868. var
  869. typedconstswritable: boolean;
  870. object_member_blocktype : tblock_type;
  871. fields_allowed, is_classdef, class_fields, is_final, final_fields: boolean;
  872. vdoptions: tvar_dec_options;
  873. procedure parse_const;
  874. begin
  875. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  876. Message(parser_e_type_var_const_only_in_records_and_classes);
  877. consume(_CONST);
  878. object_member_blocktype:=bt_const;
  879. final_fields:=is_final;
  880. is_final:=false;
  881. end;
  882. procedure parse_var;
  883. begin
  884. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass]) and
  885. { Java interfaces can contain static final class vars }
  886. not((current_objectdef.objecttype=odt_interfacejava) and
  887. is_final and is_classdef) then
  888. Message(parser_e_type_var_const_only_in_records_and_classes);
  889. consume(_VAR);
  890. fields_allowed:=true;
  891. object_member_blocktype:=bt_general;
  892. class_fields:=is_classdef;
  893. final_fields:=is_final;
  894. is_classdef:=false;
  895. is_final:=false;
  896. end;
  897. procedure parse_class;
  898. begin
  899. is_classdef:=false;
  900. { read class method/field/property }
  901. consume(_CLASS);
  902. { class modifier is only allowed for procedures, functions, }
  903. { constructors, destructors, fields and properties }
  904. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  905. Message(parser_e_procedure_or_function_expected);
  906. { Java interfaces can contain final class vars }
  907. if is_interface(current_structdef) or
  908. (is_javainterface(current_structdef) and
  909. (not(is_final) or
  910. (token<>_VAR))) then
  911. Message(parser_e_no_static_method_in_interfaces)
  912. else
  913. { class methods are also allowed for Objective-C protocols }
  914. is_classdef:=true;
  915. end;
  916. procedure parse_visibility(vis: tvisibility; oo: tobjectoption);
  917. begin
  918. { Objective-C and Java classes do not support "published",
  919. as basically everything is published. }
  920. if (vis=vis_published) and
  921. (is_objc_class_or_protocol(current_structdef) or
  922. is_java_class_or_interface(current_structdef)) then
  923. Message(parser_e_no_objc_published)
  924. else if is_interface(current_structdef) or
  925. is_objc_protocol_or_category(current_structdef) or
  926. is_javainterface(current_structdef) then
  927. Message(parser_e_no_access_specifier_in_interfaces);
  928. current_structdef.symtable.currentvisibility:=vis;
  929. consume(token);
  930. if (oo<>oo_none) then
  931. include(current_structdef.objectoptions,oo);
  932. fields_allowed:=true;
  933. is_classdef:=false;
  934. class_fields:=false;
  935. is_final:=false;
  936. object_member_blocktype:=bt_general;
  937. end;
  938. begin
  939. { empty class declaration ? }
  940. if (current_objectdef.objecttype in [odt_class,odt_objcclass,odt_javaclass]) and
  941. (token=_SEMICOLON) then
  942. exit;
  943. { in "publishable" classes the default access type is published }
  944. if (oo_can_have_published in current_structdef.objectoptions) then
  945. current_structdef.symtable.currentvisibility:=vis_published
  946. else
  947. current_structdef.symtable.currentvisibility:=vis_public;
  948. fields_allowed:=true;
  949. is_classdef:=false;
  950. class_fields:=false;
  951. is_final:=false;
  952. final_fields:=false;
  953. object_member_blocktype:=bt_general;
  954. repeat
  955. case token of
  956. _TYPE :
  957. begin
  958. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  959. Message(parser_e_type_var_const_only_in_records_and_classes);
  960. consume(_TYPE);
  961. object_member_blocktype:=bt_type;
  962. end;
  963. _VAR :
  964. begin
  965. parse_var;
  966. end;
  967. _CONST:
  968. begin
  969. parse_const
  970. end;
  971. _ID :
  972. begin
  973. if is_objcprotocol(current_structdef) and
  974. ((idtoken=_REQUIRED) or
  975. (idtoken=_OPTIONAL)) then
  976. begin
  977. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  978. consume(idtoken)
  979. end
  980. else case idtoken of
  981. _PRIVATE :
  982. begin
  983. parse_visibility(vis_private,oo_has_private);
  984. end;
  985. _PROTECTED :
  986. begin
  987. parse_visibility(vis_protected,oo_has_protected);
  988. end;
  989. _PUBLIC :
  990. begin
  991. parse_visibility(vis_public,oo_none);
  992. end;
  993. _PUBLISHED :
  994. begin
  995. parse_visibility(vis_published,oo_none);
  996. end;
  997. _STRICT :
  998. begin
  999. if is_interface(current_structdef) or
  1000. is_objc_protocol_or_category(current_structdef) or
  1001. is_javainterface(current_structdef) then
  1002. Message(parser_e_no_access_specifier_in_interfaces);
  1003. consume(_STRICT);
  1004. if token=_ID then
  1005. begin
  1006. case idtoken of
  1007. _PRIVATE:
  1008. begin
  1009. consume(_PRIVATE);
  1010. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  1011. include(current_structdef.objectoptions,oo_has_strictprivate);
  1012. end;
  1013. _PROTECTED:
  1014. begin
  1015. consume(_PROTECTED);
  1016. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  1017. include(current_structdef.objectoptions,oo_has_strictprotected);
  1018. end;
  1019. else
  1020. message(parser_e_protected_or_private_expected);
  1021. end;
  1022. end
  1023. else
  1024. message(parser_e_protected_or_private_expected);
  1025. fields_allowed:=true;
  1026. is_classdef:=false;
  1027. class_fields:=false;
  1028. is_final:=false;
  1029. final_fields:=false;
  1030. object_member_blocktype:=bt_general;
  1031. end
  1032. else if (m_final_fields in current_settings.modeswitches) and
  1033. (token=_ID) and
  1034. (idtoken=_FINAL) then
  1035. begin
  1036. { currently only supported for external classes, because
  1037. requires fully working DFA otherwise }
  1038. if (current_structdef.typ<>objectdef) or
  1039. not(oo_is_external in tobjectdef(current_structdef).objectoptions) then
  1040. Message(parser_e_final_only_external);
  1041. consume(_final);
  1042. is_final:=true;
  1043. if token=_CLASS then
  1044. parse_class;
  1045. if not(token in [_CONST,_VAR]) then
  1046. message(parser_e_final_only_const_var);
  1047. end
  1048. else
  1049. begin
  1050. if object_member_blocktype=bt_general then
  1051. begin
  1052. if is_interface(current_structdef) or
  1053. is_objc_protocol_or_category(current_structdef) or
  1054. is_objectpascal_helper(current_structdef) or
  1055. (is_javainterface(current_structdef) and
  1056. not(class_fields and final_fields)) then
  1057. Message(parser_e_no_vars_in_interfaces);
  1058. if (current_structdef.symtable.currentvisibility=vis_published) and
  1059. not(oo_can_have_published in current_structdef.objectoptions) then
  1060. Message(parser_e_cant_have_published);
  1061. if (not fields_allowed) then
  1062. Message(parser_e_field_not_allowed_here);
  1063. vdoptions:=[vd_object];
  1064. if class_fields then
  1065. include(vdoptions,vd_class);
  1066. if final_fields then
  1067. include(vdoptions,vd_final);
  1068. read_record_fields(vdoptions);
  1069. end
  1070. else if object_member_blocktype=bt_type then
  1071. types_dec(true)
  1072. else if object_member_blocktype=bt_const then
  1073. begin
  1074. if final_fields then
  1075. begin
  1076. { the value of final fields cannot be changed
  1077. once they've been assigned a value }
  1078. typedconstswritable:=cs_typed_const_writable in current_settings.localswitches;
  1079. exclude(current_settings.localswitches,cs_typed_const_writable);
  1080. end;
  1081. consts_dec(true,not is_javainterface(current_structdef));
  1082. if final_fields and
  1083. typedconstswritable then
  1084. include(current_settings.localswitches,cs_typed_const_writable);
  1085. end
  1086. else
  1087. internalerror(201001110);
  1088. end;
  1089. end;
  1090. end;
  1091. _PROPERTY :
  1092. begin
  1093. struct_property_dec(is_classdef);
  1094. fields_allowed:=false;
  1095. is_classdef:=false;
  1096. end;
  1097. _CLASS:
  1098. begin
  1099. parse_class;
  1100. end;
  1101. _PROCEDURE,
  1102. _FUNCTION,
  1103. _CONSTRUCTOR,
  1104. _DESTRUCTOR :
  1105. begin
  1106. method_dec(current_structdef,is_classdef);
  1107. fields_allowed:=false;
  1108. is_classdef:=false;
  1109. end;
  1110. _END :
  1111. begin
  1112. consume(_END);
  1113. break;
  1114. end;
  1115. else
  1116. consume(_ID); { Give a ident expected message, like tp7 }
  1117. end;
  1118. until false;
  1119. end;
  1120. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  1121. var
  1122. old_current_structdef: tabstractrecorddef;
  1123. old_current_genericdef,
  1124. old_current_specializedef: tstoreddef;
  1125. hrecst: trecordsymtable;
  1126. fsym: tfieldvarsym;
  1127. old_parse_generic: boolean;
  1128. list: TFPObjectList;
  1129. s: String;
  1130. st: TSymtable;
  1131. begin
  1132. old_current_structdef:=current_structdef;
  1133. old_current_genericdef:=current_genericdef;
  1134. old_current_specializedef:=current_specializedef;
  1135. old_parse_generic:=parse_generic;
  1136. current_structdef:=nil;
  1137. current_genericdef:=nil;
  1138. current_specializedef:=nil;
  1139. { objects and class types can't be declared local }
  1140. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1141. not assigned(genericlist) then
  1142. Message(parser_e_no_local_objects);
  1143. { reuse forward objectdef? }
  1144. if assigned(fd) then
  1145. begin
  1146. if fd.objecttype<>objecttype then
  1147. begin
  1148. Message(parser_e_forward_mismatch);
  1149. { recover }
  1150. current_structdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  1151. include(current_structdef.objectoptions,oo_is_forward);
  1152. end
  1153. else
  1154. current_structdef:=fd
  1155. end
  1156. else
  1157. begin
  1158. { anonym objects aren't allow (o : object a : longint; end;) }
  1159. if n='' then
  1160. Message(parser_f_no_anonym_objects);
  1161. { create new class }
  1162. current_structdef:=tobjectdef.create(objecttype,n,nil);
  1163. { include always the forward flag, it'll be removed after the parent class have been
  1164. added. This is to prevent circular childof loops }
  1165. include(current_structdef.objectoptions,oo_is_forward);
  1166. if (cs_compilesystem in current_settings.moduleswitches) then
  1167. begin
  1168. case current_objectdef.objecttype of
  1169. odt_interfacecom :
  1170. if (current_structdef.objname^='IUNKNOWN') then
  1171. interface_iunknown:=current_objectdef
  1172. else
  1173. if (current_structdef.objname^='IDISPATCH') then
  1174. interface_idispatch:=current_objectdef;
  1175. odt_class :
  1176. if (current_structdef.objname^='TOBJECT') then
  1177. class_tobject:=current_objectdef;
  1178. odt_javaclass:
  1179. begin
  1180. if (current_structdef.objname^='TOBJECT') then
  1181. class_tobject:=current_objectdef
  1182. else if (current_objectdef.objname^='JLOBJECT') then
  1183. begin
  1184. java_jlobject:=current_objectdef;
  1185. { the methodpointer type is normally created in
  1186. psystem, but java_jlobject is not yet available
  1187. there... }
  1188. hrecst:=trecordsymtable.create('',1);
  1189. fsym:=tfieldvarsym.create('$proc',vs_value,java_jlobject,[]);
  1190. hrecst.insert(fsym);
  1191. hrecst.addfield(fsym,vis_hidden);
  1192. fsym:=tfieldvarsym.create('$data',vs_value,java_jlobject,[]);
  1193. hrecst.insert(fsym);
  1194. hrecst.addfield(fsym,vis_hidden);
  1195. methodpointertype:=trecorddef.create('',hrecst);
  1196. systemunit.insert(ttypesym.create('$methodpointer',methodpointertype));
  1197. end
  1198. else if (current_objectdef.objname^='JLTHROWABLE') then
  1199. java_jlthrowable:=current_objectdef
  1200. else if (current_objectdef.objname^='FPCBASERECORDTYPE') then
  1201. java_fpcbaserecordtype:=current_objectdef
  1202. else if (current_objectdef.objname^='JLSTRING') then
  1203. java_jlstring:=current_objectdef
  1204. else if (current_objectdef.objname^='ANSISTRINGCLASS') then
  1205. java_ansistring:=current_objectdef
  1206. else if (current_objectdef.objname^='SHORTSTRINGCLASS') then
  1207. java_shortstring:=current_objectdef
  1208. else if (current_objectdef.objname^='JLENUM') then
  1209. java_jlenum:=current_objectdef
  1210. else if (current_objectdef.objname^='JUENUMSET') then
  1211. java_juenumset:=current_objectdef
  1212. else if (current_objectdef.objname^='FPCBITSET') then
  1213. java_jubitset:=current_objectdef
  1214. else if (current_objectdef.objname^='FPCBASEPROCVARTYPE') then
  1215. java_procvarbase:=current_objectdef;
  1216. end;
  1217. end;
  1218. end;
  1219. if (current_module.modulename^='OBJCBASE') then
  1220. begin
  1221. case current_objectdef.objecttype of
  1222. odt_objcclass:
  1223. if (current_objectdef.objname^='Protocol') then
  1224. objc_protocoltype:=current_objectdef;
  1225. end;
  1226. end;
  1227. end;
  1228. { usage of specialized type inside its generic template }
  1229. if assigned(genericdef) then
  1230. current_specializedef:=current_structdef
  1231. { reject declaration of generic class inside generic class }
  1232. else if assigned(genericlist) then
  1233. current_genericdef:=current_structdef;
  1234. { set published flag in $M+ mode, it can also be inherited and will
  1235. be added when the parent class set with tobjectdef.set_parent (PFV) }
  1236. if (cs_generate_rtti in current_settings.localswitches) and
  1237. (current_objectdef.objecttype in [odt_interfacecom,odt_class,odt_helper]) then
  1238. include(current_structdef.objectoptions,oo_can_have_published);
  1239. { Objective-C/Java objectdefs can be "formal definitions", in which case
  1240. the syntax is "type tc = objcclass external;" -> we have to parse
  1241. its object options (external) already here, to make sure that such
  1242. definitions are recognised as formal defs }
  1243. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  1244. parse_object_options;
  1245. { forward def? }
  1246. if not assigned(fd) and
  1247. (token=_SEMICOLON) then
  1248. begin
  1249. { add to the list of definitions to check that the forward
  1250. is resolved. this is required for delphi mode }
  1251. current_module.checkforwarddefs.add(current_structdef);
  1252. end
  1253. else
  1254. begin
  1255. { change objccategories into objcclass helpers }
  1256. if (objecttype=odt_objccategory) then
  1257. begin
  1258. current_objectdef.objecttype:=odt_objcclass;
  1259. include(current_structdef.objectoptions,oo_is_classhelper);
  1260. end;
  1261. { include the class helper flag for Object Pascal helpers }
  1262. if (objecttype=odt_helper) then
  1263. include(current_objectdef.objectoptions,oo_is_classhelper);
  1264. { parse list of options (abstract / sealed) }
  1265. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava]) then
  1266. parse_object_options;
  1267. symtablestack.push(current_structdef.symtable);
  1268. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  1269. parse_generic:=(df_generic in current_structdef.defoptions);
  1270. { parse list of parent classes }
  1271. { for record helpers in mode Delphi this is not allowed }
  1272. if not (is_objectpascal_helper(current_objectdef) and
  1273. (m_delphi in current_settings.modeswitches) and
  1274. (helpertype=ht_record)) then
  1275. parse_parent_classes
  1276. else
  1277. { remove forward flag, is resolved (this is normally done inside
  1278. parse_parent_classes) }
  1279. exclude(current_structdef.objectoptions,oo_is_forward);
  1280. { parse extended type for helpers }
  1281. if is_objectpascal_helper(current_structdef) then
  1282. parse_extended_type(helpertype);
  1283. { parse optional GUID for interfaces }
  1284. parse_guid;
  1285. { parse and insert object members }
  1286. parse_object_members;
  1287. if not(oo_is_external in current_structdef.objectoptions) then
  1288. begin
  1289. { In Java, constructors are not automatically inherited (so you can
  1290. hide them). Emulate the Pascal behaviour for classes implemented
  1291. in Pascal (we cannot do it for classes implemented in Java, since
  1292. we obviously cannot add constructors to those) }
  1293. if is_javaclass(current_structdef) then
  1294. begin
  1295. add_missing_parent_constructors_intf(tobjectdef(current_structdef),vis_none);
  1296. {$ifdef jvm}
  1297. maybe_add_public_default_java_constructor(tobjectdef(current_structdef));
  1298. {$endif}
  1299. end;
  1300. { need method to hold the initialization code for typed constants? }
  1301. if (target_info.system in systems_typed_constants_node_init) and
  1302. not is_any_interface_kind(current_structdef) then
  1303. add_typedconst_init_routine(current_structdef);
  1304. end;
  1305. symtablestack.pop(current_structdef.symtable);
  1306. end;
  1307. { generate vmt space if needed }
  1308. if not(oo_has_vmt in current_structdef.objectoptions) and
  1309. not(oo_is_forward in current_structdef.objectoptions) and
  1310. (
  1311. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1312. (current_objectdef.objecttype in [odt_class])
  1313. ) then
  1314. current_objectdef.insertvmt;
  1315. { for implemented classes with a vmt check if there is a constructor }
  1316. if (oo_has_vmt in current_structdef.objectoptions) and
  1317. not(oo_is_forward in current_structdef.objectoptions) and
  1318. not(oo_has_constructor in current_structdef.objectoptions) and
  1319. not is_objc_class_or_protocol(current_structdef) and
  1320. not is_java_class_or_interface(current_structdef) then
  1321. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1322. if is_interface(current_structdef) or
  1323. is_objcprotocol(current_structdef) or
  1324. is_javainterface(current_structdef) then
  1325. setinterfacemethodoptions
  1326. else if is_objcclass(current_structdef) then
  1327. setobjcclassmethodoptions;
  1328. { if this helper is defined in the implementation section of the unit
  1329. or inside the main project file, the extendeddefs list of the current
  1330. module must be updated (it will be removed when poping the symtable) }
  1331. if is_objectpascal_helper(current_structdef) then
  1332. begin
  1333. { the topmost symtable must be a static symtable }
  1334. st:=current_structdef.owner;
  1335. while st.symtabletype in [objectsymtable,recordsymtable] do
  1336. st:=st.defowner.owner;
  1337. if st.symtabletype=staticsymtable then
  1338. begin
  1339. s:=make_mangledname('',current_objectdef.extendeddef.symtable,'');
  1340. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1341. if not assigned(list) then
  1342. begin
  1343. list:=TFPObjectList.Create(false);
  1344. current_module.extendeddefs.Add(s, list);
  1345. end;
  1346. list.add(current_structdef);
  1347. end;
  1348. end;
  1349. tabstractrecordsymtable(current_objectdef.symtable).addalignmentpadding;
  1350. { return defined objectdef }
  1351. result:=current_objectdef;
  1352. { restore old state }
  1353. current_structdef:=old_current_structdef;
  1354. current_genericdef:=old_current_genericdef;
  1355. current_specializedef:=old_current_specializedef;
  1356. parse_generic:=old_parse_generic;
  1357. end;
  1358. end.