pdecl.pas 39 KB

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