pdecobj.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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;helpertype:thelpertype) : 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. implementation
  31. uses
  32. sysutils,cutils,
  33. globals,verbose,systems,tokens,
  34. symbase,symsym,symtable,
  35. node,nld,nmem,ncon,ncnv,ncal,
  36. fmodule,scanner,
  37. pbase,pexpr,pdecsub,pdecvar,ptype,pdecl,ppu
  38. ;
  39. const
  40. { Please leave this here, this module should NOT use
  41. these variables.
  42. Declaring it as string here results in an error when compiling (PFV) }
  43. current_procinfo = 'error';
  44. var
  45. current_objectdef : tobjectdef absolute current_structdef;
  46. function class_constructor_head:tprocdef;
  47. var
  48. pd : tprocdef;
  49. begin
  50. result:=nil;
  51. consume(_CONSTRUCTOR);
  52. { must be at same level as in implementation }
  53. parse_proc_head(current_structdef,potype_class_constructor,pd);
  54. if not assigned(pd) then
  55. begin
  56. consume(_SEMICOLON);
  57. exit;
  58. end;
  59. pd.calcparas;
  60. if (pd.maxparacount>0) then
  61. Message(parser_e_no_paras_for_class_constructor);
  62. consume(_SEMICOLON);
  63. include(current_structdef.objectoptions,oo_has_class_constructor);
  64. current_module.flags:=current_module.flags or uf_classinits;
  65. { no return value }
  66. pd.returndef:=voidtype;
  67. result:=pd;
  68. end;
  69. function constructor_head:tprocdef;
  70. var
  71. pd : tprocdef;
  72. begin
  73. result:=nil;
  74. consume(_CONSTRUCTOR);
  75. { must be at same level as in implementation }
  76. parse_proc_head(current_structdef,potype_constructor,pd);
  77. if not assigned(pd) then
  78. begin
  79. consume(_SEMICOLON);
  80. exit;
  81. end;
  82. if (cs_constructor_name in current_settings.globalswitches) and
  83. (pd.procsym.name<>'INIT') then
  84. Message(parser_e_constructorname_must_be_init);
  85. consume(_SEMICOLON);
  86. include(current_structdef.objectoptions,oo_has_constructor);
  87. { Set return type, class and record constructors return the
  88. created instance, object constructors return boolean }
  89. if is_class(pd.struct) or is_record(pd.struct) then
  90. pd.returndef:=pd.struct
  91. else
  92. {$ifdef CPU64bitaddr}
  93. pd.returndef:=bool64type;
  94. {$else CPU64bitaddr}
  95. pd.returndef:=bool32type;
  96. {$endif CPU64bitaddr}
  97. result:=pd;
  98. end;
  99. procedure struct_property_dec(is_classproperty:boolean);
  100. var
  101. p : tpropertysym;
  102. begin
  103. { check for a class, record or helper }
  104. if not((is_class_or_interface_or_dispinterface(current_structdef) or is_record(current_structdef) or is_objectpascal_helper(current_structdef)) or
  105. (not(m_tp7 in current_settings.modeswitches) and (is_object(current_structdef)))) then
  106. Message(parser_e_syntax_error);
  107. consume(_PROPERTY);
  108. p:=read_property_dec(is_classproperty,current_structdef);
  109. consume(_SEMICOLON);
  110. if try_to_consume(_DEFAULT) then
  111. begin
  112. if oo_has_default_property in current_structdef.objectoptions then
  113. message(parser_e_only_one_default_property);
  114. include(current_structdef.objectoptions,oo_has_default_property);
  115. include(p.propoptions,ppo_defaultproperty);
  116. if not(ppo_hasparameters in p.propoptions) then
  117. message(parser_e_property_need_paras);
  118. if (token=_COLON) then
  119. begin
  120. Message(parser_e_field_not_allowed_here);
  121. consume_all_until(_SEMICOLON);
  122. end;
  123. consume(_SEMICOLON);
  124. end;
  125. { parse possible enumerator modifier }
  126. if try_to_consume(_ENUMERATOR) then
  127. begin
  128. if (token = _ID) then
  129. begin
  130. if pattern='CURRENT' then
  131. begin
  132. if oo_has_enumerator_current in current_structdef.objectoptions then
  133. message(parser_e_only_one_enumerator_current);
  134. if not p.propaccesslist[palt_read].empty then
  135. begin
  136. include(current_structdef.objectoptions,oo_has_enumerator_current);
  137. include(p.propoptions,ppo_enumerator_current);
  138. end
  139. else
  140. Message(parser_e_enumerator_current_is_not_valid) // property has no reader
  141. end
  142. else
  143. Message1(parser_e_invalid_enumerator_identifier, pattern);
  144. consume(token);
  145. end
  146. else
  147. Message(parser_e_enumerator_identifier_required);
  148. consume(_SEMICOLON);
  149. end;
  150. { hint directives, these can be separated by semicolons here,
  151. that needs to be handled here with a loop (PFV) }
  152. while try_consume_hintdirective(p.symoptions,p.deprecatedmsg) do
  153. Consume(_SEMICOLON);
  154. end;
  155. function class_destructor_head:tprocdef;
  156. var
  157. pd : tprocdef;
  158. begin
  159. result:=nil;
  160. consume(_DESTRUCTOR);
  161. parse_proc_head(current_structdef,potype_class_destructor,pd);
  162. if not assigned(pd) then
  163. begin
  164. consume(_SEMICOLON);
  165. exit;
  166. end;
  167. pd.calcparas;
  168. if (pd.maxparacount>0) then
  169. Message(parser_e_no_paras_for_class_destructor);
  170. consume(_SEMICOLON);
  171. include(current_structdef.objectoptions,oo_has_class_destructor);
  172. current_module.flags:=current_module.flags or uf_classinits;
  173. { no return value }
  174. pd.returndef:=voidtype;
  175. result:=pd;
  176. end;
  177. function destructor_head:tprocdef;
  178. var
  179. pd : tprocdef;
  180. begin
  181. result:=nil;
  182. consume(_DESTRUCTOR);
  183. parse_proc_head(current_structdef,potype_destructor,pd);
  184. if not assigned(pd) then
  185. begin
  186. consume(_SEMICOLON);
  187. exit;
  188. end;
  189. if (cs_constructor_name in current_settings.globalswitches) and
  190. (pd.procsym.name<>'DONE') then
  191. Message(parser_e_destructorname_must_be_done);
  192. pd.calcparas;
  193. if not(pd.maxparacount=0) and
  194. (m_fpc in current_settings.modeswitches) then
  195. Message(parser_e_no_paras_for_destructor);
  196. consume(_SEMICOLON);
  197. include(current_structdef.objectoptions,oo_has_destructor);
  198. { no return value }
  199. pd.returndef:=voidtype;
  200. result:=pd;
  201. end;
  202. procedure setinterfacemethodoptions;
  203. var
  204. i : longint;
  205. def : tdef;
  206. begin
  207. include(current_structdef.objectoptions,oo_has_virtual);
  208. for i:=0 to current_structdef.symtable.DefList.count-1 do
  209. begin
  210. def:=tdef(current_structdef.symtable.DefList[i]);
  211. if assigned(def) and
  212. (def.typ=procdef) then
  213. begin
  214. include(tprocdef(def).procoptions,po_virtualmethod);
  215. tprocdef(def).forwarddef:=false;
  216. end;
  217. end;
  218. end;
  219. procedure setobjcclassmethodoptions;
  220. var
  221. i : longint;
  222. def : tdef;
  223. begin
  224. for i:=0 to current_structdef.symtable.DefList.count-1 do
  225. begin
  226. def:=tdef(current_structdef.symtable.DefList[i]);
  227. if assigned(def) and
  228. (def.typ=procdef) then
  229. begin
  230. include(tprocdef(def).procoptions,po_virtualmethod);
  231. end;
  232. end;
  233. end;
  234. procedure handleImplementedInterface(intfdef : tobjectdef);
  235. begin
  236. if not is_interface(intfdef) then
  237. begin
  238. Message1(type_e_interface_type_expected,intfdef.typename);
  239. exit;
  240. end;
  241. if current_objectdef.find_implemented_interface(intfdef)<>nil then
  242. Message1(sym_e_duplicate_id,intfdef.objname^)
  243. else
  244. begin
  245. { allocate and prepare the GUID only if the class
  246. implements some interfaces. }
  247. if current_objectdef.ImplementedInterfaces.count = 0 then
  248. current_objectdef.prepareguid;
  249. current_objectdef.ImplementedInterfaces.Add(TImplementedInterface.Create(intfdef));
  250. end;
  251. end;
  252. procedure handleImplementedProtocol(intfdef : tobjectdef);
  253. begin
  254. intfdef:=find_real_objcclass_definition(intfdef,false);
  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,oo_is_formal] * 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,[stoAllowSpecialization,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. odt_helper: ; // nothing
  390. end;
  391. end;
  392. procedure parse_parent_classes;
  393. var
  394. intfchildof,
  395. childof : tobjectdef;
  396. hdef : tdef;
  397. hasparentdefined : boolean;
  398. begin
  399. childof:=nil;
  400. intfchildof:=nil;
  401. hasparentdefined:=false;
  402. { reads the parent class }
  403. if (token=_LKLAMMER) or
  404. is_objccategory(current_structdef) then
  405. begin
  406. consume(_LKLAMMER);
  407. { use single_type instead of id_type for specialize support }
  408. single_type(hdef,[stoAllowSpecialization, stoParseClassParent]);
  409. if (not assigned(hdef)) or
  410. (hdef.typ<>objectdef) then
  411. begin
  412. if assigned(hdef) then
  413. Message1(type_e_class_type_expected,hdef.typename)
  414. else if is_objccategory(current_structdef) then
  415. { a category must specify the class to extend }
  416. Message(type_e_objcclass_type_expected);
  417. end
  418. else
  419. begin
  420. childof:=tobjectdef(hdef);
  421. { a mix of class, interfaces, objects and cppclasses
  422. isn't allowed }
  423. case current_objectdef.objecttype of
  424. odt_class:
  425. if not(is_class(childof)) then
  426. begin
  427. if is_interface(childof) then
  428. begin
  429. { we insert the interface after the child
  430. is set, see below
  431. }
  432. intfchildof:=childof;
  433. childof:=class_tobject;
  434. end
  435. else
  436. Message(parser_e_mix_of_classes_and_objects);
  437. end
  438. else
  439. if oo_is_sealed in childof.objectoptions then
  440. Message1(parser_e_sealed_descendant,childof.typename);
  441. odt_interfacecorba,
  442. odt_interfacecom:
  443. begin
  444. if not(is_interface(childof)) then
  445. Message(parser_e_mix_of_classes_and_objects);
  446. current_objectdef.objecttype:=childof.objecttype;
  447. end;
  448. odt_cppclass:
  449. if not(is_cppclass(childof)) then
  450. Message(parser_e_mix_of_classes_and_objects);
  451. odt_objcclass:
  452. if not(is_objcclass(childof) or
  453. is_objccategory(childof)) then
  454. begin
  455. if is_objcprotocol(childof) then
  456. begin
  457. if not(oo_is_classhelper in current_structdef.objectoptions) then
  458. begin
  459. intfchildof:=childof;
  460. childof:=nil;
  461. CGMessage(parser_h_no_objc_parent);
  462. end
  463. else
  464. { a category must specify the class to extend }
  465. CGMessage(type_e_objcclass_type_expected);
  466. end
  467. else
  468. Message(parser_e_mix_of_classes_and_objects);
  469. end
  470. else
  471. childof:=find_real_objcclass_definition(childof,true);
  472. odt_objcprotocol:
  473. begin
  474. if not(is_objcprotocol(childof)) then
  475. Message(parser_e_mix_of_classes_and_objects);
  476. intfchildof:=childof;
  477. childof:=nil;
  478. end;
  479. odt_object:
  480. if not(is_object(childof)) then
  481. Message(parser_e_mix_of_classes_and_objects)
  482. else
  483. if oo_is_sealed in childof.objectoptions then
  484. Message1(parser_e_sealed_descendant,childof.typename);
  485. odt_dispinterface:
  486. Message(parser_e_dispinterface_cant_have_parent);
  487. odt_helper:
  488. if not is_objectpascal_helper(childof) then
  489. begin
  490. Message(type_e_helper_type_expected);
  491. childof:=nil;
  492. end;
  493. end;
  494. end;
  495. hasparentdefined:=true;
  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_dispinterface:
  508. childof:=interface_idispatch;
  509. odt_objcclass:
  510. CGMessage(parser_h_no_objc_parent);
  511. end;
  512. end;
  513. if assigned(childof) then
  514. begin
  515. { Forbid not completly defined objects to be used as parents. This will
  516. also prevent circular loops of classes, because we set the forward flag
  517. at the start of the new definition and will reset it below after the
  518. parent has been set }
  519. if (oo_is_forward in childof.objectoptions) then
  520. Message1(parser_e_forward_declaration_must_be_resolved,childof.objrealname^)
  521. else if not(oo_is_formal in childof.objectoptions) then
  522. current_objectdef.set_parent(childof)
  523. else
  524. Message1(sym_e_objc_formal_class_not_resolved,childof.objrealname^);
  525. end;
  526. { remove forward flag, is resolved }
  527. exclude(current_structdef.objectoptions,oo_is_forward);
  528. if hasparentdefined then
  529. begin
  530. if current_objectdef.objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  531. begin
  532. if assigned(intfchildof) then
  533. if current_objectdef.objecttype=odt_class then
  534. handleImplementedInterface(intfchildof)
  535. else
  536. handleImplementedProtocol(intfchildof);
  537. readImplementedInterfacesAndProtocols(current_objectdef.objecttype=odt_class);
  538. end;
  539. consume(_RKLAMMER);
  540. end;
  541. end;
  542. procedure parse_extended_type(helpertype:thelpertype);
  543. var
  544. hdef: tdef;
  545. begin
  546. if not is_objectpascal_helper(current_structdef) then
  547. Internalerror(2011021103);
  548. if helpertype=ht_none then
  549. Internalerror(2011021001);
  550. consume(_FOR);
  551. single_type(hdef,[stoParseClassParent]);
  552. if (not assigned(hdef)) or
  553. not (hdef.typ in [objectdef,recorddef]) then
  554. begin
  555. if helpertype=ht_class then
  556. Message1(type_e_class_type_expected,hdef.typename)
  557. else
  558. if helpertype=ht_record then
  559. Message1(type_e_record_type_expected,hdef.typename);
  560. end
  561. else
  562. begin
  563. case helpertype of
  564. ht_class:
  565. begin
  566. if not is_class(hdef) then
  567. Message1(type_e_class_type_expected,hdef.typename);
  568. { a class helper must extend the same class or a subclass
  569. of the class extended by the parent class helper }
  570. if assigned(current_objectdef.childof) then
  571. begin
  572. if not is_class(current_objectdef.childof.extendeddef) then
  573. Internalerror(2011021101);
  574. if not hdef.is_related(current_objectdef.childof.extendeddef) then
  575. Message1(type_e_class_helper_must_extend_subclass,current_objectdef.childof.extendeddef.typename);
  576. end;
  577. end;
  578. ht_record:
  579. begin
  580. if not is_record(hdef) then
  581. Message1(type_e_record_type_expected,hdef.typename);
  582. { a record helper must extend the same record as the
  583. parent helper }
  584. if assigned(current_objectdef.childof) then
  585. begin
  586. if not is_record(current_objectdef.childof.extendeddef) then
  587. Internalerror(2011021102);
  588. if hdef<>current_objectdef.childof.extendeddef then
  589. Message1(type_e_record_helper_must_extend_same_record,current_objectdef.childof.extendeddef.typename);
  590. end;
  591. end;
  592. end;
  593. current_objectdef.extendeddef:=tabstractrecorddef(hdef);
  594. end;
  595. end;
  596. procedure parse_guid;
  597. begin
  598. { read GUID }
  599. if (current_objectdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) and
  600. try_to_consume(_LECKKLAMMER) then
  601. begin
  602. readinterfaceiid;
  603. consume(_RECKKLAMMER);
  604. end
  605. else if (current_objectdef.objecttype=odt_dispinterface) then
  606. message(parser_e_dispinterface_needs_a_guid);
  607. end;
  608. procedure parse_object_members;
  609. procedure chkobjc(pd: tprocdef);
  610. begin
  611. if is_objc_class_or_protocol(pd.struct) then
  612. begin
  613. include(pd.procoptions,po_objc);
  614. end;
  615. end;
  616. procedure chkcpp(pd:tprocdef);
  617. begin
  618. { nothing currently }
  619. end;
  620. procedure maybe_parse_hint_directives(pd:tprocdef);
  621. var
  622. dummysymoptions : tsymoptions;
  623. deprecatedmsg : pshortstring;
  624. begin
  625. dummysymoptions:=[];
  626. deprecatedmsg:=nil;
  627. while try_consume_hintdirective(dummysymoptions,deprecatedmsg) do
  628. Consume(_SEMICOLON);
  629. if assigned(pd) then
  630. begin
  631. pd.symoptions:=pd.symoptions+dummysymoptions;
  632. pd.deprecatedmsg:=deprecatedmsg;
  633. end
  634. else
  635. stringdispose(deprecatedmsg);
  636. end;
  637. var
  638. pd : tprocdef;
  639. has_destructor,
  640. oldparse_only: boolean;
  641. object_member_blocktype : tblock_type;
  642. fields_allowed, is_classdef, classfields: boolean;
  643. vdoptions: tvar_dec_options;
  644. begin
  645. { empty class declaration ? }
  646. if (current_objectdef.objecttype in [odt_class,odt_objcclass]) and
  647. (token=_SEMICOLON) then
  648. exit;
  649. { in "publishable" classes the default access type is published }
  650. if (oo_can_have_published in current_structdef.objectoptions) then
  651. current_structdef.symtable.currentvisibility:=vis_published
  652. else
  653. current_structdef.symtable.currentvisibility:=vis_public;
  654. has_destructor:=false;
  655. fields_allowed:=true;
  656. is_classdef:=false;
  657. classfields:=false;
  658. object_member_blocktype:=bt_general;
  659. repeat
  660. case token of
  661. _TYPE :
  662. begin
  663. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper]) then
  664. Message(parser_e_type_var_const_only_in_records_and_classes);
  665. consume(_TYPE);
  666. object_member_blocktype:=bt_type;
  667. end;
  668. _VAR :
  669. begin
  670. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper]) then
  671. Message(parser_e_type_var_const_only_in_records_and_classes);
  672. consume(_VAR);
  673. fields_allowed:=true;
  674. object_member_blocktype:=bt_general;
  675. classfields:=is_classdef;
  676. is_classdef:=false;
  677. end;
  678. _CONST:
  679. begin
  680. if not(current_objectdef.objecttype in [odt_class,odt_object,odt_helper]) then
  681. Message(parser_e_type_var_const_only_in_records_and_classes);
  682. consume(_CONST);
  683. object_member_blocktype:=bt_const;
  684. end;
  685. _ID :
  686. begin
  687. if is_objcprotocol(current_structdef) and
  688. ((idtoken=_REQUIRED) or
  689. (idtoken=_OPTIONAL)) then
  690. begin
  691. current_structdef.symtable.currentlyoptional:=(idtoken=_OPTIONAL);
  692. consume(idtoken)
  693. end
  694. else case idtoken of
  695. _PRIVATE :
  696. begin
  697. if is_interface(current_structdef) or
  698. is_objc_protocol_or_category(current_structdef) then
  699. Message(parser_e_no_access_specifier_in_interfaces);
  700. consume(_PRIVATE);
  701. current_structdef.symtable.currentvisibility:=vis_private;
  702. include(current_structdef.objectoptions,oo_has_private);
  703. fields_allowed:=true;
  704. is_classdef:=false;
  705. classfields:=false;
  706. object_member_blocktype:=bt_general;
  707. end;
  708. _PROTECTED :
  709. begin
  710. if is_interface(current_structdef) or
  711. is_objc_protocol_or_category(current_structdef) then
  712. Message(parser_e_no_access_specifier_in_interfaces);
  713. consume(_PROTECTED);
  714. current_structdef.symtable.currentvisibility:=vis_protected;
  715. include(current_structdef.objectoptions,oo_has_protected);
  716. fields_allowed:=true;
  717. is_classdef:=false;
  718. classfields:=false;
  719. object_member_blocktype:=bt_general;
  720. end;
  721. _PUBLIC :
  722. begin
  723. if is_interface(current_structdef) or
  724. is_objc_protocol_or_category(current_structdef) then
  725. Message(parser_e_no_access_specifier_in_interfaces);
  726. consume(_PUBLIC);
  727. current_structdef.symtable.currentvisibility:=vis_public;
  728. fields_allowed:=true;
  729. is_classdef:=false;
  730. classfields:=false;
  731. object_member_blocktype:=bt_general;
  732. end;
  733. _PUBLISHED :
  734. begin
  735. { we've to check for a pushlished section in non- }
  736. { publishable classes later, if a real declaration }
  737. { this is the way, delphi does it }
  738. if is_interface(current_structdef) then
  739. Message(parser_e_no_access_specifier_in_interfaces);
  740. { Objective-C classes do not support "published",
  741. as basically everything is published. }
  742. if is_objc_class_or_protocol(current_structdef) then
  743. Message(parser_e_no_objc_published);
  744. consume(_PUBLISHED);
  745. current_structdef.symtable.currentvisibility:=vis_published;
  746. fields_allowed:=true;
  747. is_classdef:=false;
  748. classfields:=false;
  749. object_member_blocktype:=bt_general;
  750. end;
  751. _STRICT :
  752. begin
  753. if is_interface(current_structdef) or
  754. is_objc_protocol_or_category(current_structdef) then
  755. Message(parser_e_no_access_specifier_in_interfaces);
  756. consume(_STRICT);
  757. if token=_ID then
  758. begin
  759. case idtoken of
  760. _PRIVATE:
  761. begin
  762. consume(_PRIVATE);
  763. current_structdef.symtable.currentvisibility:=vis_strictprivate;
  764. include(current_structdef.objectoptions,oo_has_strictprivate);
  765. end;
  766. _PROTECTED:
  767. begin
  768. consume(_PROTECTED);
  769. current_structdef.symtable.currentvisibility:=vis_strictprotected;
  770. include(current_structdef.objectoptions,oo_has_strictprotected);
  771. end;
  772. else
  773. message(parser_e_protected_or_private_expected);
  774. end;
  775. end
  776. else
  777. message(parser_e_protected_or_private_expected);
  778. fields_allowed:=true;
  779. is_classdef:=false;
  780. classfields:=false;
  781. object_member_blocktype:=bt_general;
  782. end
  783. else
  784. begin
  785. if object_member_blocktype=bt_general then
  786. begin
  787. if is_interface(current_structdef) or
  788. is_objc_protocol_or_category(current_structdef) or
  789. is_objectpascal_helper(current_structdef) then
  790. Message(parser_e_no_vars_in_interfaces);
  791. if (current_structdef.symtable.currentvisibility=vis_published) and
  792. not(oo_can_have_published in current_structdef.objectoptions) then
  793. Message(parser_e_cant_have_published);
  794. if (not fields_allowed) then
  795. Message(parser_e_field_not_allowed_here);
  796. vdoptions:=[vd_object];
  797. if classfields then
  798. include(vdoptions,vd_class);
  799. read_record_fields(vdoptions);
  800. end
  801. else if object_member_blocktype=bt_type then
  802. types_dec(true)
  803. else if object_member_blocktype=bt_const then
  804. consts_dec(true)
  805. else
  806. internalerror(201001110);
  807. end;
  808. end;
  809. end;
  810. _PROPERTY :
  811. begin
  812. struct_property_dec(is_classdef);
  813. fields_allowed:=false;
  814. is_classdef:=false;
  815. end;
  816. _CLASS:
  817. begin
  818. is_classdef:=false;
  819. { read class method/field/property }
  820. consume(_CLASS);
  821. { class modifier is only allowed for procedures, functions, }
  822. { constructors, destructors, fields and properties }
  823. if not(token in [_FUNCTION,_PROCEDURE,_PROPERTY,_VAR,_CONSTRUCTOR,_DESTRUCTOR]) then
  824. Message(parser_e_procedure_or_function_expected);
  825. if is_interface(current_structdef) then
  826. Message(parser_e_no_static_method_in_interfaces)
  827. else
  828. { class methods are also allowed for Objective-C protocols }
  829. is_classdef:=true;
  830. end;
  831. _PROCEDURE,
  832. _FUNCTION:
  833. begin
  834. if (current_structdef.symtable.currentvisibility=vis_published) and
  835. not(oo_can_have_published in current_structdef.objectoptions) then
  836. Message(parser_e_cant_have_published);
  837. oldparse_only:=parse_only;
  838. parse_only:=true;
  839. pd:=parse_proc_dec(is_classdef,current_structdef);
  840. { this is for error recovery as well as forward }
  841. { interface mappings, i.e. mapping to a method }
  842. { which isn't declared yet }
  843. if assigned(pd) then
  844. begin
  845. parse_object_proc_directives(pd);
  846. { check if dispid is set }
  847. if is_dispinterface(pd.struct) and not (po_dispid in pd.procoptions) then
  848. begin
  849. pd.dispid:=tobjectdef(pd.struct).get_next_dispid;
  850. include(pd.procoptions, po_dispid);
  851. end;
  852. { all Macintosh Object Pascal methods are virtual. }
  853. { this can't be a class method, because macpas mode }
  854. { has no m_class }
  855. if (m_mac in current_settings.modeswitches) then
  856. include(pd.procoptions,po_virtualmethod);
  857. { for record helpers only static class methods are allowed }
  858. if is_objectpascal_helper(current_structdef) and
  859. is_record(current_objectdef.extendeddef) and
  860. is_classdef and not (po_staticmethod in pd.procoptions) then
  861. MessagePos(pd.fileinfo, parser_e_class_methods_only_static_in_records);
  862. handle_calling_convention(pd);
  863. { add definition to procsym }
  864. proc_add_definition(pd);
  865. { add procdef options to objectdef options }
  866. if (po_msgint in pd.procoptions) then
  867. include(current_structdef.objectoptions,oo_has_msgint);
  868. if (po_msgstr in pd.procoptions) then
  869. include(current_structdef.objectoptions,oo_has_msgstr);
  870. if (po_virtualmethod in pd.procoptions) then
  871. include(current_structdef.objectoptions,oo_has_virtual);
  872. chkcpp(pd);
  873. chkobjc(pd);
  874. end;
  875. maybe_parse_hint_directives(pd);
  876. parse_only:=oldparse_only;
  877. fields_allowed:=false;
  878. is_classdef:=false;
  879. end;
  880. _CONSTRUCTOR :
  881. begin
  882. if (current_structdef.symtable.currentvisibility=vis_published) and
  883. not(oo_can_have_published in current_structdef.objectoptions) then
  884. Message(parser_e_cant_have_published);
  885. if not is_classdef and not(current_structdef.symtable.currentvisibility in [vis_public,vis_published]) then
  886. Message(parser_w_constructor_should_be_public);
  887. if is_interface(current_structdef) then
  888. Message(parser_e_no_con_des_in_interfaces);
  889. { Objective-C does not know the concept of a constructor }
  890. if is_objc_class_or_protocol(current_structdef) then
  891. Message(parser_e_objc_no_constructor_destructor);
  892. if is_objectpascal_helper(current_structdef) then
  893. if is_classdef then
  894. { class constructors are not allowed in class helpers }
  895. Message(parser_e_no_class_constructor_in_helpers)
  896. else
  897. if is_record(current_objectdef.extendeddef) then
  898. { as long as constructors aren't allowed in records they
  899. aren't allowed in helpers either }
  900. Message(parser_e_no_constructor_in_records);
  901. { only 1 class constructor is allowed }
  902. if is_classdef and (oo_has_class_constructor in current_structdef.objectoptions) then
  903. Message1(parser_e_only_one_class_constructor_allowed, current_structdef.objrealname^);
  904. oldparse_only:=parse_only;
  905. parse_only:=true;
  906. if is_classdef then
  907. pd:=class_constructor_head
  908. else
  909. pd:=constructor_head;
  910. parse_object_proc_directives(pd);
  911. handle_calling_convention(pd);
  912. { add definition to procsym }
  913. proc_add_definition(pd);
  914. { add procdef options to objectdef options }
  915. if (po_virtualmethod in pd.procoptions) then
  916. include(current_structdef.objectoptions,oo_has_virtual);
  917. chkcpp(pd);
  918. maybe_parse_hint_directives(pd);
  919. parse_only:=oldparse_only;
  920. fields_allowed:=false;
  921. is_classdef:=false;
  922. end;
  923. _DESTRUCTOR :
  924. begin
  925. if (current_structdef.symtable.currentvisibility=vis_published) and
  926. not(oo_can_have_published in current_structdef.objectoptions) then
  927. Message(parser_e_cant_have_published);
  928. if not is_classdef then
  929. if has_destructor then
  930. Message(parser_n_only_one_destructor)
  931. else
  932. has_destructor:=true;
  933. if is_interface(current_structdef) then
  934. Message(parser_e_no_con_des_in_interfaces);
  935. { (class) destructors are not allowed in class helpers }
  936. if is_objectpascal_helper(current_structdef) then
  937. Message(parser_e_no_destructor_in_records);
  938. if not is_classdef and (current_structdef.symtable.currentvisibility<>vis_public) then
  939. Message(parser_w_destructor_should_be_public);
  940. { Objective-C does not know the concept of a destructor }
  941. if is_objc_class_or_protocol(current_structdef) then
  942. Message(parser_e_objc_no_constructor_destructor);
  943. { only 1 class destructor is allowed }
  944. if is_classdef and (oo_has_class_destructor in current_structdef.objectoptions) then
  945. Message1(parser_e_only_one_class_destructor_allowed, current_structdef.objrealname^);
  946. oldparse_only:=parse_only;
  947. parse_only:=true;
  948. if is_classdef then
  949. pd:=class_destructor_head
  950. else
  951. pd:=destructor_head;
  952. parse_object_proc_directives(pd);
  953. handle_calling_convention(pd);
  954. { add definition to procsym }
  955. proc_add_definition(pd);
  956. { add procdef options to objectdef options }
  957. if (po_virtualmethod in pd.procoptions) then
  958. include(current_structdef.objectoptions,oo_has_virtual);
  959. chkcpp(pd);
  960. maybe_parse_hint_directives(pd);
  961. parse_only:=oldparse_only;
  962. fields_allowed:=false;
  963. is_classdef:=false;
  964. end;
  965. _END :
  966. begin
  967. consume(_END);
  968. break;
  969. end;
  970. else
  971. consume(_ID); { Give a ident expected message, like tp7 }
  972. end;
  973. until false;
  974. end;
  975. function object_dec(objecttype:tobjecttyp;const n:tidstring;genericdef:tstoreddef;genericlist:TFPObjectList;fd : tobjectdef;helpertype:thelpertype) : tobjectdef;
  976. var
  977. old_current_structdef: tabstractrecorddef;
  978. old_current_genericdef,
  979. old_current_specializedef: tstoreddef;
  980. old_parse_generic: boolean;
  981. list: TFPObjectList;
  982. s: String;
  983. st: TSymtable;
  984. begin
  985. old_current_structdef:=current_structdef;
  986. old_current_genericdef:=current_genericdef;
  987. old_current_specializedef:=current_specializedef;
  988. old_parse_generic:=parse_generic;
  989. current_structdef:=nil;
  990. current_genericdef:=nil;
  991. current_specializedef:=nil;
  992. { objects and class types can't be declared local }
  993. if not(symtablestack.top.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  994. not assigned(genericlist) then
  995. Message(parser_e_no_local_objects);
  996. { reuse forward objectdef? }
  997. if assigned(fd) then
  998. begin
  999. if fd.objecttype<>objecttype then
  1000. begin
  1001. Message(parser_e_forward_mismatch);
  1002. { recover }
  1003. current_structdef:=tobjectdef.create(current_objectdef.objecttype,n,nil);
  1004. include(current_structdef.objectoptions,oo_is_forward);
  1005. end
  1006. else
  1007. current_structdef:=fd
  1008. end
  1009. else
  1010. begin
  1011. { anonym objects aren't allow (o : object a : longint; end;) }
  1012. if n='' then
  1013. Message(parser_f_no_anonym_objects);
  1014. { create new class }
  1015. current_structdef:=tobjectdef.create(objecttype,n,nil);
  1016. { include always the forward flag, it'll be removed after the parent class have been
  1017. added. This is to prevent circular childof loops }
  1018. include(current_structdef.objectoptions,oo_is_forward);
  1019. if (cs_compilesystem in current_settings.moduleswitches) then
  1020. begin
  1021. case current_objectdef.objecttype of
  1022. odt_interfacecom :
  1023. if (current_structdef.objname^='IUNKNOWN') then
  1024. interface_iunknown:=current_objectdef
  1025. else
  1026. if (current_structdef.objname^='IDISPATCH') then
  1027. interface_idispatch:=current_objectdef;
  1028. odt_class :
  1029. if (current_structdef.objname^='TOBJECT') then
  1030. class_tobject:=current_objectdef;
  1031. end;
  1032. end;
  1033. if (current_module.modulename^='OBJCBASE') then
  1034. begin
  1035. case current_objectdef.objecttype of
  1036. odt_objcclass:
  1037. if (current_objectdef.objname^='Protocol') then
  1038. objc_protocoltype:=current_objectdef;
  1039. end;
  1040. end;
  1041. end;
  1042. { usage of specialized type inside its generic template }
  1043. if assigned(genericdef) then
  1044. current_specializedef:=current_structdef
  1045. { reject declaration of generic class inside generic class }
  1046. else if assigned(genericlist) then
  1047. current_genericdef:=current_structdef;
  1048. { set published flag in $M+ mode, it can also be inherited and will
  1049. be added when the parent class set with tobjectdef.set_parent (PFV) }
  1050. if (cs_generate_rtti in current_settings.localswitches) and
  1051. (current_objectdef.objecttype in [odt_interfacecom,odt_class,odt_helper]) then
  1052. include(current_structdef.objectoptions,oo_can_have_published);
  1053. { Objective-C objectdefs can be "formal definitions", in which case
  1054. the syntax is "type tc = objcclass external;" -> we have to parse
  1055. its object options (external) already here, to make sure that such
  1056. definitions are recognised as formal defs }
  1057. if objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory] then
  1058. parse_object_options;
  1059. { forward def? }
  1060. if not assigned(fd) and
  1061. (token=_SEMICOLON) then
  1062. begin
  1063. { add to the list of definitions to check that the forward
  1064. is resolved. this is required for delphi mode }
  1065. current_module.checkforwarddefs.add(current_structdef);
  1066. end
  1067. else
  1068. begin
  1069. { change objccategories into objcclass helpers }
  1070. if (objecttype=odt_objccategory) then
  1071. begin
  1072. current_objectdef.objecttype:=odt_objcclass;
  1073. include(current_structdef.objectoptions,oo_is_classhelper);
  1074. end;
  1075. { include the class helper flag for Object Pascal helpers }
  1076. if (objecttype=odt_helper) then
  1077. include(current_objectdef.objectoptions,oo_is_classhelper);
  1078. { parse list of options (abstract / sealed) }
  1079. if not(objecttype in [odt_objcclass,odt_objcprotocol,odt_objccategory]) then
  1080. parse_object_options;
  1081. symtablestack.push(current_structdef.symtable);
  1082. insert_generic_parameter_types(current_structdef,genericdef,genericlist);
  1083. parse_generic:=(df_generic in current_structdef.defoptions);
  1084. { parse list of parent classes }
  1085. { for record helpers in mode Delphi this is not allowed }
  1086. if not (is_objectpascal_helper(current_objectdef) and
  1087. (m_delphi in current_settings.modeswitches) and
  1088. (helpertype=ht_record)) then
  1089. parse_parent_classes
  1090. else
  1091. { remove forward flag, is resolved (this is normally done inside
  1092. parse_parent_classes) }
  1093. exclude(current_structdef.objectoptions,oo_is_forward);
  1094. { parse extended type for helpers }
  1095. if is_objectpascal_helper(current_structdef) then
  1096. parse_extended_type(helpertype);
  1097. { parse optional GUID for interfaces }
  1098. parse_guid;
  1099. { parse and insert object members }
  1100. parse_object_members;
  1101. symtablestack.pop(current_structdef.symtable);
  1102. end;
  1103. { generate vmt space if needed }
  1104. if not(oo_has_vmt in current_structdef.objectoptions) and
  1105. not(oo_is_forward in current_structdef.objectoptions) and
  1106. (
  1107. ([oo_has_virtual,oo_has_constructor,oo_has_destructor]*current_structdef.objectoptions<>[]) or
  1108. (current_objectdef.objecttype in [odt_class])
  1109. ) then
  1110. current_objectdef.insertvmt;
  1111. { for implemented classes with a vmt check if there is a constructor }
  1112. if (oo_has_vmt in current_structdef.objectoptions) and
  1113. not(oo_is_forward in current_structdef.objectoptions) and
  1114. not(oo_has_constructor in current_structdef.objectoptions) and
  1115. not is_objc_class_or_protocol(current_structdef) then
  1116. Message1(parser_w_virtual_without_constructor,current_structdef.objrealname^);
  1117. if is_interface(current_structdef) or
  1118. is_objcprotocol(current_structdef) then
  1119. setinterfacemethodoptions
  1120. else if is_objcclass(current_structdef) then
  1121. setobjcclassmethodoptions;
  1122. { if this helper is defined in the implementation section of the unit
  1123. or inside the main project file, the extendeddefs list of the current
  1124. module must be updated (it will be removed when poping the symtable) }
  1125. if is_objectpascal_helper(current_structdef) then
  1126. begin
  1127. { the topmost symtable must be a static symtable }
  1128. st:=current_structdef.owner;
  1129. while st.symtabletype in [objectsymtable,recordsymtable] do
  1130. st:=st.defowner.owner;
  1131. if st.symtabletype=staticsymtable then
  1132. begin
  1133. s:=make_mangledname('',current_objectdef.extendeddef.symtable,'');
  1134. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1135. if not assigned(list) then
  1136. begin
  1137. list:=TFPObjectList.Create(false);
  1138. current_module.extendeddefs.Add(s, list);
  1139. end;
  1140. list.add(current_structdef);
  1141. end;
  1142. end;
  1143. { return defined objectdef }
  1144. result:=current_objectdef;
  1145. { restore old state }
  1146. current_structdef:=old_current_structdef;
  1147. current_genericdef:=old_current_genericdef;
  1148. current_specializedef:=old_current_specializedef;
  1149. parse_generic:=old_parse_generic;
  1150. end;
  1151. end.