pdecobj.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tobjectdef;
  25. function class_constructor_head:tprocdef;
  26. function class_destructor_head:tprocdef;
  27. function constructor_head:tprocdef;
  28. function destructor_head:tprocdef;
  29. procedure struct_property_dec(is_classproperty:boolean);
  30. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  31. implementation
  32. uses
  33. sysutils,cutils,
  34. globals,verbose,systems,tokens,
  35. symbase,symsym,symtable,
  36. node,nld,nmem,ncon,ncnv,ncal,
  37. fmodule,scanner,
  38. pbase,pexpr,pdecsub,pdecvar,ptype,pdecl,ppu
  39. ;
  40. const
  41. { Please leave this here, this module should NOT use
  42. these variables.
  43. Declaring it as string here results in an error when compiling (PFV) }
  44. current_procinfo = 'error';
  45. var
  46. current_objectdef : tobjectdef absolute current_structdef;
  47. function class_constructor_head:tprocdef;
  48. var
  49. pd : tprocdef;
  50. begin
  51. result:=nil;
  52. consume(_CONSTRUCTOR);
  53. { must be at same level as in implementation }
  54. parse_proc_head(current_structdef,potype_class_constructor,pd);
  55. if not assigned(pd) then
  56. begin
  57. consume(_SEMICOLON);
  58. exit;
  59. end;
  60. pd.calcparas;
  61. if (pd.maxparacount>0) then
  62. Message(parser_e_no_paras_for_class_constructor);
  63. consume(_SEMICOLON);
  64. include(current_structdef.objectoptions,oo_has_class_constructor);
  65. current_module.flags:=current_module.flags or uf_classinits;
  66. { no return value }
  67. pd.returndef:=voidtype;
  68. result:=pd;
  69. end;
  70. function constructor_head:tprocdef;
  71. var
  72. pd : tprocdef;
  73. begin
  74. result:=nil;
  75. consume(_CONSTRUCTOR);
  76. { must be at same level as in implementation }
  77. parse_proc_head(current_structdef,potype_constructor,pd);
  78. if not assigned(pd) then
  79. begin
  80. consume(_SEMICOLON);
  81. exit;
  82. end;
  83. if (cs_constructor_name in current_settings.globalswitches) and
  84. (pd.procsym.name<>'INIT') then
  85. Message(parser_e_constructorname_must_be_init);
  86. consume(_SEMICOLON);
  87. include(current_structdef.objectoptions,oo_has_constructor);
  88. { Set return type, class and record constructors return the
  89. created instance, object constructors return boolean }
  90. if is_class(pd.struct) or is_record(pd.struct) then
  91. pd.returndef:=pd.struct
  92. else
  93. {$ifdef CPU64bitaddr}
  94. pd.returndef:=bool64type;
  95. {$else CPU64bitaddr}
  96. pd.returndef:=bool32type;
  97. {$endif CPU64bitaddr}
  98. result:=pd;
  99. end;
  100. procedure struct_property_dec(is_classproperty:boolean);
  101. var
  102. p : tpropertysym;
  103. begin
  104. { check for a class or record }
  105. if not((is_class_or_interface_or_dispinterface(current_structdef) or is_record(current_structdef)) or
  106. (not(m_tp7 in current_settings.modeswitches) and (is_object(current_structdef)))) then
  107. Message(parser_e_syntax_error);
  108. consume(_PROPERTY);
  109. p:=read_property_dec(is_classproperty,current_structdef);
  110. consume(_SEMICOLON);
  111. if try_to_consume(_DEFAULT) then
  112. begin
  113. if oo_has_default_property in current_structdef.objectoptions then
  114. message(parser_e_only_one_default_property);
  115. include(current_structdef.objectoptions,oo_has_default_property);
  116. include(p.propoptions,ppo_defaultproperty);
  117. if not(ppo_hasparameters in p.propoptions) then
  118. message(parser_e_property_need_paras);
  119. if (token=_COLON) then
  120. begin
  121. Message(parser_e_field_not_allowed_here);
  122. consume_all_until(_SEMICOLON);
  123. end;
  124. consume(_SEMICOLON);
  125. end;
  126. { parse possible enumerator modifier }
  127. if try_to_consume(_ENUMERATOR) then
  128. begin
  129. if (token = _ID) then
  130. begin
  131. if pattern='CURRENT' then
  132. begin
  133. if oo_has_enumerator_current in current_structdef.objectoptions then
  134. message(parser_e_only_one_enumerator_current);
  135. if not p.propaccesslist[palt_read].empty then
  136. begin
  137. include(current_structdef.objectoptions,oo_has_enumerator_current);
  138. include(p.propoptions,ppo_enumerator_current);
  139. end
  140. else
  141. Message(parser_e_enumerator_current_is_not_valid) // property has no reader
  142. end
  143. else
  144. Message1(parser_e_invalid_enumerator_identifier, pattern);
  145. consume(token);
  146. end
  147. else
  148. Message(parser_e_enumerator_identifier_required);
  149. consume(_SEMICOLON);
  150. end;
  151. { hint directives, these can be separated by semicolons here,
  152. that needs to be handled here with a loop (PFV) }
  153. while try_consume_hintdirective(p.symoptions,p.deprecatedmsg) do
  154. Consume(_SEMICOLON);
  155. end;
  156. function class_destructor_head:tprocdef;
  157. var
  158. pd : tprocdef;
  159. begin
  160. result:=nil;
  161. consume(_DESTRUCTOR);
  162. parse_proc_head(current_structdef,potype_class_destructor,pd);
  163. if not assigned(pd) then
  164. begin
  165. consume(_SEMICOLON);
  166. exit;
  167. end;
  168. pd.calcparas;
  169. if (pd.maxparacount>0) then
  170. Message(parser_e_no_paras_for_class_destructor);
  171. consume(_SEMICOLON);
  172. include(current_structdef.objectoptions,oo_has_class_destructor);
  173. current_module.flags:=current_module.flags or uf_classinits;
  174. { no return value }
  175. pd.returndef:=voidtype;
  176. result:=pd;
  177. end;
  178. function destructor_head:tprocdef;
  179. var
  180. pd : tprocdef;
  181. begin
  182. result:=nil;
  183. consume(_DESTRUCTOR);
  184. parse_proc_head(current_structdef,potype_destructor,pd);
  185. if not assigned(pd) then
  186. begin
  187. consume(_SEMICOLON);
  188. exit;
  189. end;
  190. if (cs_constructor_name in current_settings.globalswitches) and
  191. (pd.procsym.name<>'DONE') then
  192. Message(parser_e_destructorname_must_be_done);
  193. pd.calcparas;
  194. if not(pd.maxparacount=0) and
  195. (m_fpc in current_settings.modeswitches) then
  196. Message(parser_e_no_paras_for_destructor);
  197. consume(_SEMICOLON);
  198. include(current_structdef.objectoptions,oo_has_destructor);
  199. { no return value }
  200. pd.returndef:=voidtype;
  201. result:=pd;
  202. end;
  203. procedure setinterfacemethodoptions;
  204. var
  205. i : longint;
  206. def : tdef;
  207. begin
  208. include(current_structdef.objectoptions,oo_has_virtual);
  209. for i:=0 to current_structdef.symtable.DefList.count-1 do
  210. begin
  211. def:=tdef(current_structdef.symtable.DefList[i]);
  212. if assigned(def) and
  213. (def.typ=procdef) then
  214. begin
  215. include(tprocdef(def).procoptions,po_virtualmethod);
  216. tprocdef(def).forwarddef:=false;
  217. end;
  218. end;
  219. end;
  220. procedure setobjcclassmethodoptions;
  221. var
  222. i : longint;
  223. def : tdef;
  224. begin
  225. for i:=0 to current_structdef.symtable.DefList.count-1 do
  226. begin
  227. def:=tdef(current_structdef.symtable.DefList[i]);
  228. if assigned(def) and
  229. (def.typ=procdef) then
  230. begin
  231. include(tprocdef(def).procoptions,po_virtualmethod);
  232. end;
  233. end;
  234. end;
  235. procedure handleImplementedInterface(intfdef : tobjectdef);
  236. begin
  237. if not is_interface(intfdef) then
  238. begin
  239. Message1(type_e_interface_type_expected,intfdef.typename);
  240. exit;
  241. end;
  242. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  243. Message1(sym_e_duplicate_id,intfdef.objname^)
  244. else
  245. begin
  246. { allocate and prepare the GUID only if the class
  247. implements some interfaces. }
  248. if current_objectdef.ImplementedInterfaces.count = 0 then
  249. current_objectdef.prepareguid;
  250. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  251. end;
  252. end;
  253. procedure handleImplementedProtocol(intfdef : tobjectdef);
  254. begin
  255. if not is_objcprotocol(intfdef) then
  256. begin
  257. Message1(type_e_protocol_type_expected,intfdef.typename);
  258. exit;
  259. end;
  260. if (oo_is_forward in intfdef.objectoptions) then
  261. begin
  262. Message1(parser_e_forward_protocol_declaration_must_be_resolved,intfdef.objrealname^);
  263. exit;
  264. end;
  265. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  266. Message1(sym_e_duplicate_id,intfdef.objname^)
  267. else
  268. begin
  269. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  270. end;
  271. end;
  272. procedure readImplementedInterfacesAndProtocols(intf: boolean);
  273. var
  274. hdef : tdef;
  275. begin
  276. while try_to_consume(_COMMA) do
  277. begin
  278. { use single_type instead of id_type for specialize support }
  279. single_type(hdef,[stoAllowTypeDef,stoParseClassParent]);
  280. if (hdef.typ<>objectdef) then
  281. begin
  282. if intf then
  283. Message1(type_e_interface_type_expected,hdef.typename)
  284. else
  285. Message1(type_e_protocol_type_expected,hdef.typename);
  286. continue;
  287. end;
  288. if intf then
  289. handleImplementedInterface(tobjectdef(hdef))
  290. else
  291. handleImplementedProtocol(tobjectdef(hdef));
  292. end;
  293. end;
  294. procedure readinterfaceiid;
  295. var
  296. p : tnode;
  297. valid : boolean;
  298. begin
  299. p:=comp_expr(true,false);
  300. if p.nodetype=stringconstn then
  301. begin
  302. stringdispose(current_objectdef.iidstr);
  303. current_objectdef.iidstr:=stringdup(strpas(tstringconstnode(p).value_str));
  304. valid:=string2guid(current_objectdef.iidstr^,current_objectdef.iidguid^);
  305. if (current_objectdef.objecttype in [odt_interfacecom,odt_dispinterface]) and
  306. not valid then
  307. Message(parser_e_improper_guid_syntax);
  308. include(current_structdef.objectoptions,oo_has_valid_guid);
  309. end
  310. else
  311. Message(parser_e_illegal_expression);
  312. p.free;
  313. end;
  314. procedure get_cpp_class_external_status(od: tobjectdef);
  315. var
  316. hs: string;
  317. begin
  318. { C++ classes can be external -> all methods inside are external
  319. (defined at the class level instead of per method, so that you cannot
  320. define some methods as external and some not)
  321. }
  322. if try_to_consume(_EXTERNAL) then
  323. begin
  324. if token in [_CSTRING,_CWSTRING,_CCHAR,_CWCHAR] then
  325. begin
  326. { Always add library prefix and suffix to create an uniform name }
  327. hs:=get_stringconst;
  328. if ExtractFileExt(hs)='' then
  329. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  330. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  331. hs:=target_info.sharedlibprefix+hs;
  332. od.import_lib:=stringdup(hs);
  333. end;
  334. include(od.objectoptions, oo_is_external);
  335. { check if we shall use another name for the class }
  336. if try_to_consume(_NAME) then
  337. od.objextname:=stringdup(get_stringconst)
  338. else
  339. od.objextname:=stringdup(od.objrealname^);
  340. include(od.objectoptions,oo_is_external);
  341. end
  342. else
  343. od.objextname:=stringdup(od.objrealname^);
  344. { ToDo: read the namespace of the class (influences the mangled name)}
  345. end;
  346. procedure get_objc_class_or_protocol_external_status(od: tobjectdef);
  347. begin
  348. { Objective-C classes can be external -> all messages inside are
  349. external (defined at the class level instead of per method, so
  350. that you cannot define some methods as external and some not)
  351. }
  352. if try_to_consume(_EXTERNAL) then
  353. begin
  354. if try_to_consume(_NAME) then
  355. od.objextname:=stringdup(get_stringconst)
  356. else
  357. { the external name doesn't matter for formally declared
  358. classes, and allowing to specify one would mean that we would
  359. have to check it for consistency with the actual definition
  360. later on }
  361. od.objextname:=stringdup(od.objrealname^);
  362. include(od.objectoptions,oo_is_external);
  363. end
  364. else
  365. od.objextname:=stringdup(od.objrealname^);
  366. end;
  367. procedure parse_object_options;
  368. begin
  369. case current_objectdef.objecttype of
  370. odt_object,odt_class:
  371. begin
  372. while true do
  373. begin
  374. if try_to_consume(_ABSTRACT) then
  375. include(current_structdef.objectoptions,oo_is_abstract)
  376. else
  377. if try_to_consume(_SEALED) then
  378. include(current_structdef.objectoptions,oo_is_sealed)
  379. else
  380. break;
  381. end;
  382. if [oo_is_abstract, oo_is_sealed] * current_structdef.objectoptions = [oo_is_abstract, oo_is_sealed] then
  383. Message(parser_e_abstract_and_sealed_conflict);
  384. end;
  385. odt_cppclass:
  386. get_cpp_class_external_status(current_objectdef);
  387. odt_objcclass,odt_objcprotocol,odt_objccategory:
  388. get_objc_class_or_protocol_external_status(current_objectdef);
  389. end;
  390. end;
  391. procedure parse_parent_classes;
  392. var
  393. intfchildof,
  394. childof : tobjectdef;
  395. hdef : tdef;
  396. hasparentdefined : boolean;
  397. begin
  398. childof:=nil;
  399. intfchildof:=nil;
  400. hasparentdefined:=false;
  401. { reads the parent class }
  402. if (token=_LKLAMMER) or
  403. is_objccategory(current_structdef) then
  404. begin
  405. consume(_LKLAMMER);
  406. { use single_type instead of id_type for specialize support }
  407. single_type(hdef,[stoAllowTypeDef, stoParseClassParent]);
  408. if (not assigned(hdef)) or
  409. (hdef.typ<>objectdef) then
  410. begin
  411. if assigned(hdef) then
  412. Message1(type_e_class_type_expected,hdef.typename)
  413. else if is_objccategory(current_structdef) then
  414. { a category must specify the class to extend }
  415. Message(type_e_objcclass_type_expected);
  416. end
  417. else
  418. begin
  419. childof:=tobjectdef(hdef);
  420. { a mix of class, interfaces, objects and cppclasses
  421. isn't allowed }
  422. case current_objectdef.objecttype of
  423. odt_class:
  424. if not(is_class(childof)) then
  425. begin
  426. if is_interface(childof) then
  427. begin
  428. { we insert the interface after the child
  429. is set, see below
  430. }
  431. intfchildof:=childof;
  432. childof:=class_tobject;
  433. end
  434. else
  435. Message(parser_e_mix_of_classes_and_objects);
  436. end
  437. else
  438. if oo_is_sealed in childof.objectoptions then
  439. Message1(parser_e_sealed_descendant,childof.typename);
  440. odt_interfacecorba,
  441. odt_interfacecom:
  442. begin
  443. if not(is_interface(childof)) then
  444. Message(parser_e_mix_of_classes_and_objects);
  445. current_objectdef.objecttype:=childof.objecttype;
  446. end;
  447. odt_cppclass:
  448. if not(is_cppclass(childof)) then
  449. Message(parser_e_mix_of_classes_and_objects);
  450. odt_objcclass:
  451. if not(is_objcclass(childof) or
  452. is_objccategory(childof)) then
  453. begin
  454. if is_objcprotocol(childof) then
  455. begin
  456. if not(oo_is_classhelper in current_structdef.objectoptions) then
  457. begin
  458. intfchildof:=childof;
  459. childof:=nil;
  460. CGMessage(parser_h_no_objc_parent);
  461. end
  462. else
  463. { a category must specify the class to extend }
  464. CGMessage(type_e_objcclass_type_expected);
  465. end
  466. else
  467. Message(parser_e_mix_of_classes_and_objects);
  468. end
  469. else
  470. childof:=find_real_objcclass_definition(childof,true);
  471. odt_objcprotocol:
  472. begin
  473. if not(is_objcprotocol(childof)) then
  474. Message(parser_e_mix_of_classes_and_objects);
  475. intfchildof:=childof;
  476. childof:=nil;
  477. end;
  478. odt_object:
  479. if not(is_object(childof)) then
  480. Message(parser_e_mix_of_classes_and_objects)
  481. else
  482. if oo_is_sealed in childof.objectoptions then
  483. Message1(parser_e_sealed_descendant,childof.typename);
  484. odt_dispinterface:
  485. Message(parser_e_dispinterface_cant_have_parent);
  486. end;
  487. end;
  488. hasparentdefined:=true;
  489. end;
  490. { if no parent class, then a class get tobject as parent }
  491. if not assigned(childof) then
  492. begin
  493. case current_objectdef.objecttype of
  494. odt_class:
  495. if current_objectdef<>class_tobject then
  496. childof:=class_tobject;
  497. odt_interfacecom:
  498. if current_objectdef<>interface_iunknown then
  499. childof:=interface_iunknown;
  500. odt_objcclass:
  501. CGMessage(parser_h_no_objc_parent);
  502. end;
  503. end;
  504. if assigned(childof) then
  505. begin
  506. { Forbid not completly defined objects to be used as parents. This will
  507. also prevent circular loops of classes, because we set the forward flag
  508. at the start of the new definition and will reset it below after the
  509. parent has been set }
  510. if (oo_is_forward in childof.objectoptions) then
  511. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  512. else if not(oo_is_formal in childof.objectoptions) then
  513. current_objectdef.set_parent(childof)
  514. else
  515. Message1(sym_e_objc_formal_class_not_resolved,childof.objrealname^);
  516. end;
  517. { remove forward flag, is resolved }
  518. exclude(current_structdef.objectoptions,oo_is_forward);
  519. if hasparentdefined then
  520. begin
  521. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  522. begin
  523. if assigned(intfchildof) then
  524. if current_objectdef.objecttype=odt_class then
  525. handleImplementedInterface(intfchildof)
  526. else
  527. handleImplementedProtocol(intfchildof);
  528. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  529. end;
  530. consume(_RKLAMMER);
  531. end;
  532. end;
  533. procedure parse_guid;
  534. begin
  535. { read GUID }
  536. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  537. try_to_consume(_LECKKLAMMER) then
  538. begin
  539. readinterfaceiid;
  540. consume(_RECKKLAMMER);
  541. end
  542. else if (current_objectdef.objecttype=odt_dispinterface) then
  543. message(parser_e_dispinterface_needs_a_guid);
  544. end;
  545. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  546. var
  547. i: longint;
  548. generictype: ttypesym;
  549. st: tsymtable;
  550. begin
  551. def.genericdef:=genericdef;
  552. if not assigned(genericlist) then
  553. exit;
  554. case def.typ of
  555. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  556. arraydef: st:=tarraydef(def).symtable;
  557. procvardef: st:=tprocvardef(def).parast;
  558. else
  559. internalerror(201101020);
  560. end;
  561. for i:=0 to genericlist.count-1 do
  562. begin
  563. generictype:=ttypesym(genericlist[i]);
  564. if generictype.typedef.typ=undefineddef then
  565. include(def.defoptions,df_generic)
  566. else
  567. include(def.defoptions,df_specialization);
  568. st.insert(generictype);
  569. end;
  570. end;
  571. procedure parse_object_members;
  572. procedure chkobjc(pd: tprocdef);
  573. begin
  574. if is_objc_class_or_protocol(pd.struct) then
  575. begin
  576. include(pd.procoptions,po_objc);
  577. end;
  578. end;
  579. procedure chkcpp(pd:tprocdef);
  580. begin
  581. { nothing currently }
  582. end;
  583. procedure maybe_parse_hint_directives(pd:tprocdef);
  584. var
  585. dummysymoptions : tsymoptions;
  586. deprecatedmsg : pshortstring;
  587. begin
  588. dummysymoptions:=[];
  589. deprecatedmsg:=nil;
  590. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  591. Consume(_SEMICOLON);
  592. if assigned(pd) then
  593. begin
  594. pd.symoptions:=pd.symoptions+dummysymoptions;
  595. pd.deprecatedmsg:=deprecatedmsg;
  596. end
  597. else
  598. stringdispose(deprecatedmsg);
  599. end;
  600. var
  601. pd : tprocdef;
  602. has_destructor,
  603. oldparse_only: boolean;
  604. object_member_blocktype : tblock_type;
  605. fields_allowed, is_classdef, classfields: boolean;
  606. vdoptions: tvar_dec_options;
  607. begin
  608. { empty class declaration ? }
  609. if (current_objectdef.objecttype in [odt_class,odt_objcclass]) and
  610. (token=_SEMICOLON) then
  611. exit;
  612. { in "publishable" classes the default access type is published }
  613. if (oo_can_have_published in current_structdef.objectoptions) then
  614. current_structdef.symtable.currentvisibility:=vis_published
  615. else
  616. current_structdef.symtable.currentvisibility:=vis_public;
  617. has_destructor:=false;
  618. fields_allowed:=true;
  619. is_classdef:=false;
  620. classfields:=false;
  621. object_member_blocktype:=bt_general;
  622. repeat
  623. case token of
  624. _TYPE :
  625. begin
  626. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  627. Message(parser_e_type_var_const_only_in_records_and_classes);
  628. consume(_TYPE);
  629. object_member_blocktype:=bt_type;
  630. end;
  631. _VAR :
  632. begin
  633. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  634. Message(parser_e_type_var_const_only_in_records_and_classes);
  635. consume(_VAR);
  636. fields_allowed:=true;
  637. object_member_blocktype:=bt_general;
  638. classfields:=is_classdef;
  639. is_classdef:=false;
  640. end;
  641. _CONST:
  642. begin
  643. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  644. Message(parser_e_type_var_const_only_in_records_and_classes);
  645. consume(_CONST);
  646. object_member_blocktype:=bt_const;
  647. end;
  648. _ID :
  649. begin
  650. if is_objcprotocol(current_structdef) and
  651. ((idtoken=_REQUIRED) or
  652. (idtoken=_OPTIONAL)) then
  653. begin
  654. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  655. consume(idtoken)
  656. end
  657. else case idtoken of
  658. _PRIVATE :
  659. begin
  660. if is_interface(current_structdef) or
  661. is_objc_protocol_or_category(current_structdef) then
  662. Message(parser_e_no_access_specifier_in_interfaces);
  663. consume(_PRIVATE);
  664. current_structdef.symtable.currentvisibility:=vis_private;
  665. include(current_structdef.objectoptions,oo_has_private);
  666. fields_allowed:=true;
  667. is_classdef:=false;
  668. classfields:=false;
  669. object_member_blocktype:=bt_general;
  670. end;
  671. _PROTECTED :
  672. begin
  673. if is_interface(current_structdef) or
  674. is_objc_protocol_or_category(current_structdef) then
  675. Message(parser_e_no_access_specifier_in_interfaces);
  676. consume(_PROTECTED);
  677. current_structdef.symtable.currentvisibility:=vis_protected;
  678. include(current_structdef.objectoptions,oo_has_protected);
  679. fields_allowed:=true;
  680. is_classdef:=false;
  681. classfields:=false;
  682. object_member_blocktype:=bt_general;
  683. end;
  684. _PUBLIC :
  685. begin
  686. if is_interface(current_structdef) or
  687. is_objc_protocol_or_category(current_structdef) then
  688. Message(parser_e_no_access_specifier_in_interfaces);
  689. consume(_PUBLIC);
  690. current_structdef.symtable.currentvisibility:=vis_public;
  691. fields_allowed:=true;
  692. is_classdef:=false;
  693. classfields:=false;
  694. object_member_blocktype:=bt_general;
  695. end;
  696. _PUBLISHED :
  697. begin
  698. { we've to check for a pushlished section in non- }
  699. { publishable classes later, if a real declaration }
  700. { this is the way, delphi does it }
  701. if is_interface(current_structdef) then
  702. Message(parser_e_no_access_specifier_in_interfaces);
  703. { Objective-C classes do not support "published",
  704. as basically everything is published. }
  705. if is_objc_class_or_protocol(current_structdef) then
  706. Message(parser_e_no_objc_published);
  707. consume(_PUBLISHED);
  708. current_structdef.symtable.currentvisibility:=vis_published;
  709. fields_allowed:=true;
  710. is_classdef:=false;
  711. classfields:=false;
  712. object_member_blocktype:=bt_general;
  713. end;
  714. _STRICT :
  715. begin
  716. if is_interface(current_structdef) or
  717. is_objc_protocol_or_category(current_structdef) then
  718. Message(parser_e_no_access_specifier_in_interfaces);
  719. consume(_STRICT);
  720. if token=_ID then
  721. begin
  722. case idtoken of
  723. _PRIVATE:
  724. begin
  725. consume(_PRIVATE);
  726. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  727. include(current_structdef.objectoptions,oo_has_strictprivate);
  728. end;
  729. _PROTECTED:
  730. begin
  731. consume(_PROTECTED);
  732. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  733. include(current_structdef.objectoptions,oo_has_strictprotected);
  734. end;
  735. else
  736. message(parser_e_protected_or_private_expected);
  737. end;
  738. end
  739. else
  740. message(parser_e_protected_or_private_expected);
  741. fields_allowed:=true;
  742. is_classdef:=false;
  743. classfields:=false;
  744. object_member_blocktype:=bt_general;
  745. end
  746. else
  747. begin
  748. if object_member_blocktype=bt_general then
  749. begin
  750. if is_interface(current_structdef) or
  751. is_objc_protocol_or_category(current_structdef) then
  752. Message(parser_e_no_vars_in_interfaces);
  753. if (current_structdef.symtable.currentvisibility=vis_published) and
  754. not(oo_can_have_published in current_structdef.objectoptions) then
  755. Message(parser_e_cant_have_published);
  756. if (not fields_allowed) then
  757. Message(parser_e_field_not_allowed_here);
  758. vdoptions:=[vd_object];
  759. if classfields then
  760. include(vdoptions,vd_class);
  761. read_record_fields(vdoptions);
  762. end
  763. else if object_member_blocktype=bt_type then
  764. types_dec(true)
  765. else if object_member_blocktype=bt_const then
  766. consts_dec(true)
  767. else
  768. internalerror(201001110);
  769. end;
  770. end;
  771. end;
  772. _PROPERTY :
  773. begin
  774. struct_property_dec(is_classdef);
  775. fields_allowed:=false;
  776. is_classdef:=false;
  777. end;
  778. _CLASS:
  779. begin
  780. is_classdef:=false;
  781. { read class method }
  782. if try_to_consume(_CLASS) then
  783. begin
  784. { class modifier is only allowed for procedures, functions, }
  785. { constructors, destructors, fields and properties }
  786. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  787. Message(parser_e_procedure_or_function_expected);
  788. if is_interface(current_structdef) then
  789. Message(parser_e_no_static_method_in_interfaces)
  790. else
  791. { class methods are also allowed for Objective-C protocols }
  792. is_classdef:=true;
  793. end;
  794. end;
  795. _PROCEDURE,
  796. _FUNCTION:
  797. begin
  798. if (current_structdef.symtable.currentvisibility=vis_published) and
  799. not(oo_can_have_published in current_structdef.objectoptions) then
  800. Message(parser_e_cant_have_published);
  801. oldparse_only:=parse_only;
  802. parse_only:=true;
  803. pd:=parse_proc_dec(is_classdef,current_structdef);
  804. { this is for error recovery as well as forward }
  805. { interface mappings, i.e. mapping to a method }
  806. { which isn't declared yet }
  807. if assigned(pd) then
  808. begin
  809. parse_object_proc_directives(pd);
  810. { check if dispid is set }
  811. if is_dispinterface(pd.struct) and not (po_dispid in pd.procoptions) then
  812. begin
  813. pd.dispid:=tobjectdef(pd.struct).get_next_dispid;
  814. include(pd.procoptions, po_dispid);
  815. end;
  816. { all Macintosh Object Pascal methods are virtual. }
  817. { this can't be a class method, because macpas mode }
  818. { has no m_class }
  819. if (m_mac in current_settings.modeswitches) then
  820. include(pd.procoptions,po_virtualmethod);
  821. handle_calling_convention(pd);
  822. { add definition to procsym }
  823. proc_add_definition(pd);
  824. { add procdef options to objectdef options }
  825. if (po_msgint in pd.procoptions) then
  826. include(current_structdef.objectoptions,oo_has_msgint);
  827. if (po_msgstr in pd.procoptions) then
  828. include(current_structdef.objectoptions,oo_has_msgstr);
  829. if (po_virtualmethod in pd.procoptions) then
  830. include(current_structdef.objectoptions,oo_has_virtual);
  831. chkcpp(pd);
  832. chkobjc(pd);
  833. end;
  834. maybe_parse_hint_directives(pd);
  835. parse_only:=oldparse_only;
  836. fields_allowed:=false;
  837. is_classdef:=false;
  838. end;
  839. _CONSTRUCTOR :
  840. begin
  841. if (current_structdef.symtable.currentvisibility=vis_published) and
  842. not(oo_can_have_published in current_structdef.objectoptions) then
  843. Message(parser_e_cant_have_published);
  844. if not is_classdef and not(current_structdef.symtable.currentvisibility in [vis_public,vis_published]) then
  845. Message(parser_w_constructor_should_be_public);
  846. if is_interface(current_structdef) then
  847. Message(parser_e_no_con_des_in_interfaces);
  848. { Objective-C does not know the concept of a constructor }
  849. if is_objc_class_or_protocol(current_structdef) then
  850. Message(parser_e_objc_no_constructor_destructor);
  851. { only 1 class constructor is allowed }
  852. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  853. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  854. oldparse_only:=parse_only;
  855. parse_only:=true;
  856. if is_classdef then
  857. pd:=class_constructor_head
  858. else
  859. pd:=constructor_head;
  860. parse_object_proc_directives(pd);
  861. handle_calling_convention(pd);
  862. { add definition to procsym }
  863. proc_add_definition(pd);
  864. { add procdef options to objectdef options }
  865. if (po_virtualmethod in pd.procoptions) then
  866. include(current_structdef.objectoptions,oo_has_virtual);
  867. chkcpp(pd);
  868. maybe_parse_hint_directives(pd);
  869. parse_only:=oldparse_only;
  870. fields_allowed:=false;
  871. is_classdef:=false;
  872. end;
  873. _DESTRUCTOR :
  874. begin
  875. if (current_structdef.symtable.currentvisibility=vis_published) and
  876. not(oo_can_have_published in current_structdef.objectoptions) then
  877. Message(parser_e_cant_have_published);
  878. if not is_classdef then
  879. if has_destructor then
  880. Message(parser_n_only_one_destructor)
  881. else
  882. has_destructor:=true;
  883. if is_interface(current_structdef) then
  884. Message(parser_e_no_con_des_in_interfaces);
  885. if not is_classdef and (current_structdef.symtable.currentvisibility<>vis_public) then
  886. Message(parser_w_destructor_should_be_public);
  887. { Objective-C does not know the concept of a destructor }
  888. if is_objc_class_or_protocol(current_structdef) then
  889. Message(parser_e_objc_no_constructor_destructor);
  890. { only 1 class destructor is allowed }
  891. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  892. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  893. oldparse_only:=parse_only;
  894. parse_only:=true;
  895. if is_classdef then
  896. pd:=class_destructor_head
  897. else
  898. pd:=destructor_head;
  899. parse_object_proc_directives(pd);
  900. handle_calling_convention(pd);
  901. { add definition to procsym }
  902. proc_add_definition(pd);
  903. { add procdef options to objectdef options }
  904. if (po_virtualmethod in pd.procoptions) then
  905. include(current_structdef.objectoptions,oo_has_virtual);
  906. chkcpp(pd);
  907. maybe_parse_hint_directives(pd);
  908. parse_only:=oldparse_only;
  909. fields_allowed:=false;
  910. is_classdef:=false;
  911. end;
  912. _END :
  913. begin
  914. consume(_END);
  915. break;
  916. end;
  917. else
  918. consume(_ID); { Give a ident expected message, like tp7 }
  919. end;
  920. until false;
  921. end;
  922. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tobjectdef;
  923. var
  924. old_current_structdef: tabstractrecorddef;
  925. old_current_genericdef,
  926. old_current_specializedef: tstoreddef;
  927. old_parse_generic: boolean;
  928. begin
  929. old_current_structdef:=current_structdef;
  930. old_current_genericdef:=current_genericdef;
  931. old_current_specializedef:=current_specializedef;
  932. old_parse_generic:=parse_generic;
  933. current_structdef:=nil;
  934. current_genericdef:=nil;
  935. current_specializedef:=nil;
  936. { objects and class types can't be declared local }
  937. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable]) and
  938. not assigned(genericlist) then
  939. Message(parser_e_no_local_objects);
  940. { reuse forward objectdef? }
  941. if assigned(fd) then
  942. begin
  943. if fd.objecttype<>objecttype then
  944. begin
  945. Message(parser_e_forward_mismatch);
  946. { recover }
  947. current_structdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  948. include(current_structdef.objectoptions,oo_is_forward);
  949. end
  950. else
  951. current_structdef:=fd
  952. end
  953. else
  954. begin
  955. { anonym objects aren't allow (o : object a : longint; end;) }
  956. if n='' then
  957. Message(parser_f_no_anonym_objects);
  958. { create new class }
  959. current_structdef:=tobjectdef.create(objecttype,n,nil);
  960. { include always the forward flag, it'll be removed after the parent class have been
  961. added. This is to prevent circular childof loops }
  962. include(current_structdef.objectoptions,oo_is_forward);
  963. if (cs_compilesystem in current_settings.moduleswitches) then
  964. begin
  965. case current_objectdef.objecttype of
  966. odt_interfacecom :
  967. if (current_structdef.objname^='IUNKNOWN') then
  968. interface_iunknown:=current_objectdef;
  969. odt_class :
  970. if (current_structdef.objname^='TOBJECT') then
  971. class_tobject:=current_objectdef;
  972. end;
  973. end;
  974. if (current_module.modulename^='OBJCBASE') then
  975. begin
  976. case current_objectdef.objecttype of
  977. odt_objcclass:
  978. if (current_objectdef.objname^='Protocol') then
  979. objc_protocoltype:=current_objectdef;
  980. end;
  981. end;
  982. end;
  983. { usage of specialized type inside its generic template }
  984. if assigned(genericdef) then
  985. current_specializedef:=current_structdef
  986. { reject declaration of generic class inside generic class }
  987. else if assigned(genericlist) then
  988. current_genericdef:=current_structdef;
  989. { set published flag in $M+ mode, it can also be inherited and will
  990. be added when the parent class set with tobjectdef.set_parent (PFV) }
  991. if (cs_generate_rtti in current_settings.localswitches) and
  992. (current_objectdef.objecttype in [odt_interfacecom,odt_class]) then
  993. include(current_structdef.objectoptions,oo_can_have_published);
  994. { Objective-C objectdefs can be "formal definitions", in which case
  995. the syntax is "type tc = objcclass external;" -> we have to parse
  996. its object options (external) already here, to make sure that such
  997. definitions are recognised as formal defs }
  998. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory] then
  999. parse_object_options;
  1000. { forward def? }
  1001. if not assigned(fd) and
  1002. (token=_SEMICOLON) then
  1003. begin
  1004. { add to the list of definitions to check that the forward
  1005. is resolved. this is required for delphi mode }
  1006. current_module.checkforwarddefs.add(current_structdef);
  1007. end
  1008. else
  1009. begin
  1010. { change objccategories into objcclass helpers }
  1011. if (objecttype=odt_objccategory) then
  1012. begin
  1013. current_objectdef.objecttype:=odt_objcclass;
  1014. include(current_structdef.objectoptions,oo_is_classhelper);
  1015. end;
  1016. { parse list of options (abstract / sealed) }
  1017. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory]) then
  1018. parse_object_options;
  1019. symtablestack.push(current_structdef.symtable);
  1020. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  1021. parse_generic:=(df_generic in current_structdef.defoptions);
  1022. { parse list of parent classes }
  1023. parse_parent_classes;
  1024. { parse optional GUID for interfaces }
  1025. parse_guid;
  1026. { parse and insert object members }
  1027. parse_object_members;
  1028. symtablestack.pop(current_structdef.symtable);
  1029. end;
  1030. { generate vmt space if needed }
  1031. if not(oo_has_vmt in current_structdef.objectoptions) and
  1032. not(oo_is_forward in current_structdef.objectoptions) and
  1033. (
  1034. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1035. (current_objectdef.objecttype in [odt_class])
  1036. ) then
  1037. current_objectdef.insertvmt;
  1038. { for implemented classes with a vmt check if there is a constructor }
  1039. if (oo_has_vmt in current_structdef.objectoptions) and
  1040. not(oo_is_forward in current_structdef.objectoptions) and
  1041. not(oo_has_constructor in current_structdef.objectoptions) and
  1042. not is_objc_class_or_protocol(current_structdef) then
  1043. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1044. if is_interface(current_structdef) or
  1045. is_objcprotocol(current_structdef) then
  1046. setinterfacemethodoptions
  1047. else if is_objcclass(current_structdef) then
  1048. setobjcclassmethodoptions;
  1049. { return defined objectdef }
  1050. result:=current_objectdef;
  1051. { restore old state }
  1052. current_structdef:=old_current_structdef;
  1053. current_genericdef:=old_current_genericdef;
  1054. current_specializedef:=old_current_specializedef;
  1055. parse_generic:=old_parse_generic;
  1056. end;
  1057. end.