pdecobj.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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. { no generic as parents }
  491. if assigned(childof) and
  492. (df_generic in childof.defoptions) then
  493. begin
  494. Message(parser_e_no_generics_as_types);
  495. childof:=nil;
  496. end;
  497. { if no parent class, then a class get tobject as parent }
  498. if not assigned(childof) then
  499. begin
  500. case current_objectdef.objecttype of
  501. odt_class:
  502. if current_objectdef<>class_tobject then
  503. childof:=class_tobject;
  504. odt_interfacecom:
  505. if current_objectdef<>interface_iunknown then
  506. childof:=interface_iunknown;
  507. odt_objcclass:
  508. CGMessage(parser_h_no_objc_parent);
  509. end;
  510. end;
  511. if assigned(childof) then
  512. begin
  513. { Forbid not completly defined objects to be used as parents. This will
  514. also prevent circular loops of classes, because we set the forward flag
  515. at the start of the new definition and will reset it below after the
  516. parent has been set }
  517. if (oo_is_forward in childof.objectoptions) then
  518. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  519. else if not(oo_is_formal in childof.objectoptions) then
  520. current_objectdef.set_parent(childof)
  521. else
  522. Message1(sym_e_objc_formal_class_not_resolved,childof.objrealname^);
  523. end;
  524. { remove forward flag, is resolved }
  525. exclude(current_structdef.objectoptions,oo_is_forward);
  526. if hasparentdefined then
  527. begin
  528. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  529. begin
  530. if assigned(intfchildof) then
  531. if current_objectdef.objecttype=odt_class then
  532. handleImplementedInterface(intfchildof)
  533. else
  534. handleImplementedProtocol(intfchildof);
  535. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  536. end;
  537. consume(_RKLAMMER);
  538. end;
  539. end;
  540. procedure parse_guid;
  541. begin
  542. { read GUID }
  543. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  544. try_to_consume(_LECKKLAMMER) then
  545. begin
  546. readinterfaceiid;
  547. consume(_RECKKLAMMER);
  548. end
  549. else if (current_objectdef.objecttype=odt_dispinterface) then
  550. message(parser_e_dispinterface_needs_a_guid);
  551. end;
  552. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  553. var
  554. i: longint;
  555. generictype: ttypesym;
  556. st: tsymtable;
  557. begin
  558. def.genericdef:=genericdef;
  559. if not assigned(genericlist) then
  560. exit;
  561. case def.typ of
  562. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  563. arraydef: st:=tarraydef(def).symtable;
  564. else
  565. internalerror(201101020);
  566. end;
  567. for i:=0 to genericlist.count-1 do
  568. begin
  569. generictype:=ttypesym(genericlist[i]);
  570. if generictype.typedef.typ=undefineddef then
  571. include(def.defoptions,df_generic)
  572. else
  573. include(def.defoptions,df_specialization);
  574. st.insert(generictype);
  575. end;
  576. end;
  577. procedure parse_object_members;
  578. procedure chkobjc(pd: tprocdef);
  579. begin
  580. if is_objc_class_or_protocol(pd.struct) then
  581. begin
  582. include(pd.procoptions,po_objc);
  583. end;
  584. end;
  585. procedure chkcpp(pd:tprocdef);
  586. begin
  587. { nothing currently }
  588. end;
  589. procedure maybe_parse_hint_directives(pd:tprocdef);
  590. var
  591. dummysymoptions : tsymoptions;
  592. deprecatedmsg : pshortstring;
  593. begin
  594. dummysymoptions:=[];
  595. deprecatedmsg:=nil;
  596. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  597. Consume(_SEMICOLON);
  598. if assigned(pd) then
  599. begin
  600. pd.symoptions:=pd.symoptions+dummysymoptions;
  601. pd.deprecatedmsg:=deprecatedmsg;
  602. end
  603. else
  604. stringdispose(deprecatedmsg);
  605. end;
  606. var
  607. pd : tprocdef;
  608. has_destructor,
  609. oldparse_only: boolean;
  610. object_member_blocktype : tblock_type;
  611. fields_allowed, is_classdef, classfields: boolean;
  612. vdoptions: tvar_dec_options;
  613. begin
  614. { empty class declaration ? }
  615. if (current_objectdef.objecttype in [odt_class,odt_objcclass]) and
  616. (token=_SEMICOLON) then
  617. exit;
  618. { in "publishable" classes the default access type is published }
  619. if (oo_can_have_published in current_structdef.objectoptions) then
  620. current_structdef.symtable.currentvisibility:=vis_published
  621. else
  622. current_structdef.symtable.currentvisibility:=vis_public;
  623. has_destructor:=false;
  624. fields_allowed:=true;
  625. is_classdef:=false;
  626. classfields:=false;
  627. object_member_blocktype:=bt_general;
  628. repeat
  629. case token of
  630. _TYPE :
  631. begin
  632. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  633. Message(parser_e_type_var_const_only_in_records_and_classes);
  634. consume(_TYPE);
  635. object_member_blocktype:=bt_type;
  636. end;
  637. _VAR :
  638. begin
  639. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  640. Message(parser_e_type_var_const_only_in_records_and_classes);
  641. consume(_VAR);
  642. fields_allowed:=true;
  643. object_member_blocktype:=bt_general;
  644. classfields:=is_classdef;
  645. is_classdef:=false;
  646. end;
  647. _CONST:
  648. begin
  649. if not(current_objectdef.objecttype in [odt_class,odt_object]) then
  650. Message(parser_e_type_var_const_only_in_records_and_classes);
  651. consume(_CONST);
  652. object_member_blocktype:=bt_const;
  653. end;
  654. _ID :
  655. begin
  656. if is_objcprotocol(current_structdef) and
  657. ((idtoken=_REQUIRED) or
  658. (idtoken=_OPTIONAL)) then
  659. begin
  660. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  661. consume(idtoken)
  662. end
  663. else case idtoken of
  664. _PRIVATE :
  665. begin
  666. if is_interface(current_structdef) or
  667. is_objc_protocol_or_category(current_structdef) then
  668. Message(parser_e_no_access_specifier_in_interfaces);
  669. consume(_PRIVATE);
  670. current_structdef.symtable.currentvisibility:=vis_private;
  671. include(current_structdef.objectoptions,oo_has_private);
  672. fields_allowed:=true;
  673. is_classdef:=false;
  674. classfields:=false;
  675. object_member_blocktype:=bt_general;
  676. end;
  677. _PROTECTED :
  678. begin
  679. if is_interface(current_structdef) or
  680. is_objc_protocol_or_category(current_structdef) then
  681. Message(parser_e_no_access_specifier_in_interfaces);
  682. consume(_PROTECTED);
  683. current_structdef.symtable.currentvisibility:=vis_protected;
  684. include(current_structdef.objectoptions,oo_has_protected);
  685. fields_allowed:=true;
  686. is_classdef:=false;
  687. classfields:=false;
  688. object_member_blocktype:=bt_general;
  689. end;
  690. _PUBLIC :
  691. begin
  692. if is_interface(current_structdef) or
  693. is_objc_protocol_or_category(current_structdef) then
  694. Message(parser_e_no_access_specifier_in_interfaces);
  695. consume(_PUBLIC);
  696. current_structdef.symtable.currentvisibility:=vis_public;
  697. fields_allowed:=true;
  698. is_classdef:=false;
  699. classfields:=false;
  700. object_member_blocktype:=bt_general;
  701. end;
  702. _PUBLISHED :
  703. begin
  704. { we've to check for a pushlished section in non- }
  705. { publishable classes later, if a real declaration }
  706. { this is the way, delphi does it }
  707. if is_interface(current_structdef) then
  708. Message(parser_e_no_access_specifier_in_interfaces);
  709. { Objective-C classes do not support "published",
  710. as basically everything is published. }
  711. if is_objc_class_or_protocol(current_structdef) then
  712. Message(parser_e_no_objc_published);
  713. consume(_PUBLISHED);
  714. current_structdef.symtable.currentvisibility:=vis_published;
  715. fields_allowed:=true;
  716. is_classdef:=false;
  717. classfields:=false;
  718. object_member_blocktype:=bt_general;
  719. end;
  720. _STRICT :
  721. begin
  722. if is_interface(current_structdef) or
  723. is_objc_protocol_or_category(current_structdef) then
  724. Message(parser_e_no_access_specifier_in_interfaces);
  725. consume(_STRICT);
  726. if token=_ID then
  727. begin
  728. case idtoken of
  729. _PRIVATE:
  730. begin
  731. consume(_PRIVATE);
  732. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  733. include(current_structdef.objectoptions,oo_has_strictprivate);
  734. end;
  735. _PROTECTED:
  736. begin
  737. consume(_PROTECTED);
  738. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  739. include(current_structdef.objectoptions,oo_has_strictprotected);
  740. end;
  741. else
  742. message(parser_e_protected_or_private_expected);
  743. end;
  744. end
  745. else
  746. message(parser_e_protected_or_private_expected);
  747. fields_allowed:=true;
  748. is_classdef:=false;
  749. classfields:=false;
  750. object_member_blocktype:=bt_general;
  751. end
  752. else
  753. begin
  754. if object_member_blocktype=bt_general then
  755. begin
  756. if is_interface(current_structdef) or
  757. is_objc_protocol_or_category(current_structdef) then
  758. Message(parser_e_no_vars_in_interfaces);
  759. if (current_structdef.symtable.currentvisibility=vis_published) and
  760. not(oo_can_have_published in current_structdef.objectoptions) then
  761. Message(parser_e_cant_have_published);
  762. if (not fields_allowed) then
  763. Message(parser_e_field_not_allowed_here);
  764. vdoptions:=[vd_object];
  765. if classfields then
  766. include(vdoptions,vd_class);
  767. read_record_fields(vdoptions);
  768. end
  769. else if object_member_blocktype=bt_type then
  770. types_dec(true)
  771. else if object_member_blocktype=bt_const then
  772. consts_dec(true)
  773. else
  774. internalerror(201001110);
  775. end;
  776. end;
  777. end;
  778. _PROPERTY :
  779. begin
  780. struct_property_dec(is_classdef);
  781. fields_allowed:=false;
  782. is_classdef:=false;
  783. end;
  784. _CLASS:
  785. begin
  786. is_classdef:=false;
  787. { read class method }
  788. if try_to_consume(_CLASS) then
  789. begin
  790. { class modifier is only allowed for procedures, functions, }
  791. { constructors, destructors, fields and properties }
  792. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  793. Message(parser_e_procedure_or_function_expected);
  794. if is_interface(current_structdef) then
  795. Message(parser_e_no_static_method_in_interfaces)
  796. else
  797. { class methods are also allowed for Objective-C protocols }
  798. is_classdef:=true;
  799. end;
  800. end;
  801. _PROCEDURE,
  802. _FUNCTION:
  803. begin
  804. if (current_structdef.symtable.currentvisibility=vis_published) and
  805. not(oo_can_have_published in current_structdef.objectoptions) then
  806. Message(parser_e_cant_have_published);
  807. oldparse_only:=parse_only;
  808. parse_only:=true;
  809. pd:=parse_proc_dec(is_classdef,current_structdef);
  810. { this is for error recovery as well as forward }
  811. { interface mappings, i.e. mapping to a method }
  812. { which isn't declared yet }
  813. if assigned(pd) then
  814. begin
  815. parse_object_proc_directives(pd);
  816. { check if dispid is set }
  817. if is_dispinterface(pd.struct) and not (po_dispid in pd.procoptions) then
  818. begin
  819. pd.dispid:=tobjectdef(pd.struct).get_next_dispid;
  820. include(pd.procoptions, po_dispid);
  821. end;
  822. { all Macintosh Object Pascal methods are virtual. }
  823. { this can't be a class method, because macpas mode }
  824. { has no m_class }
  825. if (m_mac in current_settings.modeswitches) then
  826. include(pd.procoptions,po_virtualmethod);
  827. handle_calling_convention(pd);
  828. { add definition to procsym }
  829. proc_add_definition(pd);
  830. { add procdef options to objectdef options }
  831. if (po_msgint in pd.procoptions) then
  832. include(current_structdef.objectoptions,oo_has_msgint);
  833. if (po_msgstr in pd.procoptions) then
  834. include(current_structdef.objectoptions,oo_has_msgstr);
  835. if (po_virtualmethod in pd.procoptions) then
  836. include(current_structdef.objectoptions,oo_has_virtual);
  837. chkcpp(pd);
  838. chkobjc(pd);
  839. end;
  840. maybe_parse_hint_directives(pd);
  841. parse_only:=oldparse_only;
  842. fields_allowed:=false;
  843. is_classdef:=false;
  844. end;
  845. _CONSTRUCTOR :
  846. begin
  847. if (current_structdef.symtable.currentvisibility=vis_published) and
  848. not(oo_can_have_published in current_structdef.objectoptions) then
  849. Message(parser_e_cant_have_published);
  850. if not is_classdef and not(current_structdef.symtable.currentvisibility in [vis_public,vis_published]) then
  851. Message(parser_w_constructor_should_be_public);
  852. if is_interface(current_structdef) then
  853. Message(parser_e_no_con_des_in_interfaces);
  854. { Objective-C does not know the concept of a constructor }
  855. if is_objc_class_or_protocol(current_structdef) then
  856. Message(parser_e_objc_no_constructor_destructor);
  857. { only 1 class constructor is allowed }
  858. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  859. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  860. oldparse_only:=parse_only;
  861. parse_only:=true;
  862. if is_classdef then
  863. pd:=class_constructor_head
  864. else
  865. pd:=constructor_head;
  866. parse_object_proc_directives(pd);
  867. handle_calling_convention(pd);
  868. { add definition to procsym }
  869. proc_add_definition(pd);
  870. { add procdef options to objectdef options }
  871. if (po_virtualmethod in pd.procoptions) then
  872. include(current_structdef.objectoptions,oo_has_virtual);
  873. chkcpp(pd);
  874. maybe_parse_hint_directives(pd);
  875. parse_only:=oldparse_only;
  876. fields_allowed:=false;
  877. is_classdef:=false;
  878. end;
  879. _DESTRUCTOR :
  880. begin
  881. if (current_structdef.symtable.currentvisibility=vis_published) and
  882. not(oo_can_have_published in current_structdef.objectoptions) then
  883. Message(parser_e_cant_have_published);
  884. if not is_classdef then
  885. if has_destructor then
  886. Message(parser_n_only_one_destructor)
  887. else
  888. has_destructor:=true;
  889. if is_interface(current_structdef) then
  890. Message(parser_e_no_con_des_in_interfaces);
  891. if not is_classdef and (current_structdef.symtable.currentvisibility<>vis_public) then
  892. Message(parser_w_destructor_should_be_public);
  893. { Objective-C does not know the concept of a destructor }
  894. if is_objc_class_or_protocol(current_structdef) then
  895. Message(parser_e_objc_no_constructor_destructor);
  896. { only 1 class destructor is allowed }
  897. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  898. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  899. oldparse_only:=parse_only;
  900. parse_only:=true;
  901. if is_classdef then
  902. pd:=class_destructor_head
  903. else
  904. pd:=destructor_head;
  905. parse_object_proc_directives(pd);
  906. handle_calling_convention(pd);
  907. { add definition to procsym }
  908. proc_add_definition(pd);
  909. { add procdef options to objectdef options }
  910. if (po_virtualmethod in pd.procoptions) then
  911. include(current_structdef.objectoptions,oo_has_virtual);
  912. chkcpp(pd);
  913. maybe_parse_hint_directives(pd);
  914. parse_only:=oldparse_only;
  915. fields_allowed:=false;
  916. is_classdef:=false;
  917. end;
  918. _END :
  919. begin
  920. consume(_END);
  921. break;
  922. end;
  923. else
  924. consume(_ID); { Give a ident expected message, like tp7 }
  925. end;
  926. until false;
  927. end;
  928. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef) : tobjectdef;
  929. var
  930. old_current_structdef: tabstractrecorddef;
  931. old_current_genericdef,
  932. old_current_specializedef: tstoreddef;
  933. old_parse_generic: boolean;
  934. begin
  935. old_current_structdef:=current_structdef;
  936. old_current_genericdef:=current_genericdef;
  937. old_current_specializedef:=current_specializedef;
  938. old_parse_generic:=parse_generic;
  939. current_structdef:=nil;
  940. current_genericdef:=nil;
  941. current_specializedef:=nil;
  942. { objects and class types can't be declared local }
  943. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable]) and
  944. not assigned(genericlist) then
  945. Message(parser_e_no_local_objects);
  946. { reuse forward objectdef? }
  947. if assigned(fd) then
  948. begin
  949. if fd.objecttype<>objecttype then
  950. begin
  951. Message(parser_e_forward_mismatch);
  952. { recover }
  953. current_structdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  954. include(current_structdef.objectoptions,oo_is_forward);
  955. end
  956. else
  957. current_structdef:=fd
  958. end
  959. else
  960. begin
  961. { anonym objects aren't allow (o : object a : longint; end;) }
  962. if n='' then
  963. Message(parser_f_no_anonym_objects);
  964. { create new class }
  965. current_structdef:=tobjectdef.create(objecttype,n,nil);
  966. { include always the forward flag, it'll be removed after the parent class have been
  967. added. This is to prevent circular childof loops }
  968. include(current_structdef.objectoptions,oo_is_forward);
  969. if (cs_compilesystem in current_settings.moduleswitches) then
  970. begin
  971. case current_objectdef.objecttype of
  972. odt_interfacecom :
  973. if (current_structdef.objname^='IUNKNOWN') then
  974. interface_iunknown:=current_objectdef;
  975. odt_class :
  976. if (current_structdef.objname^='TOBJECT') then
  977. class_tobject:=current_objectdef;
  978. end;
  979. end;
  980. if (current_module.modulename^='OBJCBASE') then
  981. begin
  982. case current_objectdef.objecttype of
  983. odt_objcclass:
  984. if (current_objectdef.objname^='Protocol') then
  985. objc_protocoltype:=current_objectdef;
  986. end;
  987. end;
  988. end;
  989. { usage of specialized type inside its generic template }
  990. if assigned(genericdef) then
  991. current_specializedef:=current_structdef
  992. { reject declaration of generic class inside generic class }
  993. else if assigned(genericlist) then
  994. current_genericdef:=current_structdef;
  995. { set published flag in $M+ mode, it can also be inherited and will
  996. be added when the parent class set with tobjectdef.set_parent (PFV) }
  997. if (cs_generate_rtti in current_settings.localswitches) and
  998. (current_objectdef.objecttype in [odt_interfacecom,odt_class]) then
  999. include(current_structdef.objectoptions,oo_can_have_published);
  1000. { Objective-C objectdefs can be "formal definitions", in which case
  1001. the syntax is "type tc = objcclass external;" -> we have to parse
  1002. its object options (external) already here, to make sure that such
  1003. definitions are recognised as formal defs }
  1004. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory] then
  1005. parse_object_options;
  1006. { forward def? }
  1007. if not assigned(fd) and
  1008. (token=_SEMICOLON) then
  1009. begin
  1010. { add to the list of definitions to check that the forward
  1011. is resolved. this is required for delphi mode }
  1012. current_module.checkforwarddefs.add(current_structdef);
  1013. end
  1014. else
  1015. begin
  1016. { change objccategories into objcclass helpers }
  1017. if (objecttype=odt_objccategory) then
  1018. begin
  1019. current_objectdef.objecttype:=odt_objcclass;
  1020. include(current_structdef.objectoptions,oo_is_classhelper);
  1021. end;
  1022. { parse list of options (abstract / sealed) }
  1023. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory]) then
  1024. parse_object_options;
  1025. symtablestack.push(current_structdef.symtable);
  1026. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  1027. parse_generic:=(df_generic in current_structdef.defoptions);
  1028. { parse list of parent classes }
  1029. parse_parent_classes;
  1030. { parse optional GUID for interfaces }
  1031. parse_guid;
  1032. { parse and insert object members }
  1033. parse_object_members;
  1034. symtablestack.pop(current_structdef.symtable);
  1035. end;
  1036. { generate vmt space if needed }
  1037. if not(oo_has_vmt in current_structdef.objectoptions) and
  1038. not(oo_is_forward in current_structdef.objectoptions) and
  1039. (
  1040. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1041. (current_objectdef.objecttype in [odt_class])
  1042. ) then
  1043. current_objectdef.insertvmt;
  1044. { for implemented classes with a vmt check if there is a constructor }
  1045. if (oo_has_vmt in current_structdef.objectoptions) and
  1046. not(oo_is_forward in current_structdef.objectoptions) and
  1047. not(oo_has_constructor in current_structdef.objectoptions) and
  1048. not is_objc_class_or_protocol(current_structdef) then
  1049. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1050. if is_interface(current_structdef) or
  1051. is_objcprotocol(current_structdef) then
  1052. setinterfacemethodoptions
  1053. else if is_objcclass(current_structdef) then
  1054. setobjcclassmethodoptions;
  1055. { return defined objectdef }
  1056. result:=current_objectdef;
  1057. { restore old state }
  1058. current_structdef:=old_current_structdef;
  1059. current_genericdef:=old_current_genericdef;
  1060. current_specializedef:=old_current_specializedef;
  1061. parse_generic:=old_parse_generic;
  1062. end;
  1063. end.