pdecl.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. { global }
  22. globtype,
  23. { symtable }
  24. symsym,
  25. { pass_1 }
  26. node;
  27. function readconstant(const orgname:string;const filepos:tfileposinfo):tconstsym;
  28. procedure const_dec;
  29. procedure label_dec;
  30. procedure type_dec;
  31. procedure types_dec;
  32. procedure var_dec;
  33. procedure threadvar_dec;
  34. procedure property_dec;
  35. procedure resourcestring_dec;
  36. implementation
  37. uses
  38. { common }
  39. cutils,cclasses,
  40. { global }
  41. globals,tokens,verbose,widestr,constexp,
  42. systems,
  43. { aasm }
  44. aasmbase,aasmtai,aasmdata,fmodule,
  45. { symtable }
  46. symconst,symbase,symtype,symdef,symtable,paramgr,defutil,
  47. { pass 1 }
  48. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nobj,
  49. { codegen }
  50. ncgutil,
  51. { parser }
  52. scanner,
  53. pbase,pexpr,ptype,ptconst,pdecsub,pdecvar,pdecobj,
  54. { cpu-information }
  55. cpuinfo
  56. ;
  57. function readconstant(const orgname:string;const filepos:tfileposinfo):tconstsym;
  58. var
  59. hp : tconstsym;
  60. p : tnode;
  61. ps : pconstset;
  62. pd : pbestreal;
  63. pg : pguid;
  64. sp : pchar;
  65. pw : pcompilerwidestring;
  66. storetokenpos : tfileposinfo;
  67. begin
  68. readconstant:=nil;
  69. if orgname='' then
  70. internalerror(9584582);
  71. hp:=nil;
  72. p:=comp_expr(true);
  73. storetokenpos:=current_tokenpos;
  74. current_tokenpos:=filepos;
  75. case p.nodetype of
  76. ordconstn:
  77. begin
  78. if p.resultdef.typ=pointerdef then
  79. hp:=tconstsym.create_ordptr(orgname,constpointer,tordconstnode(p).value.uvalue,p.resultdef)
  80. else
  81. hp:=tconstsym.create_ord(orgname,constord,tordconstnode(p).value,p.resultdef);
  82. end;
  83. stringconstn:
  84. begin
  85. if is_wide_or_unicode_string(p.resultdef) then
  86. begin
  87. initwidestring(pw);
  88. copywidestring(pcompilerwidestring(tstringconstnode(p).value_str),pw);
  89. hp:=tconstsym.create_wstring(orgname,constwstring,pw);
  90. end
  91. else
  92. begin
  93. getmem(sp,tstringconstnode(p).len+1);
  94. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  95. hp:=tconstsym.create_string(orgname,conststring,sp,tstringconstnode(p).len);
  96. end;
  97. end;
  98. realconstn :
  99. begin
  100. new(pd);
  101. pd^:=trealconstnode(p).value_real;
  102. hp:=tconstsym.create_ptr(orgname,constreal,pd,p.resultdef);
  103. end;
  104. setconstn :
  105. begin
  106. new(ps);
  107. ps^:=tsetconstnode(p).value_set^;
  108. hp:=tconstsym.create_ptr(orgname,constset,ps,p.resultdef);
  109. end;
  110. pointerconstn :
  111. begin
  112. hp:=tconstsym.create_ordptr(orgname,constpointer,tpointerconstnode(p).value,p.resultdef);
  113. end;
  114. niln :
  115. begin
  116. hp:=tconstsym.create_ord(orgname,constnil,0,p.resultdef);
  117. end;
  118. typen :
  119. begin
  120. if is_interface(p.resultdef) then
  121. begin
  122. if assigned(tobjectdef(p.resultdef).iidguid) then
  123. begin
  124. new(pg);
  125. pg^:=tobjectdef(p.resultdef).iidguid^;
  126. hp:=tconstsym.create_ptr(orgname,constguid,pg,p.resultdef);
  127. end
  128. else
  129. Message1(parser_e_interface_has_no_guid,tobjectdef(p.resultdef).objrealname^);
  130. end
  131. else
  132. Message(parser_e_illegal_expression);
  133. end;
  134. else
  135. Message(parser_e_illegal_expression);
  136. end;
  137. current_tokenpos:=storetokenpos;
  138. p.free;
  139. readconstant:=hp;
  140. end;
  141. procedure const_dec;
  142. var
  143. orgname : TIDString;
  144. hdef : tdef;
  145. sym : tsym;
  146. dummysymoptions : tsymoptions;
  147. deprecatedmsg : pshortstring;
  148. storetokenpos,filepos : tfileposinfo;
  149. old_block_type : tblock_type;
  150. skipequal : boolean;
  151. tclist : tasmlist;
  152. varspez : tvarspez;
  153. begin
  154. consume(_CONST);
  155. old_block_type:=block_type;
  156. block_type:=bt_const;
  157. repeat
  158. orgname:=orgpattern;
  159. filepos:=current_tokenpos;
  160. consume(_ID);
  161. case token of
  162. _EQUAL:
  163. begin
  164. consume(_EQUAL);
  165. sym:=readconstant(orgname,filepos);
  166. { Support hint directives }
  167. dummysymoptions:=[];
  168. deprecatedmsg:=nil;
  169. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  170. if assigned(sym) then
  171. begin
  172. sym.symoptions:=sym.symoptions+dummysymoptions;
  173. sym.deprecatedmsg:=deprecatedmsg;
  174. symtablestack.top.insert(sym);
  175. end
  176. else
  177. stringdispose(deprecatedmsg);
  178. consume(_SEMICOLON);
  179. end;
  180. _COLON:
  181. begin
  182. { set the blocktype first so a consume also supports a
  183. caret, to support const s : ^string = nil }
  184. block_type:=bt_const_type;
  185. consume(_COLON);
  186. read_anon_type(hdef,false);
  187. block_type:=bt_const;
  188. skipequal:=false;
  189. { create symbol }
  190. storetokenpos:=current_tokenpos;
  191. current_tokenpos:=filepos;
  192. if not (cs_typed_const_writable in current_settings.localswitches) then
  193. varspez:=vs_const
  194. else
  195. varspez:=vs_value;
  196. sym:=tstaticvarsym.create(orgname,varspez,hdef,[]);
  197. current_tokenpos:=storetokenpos;
  198. symtablestack.top.insert(sym);
  199. { procvar can have proc directives, but not type references }
  200. if (hdef.typ=procvardef) and
  201. (hdef.typesym=nil) then
  202. begin
  203. { support p : procedure;stdcall=nil; }
  204. if try_to_consume(_SEMICOLON) then
  205. begin
  206. if check_proc_directive(true) then
  207. parse_var_proc_directives(sym)
  208. else
  209. begin
  210. Message(parser_e_proc_directive_expected);
  211. skipequal:=true;
  212. end;
  213. end
  214. else
  215. { support p : procedure stdcall=nil; }
  216. begin
  217. if check_proc_directive(true) then
  218. parse_var_proc_directives(sym);
  219. end;
  220. { add default calling convention }
  221. handle_calling_convention(tabstractprocdef(hdef));
  222. end;
  223. if not skipequal then
  224. begin
  225. { get init value }
  226. consume(_EQUAL);
  227. if (cs_typed_const_writable in current_settings.localswitches) then
  228. tclist:=current_asmdata.asmlists[al_rotypedconsts]
  229. else
  230. tclist:=current_asmdata.asmlists[al_typedconsts];
  231. read_typed_const(tclist,tstaticvarsym(sym));
  232. end;
  233. end;
  234. else
  235. { generate an error }
  236. consume(_EQUAL);
  237. end;
  238. until token<>_ID;
  239. block_type:=old_block_type;
  240. end;
  241. procedure label_dec;
  242. begin
  243. consume(_LABEL);
  244. if not(cs_support_goto in current_settings.moduleswitches) then
  245. Message(sym_e_goto_and_label_not_supported);
  246. repeat
  247. if not(token in [_ID,_INTCONST]) then
  248. consume(_ID)
  249. else
  250. begin
  251. if token=_ID then
  252. symtablestack.top.insert(tlabelsym.create(orgpattern))
  253. else
  254. symtablestack.top.insert(tlabelsym.create(pattern));
  255. consume(token);
  256. end;
  257. if token<>_SEMICOLON then consume(_COMMA);
  258. until not(token in [_ID,_INTCONST]);
  259. consume(_SEMICOLON);
  260. end;
  261. procedure types_dec;
  262. function parse_generic_parameters:TFPObjectList;
  263. var
  264. generictype : ttypesym;
  265. begin
  266. result:=TFPObjectList.Create(false);
  267. repeat
  268. if token=_ID then
  269. begin
  270. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  271. include(generictype.symoptions,sp_generic_para);
  272. result.add(generictype);
  273. end;
  274. consume(_ID);
  275. until not try_to_consume(_COMMA) ;
  276. end;
  277. var
  278. typename,orgtypename : TIDString;
  279. newtype : ttypesym;
  280. sym : tsym;
  281. srsymtable : TSymtable;
  282. hdef : tdef;
  283. defpos,storetokenpos : tfileposinfo;
  284. old_block_type : tblock_type;
  285. objecttype : tobjecttyp;
  286. isgeneric,
  287. isunique,
  288. istyperenaming : boolean;
  289. generictypelist : TFPObjectList;
  290. generictokenbuf : tdynamicarray;
  291. vmtbuilder : TVMTBuilder;
  292. begin
  293. old_block_type:=block_type;
  294. block_type:=bt_type;
  295. repeat
  296. defpos:=current_tokenpos;
  297. istyperenaming:=false;
  298. generictypelist:=nil;
  299. generictokenbuf:=nil;
  300. { generic declaration? }
  301. isgeneric:=try_to_consume(_GENERIC);
  302. typename:=pattern;
  303. orgtypename:=orgpattern;
  304. consume(_ID);
  305. { Generic type declaration? }
  306. if isgeneric then
  307. begin
  308. consume(_LSHARPBRACKET);
  309. generictypelist:=parse_generic_parameters;
  310. consume(_RSHARPBRACKET);
  311. end;
  312. consume(_EQUAL);
  313. { support 'ttype=type word' syntax }
  314. isunique:=try_to_consume(_TYPE);
  315. { MacPas object model is more like Delphi's than like TP's, but }
  316. { uses the object keyword instead of class }
  317. if (m_mac in current_settings.modeswitches) and
  318. (token = _OBJECT) then
  319. token := _CLASS;
  320. { Start recording a generic template }
  321. if assigned(generictypelist) then
  322. begin
  323. generictokenbuf:=tdynamicarray.create(256);
  324. current_scanner.startrecordtokens(generictokenbuf);
  325. end;
  326. { is the type already defined? }
  327. searchsym(typename,sym,srsymtable);
  328. newtype:=nil;
  329. { found a symbol with this name? }
  330. if assigned(sym) then
  331. begin
  332. if (sym.typ=typesym) then
  333. begin
  334. if ((token=_CLASS) or
  335. (token=_INTERFACE) or
  336. (token=_DISPINTERFACE)) and
  337. (assigned(ttypesym(sym).typedef)) and
  338. is_class_or_interface_or_dispinterface(ttypesym(sym).typedef) and
  339. (oo_is_forward in tobjectdef(ttypesym(sym).typedef).objectoptions) then
  340. begin
  341. case token of
  342. _CLASS :
  343. objecttype:=odt_class;
  344. _INTERFACE :
  345. if current_settings.interfacetype=it_interfacecom then
  346. objecttype:=odt_interfacecom
  347. else
  348. objecttype:=odt_interfacecorba;
  349. _DISPINTERFACE :
  350. objecttype:=odt_dispinterface;
  351. else
  352. internalerror(200811072);
  353. end;
  354. consume(token);
  355. { we can ignore the result, the definition is modified }
  356. object_dec(objecttype,orgtypename,nil,nil,tobjectdef(ttypesym(sym).typedef));
  357. newtype:=ttypesym(sym);
  358. hdef:=newtype.typedef;
  359. end
  360. else
  361. message1(parser_h_type_redef,orgtypename);
  362. end;
  363. end;
  364. { no old type reused ? Then insert this new type }
  365. if not assigned(newtype) then
  366. begin
  367. { insert the new type first with an errordef, so that
  368. referencing the type before it's really set it
  369. will give an error (PFV) }
  370. hdef:=generrordef;
  371. storetokenpos:=current_tokenpos;
  372. newtype:=ttypesym.create(orgtypename,hdef);
  373. symtablestack.top.insert(newtype);
  374. current_tokenpos:=defpos;
  375. current_tokenpos:=storetokenpos;
  376. { read the type definition }
  377. read_named_type(hdef,orgtypename,nil,generictypelist,false);
  378. { update the definition of the type }
  379. if assigned(hdef) then
  380. begin
  381. if assigned(hdef.typesym) then
  382. istyperenaming:=true;
  383. if isunique then
  384. begin
  385. hdef:=tstoreddef(hdef).getcopy;
  386. { fix name, it is used e.g. for tables }
  387. if is_class_or_interface_or_dispinterface(hdef) then
  388. with tobjectdef(hdef) do
  389. begin
  390. stringdispose(objname);
  391. stringdispose(objrealname);
  392. objrealname:=stringdup(orgtypename);
  393. objname:=stringdup(upper(orgtypename));
  394. end;
  395. include(hdef.defoptions,df_unique);
  396. end;
  397. if not assigned(hdef.typesym) then
  398. hdef.typesym:=newtype;
  399. end;
  400. newtype.typedef:=hdef;
  401. { KAZ: handle TGUID declaration in system unit }
  402. if (cs_compilesystem in current_settings.moduleswitches) and not assigned(rec_tguid) and
  403. (typename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  404. assigned(hdef) and (hdef.typ=recorddef) and (hdef.size=16) then
  405. rec_tguid:=trecorddef(hdef);
  406. end;
  407. if assigned(hdef) then
  408. begin
  409. case hdef.typ of
  410. pointerdef :
  411. begin
  412. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  413. consume(_SEMICOLON);
  414. if try_to_consume(_FAR) then
  415. begin
  416. tpointerdef(hdef).is_far:=true;
  417. consume(_SEMICOLON);
  418. end;
  419. end;
  420. procvardef :
  421. begin
  422. { in case of type renaming, don't parse proc directives }
  423. if istyperenaming then
  424. begin
  425. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  426. consume(_SEMICOLON);
  427. end
  428. else
  429. begin
  430. if not check_proc_directive(true) then
  431. begin
  432. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  433. consume(_SEMICOLON);
  434. end;
  435. parse_var_proc_directives(tsym(newtype));
  436. handle_calling_convention(tprocvardef(hdef));
  437. if try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg) then
  438. consume(_SEMICOLON);
  439. end;
  440. end;
  441. objectdef :
  442. begin
  443. { Build VMT indexes, skip for type renaming and forward classes }
  444. if (hdef.typesym=newtype) and
  445. not(oo_is_forward in tobjectdef(hdef).objectoptions) and
  446. not(df_generic in hdef.defoptions) then
  447. begin
  448. vmtbuilder:=TVMTBuilder.Create(tobjectdef(hdef));
  449. vmtbuilder.generate_vmt;
  450. vmtbuilder.free;
  451. end;
  452. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  453. consume(_SEMICOLON);
  454. end;
  455. recorddef :
  456. begin
  457. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  458. consume(_SEMICOLON);
  459. end;
  460. else
  461. begin
  462. try_consume_hintdirective(newtype.symoptions,newtype.deprecatedmsg);
  463. consume(_SEMICOLON);
  464. end;
  465. end;
  466. end;
  467. if isgeneric and not(hdef.typ in [objectdef,recorddef]) then
  468. message(parser_e_cant_create_generics_of_this_type);
  469. { Stop recording a generic template }
  470. if assigned(generictypelist) then
  471. begin
  472. current_scanner.stoprecordtokens;
  473. tstoreddef(hdef).generictokenbuf:=generictokenbuf;
  474. { Generic is never a type renaming }
  475. hdef.typesym:=newtype;
  476. end;
  477. if assigned(generictypelist) then
  478. generictypelist.free;
  479. until token<>_ID;
  480. resolve_forward_types;
  481. block_type:=old_block_type;
  482. end;
  483. { reads a type declaration to the symbol table }
  484. procedure type_dec;
  485. begin
  486. consume(_TYPE);
  487. types_dec;
  488. end;
  489. procedure var_dec;
  490. { parses variable declarations and inserts them in }
  491. { the top symbol table of symtablestack }
  492. begin
  493. consume(_VAR);
  494. read_var_decls([]);
  495. end;
  496. procedure property_dec;
  497. var
  498. old_block_type : tblock_type;
  499. begin
  500. consume(_PROPERTY);
  501. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  502. message(parser_e_resourcestring_only_sg);
  503. old_block_type:=block_type;
  504. block_type:=bt_const;
  505. repeat
  506. read_property_dec(nil);
  507. consume(_SEMICOLON);
  508. until token<>_ID;
  509. block_type:=old_block_type;
  510. end;
  511. procedure threadvar_dec;
  512. { parses thread variable declarations and inserts them in }
  513. { the top symbol table of symtablestack }
  514. begin
  515. consume(_THREADVAR);
  516. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  517. message(parser_e_threadvars_only_sg);
  518. read_var_decls([vd_threadvar]);
  519. end;
  520. procedure resourcestring_dec;
  521. var
  522. orgname : TIDString;
  523. p : tnode;
  524. dummysymoptions : tsymoptions;
  525. deprecatedmsg : pshortstring;
  526. storetokenpos,filepos : tfileposinfo;
  527. old_block_type : tblock_type;
  528. sp : pchar;
  529. sym : tsym;
  530. begin
  531. consume(_RESOURCESTRING);
  532. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  533. message(parser_e_resourcestring_only_sg);
  534. old_block_type:=block_type;
  535. block_type:=bt_const;
  536. repeat
  537. orgname:=orgpattern;
  538. filepos:=current_tokenpos;
  539. consume(_ID);
  540. case token of
  541. _EQUAL:
  542. begin
  543. consume(_EQUAL);
  544. p:=comp_expr(true);
  545. storetokenpos:=current_tokenpos;
  546. current_tokenpos:=filepos;
  547. sym:=nil;
  548. case p.nodetype of
  549. ordconstn:
  550. begin
  551. if is_constcharnode(p) then
  552. begin
  553. getmem(sp,2);
  554. sp[0]:=chr(tordconstnode(p).value.svalue);
  555. sp[1]:=#0;
  556. sym:=tconstsym.create_string(orgname,constresourcestring,sp,1);
  557. end
  558. else
  559. Message(parser_e_illegal_expression);
  560. end;
  561. stringconstn:
  562. with Tstringconstnode(p) do
  563. begin
  564. getmem(sp,len+1);
  565. move(value_str^,sp^,len+1);
  566. sym:=tconstsym.create_string(orgname,constresourcestring,sp,len);
  567. end;
  568. else
  569. Message(parser_e_illegal_expression);
  570. end;
  571. current_tokenpos:=storetokenpos;
  572. { Support hint directives }
  573. dummysymoptions:=[];
  574. deprecatedmsg:=nil;
  575. try_consume_hintdirective(dummysymoptions,deprecatedmsg);
  576. if assigned(sym) then
  577. begin
  578. sym.symoptions:=sym.symoptions+dummysymoptions;
  579. sym.deprecatedmsg:=deprecatedmsg;
  580. symtablestack.top.insert(sym);
  581. end
  582. else
  583. stringdispose(deprecatedmsg);
  584. consume(_SEMICOLON);
  585. p.free;
  586. end;
  587. else consume(_EQUAL);
  588. end;
  589. until token<>_ID;
  590. block_type:=old_block_type;
  591. end;
  592. end.