pdecobj.pas 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  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,[],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. 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. flags : tparse_proc_flags;
  841. begin
  842. case token of
  843. _PROCEDURE,
  844. _FUNCTION:
  845. begin
  846. if (astruct.symtable.currentvisibility=vis_published) and
  847. not(oo_can_have_published in astruct.objectoptions) then
  848. Message(parser_e_cant_have_published);
  849. oldparse_only:=parse_only;
  850. parse_only:=true;
  851. flags:=[];
  852. if is_classdef then
  853. include(flags,ppf_classmethod);
  854. if hadgeneric then
  855. include(flags,ppf_generic);
  856. result:=parse_proc_dec(flags,astruct);
  857. { this is for error recovery as well as forward }
  858. { interface mappings, i.e. mapping to a method }
  859. { which isn't declared yet }
  860. if assigned(result) then
  861. begin
  862. parse_object_proc_directives(result);
  863. { check if dispid is set }
  864. if is_dispinterface(result.struct) and not (po_dispid in result.procoptions) then
  865. begin
  866. result.dispid:=tobjectdef(result.struct).get_next_dispid;
  867. include(result.procoptions, po_dispid);
  868. end;
  869. { all Macintosh Object Pascal methods are virtual. }
  870. { this can't be a class method, because macpas mode }
  871. { has no m_class }
  872. if (m_mac in current_settings.modeswitches) then
  873. include(result.procoptions,po_virtualmethod);
  874. { for record and type helpers only static class methods are
  875. allowed }
  876. if is_objectpascal_helper(astruct) and
  877. (
  878. (tobjectdef(astruct).extendeddef.typ<>objectdef) or
  879. (tobjectdef(tobjectdef(astruct).extendeddef).objecttype<>odt_class)
  880. ) and
  881. is_classdef and not (po_staticmethod in result.procoptions) then
  882. MessagePos(result.fileinfo,parser_e_class_methods_only_static_in_records);
  883. handle_calling_convention(result,hcc_default_actions_intf_struct);
  884. { add definition to procsym }
  885. proc_add_definition(result);
  886. { add procdef options to objectdef options }
  887. if (po_msgint in result.procoptions) then
  888. include(astruct.objectoptions,oo_has_msgint);
  889. if (po_msgstr in result.procoptions) then
  890. include(astruct.objectoptions,oo_has_msgstr);
  891. if (po_virtualmethod in result.procoptions) then
  892. include(astruct.objectoptions,oo_has_virtual);
  893. if result.is_generic then
  894. astruct.symtable.includeoption(sto_has_generic);
  895. chkcpp(result);
  896. chkobjc(result);
  897. chkjava(result);
  898. end;
  899. maybe_parse_hint_directives(result);
  900. parse_only:=oldparse_only;
  901. end;
  902. _CONSTRUCTOR :
  903. begin
  904. if (astruct.symtable.currentvisibility=vis_published) and
  905. not(oo_can_have_published in astruct.objectoptions) then
  906. Message(parser_e_cant_have_published);
  907. if not is_classdef and not(astruct.symtable.currentvisibility in [vis_public,vis_published]) then
  908. Message(parser_w_constructor_should_be_public);
  909. if is_interface(astruct) then
  910. Message(parser_e_no_con_des_in_interfaces);
  911. { Objective-C does not know the concept of a constructor }
  912. if is_objc_class_or_protocol(astruct) then
  913. Message(parser_e_objc_no_constructor_destructor);
  914. if is_objectpascal_helper(astruct) then
  915. if is_classdef then
  916. { class constructors are not allowed in class helpers }
  917. Message(parser_e_no_class_constructor_in_helpers);
  918. { only 1 class constructor is allowed }
  919. if is_classdef and (oo_has_class_constructor in astruct.objectoptions) then
  920. Message1(parser_e_only_one_class_constructor_allowed, astruct.objrealname^);
  921. oldparse_only:=parse_only;
  922. parse_only:=true;
  923. if is_classdef then
  924. result:=class_constructor_head(current_structdef)
  925. else
  926. begin
  927. result:=constructor_head;
  928. if is_objectpascal_helper(astruct) and
  929. (tobjectdef(astruct).extendeddef.typ<>objectdef) and
  930. (result.minparacount=0) then
  931. { as long as parameterless constructors aren't allowed in records they
  932. aren't allowed in record/type helpers either }
  933. MessagePos(result.procsym.fileinfo,parser_e_no_parameterless_constructor_in_records);
  934. end;
  935. chkcpp(result);
  936. parse_only:=oldparse_only;
  937. end;
  938. _DESTRUCTOR :
  939. begin
  940. if (astruct.symtable.currentvisibility=vis_published) and
  941. not(oo_can_have_published in astruct.objectoptions) then
  942. Message(parser_e_cant_have_published);
  943. if not is_classdef then
  944. if (oo_has_new_destructor in astruct.objectoptions) then
  945. Message(parser_n_only_one_destructor);
  946. if is_interface(astruct) then
  947. Message(parser_e_no_con_des_in_interfaces);
  948. { (class) destructors are not allowed in class helpers }
  949. if is_objectpascal_helper(astruct) then
  950. Message(parser_e_no_destructor_in_records);
  951. if not is_classdef and (astruct.symtable.currentvisibility<>vis_public) then
  952. Message(parser_w_destructor_should_be_public);
  953. { Objective-C does not know the concept of a destructor }
  954. if is_objc_class_or_protocol(astruct) then
  955. Message(parser_e_objc_no_constructor_destructor);
  956. { only 1 class destructor is allowed }
  957. if is_classdef and (oo_has_class_destructor in astruct.objectoptions) then
  958. Message1(parser_e_only_one_class_destructor_allowed, astruct.objrealname^);
  959. oldparse_only:=parse_only;
  960. parse_only:=true;
  961. if is_classdef then
  962. result:=class_destructor_head(current_structdef)
  963. else
  964. result:=destructor_head;
  965. chkcpp(result);
  966. parse_only:=oldparse_only;
  967. end;
  968. else
  969. internalerror(2011032102);
  970. end;
  971. end;
  972. procedure parse_object_members;
  973. var
  974. typedconstswritable: boolean;
  975. object_member_blocktype : tblock_type;
  976. hadgeneric,
  977. fields_allowed, is_classdef, class_fields, is_final, final_fields,
  978. threadvar_fields : boolean;
  979. vdoptions: tvar_dec_options;
  980. fieldlist: tfpobjectlist;
  981. rtti_attrs_def: trtti_attribute_list;
  982. procedure parse_const;
  983. begin
  984. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  985. Message(parser_e_type_var_const_only_in_records_and_classes);
  986. consume(_CONST);
  987. object_member_blocktype:=bt_const;
  988. final_fields:=is_final;
  989. is_final:=false;
  990. end;
  991. procedure parse_var(isthreadvar:boolean);
  992. begin
  993. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass]) and
  994. { Java interfaces can contain static final class vars }
  995. not((current_objectdef.objecttype=odt_interfacejava) and
  996. is_final and is_classdef) then
  997. Message(parser_e_type_var_const_only_in_records_and_classes);
  998. if isthreadvar then
  999. consume(_THREADVAR)
  1000. else
  1001. consume(_VAR);
  1002. fields_allowed:=true;
  1003. object_member_blocktype:=bt_general;
  1004. class_fields:=is_classdef;
  1005. final_fields:=is_final;
  1006. threadvar_fields:=isthreadvar;
  1007. is_classdef:=false;
  1008. is_final:=false;
  1009. end;
  1010. procedure parse_class;
  1011. begin
  1012. is_classdef:=false;
  1013. { read class method/field/property }
  1014. consume(_CLASS);
  1015. { class modifier is only allowed for procedures, functions, }
  1016. { constructors, destructors, fields and properties }
  1017. if not((token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_DESTRUCTOR,_THREADVAR]) or (token=_CONSTRUCTOR)) then
  1018. Message(parser_e_procedure_or_function_expected);
  1019. { Java interfaces can contain final class vars }
  1020. if is_interface(current_structdef) or
  1021. (is_javainterface(current_structdef) and
  1022. (not(is_final) or
  1023. (token<>_VAR))) then
  1024. Message(parser_e_no_static_method_in_interfaces)
  1025. else
  1026. { class methods are also allowed for Objective-C protocols }
  1027. is_classdef:=true;
  1028. end;
  1029. procedure parse_visibility(vis: tvisibility; oo: tobjectoption);
  1030. begin
  1031. { Objective-C and Java classes do not support "published",
  1032. as basically everything is published. }
  1033. if (vis=vis_published) and
  1034. (is_objc_class_or_protocol(current_structdef) or
  1035. is_java_class_or_interface(current_structdef)) then
  1036. Message(parser_e_no_objc_published)
  1037. else if is_interface(current_structdef) or
  1038. is_objc_protocol_or_category(current_structdef) or
  1039. is_javainterface(current_structdef) then
  1040. Message(parser_e_no_access_specifier_in_interfaces);
  1041. current_structdef.symtable.currentvisibility:=vis;
  1042. consume(token);
  1043. if (oo<>oo_none) then
  1044. include(current_structdef.objectoptions,oo);
  1045. fields_allowed:=true;
  1046. is_classdef:=false;
  1047. class_fields:=false;
  1048. threadvar_fields:=false;
  1049. is_final:=false;
  1050. object_member_blocktype:=bt_general;
  1051. end;
  1052. procedure check_unbound_attributes;
  1053. begin
  1054. if assigned(rtti_attrs_def) and (rtti_attrs_def.get_attribute_count>0) then
  1055. Message1(parser_e_unbound_attribute,trtti_attribute(rtti_attrs_def.rtti_attributes[0]).typesym.prettyname);
  1056. rtti_attrs_def.free;
  1057. rtti_attrs_def:=nil;
  1058. end;
  1059. begin
  1060. { empty class declaration ? }
  1061. if (current_objectdef.objecttype in [odt_class,odt_objcclass,odt_javaclass]) and
  1062. (token=_SEMICOLON) then
  1063. exit;
  1064. { in "publishable" classes the default access type is published }
  1065. if (oo_can_have_published in current_structdef.objectoptions) then
  1066. current_structdef.symtable.currentvisibility:=vis_published
  1067. else
  1068. current_structdef.symtable.currentvisibility:=vis_public;
  1069. fields_allowed:=true;
  1070. is_classdef:=false;
  1071. class_fields:=false;
  1072. is_final:=false;
  1073. final_fields:=false;
  1074. rtti_attrs_def:=nil;
  1075. hadgeneric:=false;
  1076. threadvar_fields:=false;
  1077. object_member_blocktype:=bt_general;
  1078. fieldlist:=tfpobjectlist.create(false);
  1079. repeat
  1080. case token of
  1081. _TYPE :
  1082. begin
  1083. check_unbound_attributes;
  1084. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper,odt_javaclass,odt_interfacejava]) then
  1085. Message(parser_e_type_var_const_only_in_records_and_classes);
  1086. consume(_TYPE);
  1087. object_member_blocktype:=bt_type;
  1088. { expect at least one type declaration }
  1089. if token<>_ID then
  1090. consume(_ID);
  1091. end;
  1092. _VAR :
  1093. begin
  1094. check_unbound_attributes;
  1095. rtti_attrs_def := nil;
  1096. parse_var(false);
  1097. { expect at least one var declaration }
  1098. if token<>_ID then
  1099. consume(_ID);
  1100. end;
  1101. _CONST:
  1102. begin
  1103. check_unbound_attributes;
  1104. rtti_attrs_def := nil;
  1105. parse_const;
  1106. { expect at least one constant declaration }
  1107. if token<>_ID then
  1108. consume(_ID);
  1109. end;
  1110. _THREADVAR :
  1111. begin
  1112. check_unbound_attributes;
  1113. if not is_classdef then
  1114. begin
  1115. Message(parser_e_threadvar_must_be_class);
  1116. { for error recovery we enforce class fields }
  1117. is_classdef:=true;
  1118. end;
  1119. parse_var(true);
  1120. { expect at least one threadvar declaration }
  1121. if token<>_ID then
  1122. consume(_ID);
  1123. end;
  1124. _ID :
  1125. begin
  1126. check_unbound_attributes;
  1127. if is_objcprotocol(current_structdef) and
  1128. ((idtoken=_REQUIRED) or
  1129. (idtoken=_OPTIONAL)) then
  1130. begin
  1131. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  1132. consume(idtoken)
  1133. end
  1134. else case idtoken of
  1135. _PRIVATE :
  1136. begin
  1137. parse_visibility(vis_private,oo_has_private);
  1138. end;
  1139. _PROTECTED :
  1140. begin
  1141. parse_visibility(vis_protected,oo_has_protected);
  1142. end;
  1143. _PUBLIC :
  1144. begin
  1145. parse_visibility(vis_public,oo_none);
  1146. end;
  1147. _PUBLISHED :
  1148. begin
  1149. parse_visibility(vis_published,oo_none);
  1150. end;
  1151. _STRICT :
  1152. begin
  1153. if is_interface(current_structdef) or
  1154. is_objc_protocol_or_category(current_structdef) or
  1155. is_javainterface(current_structdef) then
  1156. Message(parser_e_no_access_specifier_in_interfaces);
  1157. consume(_STRICT);
  1158. if token=_ID then
  1159. begin
  1160. case idtoken of
  1161. _PRIVATE:
  1162. begin
  1163. consume(_PRIVATE);
  1164. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  1165. include(current_structdef.objectoptions,oo_has_strictprivate);
  1166. end;
  1167. _PROTECTED:
  1168. begin
  1169. consume(_PROTECTED);
  1170. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  1171. include(current_structdef.objectoptions,oo_has_strictprotected);
  1172. end;
  1173. else
  1174. message(parser_e_protected_or_private_expected);
  1175. end;
  1176. end
  1177. else
  1178. message(parser_e_protected_or_private_expected);
  1179. fields_allowed:=true;
  1180. is_classdef:=false;
  1181. class_fields:=false;
  1182. threadvar_fields:=false;
  1183. is_final:=false;
  1184. final_fields:=false;
  1185. object_member_blocktype:=bt_general;
  1186. end
  1187. else if (m_final_fields in current_settings.modeswitches) and
  1188. (token=_ID) and
  1189. (idtoken=_FINAL) then
  1190. begin
  1191. { currently only supported for external classes, because
  1192. requires fully working DFA otherwise }
  1193. if (current_structdef.typ<>objectdef) or
  1194. not(oo_is_external in tobjectdef(current_structdef).objectoptions) then
  1195. Message(parser_e_final_only_external);
  1196. consume(_final);
  1197. is_final:=true;
  1198. if token=_CLASS then
  1199. parse_class;
  1200. if not(token in [_CONST,_VAR]) then
  1201. message(parser_e_final_only_const_var);
  1202. end
  1203. else
  1204. begin
  1205. if object_member_blocktype=bt_general then
  1206. begin
  1207. rtti_attrs_def := nil;
  1208. if (idtoken=_GENERIC) and
  1209. not (m_delphi in current_settings.modeswitches) and
  1210. (
  1211. not fields_allowed or
  1212. is_objectpascal_helper(current_structdef)
  1213. ) then
  1214. begin
  1215. if hadgeneric then
  1216. Message(parser_e_procedure_or_function_expected);
  1217. consume(_ID);
  1218. hadgeneric:=true;
  1219. if not (token in [_PROCEDURE,_FUNCTION,_CLASS]) then
  1220. Message(parser_e_procedure_or_function_expected);
  1221. end
  1222. else
  1223. begin
  1224. if is_interface(current_structdef) or
  1225. is_objc_protocol_or_category(current_structdef) or
  1226. (
  1227. is_objectpascal_helper(current_structdef) and
  1228. not class_fields
  1229. ) or
  1230. (is_javainterface(current_structdef) and
  1231. not(class_fields and final_fields)) then
  1232. Message(parser_e_no_vars_in_interfaces);
  1233. if (current_structdef.symtable.currentvisibility=vis_published) and
  1234. not(oo_can_have_published in current_structdef.objectoptions) then
  1235. Message(parser_e_cant_have_published);
  1236. if (not fields_allowed) then
  1237. Message(parser_e_field_not_allowed_here);
  1238. vdoptions:=[vd_object];
  1239. if not (m_delphi in current_settings.modeswitches) then
  1240. include(vdoptions,vd_check_generic);
  1241. if class_fields then
  1242. include(vdoptions,vd_class);
  1243. if is_class(current_structdef) then
  1244. include(vdoptions,vd_canreorder);
  1245. if final_fields then
  1246. include(vdoptions,vd_final);
  1247. if threadvar_fields then
  1248. include(vdoptions,vd_threadvar);
  1249. read_record_fields(vdoptions,fieldlist,nil,hadgeneric);
  1250. end;
  1251. end
  1252. else if object_member_blocktype=bt_type then
  1253. types_dec(true,hadgeneric, rtti_attrs_def)
  1254. else if object_member_blocktype=bt_const then
  1255. begin
  1256. typedconstswritable:=false;
  1257. if final_fields then
  1258. begin
  1259. { the value of final fields cannot be changed
  1260. once they've been assigned a value }
  1261. typedconstswritable:=cs_typed_const_writable in current_settings.localswitches;
  1262. exclude(current_settings.localswitches,cs_typed_const_writable);
  1263. end;
  1264. consts_dec(true,not is_javainterface(current_structdef),hadgeneric);
  1265. if final_fields and
  1266. typedconstswritable then
  1267. include(current_settings.localswitches,cs_typed_const_writable);
  1268. end
  1269. else
  1270. internalerror(2010011103);
  1271. end;
  1272. end;
  1273. end;
  1274. _PROPERTY :
  1275. begin
  1276. { for now attributes are only allowed on published properties }
  1277. if current_structdef.symtable.currentvisibility<>vis_published then
  1278. check_unbound_attributes;
  1279. struct_property_dec(is_classdef, rtti_attrs_def);
  1280. fields_allowed:=false;
  1281. is_classdef:=false;
  1282. end;
  1283. _CLASS:
  1284. begin
  1285. { class properties currently can't have attributes, so it's safe
  1286. to check for unbound attributes here }
  1287. check_unbound_attributes;
  1288. parse_class;
  1289. end;
  1290. _PROCEDURE,
  1291. _FUNCTION,
  1292. _CONSTRUCTOR,
  1293. _DESTRUCTOR :
  1294. begin
  1295. check_unbound_attributes;
  1296. rtti_attrs_def := nil;
  1297. method_dec(current_structdef,is_classdef,hadgeneric);
  1298. fields_allowed:=false;
  1299. is_classdef:=false;
  1300. hadgeneric:=false;
  1301. end;
  1302. _LECKKLAMMER:
  1303. begin
  1304. if m_prefixed_attributes in current_settings.modeswitches then
  1305. parse_rttiattributes(rtti_attrs_def)
  1306. else
  1307. consume(_ID);
  1308. end;
  1309. _END :
  1310. begin
  1311. check_unbound_attributes;
  1312. consume(_END);
  1313. break;
  1314. end;
  1315. else
  1316. consume(_ID); { Give a ident expected message, like tp7 }
  1317. end;
  1318. until false;
  1319. if is_class(current_structdef) then
  1320. tabstractrecordsymtable(current_structdef.symtable).addfieldlist(fieldlist,true);
  1321. fieldlist.free;
  1322. end;
  1323. function object_dec(objecttype:tobjecttyp;const n:tidstring;objsym:tsym;genericdef:tstoreddef;genericlist:tfphashobjectlist;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  1324. var
  1325. old_current_structdef: tabstractrecorddef;
  1326. old_current_genericdef,
  1327. old_current_specializedef: tstoreddef;
  1328. old_parse_generic: boolean;
  1329. list: TFPObjectList;
  1330. s: TSymStr;
  1331. st: TSymtable;
  1332. olddef: tdef;
  1333. begin
  1334. old_current_structdef:=current_structdef;
  1335. old_current_genericdef:=current_genericdef;
  1336. old_current_specializedef:=current_specializedef;
  1337. old_parse_generic:=parse_generic;
  1338. current_structdef:=nil;
  1339. current_genericdef:=nil;
  1340. current_specializedef:=nil;
  1341. { objects and class types can't be declared local }
  1342. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  1343. not assigned(genericlist) then
  1344. Message(parser_e_no_local_objects);
  1345. { reuse forward objectdef? }
  1346. if assigned(fd) then
  1347. begin
  1348. if (fd.objecttype<>objecttype) or ((fd.is_generic or fd.is_specialization) xor assigned(genericlist)) then
  1349. begin
  1350. Message(parser_e_forward_mismatch);
  1351. { recover }
  1352. current_structdef:=cobjectdef.create(objecttype,n,nil,true);
  1353. include(current_structdef.objectoptions,oo_is_forward);
  1354. end
  1355. else
  1356. current_structdef:=fd
  1357. end
  1358. else
  1359. begin
  1360. { anonym objects aren't allow (o : object a : longint; end;) }
  1361. if n='' then
  1362. Message(parser_f_no_anonym_objects);
  1363. { create new class }
  1364. current_structdef:=cobjectdef.create(objecttype,n,nil,true);
  1365. tobjectdef(current_structdef).helpertype:=helpertype;
  1366. { include always the forward flag, it'll be removed after the parent class have been
  1367. added. This is to prevent circular childof loops }
  1368. include(current_structdef.objectoptions,oo_is_forward);
  1369. if (cs_compilesystem in current_settings.moduleswitches) then
  1370. begin
  1371. case current_objectdef.objecttype of
  1372. odt_interfacecom :
  1373. if (current_structdef.objname^='IUNKNOWN') then
  1374. interface_iunknown:=current_objectdef
  1375. else
  1376. if (current_structdef.objname^='IDISPATCH') then
  1377. interface_idispatch:=current_objectdef;
  1378. odt_class :
  1379. if (current_structdef.objname^='TOBJECT') then
  1380. class_tobject:=current_objectdef;
  1381. odt_javaclass:
  1382. begin
  1383. if (current_structdef.objname^='TOBJECT') then
  1384. class_tobject:=current_objectdef
  1385. else if (current_objectdef.objname^='JLOBJECT') then
  1386. java_jlobject:=current_objectdef
  1387. else if (current_objectdef.objname^='JLTHROWABLE') then
  1388. java_jlthrowable:=current_objectdef
  1389. else if (current_objectdef.objname^='FPCBASERECORDTYPE') then
  1390. java_fpcbaserecordtype:=current_objectdef
  1391. else if (current_objectdef.objname^='JLSTRING') then
  1392. java_jlstring:=current_objectdef
  1393. else if (current_objectdef.objname^='ANSISTRINGCLASS') then
  1394. java_ansistring:=current_objectdef
  1395. else if (current_objectdef.objname^='SHORTSTRINGCLASS') then
  1396. java_shortstring:=current_objectdef
  1397. else if (current_objectdef.objname^='JLENUM') then
  1398. java_jlenum:=current_objectdef
  1399. else if (current_objectdef.objname^='JUENUMSET') then
  1400. java_juenumset:=current_objectdef
  1401. else if (current_objectdef.objname^='FPCBITSET') then
  1402. java_jubitset:=current_objectdef
  1403. else if (current_objectdef.objname^='FPCBASEPROCVARTYPE') then
  1404. java_procvarbase:=current_objectdef;
  1405. end;
  1406. else
  1407. ;
  1408. end;
  1409. end;
  1410. if (current_module.modulename^='OBJCBASE') then
  1411. begin
  1412. case current_objectdef.objecttype of
  1413. odt_objcclass:
  1414. if (current_objectdef.objname^='Protocol') then
  1415. objc_protocoltype:=current_objectdef;
  1416. else
  1417. ;
  1418. end;
  1419. end;
  1420. end;
  1421. { usage of specialized type inside its generic template }
  1422. if assigned(genericdef) then
  1423. current_specializedef:=current_structdef;
  1424. { reject declaration of generic class inside generic class }
  1425. if assigned(genericlist) then
  1426. current_genericdef:=current_structdef;
  1427. { nested types of specializations are specializations as well }
  1428. if assigned(old_current_structdef) and
  1429. (df_specialization in old_current_structdef.defoptions) then
  1430. include(current_structdef.defoptions,df_specialization);
  1431. if assigned(old_current_structdef) and
  1432. (df_generic in old_current_structdef.defoptions) then
  1433. begin
  1434. include(current_structdef.defoptions,df_generic);
  1435. current_genericdef:=current_structdef;
  1436. end;
  1437. { set published flag in $M+ mode, it can also be inherited and will
  1438. be added when the parent class set with tobjectdef.set_parent (PFV) }
  1439. if (cs_generate_rtti in current_settings.localswitches) and
  1440. (current_objectdef.objecttype in [odt_interfacecom,odt_class,odt_helper]) then
  1441. include(current_structdef.objectoptions,oo_can_have_published);
  1442. { Objective-C/Java objectdefs can be "formal definitions", in which case
  1443. the syntax is "type tc = objcclass external;" -> we have to parse
  1444. its object options (external) already here, to make sure that such
  1445. definitions are recognised as formal defs }
  1446. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  1447. parse_object_options;
  1448. { forward def? }
  1449. if not assigned(fd) and
  1450. (token=_SEMICOLON) then
  1451. begin
  1452. if is_objectpascal_helper(current_structdef) then
  1453. consume(_FOR);
  1454. { add to the list of definitions to check that the forward
  1455. is resolved. this is required for delphi mode }
  1456. current_module.checkforwarddefs.add(current_structdef);
  1457. symtablestack.push(current_structdef.symtable);
  1458. insert_generic_parameter_types(current_structdef,genericdef,genericlist,false);
  1459. { when we are parsing a generic already then this is a generic as
  1460. well }
  1461. if old_parse_generic then
  1462. include(current_structdef.defoptions,df_generic);
  1463. parse_generic:=(df_generic in current_structdef.defoptions);
  1464. { *don't* add the strict private symbol for non-Delphi modes for
  1465. forward defs }
  1466. symtablestack.pop(current_structdef.symtable);
  1467. end
  1468. else
  1469. begin
  1470. { change objccategories into objcclass helpers }
  1471. if (objecttype=odt_objccategory) then
  1472. begin
  1473. current_objectdef.objecttype:=odt_objcclass;
  1474. include(current_structdef.objectoptions,oo_is_classhelper);
  1475. end;
  1476. { include the class helper flag for Object Pascal helpers }
  1477. if (objecttype=odt_helper) then
  1478. include(current_objectdef.objectoptions,oo_is_classhelper);
  1479. { parse list of options (abstract / sealed) }
  1480. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava]) then
  1481. parse_object_options;
  1482. symtablestack.push(current_structdef.symtable);
  1483. insert_generic_parameter_types(current_structdef,genericdef,genericlist,assigned(fd));
  1484. { when we are parsing a generic already then this is a generic as
  1485. well }
  1486. if old_parse_generic then
  1487. include(current_structdef.defoptions, df_generic);
  1488. parse_generic:=(df_generic in current_structdef.defoptions);
  1489. { in non-Delphi modes we need a strict private symbol without type
  1490. count and type parameters in the name to simplify resolving }
  1491. maybe_insert_generic_rename_symbol(n,genericlist);
  1492. { parse list of parent classes }
  1493. { for record helpers in mode Delphi this is not allowed }
  1494. if not (is_objectpascal_helper(current_objectdef) and
  1495. (m_delphi in current_settings.modeswitches) and
  1496. (helpertype=ht_record)) then
  1497. parse_parent_classes
  1498. else
  1499. { remove forward flag, is resolved (this is normally done inside
  1500. parse_parent_classes) }
  1501. exclude(current_structdef.objectoptions,oo_is_forward);
  1502. { parse extended type for helpers }
  1503. if is_objectpascal_helper(current_structdef) then
  1504. parse_extended_type(helpertype);
  1505. { parse optional GUID for interfaces }
  1506. parse_guid;
  1507. { classes can handle links to themself not only inside type blocks
  1508. but in const blocks too. Additionally this is needed to parse parameters that are
  1509. specializations of the currently parsed type in basically everything except C++ and
  1510. ObjC classes. To make this possible we need to set their symbols to real defs instead
  1511. of errordef }
  1512. if assigned(objsym) and not (objecttype in [odt_cppclass,odt_objccategory,odt_objcclass,odt_objcprotocol]) then
  1513. begin
  1514. olddef:=ttypesym(objsym).typedef;
  1515. ttypesym(objsym).typedef:=current_structdef;
  1516. current_structdef.typesym:=objsym;
  1517. end
  1518. else
  1519. olddef:=nil;
  1520. { parse and insert object members }
  1521. parse_object_members;
  1522. if assigned(olddef) then
  1523. begin
  1524. ttypesym(objsym).typedef:=olddef;
  1525. current_structdef.typesym:=nil;
  1526. end;
  1527. if not(oo_is_external in current_structdef.objectoptions) then
  1528. begin
  1529. { In Java, constructors are not automatically inherited (so you can
  1530. hide them). Emulate the Pascal behaviour for classes implemented
  1531. in Pascal (we cannot do it for classes implemented in Java, since
  1532. we obviously cannot add constructors to those) }
  1533. if is_javaclass(current_structdef) then
  1534. begin
  1535. add_missing_parent_constructors_intf(tobjectdef(current_structdef),true,vis_none);
  1536. {$ifdef jvm}
  1537. maybe_add_public_default_java_constructor(tobjectdef(current_structdef));
  1538. jvm_wrap_virtual_class_methods(tobjectdef(current_structdef));
  1539. {$endif}
  1540. end;
  1541. { need method to hold the initialization code for typed constants? }
  1542. if (target_info.system in systems_typed_constants_node_init) and
  1543. not is_any_interface_kind(current_structdef) then
  1544. add_typedconst_init_routine(current_structdef);
  1545. end;
  1546. symtablestack.pop(current_structdef.symtable);
  1547. end;
  1548. { generate vmt space if needed }
  1549. if not(oo_has_vmt in current_structdef.objectoptions) and
  1550. not(oo_is_forward in current_structdef.objectoptions) and
  1551. not(parse_generic) and
  1552. { no vmt for helpers ever }
  1553. not is_objectpascal_helper(current_structdef) and
  1554. (
  1555. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1556. (current_objectdef.objecttype in [odt_class])
  1557. ) then
  1558. current_objectdef.insertvmt;
  1559. { for implemented classes with a vmt check if there is a constructor }
  1560. if (oo_has_vmt in current_structdef.objectoptions) and
  1561. not(oo_is_forward in current_structdef.objectoptions) and
  1562. not(oo_has_constructor in current_structdef.objectoptions) and
  1563. not is_objc_class_or_protocol(current_structdef) and
  1564. not is_java_class_or_interface(current_structdef) then
  1565. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1566. if is_interface(current_structdef) or
  1567. is_objcprotocol(current_structdef) or
  1568. is_javainterface(current_structdef) then
  1569. setinterfacemethodoptions
  1570. else if is_objcclass(current_structdef) then
  1571. setobjcclassmethodoptions;
  1572. { we need to add this helper to the extendeddefs of the current module,
  1573. as the global and static symtable are not pushed onto the symtable
  1574. stack again (it will be removed when poping the symtable) }
  1575. if is_objectpascal_helper(current_structdef) and
  1576. (current_objectdef.extendeddef.typ<>errordef) then
  1577. begin
  1578. { the topmost symtable must be a static symtable }
  1579. st:=current_structdef.owner;
  1580. while st.symtabletype in [objectsymtable,recordsymtable] do
  1581. st:=st.defowner.owner;
  1582. if st.symtabletype in [staticsymtable,globalsymtable] then
  1583. begin
  1584. if current_objectdef.extendeddef.typ in [recorddef,objectdef] then
  1585. s:=make_mangledname('',tabstractrecorddef(current_objectdef.extendeddef).symtable,'')
  1586. else
  1587. s:=make_mangledname('',current_objectdef.extendeddef.owner,current_objectdef.extendeddef.typesym.name);
  1588. Message1(sym_d_adding_helper_for,s);
  1589. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1590. if not assigned(list) then
  1591. begin
  1592. list:=TFPObjectList.Create(false);
  1593. current_module.extendeddefs.Add(s, list);
  1594. end;
  1595. list.add(current_structdef);
  1596. end;
  1597. end;
  1598. tabstractrecordsymtable(current_objectdef.symtable).addalignmentpadding;
  1599. { return defined objectdef }
  1600. result:=current_objectdef;
  1601. { restore old state }
  1602. current_structdef:=old_current_structdef;
  1603. current_genericdef:=old_current_genericdef;
  1604. current_specializedef:=old_current_specializedef;
  1605. parse_generic:=old_parse_generic;
  1606. end;
  1607. end.