pdecl.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. globals,
  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. globtype,tokens,verbose,widestr,
  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,
  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,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_widestring(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. storetokenpos,filepos : tfileposinfo;
  148. old_block_type : tblock_type;
  149. skipequal : boolean;
  150. tclist : tasmlist;
  151. varspez : tvarspez;
  152. begin
  153. consume(_CONST);
  154. old_block_type:=block_type;
  155. block_type:=bt_const;
  156. repeat
  157. orgname:=orgpattern;
  158. filepos:=current_tokenpos;
  159. consume(_ID);
  160. case token of
  161. _EQUAL:
  162. begin
  163. consume(_EQUAL);
  164. sym:=readconstant(orgname,filepos);
  165. { Support hint directives }
  166. dummysymoptions:=[];
  167. try_consume_hintdirective(dummysymoptions);
  168. if assigned(sym) then
  169. begin
  170. sym.symoptions:=sym.symoptions+dummysymoptions;
  171. symtablestack.top.insert(sym);
  172. end;
  173. consume(_SEMICOLON);
  174. end;
  175. _COLON:
  176. begin
  177. { set the blocktype first so a consume also supports a
  178. caret, to support const s : ^string = nil }
  179. block_type:=bt_type;
  180. consume(_COLON);
  181. ignore_equal:=true;
  182. read_anon_type(hdef,false);
  183. ignore_equal:=false;
  184. block_type:=bt_const;
  185. skipequal:=false;
  186. { create symbol }
  187. storetokenpos:=current_tokenpos;
  188. current_tokenpos:=filepos;
  189. if not (cs_typed_const_writable in current_settings.localswitches) then
  190. varspez:=vs_const
  191. else
  192. varspez:=vs_value;
  193. sym:=tstaticvarsym.create(orgname,varspez,hdef,[]);
  194. current_tokenpos:=storetokenpos;
  195. symtablestack.top.insert(sym);
  196. { procvar can have proc directives, but not type references }
  197. if (hdef.typ=procvardef) and
  198. (hdef.typesym=nil) then
  199. begin
  200. { support p : procedure;stdcall=nil; }
  201. if try_to_consume(_SEMICOLON) then
  202. begin
  203. if check_proc_directive(true) then
  204. parse_var_proc_directives(sym)
  205. else
  206. begin
  207. Message(parser_e_proc_directive_expected);
  208. skipequal:=true;
  209. end;
  210. end
  211. else
  212. { support p : procedure stdcall=nil; }
  213. begin
  214. if check_proc_directive(true) then
  215. parse_var_proc_directives(sym);
  216. end;
  217. { add default calling convention }
  218. handle_calling_convention(tabstractprocdef(hdef));
  219. end;
  220. { write rtti/init tables }
  221. write_persistent_type_info(hdef);
  222. if not skipequal then
  223. begin
  224. { get init value }
  225. consume(_EQUAL);
  226. if (cs_typed_const_writable in current_settings.localswitches) then
  227. tclist:=current_asmdata.asmlists[al_rotypedconsts]
  228. else
  229. tclist:=current_asmdata.asmlists[al_typedconsts];
  230. read_typed_const(tclist,tstaticvarsym(sym));
  231. consume(_SEMICOLON);
  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. { search in symtablestack used, but not defined type }
  262. procedure resolve_type_forward(p:TObject;arg:pointer);
  263. var
  264. hpd,pd : tdef;
  265. stpos : tfileposinfo;
  266. again : boolean;
  267. srsym : tsym;
  268. srsymtable : TSymtable;
  269. begin
  270. { Check only typesyms or record/object fields }
  271. case tsym(p).typ of
  272. typesym :
  273. pd:=ttypesym(p).typedef;
  274. fieldvarsym :
  275. pd:=tfieldvarsym(p).vardef
  276. else
  277. exit;
  278. end;
  279. repeat
  280. again:=false;
  281. case pd.typ of
  282. arraydef :
  283. begin
  284. { elementdef could also be defined using a forwarddef }
  285. pd:=tarraydef(pd).elementdef;
  286. again:=true;
  287. end;
  288. pointerdef,
  289. classrefdef :
  290. begin
  291. { classrefdef inherits from pointerdef }
  292. hpd:=tabstractpointerdef(pd).pointeddef;
  293. { still a forward def ? }
  294. if hpd.typ=forwarddef then
  295. begin
  296. { try to resolve the forward }
  297. { get the correct position for it }
  298. stpos:=current_tokenpos;
  299. current_tokenpos:=tforwarddef(hpd).forwardpos;
  300. resolving_forward:=true;
  301. if not assigned(tforwarddef(hpd).tosymname) then
  302. internalerror(20021120);
  303. searchsym(tforwarddef(hpd).tosymname^,srsym,srsymtable);
  304. resolving_forward:=false;
  305. current_tokenpos:=stpos;
  306. { we don't need the forwarddef anymore, dispose it }
  307. hpd.free;
  308. tabstractpointerdef(pd).pointeddef:=nil; { if error occurs }
  309. { was a type sym found ? }
  310. if assigned(srsym) and
  311. (srsym.typ=typesym) then
  312. begin
  313. tabstractpointerdef(pd).pointeddef:=ttypesym(srsym).typedef;
  314. { avoid wrong unused warnings web bug 801 PM }
  315. inc(ttypesym(srsym).refs);
  316. { we need a class type for classrefdef }
  317. if (pd.typ=classrefdef) and
  318. not(is_class(ttypesym(srsym).typedef)) then
  319. Message1(type_e_class_type_expected,ttypesym(srsym).typedef.typename);
  320. end
  321. else
  322. begin
  323. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  324. { try to recover }
  325. tabstractpointerdef(pd).pointeddef:=generrordef;
  326. end;
  327. end;
  328. end;
  329. recorddef :
  330. trecorddef(pd).symtable.SymList.ForEachCall(@resolve_type_forward,nil);
  331. objectdef :
  332. begin
  333. if not(m_fpc in current_settings.modeswitches) and
  334. (oo_is_forward in tobjectdef(pd).objectoptions) then
  335. begin
  336. { only give an error as the implementation may follow in an
  337. other type block which is allowed by FPC modes }
  338. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  339. end
  340. else
  341. begin
  342. { Check all fields of the object declaration, but don't
  343. check objectdefs in objects/records, because these
  344. can't exist (anonymous objects aren't allowed) }
  345. if not(tsym(p).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  346. tobjectdef(pd).symtable.SymList.ForEachCall(@resolve_type_forward,nil);
  347. end;
  348. end;
  349. end;
  350. until not again;
  351. end;
  352. procedure types_dec;
  353. function parse_generic_parameters:TFPObjectList;
  354. var
  355. generictype : ttypesym;
  356. begin
  357. result:=TFPObjectList.Create(false);
  358. repeat
  359. if token=_ID then
  360. begin
  361. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  362. result.add(generictype);
  363. end;
  364. consume(_ID);
  365. until not try_to_consume(_COMMA) ;
  366. end;
  367. var
  368. typename,orgtypename : TIDString;
  369. newtype : ttypesym;
  370. sym : tsym;
  371. srsymtable : TSymtable;
  372. hdef : tdef;
  373. oldfilepos,
  374. defpos,storetokenpos : tfileposinfo;
  375. old_block_type : tblock_type;
  376. isgeneric,
  377. isunique,
  378. istyperenaming : boolean;
  379. generictypelist : TFPObjectList;
  380. generictokenbuf : tdynamicarray;
  381. begin
  382. old_block_type:=block_type;
  383. block_type:=bt_type;
  384. typecanbeforward:=true;
  385. repeat
  386. defpos:=current_tokenpos;
  387. istyperenaming:=false;
  388. generictypelist:=nil;
  389. generictokenbuf:=nil;
  390. { generic declaration? }
  391. isgeneric:=try_to_consume(_GENERIC);
  392. typename:=pattern;
  393. orgtypename:=orgpattern;
  394. consume(_ID);
  395. { Generic type declaration? }
  396. if isgeneric then
  397. begin
  398. consume(_LSHARPBRACKET);
  399. generictypelist:=parse_generic_parameters;
  400. consume(_RSHARPBRACKET);
  401. end;
  402. consume(_EQUAL);
  403. { support 'ttype=type word' syntax }
  404. isunique:=try_to_consume(_TYPE);
  405. { MacPas object model is more like Delphi's than like TP's, but }
  406. { uses the object keyword instead of class }
  407. if (m_mac in current_settings.modeswitches) and
  408. (token = _OBJECT) then
  409. token := _CLASS;
  410. { Start recording a generic template }
  411. if assigned(generictypelist) then
  412. begin
  413. generictokenbuf:=tdynamicarray.create(256);
  414. current_scanner.startrecordtokens(generictokenbuf);
  415. end;
  416. { is the type already defined? }
  417. searchsym(typename,sym,srsymtable);
  418. newtype:=nil;
  419. { found a symbol with this name? }
  420. if assigned(sym) then
  421. begin
  422. if (sym.typ=typesym) then
  423. begin
  424. if ((token=_CLASS) or
  425. (token=_INTERFACE) or
  426. (token=_DISPINTERFACE)) and
  427. (assigned(ttypesym(sym).typedef)) and
  428. is_class_or_interface_or_dispinterface(ttypesym(sym).typedef) and
  429. (oo_is_forward in tobjectdef(ttypesym(sym).typedef).objectoptions) then
  430. begin
  431. { we can ignore the result }
  432. { the definition is modified }
  433. object_dec(orgtypename,nil,nil,tobjectdef(ttypesym(sym).typedef));
  434. newtype:=ttypesym(sym);
  435. hdef:=newtype.typedef;
  436. end
  437. else
  438. message1(parser_h_type_redef,orgtypename);
  439. end;
  440. end;
  441. { no old type reused ? Then insert this new type }
  442. if not assigned(newtype) then
  443. begin
  444. { insert the new type first with an errordef, so that
  445. referencing the type before it's really set it
  446. will give an error (PFV) }
  447. hdef:=generrordef;
  448. storetokenpos:=current_tokenpos;
  449. newtype:=ttypesym.create(orgtypename,hdef);
  450. symtablestack.top.insert(newtype);
  451. current_tokenpos:=defpos;
  452. current_tokenpos:=storetokenpos;
  453. { read the type definition }
  454. read_named_type(hdef,orgtypename,nil,generictypelist,false);
  455. { update the definition of the type }
  456. if assigned(hdef) then
  457. begin
  458. if assigned(hdef.typesym) then
  459. istyperenaming:=true;
  460. if isunique then
  461. begin
  462. hdef:=tstoreddef(hdef).getcopy;
  463. include(hdef.defoptions,df_unique);
  464. end;
  465. if not assigned(hdef.typesym) then
  466. hdef.typesym:=newtype;
  467. end;
  468. newtype.typedef:=hdef;
  469. { KAZ: handle TGUID declaration in system unit }
  470. if (cs_compilesystem in current_settings.moduleswitches) and not assigned(rec_tguid) and
  471. (typename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  472. assigned(hdef) and (hdef.typ=recorddef) and (hdef.size=16) then
  473. rec_tguid:=trecorddef(hdef);
  474. end;
  475. if assigned(hdef) then
  476. begin
  477. case hdef.typ of
  478. pointerdef :
  479. begin
  480. consume(_SEMICOLON);
  481. if try_to_consume(_FAR) then
  482. begin
  483. tpointerdef(hdef).is_far:=true;
  484. consume(_SEMICOLON);
  485. end;
  486. end;
  487. procvardef :
  488. begin
  489. { in case of type renaming, don't parse proc directives }
  490. if istyperenaming then
  491. consume(_SEMICOLON)
  492. else
  493. begin
  494. if not check_proc_directive(true) then
  495. consume(_SEMICOLON);
  496. parse_var_proc_directives(tsym(newtype));
  497. handle_calling_convention(tprocvardef(hdef));
  498. end;
  499. end;
  500. objectdef,
  501. recorddef :
  502. begin
  503. try_consume_hintdirective(newtype.symoptions);
  504. consume(_SEMICOLON);
  505. end;
  506. else
  507. consume(_SEMICOLON);
  508. end;
  509. end;
  510. if isgeneric and not(hdef.typ in [objectdef,recorddef]) then
  511. message(parser_e_cant_create_generics_of_this_type);
  512. { Stop recording a generic template }
  513. if assigned(generictypelist) then
  514. begin
  515. current_scanner.stoprecordtokens;
  516. tstoreddef(hdef).generictokenbuf:=generictokenbuf;
  517. { Generic is never a type renaming }
  518. hdef.typesym:=newtype;
  519. end;
  520. { Write tables if there are no errors and we are the typesym that
  521. defines this type, so this will not be done for simple type renamings }
  522. if (hdef.typ<>errordef) and
  523. (hdef.typesym=newtype) then
  524. begin
  525. { file position }
  526. oldfilepos:=current_filepos;
  527. current_filepos:=newtype.fileinfo;
  528. write_persistent_type_info(hdef);
  529. current_filepos:=oldfilepos;
  530. end;
  531. until token<>_ID;
  532. typecanbeforward:=false;
  533. symtablestack.top.SymList.ForEachCall(@resolve_type_forward,nil);
  534. block_type:=old_block_type;
  535. end;
  536. { reads a type declaration to the symbol table }
  537. procedure type_dec;
  538. begin
  539. consume(_TYPE);
  540. types_dec;
  541. end;
  542. procedure var_dec;
  543. { parses variable declarations and inserts them in }
  544. { the top symbol table of symtablestack }
  545. begin
  546. consume(_VAR);
  547. read_var_decls([]);
  548. end;
  549. procedure property_dec;
  550. var
  551. old_block_type : tblock_type;
  552. begin
  553. consume(_PROPERTY);
  554. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  555. message(parser_e_resourcestring_only_sg);
  556. old_block_type:=block_type;
  557. block_type:=bt_const;
  558. repeat
  559. read_property_dec(nil);
  560. consume(_SEMICOLON);
  561. until token<>_ID;
  562. block_type:=old_block_type;
  563. end;
  564. procedure threadvar_dec;
  565. { parses thread variable declarations and inserts them in }
  566. { the top symbol table of symtablestack }
  567. begin
  568. consume(_THREADVAR);
  569. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  570. message(parser_e_threadvars_only_sg);
  571. read_var_decls([vd_threadvar]);
  572. end;
  573. procedure resourcestring_dec;
  574. var
  575. orgname : TIDString;
  576. p : tnode;
  577. dummysymoptions : tsymoptions;
  578. storetokenpos,filepos : tfileposinfo;
  579. old_block_type : tblock_type;
  580. sp : pchar;
  581. sym : tsym;
  582. begin
  583. consume(_RESOURCESTRING);
  584. if not(symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) then
  585. message(parser_e_resourcestring_only_sg);
  586. old_block_type:=block_type;
  587. block_type:=bt_const;
  588. repeat
  589. orgname:=orgpattern;
  590. filepos:=current_tokenpos;
  591. consume(_ID);
  592. case token of
  593. _EQUAL:
  594. begin
  595. consume(_EQUAL);
  596. p:=comp_expr(true);
  597. storetokenpos:=current_tokenpos;
  598. current_tokenpos:=filepos;
  599. sym:=nil;
  600. case p.nodetype of
  601. ordconstn:
  602. begin
  603. if is_constcharnode(p) then
  604. begin
  605. getmem(sp,2);
  606. sp[0]:=chr(tordconstnode(p).value);
  607. sp[1]:=#0;
  608. sym:=tconstsym.create_string(orgname,constresourcestring,sp,1);
  609. end
  610. else
  611. Message(parser_e_illegal_expression);
  612. end;
  613. stringconstn:
  614. with Tstringconstnode(p) do
  615. begin
  616. getmem(sp,len+1);
  617. move(value_str^,sp^,len+1);
  618. sym:=tconstsym.create_string(orgname,constresourcestring,sp,len);
  619. end;
  620. else
  621. Message(parser_e_illegal_expression);
  622. end;
  623. current_tokenpos:=storetokenpos;
  624. { Support hint directives }
  625. dummysymoptions:=[];
  626. try_consume_hintdirective(dummysymoptions);
  627. if assigned(sym) then
  628. begin
  629. sym.symoptions:=sym.symoptions+dummysymoptions;
  630. symtablestack.top.insert(sym);
  631. end;
  632. consume(_SEMICOLON);
  633. p.free;
  634. end;
  635. else consume(_EQUAL);
  636. end;
  637. until token<>_ID;
  638. block_type:=old_block_type;
  639. end;
  640. end.