pdecobj.pas 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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;objsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  25. { parses a (class) method declaration }
  26. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric: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;var rtti_attrs_def: trtti_attribute_list);
  32. implementation
  33. uses
  34. sysutils,cutils,
  35. globals,verbose,systems,tokens,
  36. symbase,symsym,symtable,symcreat,defcmp,
  37. node,ncon,
  38. fmodule,scanner,
  39. pbase,pexpr,pdecsub,pdecvar,ptype,pdecl,pgenutil,pparautl,ppu
  40. {$ifdef jvm}
  41. ,jvmdef,pjvm;
  42. {$else}
  43. ;
  44. {$endif}
  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. begin
  57. parse_record_proc_directives(pd);
  58. end;
  59. objectdef:
  60. begin
  61. parse_object_proc_directives(pd);
  62. end
  63. else
  64. internalerror(2011040502);
  65. end;
  66. // We can't add hidden params here because record is not yet defined
  67. // and therefore record size which has influence on parameter passing rules may change too
  68. // look at record_dec to see where calling conventions are applied (issue #0021044).
  69. // The same goes for objects/classes due to the calling convention that may only be set
  70. // later (mantis #35233).
  71. handle_calling_convention(pd,hcc_default_actions_intf_struct);
  72. { add definition to procsym }
  73. proc_add_definition(pd);
  74. { add procdef options to objectdef options }
  75. if (po_virtualmethod in pd.procoptions) then
  76. include(astruct.objectoptions,oo_has_virtual);
  77. maybe_parse_hint_directives(pd);
  78. end;
  79. function class_constructor_head(astruct: tabstractrecorddef):tprocdef;
  80. var
  81. pd : tprocdef;
  82. begin
  83. result:=nil;
  84. consume(_CONSTRUCTOR);
  85. { must be at same level as in implementation }
  86. parse_proc_head(current_structdef,potype_class_constructor,[],nil,nil,pd);
  87. if not assigned(pd) then
  88. begin
  89. consume(_SEMICOLON);
  90. exit;
  91. end;
  92. pd.calcparas;
  93. if (pd.maxparacount>0) then
  94. Message(parser_e_no_paras_for_class_constructor);
  95. consume(_SEMICOLON);
  96. include(astruct.objectoptions,oo_has_class_constructor);
  97. include(current_module.moduleflags,mf_classinits);
  98. { no return value }
  99. pd.returndef:=voidtype;
  100. constr_destr_finish_head(pd,astruct);
  101. result:=pd;
  102. end;
  103. function constructor_head:tprocdef;
  104. var
  105. pd : tprocdef;
  106. begin
  107. result:=nil;
  108. consume(_CONSTRUCTOR);
  109. { must be at same level as in implementation }
  110. parse_proc_head(current_structdef,potype_constructor,[],nil,nil,pd);
  111. if not assigned(pd) then
  112. begin
  113. consume(_SEMICOLON);
  114. exit;
  115. end;
  116. if (cs_constructor_name in current_settings.globalswitches) and
  117. (pd.procsym.name<>'INIT') then
  118. Message(parser_e_constructorname_must_be_init);
  119. consume(_SEMICOLON);
  120. include(current_structdef.objectoptions,oo_has_constructor);
  121. { Set return type, class and record constructors return the
  122. created instance, helper types return the extended type,
  123. object constructors return boolean }
  124. if is_class(pd.struct) or
  125. is_record(pd.struct) or
  126. is_javaclass(pd.struct) then
  127. pd.returndef:=pd.struct
  128. else
  129. if is_objectpascal_helper(pd.struct) then
  130. pd.returndef:=tobjectdef(pd.struct).extendeddef
  131. else
  132. {$ifdef CPU64bitaddr}
  133. pd.returndef:=bool64type;
  134. {$else CPU64bitaddr}
  135. pd.returndef:=bool32type;
  136. {$endif CPU64bitaddr}
  137. constr_destr_finish_head(pd,pd.struct);
  138. result:=pd;
  139. end;
  140. procedure struct_property_dec(is_classproperty:boolean;var rtti_attrs_def: trtti_attribute_list);
  141. var
  142. p : tpropertysym;
  143. _deprecatedmsg: pshortstring;
  144. _symoptions: tsymoptions;
  145. begin
  146. { check for a class, record or helper }
  147. if not((is_class_or_interface_or_dispinterface(current_structdef) or is_record(current_structdef) or
  148. is_objectpascal_helper(current_structdef) or is_java_class_or_interface(current_structdef)) or
  149. (not(m_tp7 in current_settings.modeswitches) and (is_object(current_structdef)))) then
  150. Message(parser_e_syntax_error);
  151. consume(_PROPERTY);
  152. p:=read_property_dec(is_classproperty,current_structdef);
  153. consume(_SEMICOLON);
  154. if try_to_consume(_DEFAULT) then
  155. begin
  156. if oo_has_default_property in current_structdef.objectoptions then
  157. message(parser_e_only_one_default_property);
  158. include(current_structdef.objectoptions,oo_has_default_property);
  159. include(p.propoptions,ppo_defaultproperty);
  160. if not(ppo_hasparameters in p.propoptions) then
  161. message(parser_e_property_need_paras);
  162. if (token=_COLON) then
  163. begin
  164. Message(parser_e_field_not_allowed_here);
  165. consume_all_until(_SEMICOLON);
  166. end;
  167. consume(_SEMICOLON);
  168. end;
  169. { parse possible enumerator modifier }
  170. if try_to_consume(_ENUMERATOR) then
  171. begin
  172. if (token = _ID) then
  173. begin
  174. if pattern='CURRENT' then
  175. begin
  176. if oo_has_enumerator_current in current_structdef.objectoptions then
  177. message(parser_e_only_one_enumerator_current);
  178. if not p.propaccesslist[palt_read].empty then
  179. begin
  180. include(current_structdef.objectoptions,oo_has_enumerator_current);
  181. include(p.propoptions,ppo_enumerator_current);
  182. end
  183. else
  184. Message(parser_e_enumerator_current_is_not_valid) // property has no reader
  185. end
  186. else
  187. Message1(parser_e_invalid_enumerator_identifier, pattern);
  188. consume(token);
  189. end
  190. else
  191. Message(parser_e_enumerator_identifier_required);
  192. consume(_SEMICOLON);
  193. end;
  194. { in case of a previous error, p might not be assigned }
  195. if assigned(p) then
  196. begin
  197. trtti_attribute_list.bind(rtti_attrs_def,p.rtti_attribute_list);
  198. { hint directives, these can be separated by semicolons here,
  199. that needs to be handled here with a loop (PFV) }
  200. while try_consume_hintdirective(p.symoptions,p.deprecatedmsg) do
  201. Consume(_SEMICOLON);
  202. end
  203. else
  204. begin
  205. { recover from error so we can continue to parse }
  206. { hint directives, these can be separated by semicolons here,
  207. that needs to be handled here with a loop (PFV) }
  208. while try_consume_hintdirective(_symoptions,_deprecatedmsg) do
  209. Consume(_SEMICOLON);
  210. end;
  211. end;
  212. function class_destructor_head(astruct: tabstractrecorddef):tprocdef;
  213. var
  214. pd : tprocdef;
  215. begin
  216. result:=nil;
  217. consume(_DESTRUCTOR);
  218. parse_proc_head(current_structdef,potype_class_destructor,[],nil,nil,pd);
  219. if not assigned(pd) then
  220. begin
  221. consume(_SEMICOLON);
  222. exit;
  223. end;
  224. pd.calcparas;
  225. if (pd.maxparacount>0) then
  226. Message(parser_e_no_paras_for_class_destructor);
  227. consume(_SEMICOLON);
  228. include(astruct.objectoptions,oo_has_class_destructor);
  229. include(current_module.moduleflags,mf_classinits);
  230. { no return value }
  231. pd.returndef:=voidtype;
  232. constr_destr_finish_head(pd,astruct);
  233. result:=pd;
  234. end;
  235. function destructor_head:tprocdef;
  236. var
  237. pd : tprocdef;
  238. begin
  239. result:=nil;
  240. consume(_DESTRUCTOR);
  241. parse_proc_head(current_structdef,potype_destructor,[],nil,nil,pd);
  242. if not assigned(pd) then
  243. begin
  244. consume(_SEMICOLON);
  245. exit;
  246. end;
  247. if (cs_constructor_name in current_settings.globalswitches) and
  248. (pd.procsym.name<>'DONE') then
  249. Message(parser_e_destructorname_must_be_done);
  250. pd.calcparas;
  251. if not(pd.maxparacount=0) and
  252. (m_fpc in current_settings.modeswitches) then
  253. Message(parser_e_no_paras_for_destructor);
  254. consume(_SEMICOLON);
  255. include(current_structdef.objectoptions,oo_has_destructor);
  256. include(current_structdef.objectoptions,oo_has_new_destructor);
  257. { no return value }
  258. pd.returndef:=voidtype;
  259. constr_destr_finish_head(pd,pd.struct);
  260. result:=pd;
  261. end;
  262. procedure setinterfacemethodoptions;
  263. var
  264. i : longint;
  265. def : tdef;
  266. begin
  267. include(current_structdef.objectoptions,oo_has_virtual);
  268. for i:=0 to current_structdef.symtable.DefList.count-1 do
  269. begin
  270. def:=tdef(current_structdef.symtable.DefList[i]);
  271. if assigned(def) and
  272. (def.typ=procdef) then
  273. begin
  274. include(tprocdef(def).procoptions,po_virtualmethod);
  275. tprocdef(def).forwarddef:=false;
  276. end;
  277. end;
  278. end;
  279. procedure setobjcclassmethodoptions;
  280. var
  281. i : longint;
  282. def : tdef;
  283. begin
  284. for i:=0 to current_structdef.symtable.DefList.count-1 do
  285. begin
  286. def:=tdef(current_structdef.symtable.DefList[i]);
  287. if assigned(def) and
  288. (def.typ=procdef) then
  289. begin
  290. include(tprocdef(def).procoptions,po_virtualmethod);
  291. end;
  292. end;
  293. end;
  294. procedure handleImplementedInterface(intfdef : tobjectdef);
  295. begin
  296. if not is_interface(intfdef) then
  297. begin
  298. Message1(type_e_interface_type_expected,intfdef.typename);
  299. exit;
  300. end;
  301. if ([oo_is_forward,oo_is_formal] * intfdef.objectoptions <> []) then
  302. begin
  303. Message1(parser_e_forward_intf_declaration_must_be_resolved,intfdef.objrealname^);
  304. exit;
  305. end;
  306. if find_implemented_interface(current_objectdef,intfdef)<>nil then
  307. Message1(sym_e_duplicate_id,intfdef.objname^)
  308. else
  309. current_objectdef.register_implemented_interface(intfdef,true);
  310. end;
  311. procedure handleImplementedProtocolOrJavaIntf(intfdef : tobjectdef);
  312. begin
  313. intfdef:=find_real_class_definition(intfdef,false);
  314. case current_objectdef.objecttype of
  315. odt_objcclass,
  316. odt_objccategory,
  317. odt_objcprotocol:
  318. if not is_objcprotocol(intfdef) then
  319. begin
  320. Message1(type_e_protocol_type_expected,intfdef.typename);
  321. exit;
  322. end;
  323. odt_javaclass,
  324. odt_interfacejava:
  325. if not is_javainterface(intfdef) then
  326. begin
  327. Message1(type_e_interface_type_expected,intfdef.typename);
  328. exit
  329. end;
  330. else
  331. internalerror(2011010807);
  332. end;
  333. if ([oo_is_forward,oo_is_formal] * intfdef.objectoptions <> []) then
  334. begin
  335. Message1(parser_e_forward_intf_declaration_must_be_resolved,intfdef.objrealname^);
  336. exit;
  337. end;
  338. if find_implemented_interface(current_objectdef,intfdef)<>nil then
  339. Message1(sym_e_duplicate_id,intfdef.objname^)
  340. else
  341. current_objectdef.register_implemented_interface(intfdef,false);
  342. end;
  343. procedure readImplementedInterfacesAndProtocols(intf: boolean);
  344. var
  345. hdef : tdef;
  346. begin
  347. while try_to_consume(_COMMA) do
  348. begin
  349. { use single_type instead of id_type for specialize support }
  350. single_type(hdef,[stoAllowSpecialization,stoParseClassParent]);
  351. if (hdef.typ<>objectdef) then
  352. begin
  353. if intf then
  354. Message1(type_e_interface_type_expected,hdef.typename)
  355. else
  356. Message1(type_e_protocol_type_expected,hdef.typename);
  357. continue;
  358. end;
  359. if intf then
  360. handleImplementedInterface(tobjectdef(hdef))
  361. else
  362. handleImplementedProtocolOrJavaIntf(tobjectdef(hdef));
  363. end;
  364. end;
  365. procedure readinterfaceiid;
  366. var
  367. p : tnode;
  368. valid : boolean;
  369. begin
  370. p:=comp_expr([ef_accept_equal]);
  371. if p.nodetype=stringconstn then
  372. begin
  373. stringdispose(current_objectdef.iidstr);
  374. current_objectdef.iidstr:=stringdup(tstringconstnode(p).asrawbytestring);
  375. valid:=string2guid(current_objectdef.iidstr^,current_objectdef.iidguid^);
  376. if (current_objectdef.objecttype in [odt_interfacecom,odt_dispinterface]) and
  377. not valid then
  378. Message(parser_e_improper_guid_syntax);
  379. include(current_structdef.objectoptions,oo_has_valid_guid);
  380. end
  381. else
  382. Message(parser_e_illegal_expression);
  383. p.free;
  384. end;
  385. procedure get_cpp_or_java_class_external_status(od: tobjectdef);
  386. var
  387. hs: string;
  388. begin
  389. { C++ classes can be external -> all methods inside are external
  390. (defined at the class level instead of per method, so that you cannot
  391. define some methods as external and some not)
  392. }
  393. if try_to_consume(_EXTERNAL) then
  394. begin
  395. hs:='';
  396. if token in [_CSTRING,_CWSTRING,_CCHAR,_CWCHAR] then
  397. begin
  398. { Always add library prefix and suffix to create an uniform name }
  399. hs:=get_stringconst;
  400. if ExtractFileExt(hs)='' then
  401. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  402. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  403. hs:=target_info.sharedlibprefix+hs;
  404. end;
  405. if hs<>'' then
  406. begin
  407. { the JVM expects java/lang/Object rather than java.lang.Object }
  408. if target_info.system in systems_jvm then
  409. Replace(hs,'.','/');
  410. stringdispose(od.import_lib);
  411. od.import_lib:=stringdup(hs);
  412. end;
  413. { check if we shall use another name for the class }
  414. if try_to_consume(_NAME) then
  415. od.objextname:=stringdup(get_stringconst)
  416. else
  417. od.objextname:=stringdup(od.objrealname^);
  418. include(od.objectoptions,oo_is_external);
  419. end
  420. else
  421. begin
  422. od.objextname:=stringdup(od.objrealname^);
  423. end;
  424. end;
  425. procedure get_objc_class_or_protocol_external_status(od: tobjectdef);
  426. begin
  427. { Objective-C classes can be external -> all messages inside are
  428. external (defined at the class level instead of per method, so
  429. that you cannot define some methods as external and some not)
  430. }
  431. if try_to_consume(_EXTERNAL) then
  432. begin
  433. if try_to_consume(_NAME) then
  434. od.objextname:=stringdup(get_stringconst)
  435. else
  436. { the external name doesn't matter for formally declared
  437. classes, and allowing to specify one would mean that we would
  438. have to check it for consistency with the actual definition
  439. later on }
  440. od.objextname:=stringdup(od.objrealname^);
  441. include(od.objectoptions,oo_is_external);
  442. end
  443. else
  444. od.objextname:=stringdup(od.objrealname^);
  445. end;
  446. procedure parse_object_options;
  447. var
  448. gotexternal: boolean;
  449. begin
  450. case current_objectdef.objecttype of
  451. odt_object,odt_class,
  452. odt_javaclass:
  453. begin
  454. gotexternal:=false;
  455. while true do
  456. begin
  457. if try_to_consume(_ABSTRACT) then
  458. include(current_structdef.objectoptions,oo_is_abstract)
  459. else
  460. if try_to_consume(_SEALED) then
  461. include(current_structdef.objectoptions,oo_is_sealed)
  462. else if (current_objectdef.objecttype=odt_javaclass) and
  463. (token=_ID) and
  464. (idtoken=_EXTERNAL) then
  465. begin
  466. get_cpp_or_java_class_external_status(current_objectdef);
  467. gotexternal:=true;
  468. end
  469. else
  470. break;
  471. end;
  472. { don't use <=, because there's a bug in the 2.6.0 SPARC code
  473. generator regarding handling this expression }
  474. if ([oo_is_abstract, oo_is_sealed] * current_structdef.objectoptions) = [oo_is_abstract, oo_is_sealed] then
  475. Message(parser_e_abstract_and_sealed_conflict);
  476. { set default external name in case of no external directive }
  477. if (current_objectdef.objecttype=odt_javaclass) and
  478. not gotexternal then
  479. get_cpp_or_java_class_external_status(current_objectdef)
  480. end;
  481. odt_cppclass,
  482. odt_interfacejava:
  483. get_cpp_or_java_class_external_status(current_objectdef);
  484. odt_objcclass,odt_objcprotocol,odt_objccategory:
  485. get_objc_class_or_protocol_external_status(current_objectdef);
  486. odt_helper: ; // nothing
  487. else
  488. ;
  489. end;
  490. end;
  491. procedure parse_parent_classes;
  492. var
  493. intfchildof,
  494. childof : tobjectdef;
  495. hdef : tdef;
  496. hasparentdefined : boolean;
  497. begin
  498. childof:=nil;
  499. intfchildof:=nil;
  500. hasparentdefined:=false;
  501. { reads the parent class }
  502. if (token=_LKLAMMER) or
  503. is_objccategory(current_structdef) then
  504. begin
  505. consume(_LKLAMMER);
  506. { use single_type instead of id_type for specialize support }
  507. single_type(hdef,[stoAllowSpecialization, stoParseClassParent]);
  508. if (not assigned(hdef)) or
  509. (hdef.typ<>objectdef) then
  510. begin
  511. if assigned(hdef) then
  512. Message1(type_e_class_type_expected,hdef.typename)
  513. else if is_objccategory(current_structdef) then
  514. { a category must specify the class to extend }
  515. Message(type_e_objcclass_type_expected);
  516. end
  517. else
  518. begin
  519. childof:=tobjectdef(hdef);
  520. { a mix of class, interfaces, objects and cppclasses
  521. isn't allowed }
  522. case current_objectdef.objecttype of
  523. odt_class,
  524. odt_javaclass:
  525. if (childof.objecttype<>current_objectdef.objecttype) then
  526. begin
  527. if (is_interface(childof) and
  528. is_class(current_objectdef)) or
  529. (is_javainterface(childof) and
  530. is_javaclass(current_objectdef)) then
  531. begin
  532. { we insert the interface after the child
  533. is set, see below
  534. }
  535. intfchildof:=childof;
  536. childof:=class_tobject;
  537. end
  538. else
  539. Message(parser_e_mix_of_classes_and_objects);
  540. end
  541. else
  542. if oo_is_sealed in childof.objectoptions then
  543. Message1(parser_e_sealed_descendant,childof.typesymbolprettyname)
  544. else
  545. childof:=find_real_class_definition(childof,true);
  546. odt_interfacecorba,
  547. odt_interfacecom:
  548. begin
  549. if not(is_interface(childof)) then
  550. Message(parser_e_mix_of_classes_and_objects);
  551. current_objectdef.objecttype:=childof.objecttype;
  552. end;
  553. odt_cppclass:
  554. if not(is_cppclass(childof)) then
  555. Message(parser_e_mix_of_classes_and_objects);
  556. odt_objcclass:
  557. if not(is_objcclass(childof) or
  558. is_objccategory(childof)) then
  559. begin
  560. if is_objcprotocol(childof) then
  561. begin
  562. if not(oo_is_classhelper in current_structdef.objectoptions) then
  563. begin
  564. intfchildof:=childof;
  565. childof:=nil;
  566. CGMessage(parser_h_no_objc_parent);
  567. end
  568. else
  569. { a category must specify the class to extend }
  570. CGMessage(type_e_objcclass_type_expected);
  571. end
  572. else
  573. Message(parser_e_mix_of_classes_and_objects);
  574. end
  575. else
  576. childof:=find_real_class_definition(childof,true);
  577. odt_objcprotocol:
  578. begin
  579. if not(is_objcprotocol(childof)) then
  580. Message(parser_e_mix_of_classes_and_objects);
  581. intfchildof:=childof;
  582. childof:=nil;
  583. end;
  584. odt_interfacejava:
  585. begin
  586. if not(is_javainterface(childof)) then
  587. Message(parser_e_mix_of_classes_and_objects);
  588. intfchildof:=find_real_class_definition(childof,true);
  589. childof:=nil;
  590. end;
  591. odt_object:
  592. if not(is_object(childof)) then
  593. Message(parser_e_mix_of_classes_and_objects)
  594. else
  595. if oo_is_sealed in childof.objectoptions then
  596. Message1(parser_e_sealed_descendant,childof.typename);
  597. odt_dispinterface:
  598. Message(parser_e_dispinterface_cant_have_parent);
  599. odt_helper:
  600. if not is_objectpascal_helper(childof) then
  601. begin
  602. Message(type_e_helper_type_expected);
  603. childof:=nil;
  604. end;
  605. else
  606. ;
  607. end;
  608. end;
  609. hasparentdefined:=true;
  610. end;
  611. { if no parent class, then a class get tobject as parent }
  612. if not assigned(childof) then
  613. begin
  614. case current_objectdef.objecttype of
  615. odt_class:
  616. if current_objectdef<>class_tobject then
  617. childof:=class_tobject;
  618. odt_interfacecom:
  619. if current_objectdef<>interface_iunknown then
  620. childof:=interface_iunknown;
  621. odt_dispinterface:
  622. childof:=interface_idispatch;
  623. odt_objcclass:
  624. CGMessage(parser_h_no_objc_parent);
  625. odt_javaclass:
  626. { inherit from TObject by default for compatibility }
  627. if current_objectdef<>java_jlobject then
  628. childof:=class_tobject;
  629. else
  630. ;
  631. end;
  632. end;
  633. if assigned(childof) then
  634. begin
  635. { Forbid not completly defined objects to be used as parents. This will
  636. also prevent circular loops of classes, because we set the forward flag
  637. at the start of the new definition and will reset it below after the
  638. parent has been set }
  639. if (oo_is_forward in childof.objectoptions) then
  640. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  641. else if not(oo_is_formal in childof.objectoptions) then
  642. current_objectdef.set_parent(childof)
  643. else
  644. Message1(sym_e_formal_class_not_resolved,childof.objrealname^);
  645. end;
  646. if hasparentdefined then
  647. begin
  648. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  649. begin
  650. if assigned(intfchildof) then
  651. if current_objectdef.objecttype=odt_class then
  652. handleImplementedInterface(intfchildof)
  653. else
  654. handleImplementedProtocolOrJavaIntf(intfchildof);
  655. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  656. end;
  657. consume(_RKLAMMER);
  658. end;
  659. end;
  660. procedure parse_extended_type(helpertype:thelpertype);
  661. procedure validate_extendeddef_typehelper(var def:tdef);
  662. begin
  663. if (def.typ in [undefineddef,procvardef,procdef,
  664. filedef,classrefdef,abstractdef,forwarddef,formaldef]) or
  665. (
  666. (def.typ=objectdef) and
  667. not (tobjectdef(def).objecttype in objecttypes_with_helpers)
  668. ) then
  669. begin
  670. Message1(type_e_type_not_allowed_for_type_helper,def.typename);
  671. def:=generrordef;
  672. end;
  673. end;
  674. procedure check_inheritance_record_type_helper(var def:tdef);
  675. var
  676. tmp : tstoreddef;
  677. begin
  678. if (def.typ<>errordef) and assigned(current_objectdef.childof) then
  679. begin
  680. if def<>current_objectdef.childof.extendeddef then
  681. begin
  682. { a type helper may extend a type alias of the type its
  683. parent type helper extends }
  684. tmp:=tstoreddef(def);
  685. while (df_unique in tmp.defoptions) and assigned(tstoreddef(tmp).orgdef) do
  686. begin
  687. if tmp.orgdef=current_objectdef.childof.extendeddef then
  688. exit;
  689. tmp:=tstoreddef(tmp.orgdef);
  690. end;
  691. Message1(type_e_record_helper_must_extend_same_record,current_objectdef.childof.extendeddef.typename);
  692. def:=generrordef;
  693. end;
  694. end;
  695. end;
  696. procedure check_inheritance_class_helper(var def:tdef);
  697. begin
  698. if (def.typ<>errordef) and assigned(current_objectdef.childof) then
  699. begin
  700. if (current_objectdef.childof.extendeddef.typ<>objectdef) or
  701. not (tobjectdef(current_objectdef.childof.extendeddef).objecttype in objecttypes_with_helpers) then
  702. Internalerror(2011021101);
  703. if not def_is_related(def,current_objectdef.childof.extendeddef) then
  704. begin
  705. Message1(type_e_class_helper_must_extend_subclass,current_objectdef.childof.extendeddef.typename);
  706. def:=generrordef;
  707. end;
  708. end;
  709. end;
  710. var
  711. hdef: tdef;
  712. begin
  713. if not is_objectpascal_helper(current_structdef) then
  714. Internalerror(2011021103);
  715. consume(_FOR);
  716. { set extendeddef to non-Nil so that potential checks for it won't trigger
  717. access violations }
  718. current_objectdef.extendeddef:=generrordef;
  719. single_type(hdef,[stoParseClassParent]);
  720. if not assigned(hdef) or (hdef.typ=errordef) then
  721. begin
  722. case helpertype of
  723. ht_class:
  724. Message1(type_e_class_type_expected,hdef.typename);
  725. ht_record:
  726. Message(type_e_record_type_expected);
  727. ht_type:
  728. Message1(type_e_type_id_expected,hdef.typename);
  729. else
  730. internalerror(2019050532);
  731. end;
  732. end
  733. else
  734. begin
  735. case helpertype of
  736. ht_class:
  737. if (hdef.typ<>objectdef) or
  738. not is_class(hdef) then
  739. Message1(type_e_class_type_expected,hdef.typename)
  740. else
  741. begin
  742. { a class helper must extend the same class or a subclass
  743. of the class extended by the parent class helper }
  744. check_inheritance_class_helper(hdef);
  745. end;
  746. ht_record:
  747. if (hdef.typ=objectdef) or
  748. (
  749. { primitive types are allowed for record helpers in mode
  750. delphi }
  751. (hdef.typ<>recorddef) and
  752. not (m_delphi in current_settings.modeswitches)
  753. ) then
  754. Message1(type_e_record_type_expected,hdef.typename)
  755. else
  756. begin
  757. if hdef.typ<>recorddef then
  758. { this is a primitive type in mode delphi, so validate
  759. the def }
  760. validate_extendeddef_typehelper(hdef);
  761. { a record helper must extend the same record as the
  762. parent helper }
  763. check_inheritance_record_type_helper(hdef);
  764. end;
  765. ht_type:
  766. begin
  767. validate_extendeddef_typehelper(hdef);
  768. if (hdef.typ=objectdef) and
  769. (tobjectdef(hdef).objecttype in objecttypes_with_helpers) then
  770. check_inheritance_class_helper(hdef)
  771. else
  772. { a type helper must extend the same type as the
  773. parent helper }
  774. check_inheritance_record_type_helper(hdef);
  775. end;
  776. else
  777. internalerror(2019050531);
  778. end;
  779. end;
  780. if assigned(hdef) then
  781. current_objectdef.extendeddef:=hdef;
  782. end;
  783. procedure parse_guid;
  784. begin
  785. { read GUID }
  786. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  787. try_to_consume(_LECKKLAMMER) then
  788. begin
  789. readinterfaceiid;
  790. consume(_RECKKLAMMER);
  791. end
  792. else if (current_objectdef.objecttype=odt_dispinterface) then
  793. message(parser_e_dispinterface_needs_a_guid);
  794. end;
  795. function method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  796. procedure chkobjc(pd: tprocdef);
  797. begin
  798. if is_objc_class_or_protocol(pd.struct) then
  799. begin
  800. include(pd.procoptions,po_objc);
  801. end;
  802. end;
  803. procedure chkjava(pd: tprocdef);
  804. begin
  805. {$ifdef jvm}
  806. if is_java_class_or_interface(pd.struct) then
  807. begin
  808. { mark all non-virtual instance methods as "virtual; final;",
  809. because
  810. a) that's the only way to guarantee "non-virtual" behaviour
  811. (other than making them class methods with an explicit self
  812. pointer, but that causes problems with interface mappings
  813. and procvars)
  814. b) if we don't mark them virtual, they don't get added to the
  815. vmt and we can't check whether child classes try to override
  816. them
  817. }
  818. if is_javaclass(pd.struct) then
  819. begin
  820. if not(po_virtualmethod in pd.procoptions) and
  821. not(po_classmethod in pd.procoptions) then
  822. begin
  823. include(pd.procoptions,po_virtualmethod);
  824. include(pd.procoptions,po_finalmethod);
  825. include(pd.procoptions,po_java_nonvirtual);
  826. end
  827. else if [po_virtualmethod,po_classmethod]<=pd.procoptions then
  828. begin
  829. if po_staticmethod in pd.procoptions then
  830. Message(type_e_java_class_method_not_static_virtual);
  831. end;
  832. end;
  833. end;
  834. {$endif}
  835. end;
  836. procedure chkcpp(pd:tprocdef);
  837. begin
  838. { nothing currently }
  839. end;
  840. var
  841. oldparse_only: boolean;
  842. flags : tparse_proc_flags;
  843. begin
  844. case token of
  845. _PROCEDURE,
  846. _FUNCTION:
  847. begin
  848. if (astruct.symtable.currentvisibility=vis_published) and
  849. not(oo_can_have_published in astruct.objectoptions) then
  850. Message(parser_e_cant_have_published);
  851. oldparse_only:=parse_only;
  852. parse_only:=true;
  853. flags:=[];
  854. if is_classdef then
  855. include(flags,ppf_classmethod);
  856. if hadgeneric then
  857. include(flags,ppf_generic);
  858. result:=parse_proc_dec(flags,astruct);
  859. { this is for error recovery as well as forward }
  860. { interface mappings, i.e. mapping to a method }
  861. { which isn't declared yet }
  862. if assigned(result) then
  863. begin
  864. parse_object_proc_directives(result);
  865. { check if dispid is set }
  866. if is_dispinterface(result.struct) and not (po_dispid in result.procoptions) then
  867. begin
  868. result.dispid:=tobjectdef(result.struct).get_next_dispid;
  869. include(result.procoptions, po_dispid);
  870. end;
  871. { all Macintosh Object Pascal methods are virtual. }
  872. { this can't be a class method, because macpas mode }
  873. { has no m_class }
  874. if (m_mac in current_settings.modeswitches) then
  875. include(result.procoptions,po_virtualmethod);
  876. { for record and type helpers only static class methods are
  877. allowed }
  878. if is_objectpascal_helper(astruct) and
  879. (
  880. (tobjectdef(astruct).extendeddef.typ<>objectdef) or
  881. (tobjectdef(tobjectdef(astruct).extendeddef).objecttype<>odt_class)
  882. ) and
  883. is_classdef and not (po_staticmethod in result.procoptions) then
  884. MessagePos(result.fileinfo,parser_e_class_methods_only_static_in_records);
  885. handle_calling_convention(result,hcc_default_actions_intf_struct);
  886. { add definition to procsym }
  887. proc_add_definition(result);
  888. { add procdef options to objectdef options }
  889. if (po_msgint in result.procoptions) then
  890. include(astruct.objectoptions,oo_has_msgint);
  891. if (po_msgstr in result.procoptions) then
  892. include(astruct.objectoptions,oo_has_msgstr);
  893. if (po_virtualmethod in result.procoptions) then
  894. include(astruct.objectoptions,oo_has_virtual);
  895. if result.is_generic then
  896. astruct.symtable.includeoption(sto_has_generic);
  897. chkcpp(result);
  898. chkobjc(result);
  899. chkjava(result);
  900. end;
  901. maybe_parse_hint_directives(result);
  902. parse_only:=oldparse_only;
  903. end;
  904. _CONSTRUCTOR :
  905. begin
  906. if (astruct.symtable.currentvisibility=vis_published) and
  907. not(oo_can_have_published in astruct.objectoptions) then
  908. Message(parser_e_cant_have_published);
  909. if not is_classdef and not(astruct.symtable.currentvisibility in [vis_public,vis_published]) then
  910. Message(parser_w_constructor_should_be_public);
  911. if is_interface(astruct) then
  912. Message(parser_e_no_con_des_in_interfaces);
  913. { Objective-C does not know the concept of a constructor }
  914. if is_objc_class_or_protocol(astruct) then
  915. Message(parser_e_objc_no_constructor_destructor);
  916. if is_objectpascal_helper(astruct) then
  917. if is_classdef then
  918. { class constructors are not allowed in class helpers }
  919. Message(parser_e_no_class_constructor_in_helpers);
  920. { only 1 class constructor is allowed }
  921. if is_classdef and (oo_has_class_constructor in astruct.objectoptions) then
  922. Message1(parser_e_only_one_class_constructor_allowed, astruct.objrealname^);
  923. oldparse_only:=parse_only;
  924. parse_only:=true;
  925. if is_classdef then
  926. result:=class_constructor_head(current_structdef)
  927. else
  928. begin
  929. result:=constructor_head;
  930. if is_objectpascal_helper(astruct) and
  931. (tobjectdef(astruct).extendeddef.typ<>objectdef) and
  932. (result.minparacount=0) then
  933. { as long as parameterless constructors aren't allowed in records they
  934. aren't allowed in record/type helpers either }
  935. MessagePos(result.procsym.fileinfo,parser_e_no_parameterless_constructor_in_records);
  936. end;
  937. chkcpp(result);
  938. parse_only:=oldparse_only;
  939. end;
  940. _DESTRUCTOR :
  941. begin
  942. if (astruct.symtable.currentvisibility=vis_published) and
  943. not(oo_can_have_published in astruct.objectoptions) then
  944. Message(parser_e_cant_have_published);
  945. if not is_classdef then
  946. if (oo_has_new_destructor in astruct.objectoptions) then
  947. Message(parser_n_only_one_destructor);
  948. if is_interface(astruct) then
  949. Message(parser_e_no_con_des_in_interfaces);
  950. { (class) destructors are not allowed in class helpers }
  951. if is_objectpascal_helper(astruct) then
  952. Message(parser_e_no_destructor_in_records);
  953. if not is_classdef and (astruct.symtable.currentvisibility<>vis_public) then
  954. Message(parser_w_destructor_should_be_public);
  955. { Objective-C does not know the concept of a destructor }
  956. if is_objc_class_or_protocol(astruct) then
  957. Message(parser_e_objc_no_constructor_destructor);
  958. { only 1 class destructor is allowed }
  959. if is_classdef and (oo_has_class_destructor in astruct.objectoptions) then
  960. Message1(parser_e_only_one_class_destructor_allowed, astruct.objrealname^);
  961. oldparse_only:=parse_only;
  962. parse_only:=true;
  963. if is_classdef then
  964. result:=class_destructor_head(current_structdef)
  965. else
  966. result:=destructor_head;
  967. chkcpp(result);
  968. parse_only:=oldparse_only;
  969. end;
  970. else
  971. internalerror(2011032102);
  972. end;
  973. end;
  974. procedure parse_object_members;
  975. var
  976. typedconstswritable: boolean;
  977. object_member_blocktype : tblock_type;
  978. hadgeneric,
  979. fields_allowed, is_classdef, class_fields, is_final, final_fields,
  980. threadvar_fields : boolean;
  981. vdoptions: tvar_dec_options;
  982. fieldlist: tfpobjectlist;
  983. rtti_attrs_def: trtti_attribute_list;
  984. attr_element_count,fldCount : Integer;
  985. method_def : tprocdef;
  986. procedure check_unbound_attributes;
  987. begin
  988. if assigned(rtti_attrs_def) and (rtti_attrs_def.get_attribute_count>0) then
  989. Message1(parser_e_unbound_attribute,trtti_attribute(rtti_attrs_def.rtti_attributes[0]).typesym.prettyname);
  990. rtti_attrs_def.free;
  991. rtti_attrs_def:=nil;
  992. end;
  993. procedure parse_const;
  994. begin
  995. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  996. Message(parser_e_type_var_const_only_in_records_and_classes);
  997. consume(_CONST);
  998. object_member_blocktype:=bt_const;
  999. final_fields:=is_final;
  1000. is_final:=false;
  1001. end;
  1002. procedure parse_var(isthreadvar:boolean);
  1003. begin
  1004. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass]) and
  1005. { Java interfaces can contain static final class vars }
  1006. not((current_objectdef.objecttype=odt_interfacejava) and
  1007. is_final and is_classdef) then
  1008. Message(parser_e_type_var_const_only_in_records_and_classes);
  1009. if isthreadvar then
  1010. consume(_THREADVAR)
  1011. else
  1012. consume(_VAR);
  1013. fields_allowed:=true;
  1014. object_member_blocktype:=bt_general;
  1015. class_fields:=is_classdef;
  1016. final_fields:=is_final;
  1017. threadvar_fields:=isthreadvar;
  1018. is_classdef:=false;
  1019. is_final:=false;
  1020. end;
  1021. procedure parse_class;
  1022. begin
  1023. is_classdef:=false;
  1024. { read class method/field/property }
  1025. consume(_CLASS);
  1026. { class modifier is only allowed for procedures, functions, }
  1027. { constructors, destructors, fields and properties }
  1028. if not((token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_DESTRUCTOR,_THREADVAR]) or (token=_CONSTRUCTOR)) then
  1029. Message(parser_e_procedure_or_function_expected);
  1030. { class properties currently can't have attributes }
  1031. if not(token in [_FUNCTION,_PROCEDURE]) then
  1032. check_unbound_attributes;
  1033. { Java interfaces can contain final class vars }
  1034. if is_interface(current_structdef) or
  1035. (is_javainterface(current_structdef) and
  1036. (not(is_final) or
  1037. (token<>_VAR))) then
  1038. Message(parser_e_no_static_method_in_interfaces)
  1039. else
  1040. { class methods are also allowed for Objective-C protocols }
  1041. is_classdef:=true;
  1042. end;
  1043. procedure parse_visibility(vis: tvisibility; oo: tobjectoption);
  1044. begin
  1045. { Objective-C and Java classes do not support "published",
  1046. as basically everything is published. }
  1047. if (vis=vis_published) and
  1048. (is_objc_class_or_protocol(current_structdef) or
  1049. is_java_class_or_interface(current_structdef)) then
  1050. Message(parser_e_no_objc_published)
  1051. else if is_interface(current_structdef) or
  1052. is_objc_protocol_or_category(current_structdef) or
  1053. is_javainterface(current_structdef) then
  1054. Message(parser_e_no_access_specifier_in_interfaces);
  1055. current_structdef.symtable.currentvisibility:=vis;
  1056. consume(token);
  1057. if (oo<>oo_none) then
  1058. include(current_structdef.objectoptions,oo);
  1059. fields_allowed:=true;
  1060. is_classdef:=false;
  1061. class_fields:=false;
  1062. threadvar_fields:=false;
  1063. is_final:=false;
  1064. object_member_blocktype:=bt_general;
  1065. end;
  1066. begin
  1067. { empty class declaration ? }
  1068. if (current_objectdef.objecttype in [odt_class,odt_objcclass,odt_javaclass]) and
  1069. (token=_SEMICOLON) then
  1070. exit;
  1071. { in "publishable" classes the default access type is published }
  1072. if (oo_can_have_published in current_structdef.objectoptions) then
  1073. current_structdef.symtable.currentvisibility:=vis_published
  1074. else
  1075. current_structdef.symtable.currentvisibility:=vis_public;
  1076. fields_allowed:=true;
  1077. is_classdef:=false;
  1078. class_fields:=false;
  1079. is_final:=false;
  1080. final_fields:=false;
  1081. rtti_attrs_def:=nil;
  1082. hadgeneric:=false;
  1083. threadvar_fields:=false;
  1084. object_member_blocktype:=bt_general;
  1085. fieldlist:=tfpobjectlist.create(false);
  1086. repeat
  1087. case token of
  1088. _TYPE :
  1089. begin
  1090. check_unbound_attributes;
  1091. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  1092. Message(parser_e_type_var_const_only_in_records_and_classes);
  1093. consume(_TYPE);
  1094. object_member_blocktype:=bt_type;
  1095. if (token=_LECKKLAMMER) and (m_prefixed_attributes in current_settings.modeswitches) then
  1096. begin
  1097. check_unbound_attributes;
  1098. types_dec(true,hadgeneric, rtti_attrs_def);
  1099. end
  1100. else
  1101. { expect at least one type declaration }
  1102. if token<>_ID then
  1103. consume(_ID);
  1104. end;
  1105. _VAR :
  1106. begin
  1107. check_unbound_attributes;
  1108. rtti_attrs_def := nil;
  1109. parse_var(false);
  1110. { expect at least one var declaration }
  1111. if token<>_ID then
  1112. consume(_ID);
  1113. end;
  1114. _CONST:
  1115. begin
  1116. check_unbound_attributes;
  1117. rtti_attrs_def := nil;
  1118. parse_const;
  1119. { expect at least one constant declaration }
  1120. if token<>_ID then
  1121. consume(_ID);
  1122. end;
  1123. _THREADVAR :
  1124. begin
  1125. check_unbound_attributes;
  1126. if not is_classdef then
  1127. begin
  1128. Message(parser_e_threadvar_must_be_class);
  1129. { for error recovery we enforce class fields }
  1130. is_classdef:=true;
  1131. end;
  1132. parse_var(true);
  1133. { expect at least one threadvar declaration }
  1134. if token<>_ID then
  1135. consume(_ID);
  1136. end;
  1137. _ID :
  1138. begin
  1139. if is_objcprotocol(current_structdef) and
  1140. ((idtoken=_REQUIRED) or
  1141. (idtoken=_OPTIONAL)) then
  1142. begin
  1143. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  1144. consume(idtoken)
  1145. end
  1146. else case idtoken of
  1147. _PRIVATE :
  1148. begin
  1149. parse_visibility(vis_private,oo_has_private);
  1150. end;
  1151. _PROTECTED :
  1152. begin
  1153. parse_visibility(vis_protected,oo_has_protected);
  1154. end;
  1155. _PUBLIC :
  1156. begin
  1157. parse_visibility(vis_public,oo_none);
  1158. end;
  1159. _PUBLISHED :
  1160. begin
  1161. parse_visibility(vis_published,oo_none);
  1162. end;
  1163. _STRICT :
  1164. begin
  1165. if is_interface(current_structdef) or
  1166. is_objc_protocol_or_category(current_structdef) or
  1167. is_javainterface(current_structdef) then
  1168. Message(parser_e_no_access_specifier_in_interfaces);
  1169. consume(_STRICT);
  1170. if token=_ID then
  1171. begin
  1172. case idtoken of
  1173. _PRIVATE:
  1174. begin
  1175. consume(_PRIVATE);
  1176. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  1177. include(current_structdef.objectoptions,oo_has_strictprivate);
  1178. end;
  1179. _PROTECTED:
  1180. begin
  1181. consume(_PROTECTED);
  1182. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  1183. include(current_structdef.objectoptions,oo_has_strictprotected);
  1184. end;
  1185. else
  1186. message(parser_e_protected_or_private_expected);
  1187. end;
  1188. end
  1189. else
  1190. message(parser_e_protected_or_private_expected);
  1191. fields_allowed:=true;
  1192. is_classdef:=false;
  1193. class_fields:=false;
  1194. threadvar_fields:=false;
  1195. is_final:=false;
  1196. final_fields:=false;
  1197. object_member_blocktype:=bt_general;
  1198. end
  1199. else if (m_final_fields in current_settings.modeswitches) and
  1200. (token=_ID) and
  1201. (idtoken=_FINAL) then
  1202. begin
  1203. { currently only supported for external classes, because
  1204. requires fully working DFA otherwise }
  1205. if (current_structdef.typ<>objectdef) or
  1206. not(oo_is_external in tobjectdef(current_structdef).objectoptions) then
  1207. Message(parser_e_final_only_external);
  1208. consume(_final);
  1209. is_final:=true;
  1210. if token=_CLASS then
  1211. parse_class;
  1212. if not(token in [_CONST,_VAR]) then
  1213. message(parser_e_final_only_const_var);
  1214. end
  1215. else
  1216. begin
  1217. if object_member_blocktype=bt_general then
  1218. begin
  1219. if (idtoken=_GENERIC) and
  1220. not (m_delphi in current_settings.modeswitches) and
  1221. (
  1222. not fields_allowed or
  1223. is_objectpascal_helper(current_structdef)
  1224. ) then
  1225. begin
  1226. if hadgeneric then
  1227. Message(parser_e_procedure_or_function_expected);
  1228. consume(_ID);
  1229. hadgeneric:=true;
  1230. if not (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  1231. Message(parser_e_procedure_or_function_expected);
  1232. end
  1233. else
  1234. begin
  1235. if is_interface(current_structdef) or
  1236. is_objc_protocol_or_category(current_structdef) or
  1237. (
  1238. is_objectpascal_helper(current_structdef) and
  1239. not class_fields
  1240. ) or
  1241. (is_javainterface(current_structdef) and
  1242. not(class_fields and final_fields)) then
  1243. Message(parser_e_no_vars_in_interfaces);
  1244. if (current_structdef.symtable.currentvisibility=vis_published) and
  1245. not(oo_can_have_published in current_structdef.objectoptions) then
  1246. Message(parser_e_cant_have_published);
  1247. if (not fields_allowed) then
  1248. Message(parser_e_field_not_allowed_here);
  1249. vdoptions:=[vd_object];
  1250. if not (m_delphi in current_settings.modeswitches) then
  1251. include(vdoptions,vd_check_generic);
  1252. if class_fields then
  1253. include(vdoptions,vd_class);
  1254. if is_class(current_structdef) then
  1255. include(vdoptions,vd_canreorder);
  1256. if final_fields then
  1257. include(vdoptions,vd_final);
  1258. if threadvar_fields then
  1259. include(vdoptions,vd_threadvar);
  1260. // Record count
  1261. fldCount:=FieldList.Count;
  1262. read_record_fields(vdoptions,fieldlist,nil,hadgeneric,attr_element_count);
  1263. {
  1264. attr_element_count returns the number of fields to which the attribute must be applied.
  1265. For
  1266. [someattr]
  1267. a : integer;
  1268. b : integer;
  1269. attr_element_count returns 1. For
  1270. [someattr]
  1271. a, b : integer;
  1272. it returns 2.
  1273. Basically the number of variables before the first colon.
  1274. }
  1275. if assigned(rtti_attrs_def) then
  1276. begin
  1277. { read_record_fields can read a list of fields with the same type.
  1278. for the first fields, we simply copy. for the last one we bind.}
  1279. While (attr_element_count>1) do
  1280. begin
  1281. trtti_attribute_list.copyandbind(rtti_attrs_def,tfieldvarsym(fieldlist[FldCount]).rtti_attribute_list);
  1282. inc(fldcount);
  1283. dec(attr_element_count);
  1284. end;
  1285. if fldCount<FieldList.Count then
  1286. trtti_attribute_list.bind(rtti_attrs_def,tfieldvarsym(fieldlist[FldCount]).rtti_attribute_list)
  1287. else
  1288. rtti_attrs_def.free;
  1289. end;
  1290. rtti_attrs_def:=nil;
  1291. end;
  1292. end
  1293. else if object_member_blocktype=bt_type then
  1294. begin
  1295. check_unbound_attributes;
  1296. types_dec(true,hadgeneric, rtti_attrs_def)
  1297. end
  1298. else if object_member_blocktype=bt_const then
  1299. begin
  1300. check_unbound_attributes;
  1301. typedconstswritable:=false;
  1302. if final_fields then
  1303. begin
  1304. { the value of final fields cannot be changed
  1305. once they've been assigned a value }
  1306. typedconstswritable:=cs_typed_const_writable in current_settings.localswitches;
  1307. exclude(current_settings.localswitches,cs_typed_const_writable);
  1308. end;
  1309. consts_dec(true,not is_javainterface(current_structdef),hadgeneric);
  1310. if final_fields and
  1311. typedconstswritable then
  1312. include(current_settings.localswitches,cs_typed_const_writable);
  1313. end
  1314. else
  1315. internalerror(2010011103);
  1316. end;
  1317. end;
  1318. end;
  1319. _PROPERTY :
  1320. begin
  1321. struct_property_dec(is_classdef, rtti_attrs_def);
  1322. fields_allowed:=false;
  1323. is_classdef:=false;
  1324. end;
  1325. _CLASS:
  1326. parse_class;
  1327. _PROCEDURE,
  1328. _FUNCTION,
  1329. _CONSTRUCTOR,
  1330. _DESTRUCTOR :
  1331. begin
  1332. method_def:=method_dec(current_structdef,is_classdef,hadgeneric);
  1333. if assigned(rtti_attrs_def) then
  1334. begin
  1335. trtti_attribute_list.bind(rtti_attrs_def,method_def.rtti_attribute_list);
  1336. rtti_attrs_def:=nil;
  1337. end;
  1338. fields_allowed:=false;
  1339. is_classdef:=false;
  1340. hadgeneric:=false;
  1341. end;
  1342. _LECKKLAMMER:
  1343. begin
  1344. if m_prefixed_attributes in current_settings.modeswitches then
  1345. parse_rttiattributes(rtti_attrs_def)
  1346. else
  1347. consume(_ID);
  1348. end;
  1349. _END :
  1350. begin
  1351. check_unbound_attributes;
  1352. consume(_END);
  1353. break;
  1354. end;
  1355. else
  1356. consume(_ID); { Give a ident expected message, like tp7 }
  1357. end;
  1358. until false;
  1359. if is_class(current_structdef) then
  1360. tabstractrecordsymtable(current_structdef.symtable).addfieldlist(fieldlist,true);
  1361. fieldlist.free;
  1362. end;
  1363. function object_dec(objecttype:tobjecttyp;const n:tidstring;objsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  1364. var
  1365. old_current_structdef: tabstractrecorddef;
  1366. old_current_genericdef,
  1367. old_current_specializedef: tstoreddef;
  1368. old_parse_generic: boolean;
  1369. list: TFPObjectList;
  1370. s: TSymStr;
  1371. st: TSymtable;
  1372. olddef: tdef;
  1373. begin
  1374. old_current_structdef:=current_structdef;
  1375. old_current_genericdef:=current_genericdef;
  1376. old_current_specializedef:=current_specializedef;
  1377. old_parse_generic:=parse_generic;
  1378. current_structdef:=nil;
  1379. current_genericdef:=nil;
  1380. current_specializedef:=nil;
  1381. { objects and class types can't be declared local }
  1382. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1383. not assigned(genericlist) then
  1384. Message(parser_e_no_local_objects);
  1385. { reuse forward objectdef? }
  1386. if assigned(fd) then
  1387. begin
  1388. if (fd.objecttype<>objecttype) or ((fd.is_generic or fd.is_specialization) xor assigned(genericlist)) then
  1389. begin
  1390. Message(parser_e_forward_mismatch);
  1391. { recover }
  1392. current_structdef:=cobjectdef.create(objecttype,n,nil,true);
  1393. include(current_structdef.objectoptions,oo_is_forward);
  1394. end
  1395. else
  1396. current_structdef:=fd
  1397. end
  1398. else
  1399. begin
  1400. { anonym objects aren't allow (o : object a : longint; end;) }
  1401. if n='' then
  1402. Message(parser_f_no_anonym_objects);
  1403. { create new class }
  1404. current_structdef:=cobjectdef.create(objecttype,n,nil,true);
  1405. tobjectdef(current_structdef).helpertype:=helpertype;
  1406. { include always the forward flag, it'll be removed once the whole
  1407. class has been parsed so that it can be used as a parent class
  1408. of a nested class;
  1409. Exception: for external classes this will be removed once the
  1410. parent classes have been parsed }
  1411. include(current_structdef.objectoptions,oo_is_forward);
  1412. if (cs_compilesystem in current_settings.moduleswitches) then
  1413. begin
  1414. case current_objectdef.objecttype of
  1415. odt_interfacecom :
  1416. if (current_structdef.objname^='IUNKNOWN') then
  1417. interface_iunknown:=current_objectdef
  1418. else
  1419. if (current_structdef.objname^='IDISPATCH') then
  1420. interface_idispatch:=current_objectdef;
  1421. odt_class :
  1422. if (current_structdef.objname^='TOBJECT') then
  1423. class_tobject:=current_objectdef;
  1424. odt_javaclass:
  1425. begin
  1426. if (current_structdef.objname^='TOBJECT') then
  1427. class_tobject:=current_objectdef
  1428. else if (current_objectdef.objname^='JLOBJECT') then
  1429. java_jlobject:=current_objectdef
  1430. else if (current_objectdef.objname^='JLTHROWABLE') then
  1431. java_jlthrowable:=current_objectdef
  1432. else if (current_objectdef.objname^='FPCBASERECORDTYPE') then
  1433. java_fpcbaserecordtype:=current_objectdef
  1434. else if (current_objectdef.objname^='JLSTRING') then
  1435. java_jlstring:=current_objectdef
  1436. else if (current_objectdef.objname^='ANSISTRINGCLASS') then
  1437. java_ansistring:=current_objectdef
  1438. else if (current_objectdef.objname^='SHORTSTRINGCLASS') then
  1439. java_shortstring:=current_objectdef
  1440. else if (current_objectdef.objname^='JLENUM') then
  1441. java_jlenum:=current_objectdef
  1442. else if (current_objectdef.objname^='JUENUMSET') then
  1443. java_juenumset:=current_objectdef
  1444. else if (current_objectdef.objname^='FPCBITSET') then
  1445. java_jubitset:=current_objectdef
  1446. else if (current_objectdef.objname^='FPCBASEPROCVARTYPE') then
  1447. java_procvarbase:=current_objectdef;
  1448. end;
  1449. else
  1450. ;
  1451. end;
  1452. end;
  1453. if (current_module.modulename^='OBJCBASE') then
  1454. begin
  1455. case current_objectdef.objecttype of
  1456. odt_objcclass:
  1457. if (current_objectdef.objname^='Protocol') then
  1458. objc_protocoltype:=current_objectdef;
  1459. else
  1460. ;
  1461. end;
  1462. end;
  1463. end;
  1464. { usage of specialized type inside its generic template }
  1465. if assigned(genericdef) then
  1466. current_specializedef:=current_structdef;
  1467. { reject declaration of generic class inside generic class }
  1468. if assigned(genericlist) then
  1469. current_genericdef:=current_structdef;
  1470. { nested types of specializations are specializations as well }
  1471. if assigned(old_current_structdef) and
  1472. (df_specialization in old_current_structdef.defoptions) then
  1473. include(current_structdef.defoptions,df_specialization);
  1474. if assigned(old_current_structdef) and
  1475. (df_generic in old_current_structdef.defoptions) then
  1476. begin
  1477. include(current_structdef.defoptions,df_generic);
  1478. current_genericdef:=current_structdef;
  1479. end;
  1480. { set published flag in $M+ mode, it can also be inherited and will
  1481. be added when the parent class set with tobjectdef.set_parent (PFV) }
  1482. if (cs_generate_rtti in current_settings.localswitches) and
  1483. (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_class,odt_helper]) then
  1484. include(current_structdef.objectoptions,oo_can_have_published);
  1485. { Objective-C/Java objectdefs can be "formal definitions", in which case
  1486. the syntax is "type tc = objcclass external;" -> we have to parse
  1487. its object options (external) already here, to make sure that such
  1488. definitions are recognised as formal defs }
  1489. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  1490. parse_object_options;
  1491. { forward def? }
  1492. if token=_SEMICOLON then
  1493. begin
  1494. if assigned(fd) then
  1495. Message1(parser_e_type_alread_forward,n)
  1496. else
  1497. begin
  1498. if is_objectpascal_helper(current_structdef) then
  1499. consume(_FOR);
  1500. { add to the list of definitions to check that the forward
  1501. is resolved. this is required for delphi mode }
  1502. current_module.checkforwarddefs.add(current_structdef);
  1503. symtablestack.push(current_structdef.symtable);
  1504. insert_generic_parameter_types(current_structdef,genericdef,genericlist,false);
  1505. { when we are parsing a generic already then this is a generic as
  1506. well }
  1507. if old_parse_generic then
  1508. include(current_structdef.defoptions,df_generic);
  1509. parse_generic:=(df_generic in current_structdef.defoptions);
  1510. { *don't* add the strict private symbol for non-Delphi modes for
  1511. forward defs }
  1512. symtablestack.pop(current_structdef.symtable);
  1513. end;
  1514. end
  1515. else
  1516. begin
  1517. { change objccategories into objcclass helpers }
  1518. if (objecttype=odt_objccategory) then
  1519. begin
  1520. current_objectdef.objecttype:=odt_objcclass;
  1521. include(current_structdef.objectoptions,oo_is_classhelper);
  1522. end;
  1523. { include the class helper flag for Object Pascal helpers }
  1524. if (objecttype=odt_helper) then
  1525. include(current_objectdef.objectoptions,oo_is_classhelper);
  1526. { parse list of options (abstract / sealed) }
  1527. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava]) then
  1528. parse_object_options;
  1529. symtablestack.push(current_structdef.symtable);
  1530. insert_generic_parameter_types(current_structdef,genericdef,genericlist,assigned(fd));
  1531. { when we are parsing a generic already then this is a generic as
  1532. well }
  1533. if old_parse_generic then
  1534. include(current_structdef.defoptions, df_generic);
  1535. parse_generic:=(df_generic in current_structdef.defoptions);
  1536. { in non-Delphi modes we need a strict private symbol without type
  1537. count and type parameters in the name to simplify resolving }
  1538. maybe_insert_generic_rename_symbol(n,genericlist);
  1539. { parse list of parent classes }
  1540. { for record helpers in mode Delphi this is not allowed }
  1541. if not (is_objectpascal_helper(current_objectdef) and
  1542. (m_delphi in current_settings.modeswitches) and
  1543. (helpertype=ht_record)) then
  1544. parse_parent_classes;
  1545. { for external classes we remove the external flag here already }
  1546. if oo_is_external in current_objectdef.objectoptions then
  1547. exclude(current_objectdef.objectoptions,oo_is_forward);
  1548. { parse extended type for helpers }
  1549. if is_objectpascal_helper(current_structdef) then
  1550. parse_extended_type(helpertype);
  1551. if is_interface(current_objectdef) and
  1552. is_interface(current_objectdef.childof) and
  1553. (oo_is_invokable in tobjectdef(current_objectdef.childof).objectoptions) then
  1554. include(current_objectdef.objectoptions,oo_is_invokable);
  1555. { parse optional GUID for interfaces }
  1556. parse_guid;
  1557. { classes can handle links to themself not only inside type blocks
  1558. but in const blocks too. Additionally this is needed to parse parameters that are
  1559. specializations of the currently parsed type in basically everything except C++ and
  1560. ObjC classes. To make this possible we need to set their symbols to real defs instead
  1561. of errordef }
  1562. if assigned(objsym) and not (objecttype in [odt_cppclass,odt_objccategory,odt_objcclass,odt_objcprotocol]) then
  1563. begin
  1564. olddef:=ttypesym(objsym).typedef;
  1565. ttypesym(objsym).typedef:=current_structdef;
  1566. current_structdef.typesym:=objsym;
  1567. end
  1568. else
  1569. olddef:=nil;
  1570. { if set explicitly, apply $RTTI directive to current object }
  1571. if current_module.rtti_directive.clause<>rtc_none then
  1572. current_structdef.apply_rtti_directive(current_module.rtti_directive)
  1573. else
  1574. { if not set, and class has a parent, take parent object settings }
  1575. if (objectType = odt_class) and assigned(current_objectdef.childof) then
  1576. current_structdef.apply_rtti_directive(current_objectdef.childof.rtti);
  1577. { generate TObject VMT space }
  1578. { We must insert the VMT at the start for system.tobject, and class_tobject was already set.
  1579. The cs_compilesystem is superfluous, but we add it for safety.
  1580. }
  1581. if (current_objectdef=class_tobject) and (cs_compilesystem in current_settings.moduleswitches) then
  1582. current_objectdef.insertvmt;
  1583. { parse and insert object members }
  1584. parse_object_members;
  1585. if assigned(olddef) then
  1586. begin
  1587. ttypesym(objsym).typedef:=olddef;
  1588. current_structdef.typesym:=nil;
  1589. end;
  1590. if not(oo_is_external in current_structdef.objectoptions) then
  1591. begin
  1592. { In Java, constructors are not automatically inherited (so you can
  1593. hide them). Emulate the Pascal behaviour for classes implemented
  1594. in Pascal (we cannot do it for classes implemented in Java, since
  1595. we obviously cannot add constructors to those) }
  1596. if is_javaclass(current_structdef) then
  1597. begin
  1598. add_missing_parent_constructors_intf(tobjectdef(current_structdef),true,vis_none);
  1599. {$ifdef jvm}
  1600. maybe_add_public_default_java_constructor(tobjectdef(current_structdef));
  1601. jvm_wrap_virtual_class_methods(tobjectdef(current_structdef));
  1602. {$endif}
  1603. end;
  1604. { need method to hold the initialization code for typed constants? }
  1605. if (target_info.system in systems_typed_constants_node_init) and
  1606. not is_any_interface_kind(current_structdef) then
  1607. add_typedconst_init_routine(current_structdef);
  1608. end;
  1609. symtablestack.pop(current_structdef.symtable);
  1610. exclude(current_structdef.objectoptions,oo_is_forward);
  1611. end;
  1612. { generate vmt space if needed }
  1613. {
  1614. Here we only do it for non-classes, all classes have it since they depend on TObject
  1615. so their vmt is already created when TObject was parsed.
  1616. }
  1617. if not(oo_has_vmt in current_structdef.objectoptions) and
  1618. not(current_objectdef.objecttype in [odt_class]) and
  1619. not(oo_is_forward in current_structdef.objectoptions) and
  1620. not(parse_generic) and
  1621. { no vmt for helpers ever }
  1622. not is_objectpascal_helper(current_structdef) and
  1623. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[])
  1624. then
  1625. current_objectdef.insertvmt;
  1626. { for implemented classes with a vmt check if there is a constructor }
  1627. if (oo_has_vmt in current_structdef.objectoptions) and
  1628. not(oo_is_forward in current_structdef.objectoptions) and
  1629. not(oo_has_constructor in current_structdef.objectoptions) and
  1630. not is_objc_class_or_protocol(current_structdef) and
  1631. not is_java_class_or_interface(current_structdef) then
  1632. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1633. if is_interface(current_structdef) or
  1634. is_objcprotocol(current_structdef) or
  1635. is_javainterface(current_structdef) then
  1636. setinterfacemethodoptions
  1637. else if is_objcclass(current_structdef) then
  1638. setobjcclassmethodoptions;
  1639. { we need to add this helper to the extendeddefs of the current module,
  1640. as the global and static symtable are not pushed onto the symtable
  1641. stack again (it will be removed when poping the symtable) }
  1642. if is_objectpascal_helper(current_structdef) and
  1643. (current_objectdef.extendeddef.typ<>errordef) then
  1644. begin
  1645. { the topmost symtable must be a static symtable }
  1646. st:=current_structdef.owner;
  1647. while st.symtabletype in [objectsymtable,recordsymtable] do
  1648. st:=st.defowner.owner;
  1649. if st.symtabletype in [staticsymtable,globalsymtable] then
  1650. begin
  1651. if current_objectdef.extendeddef.typ in [recorddef,objectdef] then
  1652. s:=make_mangledname('',tabstractrecorddef(current_objectdef.extendeddef).symtable,'')
  1653. else
  1654. s:=make_mangledname('',current_objectdef.extendeddef.owner,current_objectdef.extendeddef.typesym.name);
  1655. Message1(sym_d_adding_helper_for,s);
  1656. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1657. if not assigned(list) then
  1658. begin
  1659. list:=TFPObjectList.Create(false);
  1660. current_module.extendeddefs.Add(s, list);
  1661. end;
  1662. list.add(current_structdef);
  1663. end;
  1664. end;
  1665. tabstractrecordsymtable(current_objectdef.symtable).addalignmentpadding;
  1666. { return defined objectdef }
  1667. result:=current_objectdef;
  1668. { restore old state }
  1669. current_structdef:=old_current_structdef;
  1670. current_genericdef:=old_current_genericdef;
  1671. current_specializedef:=old_current_specializedef;
  1672. parse_generic:=old_parse_generic;
  1673. end;
  1674. end.