pdecl.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. p1.Free;
  378. block_type:=oldblock_type;
  379. { Add attribute to attribute list which will be added
  380. to the property which is defined next. }
  381. if not assigned(rtti_attributes) then
  382. rtti_attributes := trtti_attributesdef.create;
  383. rtti_attributes.addattribute(typesym,p1);
  384. end;
  385. p.free;
  386. consume(_RECKKLAMMER);
  387. end;
  388. procedure add_synthetic_rtti_funtion_declarations(rtti_attributesdef: trtti_attributesdef; name: shortstring);
  389. var
  390. i: Integer;
  391. sstate: tscannerstate;
  392. attribute: trtti_attribute;
  393. pd: tprocdef;
  394. begin
  395. for i := 0 to rtti_attributesdef.get_attribute_count-1 do
  396. begin
  397. attribute := trtti_attribute(rtti_attributesdef.rtti_attributes[i]);
  398. replace_scanner('rtti_class_attributes',sstate);
  399. if str_parse_method_dec('function rtti_'+name+'_'+IntToStr(i)+':'+ attribute.typesym.Name +';',potype_function,false,tabstractrecorddef(ttypesym(attribute.typesym).typedef),pd) then
  400. pd.synthetickind:=tsk_get_rttiattribute
  401. else
  402. internalerror(2012052601);
  403. pd.skpara:=attribute;
  404. attribute.symbolname:=pd.mangledname;
  405. restore_scanner(sstate);
  406. end;
  407. end;
  408. procedure types_dec(in_structure: boolean);
  409. procedure finalize_class_external_status(od: tobjectdef);
  410. begin
  411. if [oo_is_external,oo_is_forward] <= od.objectoptions then
  412. begin
  413. { formal definition: x = objcclass external; }
  414. exclude(od.objectoptions,oo_is_forward);
  415. include(od.objectoptions,oo_is_formal);
  416. end;
  417. end;
  418. var
  419. typename,orgtypename,
  420. gentypename,genorgtypename : TIDString;
  421. newtype : ttypesym;
  422. sym : tsym;
  423. hdef : tdef;
  424. defpos,storetokenpos : tfileposinfo;
  425. old_block_type : tblock_type;
  426. old_checkforwarddefs: TFPObjectList;
  427. objecttype : tobjecttyp;
  428. isgeneric,
  429. isunique,
  430. istyperenaming : boolean;
  431. generictypelist : TFPObjectList;
  432. generictokenbuf : tdynamicarray;
  433. vmtbuilder : TVMTBuilder;
  434. p:tnode;
  435. gendef : tstoreddef;
  436. s : shortstring;
  437. pd: tprocdef;
  438. hashedid : thashedidstring;
  439. begin
  440. old_block_type:=block_type;
  441. { save unit container of forward declarations -
  442. we can be inside nested class type block }
  443. old_checkforwarddefs:=current_module.checkforwarddefs;
  444. current_module.checkforwarddefs:=TFPObjectList.Create(false);
  445. block_type:=bt_type;
  446. repeat
  447. defpos:=current_tokenpos;
  448. istyperenaming:=false;
  449. generictypelist:=nil;
  450. generictokenbuf:=nil;
  451. { fpc generic declaration? }
  452. isgeneric:=not(m_delphi in current_settings.modeswitches) and try_to_consume(_GENERIC);
  453. { class attribute definitions? }
  454. while token=_LECKKLAMMER do
  455. begin
  456. parse_rttiattributes(current_rtticlassattributesdef);
  457. end;
  458. typename:=pattern;
  459. orgtypename:=orgpattern;
  460. consume(_ID);
  461. { delphi generic declaration? }
  462. if (m_delphi in current_settings.modeswitches) then
  463. isgeneric:=token=_LSHARPBRACKET;
  464. { Generic type declaration? }
  465. if isgeneric then
  466. begin
  467. if assigned(current_genericdef) then
  468. Message(parser_f_no_generic_inside_generic);
  469. consume(_LSHARPBRACKET);
  470. generictypelist:=parse_generic_parameters;
  471. consume(_RSHARPBRACKET);
  472. str(generictypelist.Count,s);
  473. gentypename:=typename+'$'+s;
  474. genorgtypename:=orgtypename+'$'+s;
  475. end
  476. else
  477. begin
  478. gentypename:=typename;
  479. genorgtypename:=orgtypename;
  480. end;
  481. consume(_EQ);
  482. { support 'ttype=type word' syntax }
  483. isunique:=try_to_consume(_TYPE);
  484. { MacPas object model is more like Delphi's than like TP's, but }
  485. { uses the object keyword instead of class }
  486. if (m_mac in current_settings.modeswitches) and
  487. (token = _OBJECT) then
  488. token := _CLASS;
  489. { Start recording a generic template }
  490. if assigned(generictypelist) then
  491. begin
  492. generictokenbuf:=tdynamicarray.create(256);
  493. current_scanner.startrecordtokens(generictokenbuf);
  494. end;
  495. { is the type already defined? -- must be in the current symtable,
  496. not in a nested symtable or one higher up the stack -> don't
  497. use searchsym & frinds! }
  498. sym:=tsym(symtablestack.top.find(gentypename));
  499. newtype:=nil;
  500. { found a symbol with this name? }
  501. if assigned(sym) then
  502. begin
  503. if (sym.typ=typesym) and
  504. { this should not be a symbol that was created by a generic
  505. that was declared earlier }
  506. not (
  507. (ttypesym(sym).typedef.typ=undefineddef) and
  508. (sp_generic_dummy in sym.symoptions)
  509. ) then
  510. begin
  511. if ((token=_CLASS) or
  512. (token=_INTERFACE) or
  513. (token=_DISPINTERFACE) or
  514. (token=_OBJCCLASS) or
  515. (token=_OBJCPROTOCOL) or
  516. (token=_OBJCCATEGORY)) and
  517. (assigned(ttypesym(sym).typedef)) and
  518. is_implicit_pointer_object_type(ttypesym(sym).typedef) and
  519. (oo_is_forward in tobjectdef(ttypesym(sym).typedef).objectoptions) then
  520. begin
  521. case token of
  522. _CLASS :
  523. objecttype:=default_class_type;
  524. _INTERFACE :
  525. case current_settings.interfacetype of
  526. it_interfacecom:
  527. objecttype:=odt_interfacecom;
  528. it_interfacecorba:
  529. objecttype:=odt_interfacecorba;
  530. it_interfacejava:
  531. objecttype:=odt_interfacejava;
  532. else
  533. internalerror(2010122611);
  534. end;
  535. _DISPINTERFACE :
  536. objecttype:=odt_dispinterface;
  537. _OBJCCLASS,
  538. _OBJCCATEGORY :
  539. objecttype:=odt_objcclass;
  540. _OBJCPROTOCOL :
  541. objecttype:=odt_objcprotocol;
  542. else
  543. internalerror(200811072);
  544. end;
  545. consume(token);
  546. { we can ignore the result, the definition is modified }
  547. object_dec(objecttype,genorgtypename,newtype,nil,nil,tobjectdef(ttypesym(sym).typedef),ht_none);
  548. newtype:=ttypesym(sym);
  549. hdef:=newtype.typedef;
  550. end
  551. else
  552. message1(parser_h_type_redef,genorgtypename);
  553. end;
  554. end;
  555. { no old type reused ? Then insert this new type }
  556. if not assigned(newtype) then
  557. begin
  558. { insert the new type first with an errordef, so that
  559. referencing the type before it's really set it
  560. will give an error (PFV) }
  561. hdef:=generrordef;
  562. gendef:=nil;
  563. storetokenpos:=current_tokenpos;
  564. if isgeneric then
  565. begin
  566. { for generics we need to check whether a non-generic type
  567. already exists and if not we need to insert a symbol with
  568. the non-generic name (available in (org)typename) that is a
  569. undefineddef, so that inline specializations can be used }
  570. sym:=tsym(symtablestack.top.Find(typename));
  571. if not assigned(sym) then
  572. begin
  573. sym:=ttypesym.create(orgtypename,tundefineddef.create);
  574. Include(sym.symoptions,sp_generic_dummy);
  575. ttypesym(sym).typedef.typesym:=sym;
  576. sym.visibility:=symtablestack.top.currentvisibility;
  577. symtablestack.top.insert(sym);
  578. ttypesym(sym).typedef.owner:=sym.owner;
  579. end
  580. else
  581. { this is not allowed in non-Delphi modes }
  582. if not (m_delphi in current_settings.modeswitches) then
  583. Message1(sym_e_duplicate_id,genorgtypename)
  584. else
  585. { we need to find this symbol even if it's a variable or
  586. something else when doing an inline specialization }
  587. Include(sym.symoptions,sp_generic_dummy);
  588. end
  589. else
  590. begin
  591. if assigned(sym) and (sym.typ=typesym) and
  592. (ttypesym(sym).typedef.typ=undefineddef) and
  593. (sp_generic_dummy in sym.symoptions) then
  594. begin
  595. { this is a symbol that was added by an earlier generic
  596. declaration, reuse it }
  597. newtype:=ttypesym(sym);
  598. newtype.typedef:=hdef;
  599. sym:=nil;
  600. end;
  601. { check whether this is a declaration of a type inside a
  602. specialization }
  603. if assigned(current_structdef) and
  604. (df_specialization in current_structdef.defoptions) then
  605. begin
  606. if not assigned(current_structdef.genericdef) or
  607. not (current_structdef.genericdef.typ in [recorddef,objectdef]) then
  608. internalerror(2011052301);
  609. hashedid.id:=gentypename;
  610. { we could be inside a method of the specialization
  611. instead of its declaration, so check that first (as
  612. local nested types aren't allowed we don't need to
  613. walk the symtablestack to find the localsymtable) }
  614. if symtablestack.top.symtabletype=localsymtable then
  615. begin
  616. { we are in a method }
  617. if not assigned(symtablestack.top.defowner) or
  618. (symtablestack.top.defowner.typ<>procdef) then
  619. internalerror(2011120701);
  620. pd:=tprocdef(symtablestack.top.defowner);
  621. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  622. internalerror(2011120702);
  623. sym:=tsym(tprocdef(pd.genericdef).localst.findwithhash(hashedid));
  624. end
  625. else
  626. sym:=nil;
  627. if not assigned(sym) or not (sym.typ=typesym) then
  628. begin
  629. { now search in the declaration of the generic }
  630. sym:=tsym(tabstractrecorddef(current_structdef.genericdef).symtable.findwithhash(hashedid));
  631. if not assigned(sym) or not (sym.typ=typesym) then
  632. internalerror(2011052302);
  633. end;
  634. { use the corresponding type in the generic's symtable as
  635. genericdef for the specialized type }
  636. gendef:=tstoreddef(ttypesym(sym).typedef);
  637. end;
  638. end;
  639. { insert a new type if we don't reuse an existing symbol }
  640. if not assigned(newtype) then
  641. begin
  642. newtype:=ttypesym.create(genorgtypename,hdef);
  643. newtype.visibility:=symtablestack.top.currentvisibility;
  644. symtablestack.top.insert(newtype);
  645. end;
  646. current_tokenpos:=defpos;
  647. current_tokenpos:=storetokenpos;
  648. { read the type definition }
  649. read_named_type(hdef,newtype,gendef,generictypelist,false);
  650. { update the definition of the type }
  651. if assigned(hdef) then
  652. begin
  653. if assigned(hdef.typesym) then
  654. begin
  655. istyperenaming:=true;
  656. include(newtype.symoptions,sp_explicitrename);
  657. end;
  658. if isunique then
  659. begin
  660. if is_objc_class_or_protocol(hdef) or
  661. is_java_class_or_interface(hdef) then
  662. Message(parser_e_unique_unsupported);
  663. hdef:=tstoreddef(hdef).getcopy;
  664. { check if it is an ansistirng(codepage) declaration }
  665. if is_ansistring(hdef) and try_to_consume(_LKLAMMER) then
  666. begin
  667. p:=comp_expr(true,false);
  668. consume(_RKLAMMER);
  669. if not is_constintnode(p) then
  670. begin
  671. Message(parser_e_illegal_expression);
  672. { error recovery }
  673. end
  674. else
  675. begin
  676. if (tordconstnode(p).value<0) or (tordconstnode(p).value>65535) then
  677. begin
  678. Message(parser_e_invalid_codepage);
  679. tordconstnode(p).value:=0;
  680. end;
  681. tstringdef(hdef).encoding:=int64(tordconstnode(p).value);
  682. end;
  683. p.free;
  684. end;
  685. { fix name, it is used e.g. for tables }
  686. if is_class_or_interface_or_dispinterface(hdef) then
  687. with tobjectdef(hdef) do
  688. begin
  689. stringdispose(objname);
  690. stringdispose(objrealname);
  691. objrealname:=stringdup(genorgtypename);
  692. objname:=stringdup(upper(genorgtypename));
  693. end;
  694. include(hdef.defoptions,df_unique);
  695. if (hdef.typ in [pointerdef,classrefdef]) and
  696. (tabstractpointerdef(hdef).pointeddef.typ=forwarddef) then
  697. current_module.checkforwarddefs.add(hdef);
  698. end;
  699. if not assigned(hdef.typesym) then
  700. hdef.typesym:=newtype;
  701. end;
  702. { in non-Delphi modes we need a reference to the generic def
  703. without the generic suffix, so it can be found easily when
  704. parsing method implementations }
  705. if isgeneric and assigned(sym) and
  706. not (m_delphi in current_settings.modeswitches) and
  707. (ttypesym(sym).typedef.typ=undefineddef) then
  708. { don't free the undefineddef as the defids rely on the count
  709. of the defs in the def list of the module}
  710. ttypesym(sym).typedef:=hdef;
  711. newtype.typedef:=hdef;
  712. { KAZ: handle TGUID declaration in system unit }
  713. if (cs_compilesystem in current_settings.moduleswitches) and not assigned(rec_tguid) and
  714. (gentypename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  715. assigned(hdef) and (hdef.typ=recorddef) and (hdef.size=16) then
  716. rec_tguid:=trecorddef(hdef);
  717. end;
  718. if assigned(hdef) then
  719. begin
  720. case hdef.typ of
  721. pointerdef :
  722. begin
  723. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  724. consume(_SEMICOLON);
  725. if try_to_consume(_FAR) then
  726. begin
  727. tpointerdef(hdef).is_far:=true;
  728. consume(_SEMICOLON);
  729. end;
  730. end;
  731. procvardef :
  732. begin
  733. { in case of type renaming, don't parse proc directives }
  734. if istyperenaming then
  735. begin
  736. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  737. consume(_SEMICOLON);
  738. end
  739. else
  740. begin
  741. if not check_proc_directive(true) then
  742. begin
  743. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  744. consume(_SEMICOLON);
  745. end;
  746. parse_var_proc_directives(tsym(newtype));
  747. handle_calling_convention(tprocvardef(hdef));
  748. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  749. consume(_SEMICOLON);
  750. end;
  751. end;
  752. objectdef :
  753. begin
  754. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  755. consume(_SEMICOLON);
  756. { change a forward and external class declaration into
  757. formal external definition, so the compiler does not
  758. expect an real definition later }
  759. if is_objc_class_or_protocol(hdef) or
  760. is_java_class_or_interface(hdef) then
  761. finalize_class_external_status(tobjectdef(hdef));
  762. { Build VMT indexes, skip for type renaming and forward classes }
  763. if (hdef.typesym=newtype) and
  764. not(oo_is_forward in tobjectdef(hdef).objectoptions) and
  765. not(df_generic in hdef.defoptions) then
  766. begin
  767. vmtbuilder:=TVMTBuilder.Create(tobjectdef(hdef));
  768. vmtbuilder.generate_vmt;
  769. vmtbuilder.free;
  770. end;
  771. { If there are attribute-properties available, bind them to
  772. this object }
  773. if assigned(current_rtticlassattributesdef) then
  774. begin
  775. add_synthetic_rtti_funtion_declarations(current_rtticlassattributesdef,hdef.typesym.Name);
  776. tobjectdef(hdef).rtti_attributesdef:=current_rtticlassattributesdef;
  777. current_rtticlassattributesdef := nil;
  778. end;
  779. { In case of an objcclass, verify that all methods have a message
  780. name set. We only check this now, because message names can be set
  781. during the protocol (interface) mapping. At the same time, set the
  782. mangled names (these depend on the "external" name of the class),
  783. and mark private fields of external classes as "used" (to avoid
  784. bogus notes about them being unused)
  785. }
  786. { watch out for crashes in case of errors }
  787. if is_objc_class_or_protocol(hdef) and
  788. (not is_objccategory(hdef) or
  789. assigned(tobjectdef(hdef).childof)) then
  790. tobjectdef(hdef).finish_objc_data;
  791. if is_cppclass(hdef) then
  792. tobjectdef(hdef).finish_cpp_data;
  793. end;
  794. recorddef :
  795. begin
  796. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  797. consume(_SEMICOLON);
  798. end;
  799. else
  800. begin
  801. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  802. consume(_SEMICOLON);
  803. end;
  804. end;
  805. end;
  806. if isgeneric and (not(hdef.typ in [objectdef,recorddef,arraydef,procvardef])
  807. or is_objectpascal_helper(hdef)) then
  808. message(parser_e_cant_create_generics_of_this_type);
  809. { Stop recording a generic template }
  810. if assigned(generictypelist) then
  811. begin
  812. current_scanner.stoprecordtokens;
  813. tstoreddef(hdef).generictokenbuf:=generictokenbuf;
  814. { Generic is never a type renaming }
  815. hdef.typesym:=newtype;
  816. generictypelist.free;
  817. end;
  818. if Assigned(current_rtticlassattributesdef) then
  819. internalerror(202105250);
  820. until ((token<>_ID) and (token<>_LECKKLAMMER)) or
  821. (in_structure and
  822. ((idtoken in [_PRIVATE,_PROTECTED,_PUBLIC,_PUBLISHED,_STRICT]) or
  823. ((m_final_fields in current_settings.modeswitches) and
  824. (idtoken=_FINAL))));
  825. { resolve type block forward declarations and restore a unit
  826. container for them }
  827. resolve_forward_types;
  828. current_module.checkforwarddefs.free;
  829. current_module.checkforwarddefs:=old_checkforwarddefs;
  830. block_type:=old_block_type;
  831. end;
  832. { reads a type declaration to the symbol table }
  833. procedure type_dec;
  834. begin
  835. consume(_TYPE);
  836. types_dec(false);
  837. end;
  838. procedure var_dec;
  839. { parses variable declarations and inserts them in }
  840. { the top symbol table of symtablestack }
  841. begin
  842. consume(_VAR);
  843. read_var_decls([]);
  844. end;
  845. procedure property_dec;
  846. { parses a global property (fpc mode feature) }
  847. var
  848. old_block_type: tblock_type;
  849. begin
  850. consume(_PROPERTY);
  851. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  852. message(parser_e_resourcestring_only_sg);
  853. old_block_type:=block_type;
  854. block_type:=bt_const;
  855. repeat
  856. read_property_dec(false, nil);
  857. consume(_SEMICOLON);
  858. until token<>_ID;
  859. block_type:=old_block_type;
  860. end;
  861. procedure threadvar_dec;
  862. { parses thread variable declarations and inserts them in }
  863. { the top symbol table of symtablestack }
  864. begin
  865. consume(_THREADVAR);
  866. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  867. message(parser_e_threadvars_only_sg);
  868. read_var_decls([vd_threadvar]);
  869. end;
  870. procedure resourcestring_dec;
  871. var
  872. orgname : TIDString;
  873. p : tnode;
  874. dummysymoptions : tsymoptions;
  875. deprecatedmsg : pshortstring;
  876. storetokenpos,filepos : tfileposinfo;
  877. old_block_type : tblock_type;
  878. sp : pchar;
  879. sym : tsym;
  880. begin
  881. if target_info.system in systems_managed_vm then
  882. message(parser_e_feature_unsupported_for_vm);
  883. consume(_RESOURCESTRING);
  884. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  885. message(parser_e_resourcestring_only_sg);
  886. old_block_type:=block_type;
  887. block_type:=bt_const;
  888. repeat
  889. orgname:=orgpattern;
  890. filepos:=current_tokenpos;
  891. consume(_ID);
  892. case token of
  893. _EQ:
  894. begin
  895. consume(_EQ);
  896. p:=comp_expr(true,false);
  897. storetokenpos:=current_tokenpos;
  898. current_tokenpos:=filepos;
  899. sym:=nil;
  900. case p.nodetype of
  901. ordconstn:
  902. begin
  903. if is_constcharnode(p) then
  904. begin
  905. getmem(sp,2);
  906. sp[0]:=chr(tordconstnode(p).value.svalue);
  907. sp[1]:=#0;
  908. sym:=tconstsym.create_string(orgname,constresourcestring,sp,1);
  909. end
  910. else
  911. Message(parser_e_illegal_expression);
  912. end;
  913. stringconstn:
  914. with Tstringconstnode(p) do
  915. begin
  916. getmem(sp,len+1);
  917. move(value_str^,sp^,len+1);
  918. sym:=tconstsym.create_string(orgname,constresourcestring,sp,len);
  919. end;
  920. else
  921. Message(parser_e_illegal_expression);
  922. end;
  923. current_tokenpos:=storetokenpos;
  924. { Support hint directives }
  925. dummysymoptions:=[];
  926. deprecatedmsg:=nil;
  927. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  928. if assigned(sym) then
  929. begin
  930. sym.symoptions:=sym.symoptions+dummysymoptions;
  931. sym.deprecatedmsg:=deprecatedmsg;
  932. symtablestack.top.insert(sym);
  933. end
  934. else
  935. stringdispose(deprecatedmsg);
  936. consume(_SEMICOLON);
  937. p.free;
  938. end;
  939. else consume(_EQ);
  940. end;
  941. until token<>_ID;
  942. block_type:=old_block_type;
  943. end;
  944. end.