pdecobj.pas 75 KB

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