pdecl.pas 37 KB

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