pdecobj.pas 66 KB

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