pdecobj.pas 71 KB

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