pdecobj.pas 70 KB

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