2
0

pdecobj.pas 74 KB

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