pdecobj.pas 58 KB

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