pdecl.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does declaration (but not type) parsing 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 pdecl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,
  25. { symtable }
  26. symsym,symdef,
  27. { pass_1 }
  28. node;
  29. function readconstant(const orgname:string;const filepos:tfileposinfo):tconstsym;
  30. procedure const_dec;
  31. procedure consts_dec(in_structure, allow_typed_const: boolean);
  32. procedure label_dec;
  33. procedure type_dec;
  34. procedure types_dec(in_structure: boolean);
  35. procedure var_dec;
  36. procedure threadvar_dec;
  37. procedure property_dec;
  38. procedure resourcestring_dec;
  39. procedure parse_rttiattributes(var rtti_attributes: trtti_attributesdef);
  40. procedure add_synthetic_rtti_funtion_declarations(rtti_attributesdef: trtti_attributesdef; name: shortstring);
  41. implementation
  42. uses
  43. SysUtils,
  44. { common }
  45. cutils,
  46. { global }
  47. globals,tokens,verbose,widestr,constexp,
  48. systems,
  49. { aasm }
  50. aasmbase,aasmtai,aasmdata,fmodule,
  51. { symtable }
  52. symconst,symbase,symtype,symtable,symcreat,paramgr,defutil,
  53. { pass 1 }
  54. htypechk,
  55. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nobj,nmem,
  56. { codegen }
  57. ncgutil,ngenutil,
  58. { parser }
  59. scanner,
  60. pbase,pexpr,ptype,ptconst,pdecsub,pdecvar,pdecobj,pgenutil,
  61. {$ifdef jvm}
  62. pjvm,
  63. {$endif}
  64. { cpu-information }
  65. cpuinfo
  66. ;
  67. var
  68. current_rtticlassattributesdef : trtti_attributesdef;
  69. function readconstant(const orgname:string;const filepos:tfileposinfo):tconstsym;
  70. var
  71. hp : tconstsym;
  72. p : tnode;
  73. ps : pconstset;
  74. pd : pbestreal;
  75. pg : pguid;
  76. sp : pchar;
  77. pw : pcompilerwidestring;
  78. storetokenpos : tfileposinfo;
  79. begin
  80. readconstant:=nil;
  81. if orgname='' then
  82. internalerror(9584582);
  83. hp:=nil;
  84. p:=comp_expr(true,false);
  85. storetokenpos:=current_tokenpos;
  86. current_tokenpos:=filepos;
  87. case p.nodetype of
  88. ordconstn:
  89. begin
  90. if p.resultdef.typ=pointerdef then
  91. hp:=tconstsym.create_ordptr(orgname,constpointer,tordconstnode(p).value.uvalue,p.resultdef)
  92. else
  93. hp:=tconstsym.create_ord(orgname,constord,tordconstnode(p).value,p.resultdef);
  94. end;
  95. stringconstn:
  96. begin
  97. if is_wide_or_unicode_string(p.resultdef) then
  98. begin
  99. initwidestring(pw);
  100. copywidestring(pcompilerwidestring(tstringconstnode(p).value_str),pw);
  101. hp:=tconstsym.create_wstring(orgname,constwstring,pw);
  102. end
  103. else
  104. begin
  105. getmem(sp,tstringconstnode(p).len+1);
  106. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  107. hp:=tconstsym.create_string(orgname,conststring,sp,tstringconstnode(p).len);
  108. end;
  109. end;
  110. realconstn :
  111. begin
  112. new(pd);
  113. pd^:=trealconstnode(p).value_real;
  114. hp:=tconstsym.create_ptr(orgname,constreal,pd,p.resultdef);
  115. end;
  116. setconstn :
  117. begin
  118. new(ps);
  119. ps^:=tsetconstnode(p).value_set^;
  120. hp:=tconstsym.create_ptr(orgname,constset,ps,p.resultdef);
  121. end;
  122. pointerconstn :
  123. begin
  124. hp:=tconstsym.create_ordptr(orgname,constpointer,tpointerconstnode(p).value,p.resultdef);
  125. end;
  126. niln :
  127. begin
  128. hp:=tconstsym.create_ord(orgname,constnil,0,p.resultdef);
  129. end;
  130. typen :
  131. begin
  132. if is_interface(p.resultdef) then
  133. begin
  134. if assigned(tobjectdef(p.resultdef).iidguid) then
  135. begin
  136. new(pg);
  137. pg^:=tobjectdef(p.resultdef).iidguid^;
  138. hp:=tconstsym.create_ptr(orgname,constguid,pg,p.resultdef);
  139. end
  140. else
  141. Message1(parser_e_interface_has_no_guid,tobjectdef(p.resultdef).objrealname^);
  142. end
  143. else
  144. Message(parser_e_illegal_expression);
  145. end;
  146. inlinen:
  147. begin
  148. { this situation only happens if a intrinsic is parsed that has a
  149. generic type as its argument. As we don't know certain
  150. information about the final type yet, we need to use safe
  151. values (mostly 0) }
  152. if not parse_generic then
  153. Message(parser_e_illegal_expression);
  154. case tinlinenode(p).inlinenumber of
  155. in_sizeof_x,
  156. in_bitsizeof_x:
  157. begin
  158. hp:=tconstsym.create_ord(orgname,constord,0,p.resultdef);
  159. end;
  160. { add other cases here if necessary }
  161. else
  162. Message(parser_e_illegal_expression);
  163. end;
  164. end;
  165. else
  166. Message(parser_e_illegal_expression);
  167. end;
  168. current_tokenpos:=storetokenpos;
  169. p.free;
  170. readconstant:=hp;
  171. end;
  172. procedure const_dec;
  173. begin
  174. consume(_CONST);
  175. consts_dec(false,true);
  176. end;
  177. procedure consts_dec(in_structure, allow_typed_const: boolean);
  178. var
  179. orgname : TIDString;
  180. hdef : tdef;
  181. sym : tsym;
  182. dummysymoptions : tsymoptions;
  183. deprecatedmsg : pshortstring;
  184. storetokenpos,filepos : tfileposinfo;
  185. old_block_type : tblock_type;
  186. skipequal : boolean;
  187. tclist : tasmlist;
  188. varspez : tvarspez;
  189. begin
  190. old_block_type:=block_type;
  191. block_type:=bt_const;
  192. repeat
  193. orgname:=orgpattern;
  194. filepos:=current_tokenpos;
  195. consume(_ID);
  196. case token of
  197. _EQ:
  198. begin
  199. consume(_EQ);
  200. sym:=readconstant(orgname,filepos);
  201. { Support hint directives }
  202. dummysymoptions:=[];
  203. deprecatedmsg:=nil;
  204. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  205. if assigned(sym) then
  206. begin
  207. sym.symoptions:=sym.symoptions+dummysymoptions;
  208. sym.deprecatedmsg:=deprecatedmsg;
  209. sym.visibility:=symtablestack.top.currentvisibility;
  210. symtablestack.top.insert(sym);
  211. {$ifdef jvm}
  212. { for the JVM target, some constants need to be
  213. initialized at run time (enums, sets) -> create fake
  214. typed const to do so }
  215. if assigned(tconstsym(sym).constdef) and
  216. (tconstsym(sym).constdef.typ in [enumdef,setdef]) then
  217. jvm_add_typed_const_initializer(tconstsym(sym));
  218. {$endif}
  219. end
  220. else
  221. stringdispose(deprecatedmsg);
  222. consume(_SEMICOLON);
  223. end;
  224. _COLON:
  225. begin
  226. if not allow_typed_const then
  227. begin
  228. Message(parser_e_no_typed_const);
  229. consume_all_until(_SEMICOLON);
  230. end;
  231. { set the blocktype first so a consume also supports a
  232. caret, to support const s : ^string = nil }
  233. block_type:=bt_const_type;
  234. consume(_COLON);
  235. read_anon_type(hdef,false);
  236. block_type:=bt_const;
  237. skipequal:=false;
  238. { create symbol }
  239. storetokenpos:=current_tokenpos;
  240. current_tokenpos:=filepos;
  241. if not (cs_typed_const_writable in current_settings.localswitches) then
  242. varspez:=vs_const
  243. else
  244. varspez:=vs_value;
  245. { if we are dealing with structure const then we need to handle it as a
  246. structure static variable: create a symbol in unit symtable and a reference
  247. to it from the structure or linking will fail }
  248. if symtablestack.top.symtabletype in [recordsymtable,ObjectSymtable] then
  249. begin
  250. sym:=tfieldvarsym.create(orgname,varspez,hdef,[]);
  251. symtablestack.top.insert(sym);
  252. sym:=make_field_static(symtablestack.top,tfieldvarsym(sym));
  253. end
  254. else
  255. begin
  256. sym:=tstaticvarsym.create(orgname,varspez,hdef,[]);
  257. sym.visibility:=symtablestack.top.currentvisibility;
  258. symtablestack.top.insert(sym);
  259. end;
  260. current_tokenpos:=storetokenpos;
  261. { procvar can have proc directives, but not type references }
  262. if (hdef.typ=procvardef) and
  263. (hdef.typesym=nil) then
  264. begin
  265. { support p : procedure;stdcall=nil; }
  266. if try_to_consume(_SEMICOLON) then
  267. begin
  268. if check_proc_directive(true) then
  269. parse_var_proc_directives(sym)
  270. else
  271. begin
  272. Message(parser_e_proc_directive_expected);
  273. skipequal:=true;
  274. end;
  275. end
  276. else
  277. { support p : procedure stdcall=nil; }
  278. begin
  279. if check_proc_directive(true) then
  280. parse_var_proc_directives(sym);
  281. end;
  282. { add default calling convention }
  283. handle_calling_convention(tabstractprocdef(hdef));
  284. end;
  285. if not skipequal then
  286. begin
  287. { get init value }
  288. consume(_EQ);
  289. if (cs_typed_const_writable in current_settings.localswitches) then
  290. tclist:=current_asmdata.asmlists[al_typedconsts]
  291. else
  292. tclist:=current_asmdata.asmlists[al_rotypedconsts];
  293. read_typed_const(tclist,tstaticvarsym(sym),in_structure);
  294. end;
  295. end;
  296. else
  297. { generate an error }
  298. consume(_EQ);
  299. end;
  300. until (token<>_ID) or
  301. (in_structure and
  302. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  303. ((m_final_fields in current_settings.modeswitches) and
  304. (idtoken=_FINAL))));
  305. block_type:=old_block_type;
  306. end;
  307. procedure label_dec;
  308. var
  309. labelsym : tlabelsym;
  310. begin
  311. consume(_LABEL);
  312. if not(cs_support_goto in current_settings.moduleswitches) then
  313. Message(sym_e_goto_and_label_not_supported);
  314. repeat
  315. if not(token in [_ID,_INTCONST]) then
  316. consume(_ID)
  317. else
  318. begin
  319. if token=_ID then
  320. labelsym:=tlabelsym.create(orgpattern)
  321. else
  322. labelsym:=tlabelsym.create(pattern);
  323. symtablestack.top.insert(labelsym);
  324. if m_non_local_goto in current_settings.modeswitches then
  325. begin
  326. if symtablestack.top.symtabletype=localsymtable then
  327. begin
  328. labelsym.jumpbuf:=tlocalvarsym.create('LABEL$_'+labelsym.name,vs_value,rec_jmp_buf,[]);
  329. symtablestack.top.insert(labelsym.jumpbuf);
  330. end
  331. else
  332. begin
  333. labelsym.jumpbuf:=tstaticvarsym.create('LABEL$_'+labelsym.name,vs_value,rec_jmp_buf,[]);
  334. symtablestack.top.insert(labelsym.jumpbuf);
  335. cnodeutils.insertbssdata(tstaticvarsym(labelsym.jumpbuf));
  336. end;
  337. include(labelsym.jumpbuf.symoptions,sp_internal);
  338. { the buffer will be setup later, but avoid a hint }
  339. tabstractvarsym(labelsym.jumpbuf).varstate:=vs_written;
  340. end;
  341. consume(token);
  342. end;
  343. if token<>_SEMICOLON then consume(_COMMA);
  344. until not(token in [_ID,_INTCONST]);
  345. consume(_SEMICOLON);
  346. end;
  347. procedure parse_rttiattributes(var rtti_attributes: trtti_attributesdef);
  348. var
  349. p, p1: tnode;
  350. paras: tnode;
  351. again: boolean;
  352. od: tobjectdef;
  353. classattrdef: tobjectdef;
  354. constrpd: tprocdef;
  355. typesym: ttypesym;
  356. oldblock_type: tblock_type;
  357. begin
  358. consume(_LECKKLAMMER);
  359. { Parse attribute type }
  360. p := factor(false,true);
  361. if p.nodetype<>errorn then
  362. begin
  363. typesym := ttypesym(ttypenode(p).typesym);
  364. od := tobjectdef(ttypenode(p).typedef);
  365. classattrdef := tobjectdef(search_system_type('TCUSTOMATTRIBUTE').typedef);
  366. if not od.is_related(classattrdef) then
  367. incompatibletypes(od,classattrdef);
  368. { Search the tprocdef of the constructor which has to be called. }
  369. constrpd := od.find_procdef_bytype(potype_constructor);
  370. { Parse the attribute-parameters as if it is a list of parameters from
  371. a call to the constrpd constructor in an execution-block. }
  372. p1 := cloadvmtaddrnode.create(ctypenode.create(od));
  373. again:=true;
  374. oldblock_type := block_type;
  375. block_type := bt_body;
  376. do_member_read(od,false,constrpd.procsym,p1,again,[]);
  377. block_type:=oldblock_type;
  378. { Add attribute to attribute list which will be added
  379. to the property which is defined next. }
  380. if not assigned(rtti_attributes) then
  381. rtti_attributes := trtti_attributesdef.create;
  382. rtti_attributes.addattribute(typesym,p1);
  383. end;
  384. p.free;
  385. consume(_RECKKLAMMER);
  386. end;
  387. procedure add_synthetic_rtti_funtion_declarations(rtti_attributesdef: trtti_attributesdef; name: shortstring);
  388. var
  389. i: Integer;
  390. sstate: tscannerstate;
  391. attribute: trtti_attribute;
  392. pd: tprocdef;
  393. begin
  394. for i := 0 to rtti_attributesdef.get_attribute_count-1 do
  395. begin
  396. attribute := trtti_attribute(rtti_attributesdef.rtti_attributes[i]);
  397. replace_scanner('rtti_class_attributes',sstate);
  398. if str_parse_method_dec('function rtti_'+name+'_'+IntToStr(i)+':'+ attribute.typesym.Name +';',potype_function,false,tabstractrecorddef(ttypesym(attribute.typesym).typedef),pd) then
  399. pd.synthetickind:=tsk_get_rttiattribute
  400. else
  401. internalerror(2012052601);
  402. pd.skpara:=attribute;
  403. attribute.symbolname:=pd.mangledname;
  404. restore_scanner(sstate);
  405. end;
  406. end;
  407. procedure types_dec(in_structure: boolean);
  408. procedure finalize_class_external_status(od: tobjectdef);
  409. begin
  410. if [oo_is_external,oo_is_forward] <= od.objectoptions then
  411. begin
  412. { formal definition: x = objcclass external; }
  413. exclude(od.objectoptions,oo_is_forward);
  414. include(od.objectoptions,oo_is_formal);
  415. end;
  416. end;
  417. var
  418. typename,orgtypename,
  419. gentypename,genorgtypename : TIDString;
  420. newtype : ttypesym;
  421. sym : tsym;
  422. hdef : tdef;
  423. defpos,storetokenpos : tfileposinfo;
  424. old_block_type : tblock_type;
  425. old_checkforwarddefs: TFPObjectList;
  426. objecttype : tobjecttyp;
  427. isgeneric,
  428. isunique,
  429. istyperenaming : boolean;
  430. generictypelist : TFPObjectList;
  431. generictokenbuf : tdynamicarray;
  432. vmtbuilder : TVMTBuilder;
  433. p:tnode;
  434. gendef : tstoreddef;
  435. s : shortstring;
  436. pd: tprocdef;
  437. hashedid : thashedidstring;
  438. begin
  439. old_block_type:=block_type;
  440. { save unit container of forward declarations -
  441. we can be inside nested class type block }
  442. old_checkforwarddefs:=current_module.checkforwarddefs;
  443. current_module.checkforwarddefs:=TFPObjectList.Create(false);
  444. block_type:=bt_type;
  445. repeat
  446. defpos:=current_tokenpos;
  447. istyperenaming:=false;
  448. generictypelist:=nil;
  449. generictokenbuf:=nil;
  450. { fpc generic declaration? }
  451. isgeneric:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_GENERIC);
  452. { class attribute definitions? }
  453. while token=_LECKKLAMMER do
  454. begin
  455. parse_rttiattributes(current_rtticlassattributesdef);
  456. end;
  457. typename:=pattern;
  458. orgtypename:=orgpattern;
  459. consume(_ID);
  460. { delphi generic declaration? }
  461. if (m_delphi in current_settings.modeswitches) then
  462. isgeneric:=token=_LSHARPBRACKET;
  463. { Generic type declaration? }
  464. if isgeneric then
  465. begin
  466. if assigned(current_genericdef) then
  467. Message(parser_f_no_generic_inside_generic);
  468. consume(_LSHARPBRACKET);
  469. generictypelist:=parse_generic_parameters;
  470. consume(_RSHARPBRACKET);
  471. str(generictypelist.Count,s);
  472. gentypename:=typename+'$'+s;
  473. genorgtypename:=orgtypename+'$'+s;
  474. end
  475. else
  476. begin
  477. gentypename:=typename;
  478. genorgtypename:=orgtypename;
  479. end;
  480. consume(_EQ);
  481. { support 'ttype=type word' syntax }
  482. isunique:=try_to_consume(_TYPE);
  483. { MacPas object model is more like Delphi's than like TP's, but }
  484. { uses the object keyword instead of class }
  485. if (m_mac in current_settings.modeswitches) and
  486. (token = _OBJECT) then
  487. token := _CLASS;
  488. { Start recording a generic template }
  489. if assigned(generictypelist) then
  490. begin
  491. generictokenbuf:=tdynamicarray.create(256);
  492. current_scanner.startrecordtokens(generictokenbuf);
  493. end;
  494. { is the type already defined? -- must be in the current symtable,
  495. not in a nested symtable or one higher up the stack -> don't
  496. use searchsym & frinds! }
  497. sym:=tsym(symtablestack.top.find(gentypename));
  498. newtype:=nil;
  499. { found a symbol with this name? }
  500. if assigned(sym) then
  501. begin
  502. if (sym.typ=typesym) and
  503. { this should not be a symbol that was created by a generic
  504. that was declared earlier }
  505. not (
  506. (ttypesym(sym).typedef.typ=undefineddef) and
  507. (sp_generic_dummy in sym.symoptions)
  508. ) then
  509. begin
  510. if ((token=_CLASS) or
  511. (token=_INTERFACE) or
  512. (token=_DISPINTERFACE) or
  513. (token=_OBJCCLASS) or
  514. (token=_OBJCPROTOCOL) or
  515. (token=_OBJCCATEGORY)) and
  516. (assigned(ttypesym(sym).typedef)) and
  517. is_implicit_pointer_object_type(ttypesym(sym).typedef) and
  518. (oo_is_forward in tobjectdef(ttypesym(sym).typedef).objectoptions) then
  519. begin
  520. case token of
  521. _CLASS :
  522. objecttype:=default_class_type;
  523. _INTERFACE :
  524. case current_settings.interfacetype of
  525. it_interfacecom:
  526. objecttype:=odt_interfacecom;
  527. it_interfacecorba:
  528. objecttype:=odt_interfacecorba;
  529. it_interfacejava:
  530. objecttype:=odt_interfacejava;
  531. else
  532. internalerror(2010122611);
  533. end;
  534. _DISPINTERFACE :
  535. objecttype:=odt_dispinterface;
  536. _OBJCCLASS,
  537. _OBJCCATEGORY :
  538. objecttype:=odt_objcclass;
  539. _OBJCPROTOCOL :
  540. objecttype:=odt_objcprotocol;
  541. else
  542. internalerror(200811072);
  543. end;
  544. consume(token);
  545. { we can ignore the result, the definition is modified }
  546. object_dec(objecttype,genorgtypename,newtype,nil,nil,tobjectdef(ttypesym(sym).typedef),ht_none);
  547. newtype:=ttypesym(sym);
  548. hdef:=newtype.typedef;
  549. end
  550. else
  551. message1(parser_h_type_redef,genorgtypename);
  552. end;
  553. end;
  554. { no old type reused ? Then insert this new type }
  555. if not assigned(newtype) then
  556. begin
  557. { insert the new type first with an errordef, so that
  558. referencing the type before it's really set it
  559. will give an error (PFV) }
  560. hdef:=generrordef;
  561. gendef:=nil;
  562. storetokenpos:=current_tokenpos;
  563. if isgeneric then
  564. begin
  565. { for generics we need to check whether a non-generic type
  566. already exists and if not we need to insert a symbol with
  567. the non-generic name (available in (org)typename) that is a
  568. undefineddef, so that inline specializations can be used }
  569. sym:=tsym(symtablestack.top.Find(typename));
  570. if not assigned(sym) then
  571. begin
  572. sym:=ttypesym.create(orgtypename,tundefineddef.create);
  573. Include(sym.symoptions,sp_generic_dummy);
  574. ttypesym(sym).typedef.typesym:=sym;
  575. sym.visibility:=symtablestack.top.currentvisibility;
  576. symtablestack.top.insert(sym);
  577. ttypesym(sym).typedef.owner:=sym.owner;
  578. end
  579. else
  580. { this is not allowed in non-Delphi modes }
  581. if not (m_delphi in current_settings.modeswitches) then
  582. Message1(sym_e_duplicate_id,genorgtypename)
  583. else
  584. { we need to find this symbol even if it's a variable or
  585. something else when doing an inline specialization }
  586. Include(sym.symoptions,sp_generic_dummy);
  587. end
  588. else
  589. begin
  590. if assigned(sym) and (sym.typ=typesym) and
  591. (ttypesym(sym).typedef.typ=undefineddef) and
  592. (sp_generic_dummy in sym.symoptions) then
  593. begin
  594. { this is a symbol that was added by an earlier generic
  595. declaration, reuse it }
  596. newtype:=ttypesym(sym);
  597. newtype.typedef:=hdef;
  598. sym:=nil;
  599. end;
  600. { check whether this is a declaration of a type inside a
  601. specialization }
  602. if assigned(current_structdef) and
  603. (df_specialization in current_structdef.defoptions) then
  604. begin
  605. if not assigned(current_structdef.genericdef) or
  606. not (current_structdef.genericdef.typ in [recorddef,objectdef]) then
  607. internalerror(2011052301);
  608. hashedid.id:=gentypename;
  609. { we could be inside a method of the specialization
  610. instead of its declaration, so check that first (as
  611. local nested types aren't allowed we don't need to
  612. walk the symtablestack to find the localsymtable) }
  613. if symtablestack.top.symtabletype=localsymtable then
  614. begin
  615. { we are in a method }
  616. if not assigned(symtablestack.top.defowner) or
  617. (symtablestack.top.defowner.typ<>procdef) then
  618. internalerror(2011120701);
  619. pd:=tprocdef(symtablestack.top.defowner);
  620. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  621. internalerror(2011120702);
  622. sym:=tsym(tprocdef(pd.genericdef).localst.findwithhash(hashedid));
  623. end
  624. else
  625. sym:=nil;
  626. if not assigned(sym) or not (sym.typ=typesym) then
  627. begin
  628. { now search in the declaration of the generic }
  629. sym:=tsym(tabstractrecorddef(current_structdef.genericdef).symtable.findwithhash(hashedid));
  630. if not assigned(sym) or not (sym.typ=typesym) then
  631. internalerror(2011052302);
  632. end;
  633. { use the corresponding type in the generic's symtable as
  634. genericdef for the specialized type }
  635. gendef:=tstoreddef(ttypesym(sym).typedef);
  636. end;
  637. end;
  638. { insert a new type if we don't reuse an existing symbol }
  639. if not assigned(newtype) then
  640. begin
  641. newtype:=ttypesym.create(genorgtypename,hdef);
  642. newtype.visibility:=symtablestack.top.currentvisibility;
  643. symtablestack.top.insert(newtype);
  644. end;
  645. current_tokenpos:=defpos;
  646. current_tokenpos:=storetokenpos;
  647. { read the type definition }
  648. read_named_type(hdef,newtype,gendef,generictypelist,false);
  649. { update the definition of the type }
  650. if assigned(hdef) then
  651. begin
  652. if assigned(hdef.typesym) then
  653. begin
  654. istyperenaming:=true;
  655. include(newtype.symoptions,sp_explicitrename);
  656. end;
  657. if isunique then
  658. begin
  659. if is_objc_class_or_protocol(hdef) or
  660. is_java_class_or_interface(hdef) then
  661. Message(parser_e_unique_unsupported);
  662. hdef:=tstoreddef(hdef).getcopy;
  663. { check if it is an ansistirng(codepage) declaration }
  664. if is_ansistring(hdef) and try_to_consume(_LKLAMMER) then
  665. begin
  666. p:=comp_expr(true,false);
  667. consume(_RKLAMMER);
  668. if not is_constintnode(p) then
  669. begin
  670. Message(parser_e_illegal_expression);
  671. { error recovery }
  672. end
  673. else
  674. begin
  675. if (tordconstnode(p).value<0) or (tordconstnode(p).value>65535) then
  676. begin
  677. Message(parser_e_invalid_codepage);
  678. tordconstnode(p).value:=0;
  679. end;
  680. tstringdef(hdef).encoding:=int64(tordconstnode(p).value);
  681. end;
  682. p.free;
  683. end;
  684. { fix name, it is used e.g. for tables }
  685. if is_class_or_interface_or_dispinterface(hdef) then
  686. with tobjectdef(hdef) do
  687. begin
  688. stringdispose(objname);
  689. stringdispose(objrealname);
  690. objrealname:=stringdup(genorgtypename);
  691. objname:=stringdup(upper(genorgtypename));
  692. end;
  693. include(hdef.defoptions,df_unique);
  694. if (hdef.typ in [pointerdef,classrefdef]) and
  695. (tabstractpointerdef(hdef).pointeddef.typ=forwarddef) then
  696. current_module.checkforwarddefs.add(hdef);
  697. end;
  698. if not assigned(hdef.typesym) then
  699. hdef.typesym:=newtype;
  700. end;
  701. { in non-Delphi modes we need a reference to the generic def
  702. without the generic suffix, so it can be found easily when
  703. parsing method implementations }
  704. if isgeneric and assigned(sym) and
  705. not (m_delphi in current_settings.modeswitches) and
  706. (ttypesym(sym).typedef.typ=undefineddef) then
  707. { don't free the undefineddef as the defids rely on the count
  708. of the defs in the def list of the module}
  709. ttypesym(sym).typedef:=hdef;
  710. newtype.typedef:=hdef;
  711. { KAZ: handle TGUID declaration in system unit }
  712. if (cs_compilesystem in current_settings.moduleswitches) and not assigned(rec_tguid) and
  713. (gentypename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  714. assigned(hdef) and (hdef.typ=recorddef) and (hdef.size=16) then
  715. rec_tguid:=trecorddef(hdef);
  716. end;
  717. if assigned(hdef) then
  718. begin
  719. case hdef.typ of
  720. pointerdef :
  721. begin
  722. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  723. consume(_SEMICOLON);
  724. if try_to_consume(_FAR) then
  725. begin
  726. tpointerdef(hdef).is_far:=true;
  727. consume(_SEMICOLON);
  728. end;
  729. end;
  730. procvardef :
  731. begin
  732. { in case of type renaming, don't parse proc directives }
  733. if istyperenaming then
  734. begin
  735. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  736. consume(_SEMICOLON);
  737. end
  738. else
  739. begin
  740. if not check_proc_directive(true) then
  741. begin
  742. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  743. consume(_SEMICOLON);
  744. end;
  745. parse_var_proc_directives(tsym(newtype));
  746. handle_calling_convention(tprocvardef(hdef));
  747. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  748. consume(_SEMICOLON);
  749. end;
  750. end;
  751. objectdef :
  752. begin
  753. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  754. consume(_SEMICOLON);
  755. { change a forward and external class declaration into
  756. formal external definition, so the compiler does not
  757. expect an real definition later }
  758. if is_objc_class_or_protocol(hdef) or
  759. is_java_class_or_interface(hdef) then
  760. finalize_class_external_status(tobjectdef(hdef));
  761. { Build VMT indexes, skip for type renaming and forward classes }
  762. if (hdef.typesym=newtype) and
  763. not(oo_is_forward in tobjectdef(hdef).objectoptions) and
  764. not(df_generic in hdef.defoptions) then
  765. begin
  766. vmtbuilder:=TVMTBuilder.Create(tobjectdef(hdef));
  767. vmtbuilder.generate_vmt;
  768. vmtbuilder.free;
  769. end;
  770. { If there are attribute-properties available, bind them to
  771. this object }
  772. if assigned(current_rtticlassattributesdef) then
  773. begin
  774. add_synthetic_rtti_funtion_declarations(current_rtticlassattributesdef,hdef.typesym.Name);
  775. tobjectdef(hdef).rtti_attributesdef:=current_rtticlassattributesdef;
  776. current_rtticlassattributesdef := nil;
  777. end;
  778. { In case of an objcclass, verify that all methods have a message
  779. name set. We only check this now, because message names can be set
  780. during the protocol (interface) mapping. At the same time, set the
  781. mangled names (these depend on the "external" name of the class),
  782. and mark private fields of external classes as "used" (to avoid
  783. bogus notes about them being unused)
  784. }
  785. { watch out for crashes in case of errors }
  786. if is_objc_class_or_protocol(hdef) and
  787. (not is_objccategory(hdef) or
  788. assigned(tobjectdef(hdef).childof)) then
  789. tobjectdef(hdef).finish_objc_data;
  790. if is_cppclass(hdef) then
  791. tobjectdef(hdef).finish_cpp_data;
  792. end;
  793. recorddef :
  794. begin
  795. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  796. consume(_SEMICOLON);
  797. end;
  798. else
  799. begin
  800. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  801. consume(_SEMICOLON);
  802. end;
  803. end;
  804. end;
  805. if isgeneric and (not(hdef.typ in [objectdef,recorddef,arraydef,procvardef])
  806. or is_objectpascal_helper(hdef)) then
  807. message(parser_e_cant_create_generics_of_this_type);
  808. { Stop recording a generic template }
  809. if assigned(generictypelist) then
  810. begin
  811. current_scanner.stoprecordtokens;
  812. tstoreddef(hdef).generictokenbuf:=generictokenbuf;
  813. { Generic is never a type renaming }
  814. hdef.typesym:=newtype;
  815. generictypelist.free;
  816. end;
  817. if Assigned(current_rtticlassattributesdef) then
  818. internalerror(202105250);
  819. until ((token<>_ID) and (token<>_LECKKLAMMER)) or
  820. (in_structure and
  821. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  822. ((m_final_fields in current_settings.modeswitches) and
  823. (idtoken=_FINAL))));
  824. { resolve type block forward declarations and restore a unit
  825. container for them }
  826. resolve_forward_types;
  827. current_module.checkforwarddefs.free;
  828. current_module.checkforwarddefs:=old_checkforwarddefs;
  829. block_type:=old_block_type;
  830. end;
  831. { reads a type declaration to the symbol table }
  832. procedure type_dec;
  833. begin
  834. consume(_TYPE);
  835. types_dec(false);
  836. end;
  837. procedure var_dec;
  838. { parses variable declarations and inserts them in }
  839. { the top symbol table of symtablestack }
  840. begin
  841. consume(_VAR);
  842. read_var_decls([]);
  843. end;
  844. procedure property_dec;
  845. { parses a global property (fpc mode feature) }
  846. var
  847. old_block_type: tblock_type;
  848. begin
  849. consume(_PROPERTY);
  850. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  851. message(parser_e_resourcestring_only_sg);
  852. old_block_type:=block_type;
  853. block_type:=bt_const;
  854. repeat
  855. read_property_dec(false, nil);
  856. consume(_SEMICOLON);
  857. until token<>_ID;
  858. block_type:=old_block_type;
  859. end;
  860. procedure threadvar_dec;
  861. { parses thread variable declarations and inserts them in }
  862. { the top symbol table of symtablestack }
  863. begin
  864. consume(_THREADVAR);
  865. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  866. message(parser_e_threadvars_only_sg);
  867. read_var_decls([vd_threadvar]);
  868. end;
  869. procedure resourcestring_dec;
  870. var
  871. orgname : TIDString;
  872. p : tnode;
  873. dummysymoptions : tsymoptions;
  874. deprecatedmsg : pshortstring;
  875. storetokenpos,filepos : tfileposinfo;
  876. old_block_type : tblock_type;
  877. sp : pchar;
  878. sym : tsym;
  879. begin
  880. if target_info.system in systems_managed_vm then
  881. message(parser_e_feature_unsupported_for_vm);
  882. consume(_RESOURCESTRING);
  883. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  884. message(parser_e_resourcestring_only_sg);
  885. old_block_type:=block_type;
  886. block_type:=bt_const;
  887. repeat
  888. orgname:=orgpattern;
  889. filepos:=current_tokenpos;
  890. consume(_ID);
  891. case token of
  892. _EQ:
  893. begin
  894. consume(_EQ);
  895. p:=comp_expr(true,false);
  896. storetokenpos:=current_tokenpos;
  897. current_tokenpos:=filepos;
  898. sym:=nil;
  899. case p.nodetype of
  900. ordconstn:
  901. begin
  902. if is_constcharnode(p) then
  903. begin
  904. getmem(sp,2);
  905. sp[0]:=chr(tordconstnode(p).value.svalue);
  906. sp[1]:=#0;
  907. sym:=tconstsym.create_string(orgname,constresourcestring,sp,1);
  908. end
  909. else
  910. Message(parser_e_illegal_expression);
  911. end;
  912. stringconstn:
  913. with Tstringconstnode(p) do
  914. begin
  915. getmem(sp,len+1);
  916. move(value_str^,sp^,len+1);
  917. sym:=tconstsym.create_string(orgname,constresourcestring,sp,len);
  918. end;
  919. else
  920. Message(parser_e_illegal_expression);
  921. end;
  922. current_tokenpos:=storetokenpos;
  923. { Support hint directives }
  924. dummysymoptions:=[];
  925. deprecatedmsg:=nil;
  926. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  927. if assigned(sym) then
  928. begin
  929. sym.symoptions:=sym.symoptions+dummysymoptions;
  930. sym.deprecatedmsg:=deprecatedmsg;
  931. symtablestack.top.insert(sym);
  932. end
  933. else
  934. stringdispose(deprecatedmsg);
  935. consume(_SEMICOLON);
  936. p.free;
  937. end;
  938. else consume(_EQ);
  939. end;
  940. until token<>_ID;
  941. block_type:=old_block_type;
  942. end;
  943. end.