pdecl.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 var_dec;
  32. procedure threadvar_dec;
  33. procedure property_dec;
  34. procedure resourcestring_dec;
  35. implementation
  36. uses
  37. { common }
  38. cutils,cclasses,
  39. { global }
  40. globtype,tokens,verbose,
  41. systems,
  42. { aasm }
  43. aasmbase,aasmtai,fmodule,
  44. { symtable }
  45. symconst,symbase,symtype,symdef,symtable,paramgr,
  46. { pass 1 }
  47. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nobj,
  48. { codegen }
  49. ncgutil,
  50. { parser }
  51. scanner,
  52. pbase,pexpr,ptype,ptconst,pdecsub,pdecvar,pdecobj,
  53. { cpu-information }
  54. cpuinfo
  55. ;
  56. function readconstant(const orgname:string;const filepos:tfileposinfo):tconstsym;
  57. var
  58. hp : tconstsym;
  59. p : tnode;
  60. ps : pconstset;
  61. pd : pbestreal;
  62. pg : pguid;
  63. sp : pchar;
  64. storetokenpos : tfileposinfo;
  65. begin
  66. readconstant:=nil;
  67. if orgname='' then
  68. internalerror(9584582);
  69. hp:=nil;
  70. p:=comp_expr(true);
  71. storetokenpos:=akttokenpos;
  72. akttokenpos:=filepos;
  73. case p.nodetype of
  74. ordconstn:
  75. begin
  76. if p.resulttype.def.deftype=pointerdef then
  77. hp:=tconstsym.create_ordptr(orgname,constpointer,tordconstnode(p).value,p.resulttype)
  78. else
  79. hp:=tconstsym.create_ord(orgname,constord,tordconstnode(p).value,p.resulttype);
  80. end;
  81. stringconstn:
  82. begin
  83. getmem(sp,tstringconstnode(p).len+1);
  84. move(tstringconstnode(p).value_str^,sp^,tstringconstnode(p).len+1);
  85. hp:=tconstsym.create_string(orgname,conststring,sp,tstringconstnode(p).len);
  86. end;
  87. realconstn :
  88. begin
  89. new(pd);
  90. pd^:=trealconstnode(p).value_real;
  91. hp:=tconstsym.create_ptr(orgname,constreal,pd,p.resulttype);
  92. end;
  93. setconstn :
  94. begin
  95. new(ps);
  96. ps^:=tsetconstnode(p).value_set^;
  97. hp:=tconstsym.create_ptr(orgname,constset,ps,p.resulttype);
  98. end;
  99. pointerconstn :
  100. begin
  101. hp:=tconstsym.create_ordptr(orgname,constpointer,tpointerconstnode(p).value,p.resulttype);
  102. end;
  103. niln :
  104. begin
  105. hp:=tconstsym.create_ord(orgname,constnil,0,p.resulttype);
  106. end;
  107. typen :
  108. begin
  109. if is_interface(p.resulttype.def) then
  110. begin
  111. if assigned(tobjectdef(p.resulttype.def).iidguid) then
  112. begin
  113. new(pg);
  114. pg^:=tobjectdef(p.resulttype.def).iidguid^;
  115. hp:=tconstsym.create_ptr(orgname,constguid,pg,p.resulttype);
  116. end
  117. else
  118. Message1(parser_e_interface_has_no_guid,tobjectdef(p.resulttype.def).objrealname^);
  119. end
  120. else
  121. Message(parser_e_illegal_expression);
  122. end;
  123. else
  124. Message(parser_e_illegal_expression);
  125. end;
  126. akttokenpos:=storetokenpos;
  127. p.free;
  128. readconstant:=hp;
  129. end;
  130. procedure const_dec;
  131. var
  132. orgname : stringid;
  133. tt : ttype;
  134. sym : tsym;
  135. dummysymoptions : tsymoptions;
  136. storetokenpos,filepos : tfileposinfo;
  137. old_block_type : tblock_type;
  138. skipequal : boolean;
  139. begin
  140. consume(_CONST);
  141. old_block_type:=block_type;
  142. block_type:=bt_const;
  143. repeat
  144. orgname:=orgpattern;
  145. filepos:=akttokenpos;
  146. consume(_ID);
  147. case token of
  148. _EQUAL:
  149. begin
  150. consume(_EQUAL);
  151. sym:=readconstant(orgname,filepos);
  152. { Support hint directives }
  153. dummysymoptions:=[];
  154. try_consume_hintdirective(dummysymoptions);
  155. if assigned(sym) then
  156. begin
  157. sym.symoptions:=sym.symoptions+dummysymoptions;
  158. symtablestack.insert(sym);
  159. end;
  160. consume(_SEMICOLON);
  161. end;
  162. _COLON:
  163. begin
  164. { set the blocktype first so a consume also supports a
  165. caret, to support const s : ^string = nil }
  166. block_type:=bt_type;
  167. consume(_COLON);
  168. ignore_equal:=true;
  169. read_type(tt,'',false);
  170. ignore_equal:=false;
  171. block_type:=bt_const;
  172. skipequal:=false;
  173. { create symbol }
  174. storetokenpos:=akttokenpos;
  175. akttokenpos:=filepos;
  176. sym:=ttypedconstsym.createtype(orgname,tt,(cs_typed_const_writable in aktlocalswitches));
  177. akttokenpos:=storetokenpos;
  178. symtablestack.insert(sym);
  179. insertconstdata(ttypedconstsym(sym));
  180. { procvar can have proc directives, but not type references }
  181. if (tt.def.deftype=procvardef) and
  182. (tt.sym=nil) then
  183. begin
  184. { support p : procedure;stdcall=nil; }
  185. if try_to_consume(_SEMICOLON) then
  186. begin
  187. if check_proc_directive(true) then
  188. parse_var_proc_directives(sym)
  189. else
  190. begin
  191. Message(parser_e_proc_directive_expected);
  192. skipequal:=true;
  193. end;
  194. end
  195. else
  196. { support p : procedure stdcall=nil; }
  197. begin
  198. if check_proc_directive(true) then
  199. parse_var_proc_directives(sym);
  200. end;
  201. { add default calling convention }
  202. handle_calling_convention(tabstractprocdef(tt.def));
  203. end;
  204. if not skipequal then
  205. begin
  206. { get init value }
  207. consume(_EQUAL);
  208. readtypedconst(tt,ttypedconstsym(sym),(cs_typed_const_writable in aktlocalswitches));
  209. try_consume_hintdirective(sym.symoptions);
  210. consume(_SEMICOLON);
  211. end;
  212. end;
  213. else
  214. { generate an error }
  215. consume(_EQUAL);
  216. end;
  217. until token<>_ID;
  218. block_type:=old_block_type;
  219. end;
  220. procedure label_dec;
  221. var
  222. hl : tasmlabel;
  223. begin
  224. consume(_LABEL);
  225. if not(cs_support_goto in aktmoduleswitches) then
  226. Message(sym_e_goto_and_label_not_supported);
  227. repeat
  228. if not(token in [_ID,_INTCONST]) then
  229. consume(_ID)
  230. else
  231. begin
  232. objectlibrary.getlabel(hl);
  233. if token=_ID then
  234. symtablestack.insert(tlabelsym.create(orgpattern,hl))
  235. else
  236. symtablestack.insert(tlabelsym.create(pattern,hl));
  237. consume(token);
  238. end;
  239. if token<>_SEMICOLON then consume(_COMMA);
  240. until not(token in [_ID,_INTCONST]);
  241. consume(_SEMICOLON);
  242. end;
  243. { search in symtablestack used, but not defined type }
  244. procedure resolve_type_forward(p : tnamedindexitem;arg:pointer);
  245. var
  246. hpd,pd : tdef;
  247. stpos : tfileposinfo;
  248. again : boolean;
  249. srsym : tsym;
  250. srsymtable : tsymtable;
  251. {$ifdef gdb_notused}
  252. stab_str:Pchar;
  253. {$endif gdb_notused}
  254. begin
  255. { Check only typesyms or record/object fields }
  256. case tsym(p).typ of
  257. typesym :
  258. pd:=ttypesym(p).restype.def;
  259. fieldvarsym :
  260. pd:=tfieldvarsym(p).vartype.def
  261. else
  262. exit;
  263. end;
  264. repeat
  265. again:=false;
  266. case pd.deftype of
  267. arraydef :
  268. begin
  269. { elementtype could also be defined using a forwarddef }
  270. pd:=tarraydef(pd).elementtype.def;
  271. again:=true;
  272. end;
  273. pointerdef,
  274. classrefdef :
  275. begin
  276. { classrefdef inherits from pointerdef }
  277. hpd:=tpointerdef(pd).pointertype.def;
  278. { still a forward def ? }
  279. if hpd.deftype=forwarddef then
  280. begin
  281. { try to resolve the forward }
  282. { get the correct position for it }
  283. stpos:=akttokenpos;
  284. akttokenpos:=tforwarddef(hpd).forwardpos;
  285. resolving_forward:=true;
  286. make_ref:=false;
  287. if not assigned(tforwarddef(hpd).tosymname) then
  288. internalerror(20021120);
  289. searchsym(tforwarddef(hpd).tosymname^,srsym,srsymtable);
  290. make_ref:=true;
  291. resolving_forward:=false;
  292. akttokenpos:=stpos;
  293. { we don't need the forwarddef anymore, dispose it }
  294. hpd.free;
  295. tpointerdef(pd).pointertype.def:=nil; { if error occurs }
  296. { was a type sym found ? }
  297. if assigned(srsym) and
  298. (srsym.typ=typesym) then
  299. begin
  300. tpointerdef(pd).pointertype.setsym(srsym);
  301. { avoid wrong unused warnings web bug 801 PM }
  302. inc(ttypesym(srsym).refs);
  303. {$ifdef GDB_UNUSED}
  304. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  305. (tsym(p).owner.symtabletype in [globalsymtable,staticsymtable]) then
  306. begin
  307. ttypesym(p).isusedinstab:=true;
  308. { ttypesym(p).concatstabto(debuglist);}
  309. {not stabs for forward defs }
  310. if not Ttypesym(p).isstabwritten then
  311. begin
  312. if Ttypesym(p).restype.def.typesym=p then
  313. Tstoreddef(Ttypesym(p).restype.def).concatstabto(debuglist)
  314. else
  315. begin
  316. stab_str:=Ttypesym(p).stabstring;
  317. if assigned(stab_str) then
  318. debuglist.concat(Tai_stabs.create(stab_str));
  319. Ttypesym(p).isstabwritten:=true;
  320. end;
  321. end;
  322. end;
  323. {$endif GDB_UNUSED}
  324. { we need a class type for classrefdef }
  325. if (pd.deftype=classrefdef) and
  326. not(is_class(ttypesym(srsym).restype.def)) then
  327. Message1(type_e_class_type_expected,ttypesym(srsym).restype.def.typename);
  328. end
  329. else
  330. begin
  331. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  332. { try to recover }
  333. tpointerdef(pd).pointertype:=generrortype;
  334. end;
  335. end;
  336. end;
  337. recorddef :
  338. trecorddef(pd).symtable.foreach_static(@resolve_type_forward,nil);
  339. objectdef :
  340. begin
  341. if not(m_fpc in aktmodeswitches) and
  342. (oo_is_forward in tobjectdef(pd).objectoptions) then
  343. begin
  344. { only give an error as the implementation may follow in an
  345. other type block which is allowed by FPC modes }
  346. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  347. end
  348. else
  349. begin
  350. { Check all fields of the object declaration, but don't
  351. check objectdefs in objects/records, because these
  352. can't exist (anonymous objects aren't allowed) }
  353. if not(tsym(p).owner.symtabletype in [objectsymtable,recordsymtable]) then
  354. tobjectdef(pd).symtable.foreach_static(@resolve_type_forward,nil);
  355. end;
  356. end;
  357. end;
  358. until not again;
  359. end;
  360. { reads a type declaration to the symbol table }
  361. procedure type_dec;
  362. var
  363. typename,orgtypename : stringid;
  364. newtype : ttypesym;
  365. sym : tsym;
  366. srsymtable : tsymtable;
  367. tt : ttype;
  368. oldfilepos,
  369. defpos,storetokenpos : tfileposinfo;
  370. old_block_type : tblock_type;
  371. ch : tclassheader;
  372. unique,istyperenaming : boolean;
  373. begin
  374. old_block_type:=block_type;
  375. block_type:=bt_type;
  376. consume(_TYPE);
  377. typecanbeforward:=true;
  378. repeat
  379. typename:=pattern;
  380. orgtypename:=orgpattern;
  381. defpos:=akttokenpos;
  382. istyperenaming:=false;
  383. consume(_ID);
  384. consume(_EQUAL);
  385. { support 'ttype=type word' syntax }
  386. unique:=try_to_consume(_TYPE);
  387. { is the type already defined? }
  388. searchsym(typename,sym,srsymtable);
  389. newtype:=nil;
  390. { found a symbol with this name? }
  391. if assigned(sym) then
  392. begin
  393. if (sym.typ=typesym) then
  394. begin
  395. if ((token=_CLASS) or
  396. (token=_INTERFACE)) and
  397. (assigned(ttypesym(sym).restype.def)) and
  398. is_class_or_interface(ttypesym(sym).restype.def) and
  399. (oo_is_forward in tobjectdef(ttypesym(sym).restype.def).objectoptions) then
  400. begin
  401. { we can ignore the result }
  402. { the definition is modified }
  403. object_dec(orgtypename,tobjectdef(ttypesym(sym).restype.def));
  404. newtype:=ttypesym(sym);
  405. tt:=newtype.restype;
  406. end
  407. else
  408. message1(parser_h_type_redef,orgtypename);
  409. end;
  410. end;
  411. { no old type reused ? Then insert this new type }
  412. if not assigned(newtype) then
  413. begin
  414. { insert the new type first with an errordef, so that
  415. referencing the type before it's really set it
  416. will give an error (PFV) }
  417. tt:=generrortype;
  418. storetokenpos:=akttokenpos;
  419. newtype:=ttypesym.create(orgtypename,tt);
  420. symtablestack.insert(newtype);
  421. akttokenpos:=defpos;
  422. akttokenpos:=storetokenpos;
  423. { read the type definition }
  424. read_type(tt,orgtypename,false);
  425. { update the definition of the type }
  426. newtype.restype:=tt;
  427. if assigned(tt.sym) then
  428. istyperenaming:=true
  429. else
  430. tt.sym:=newtype;
  431. if unique and assigned(tt.def) then
  432. begin
  433. tt.setdef(tstoreddef(tt.def).getcopy);
  434. include(tt.def.defoptions,df_unique);
  435. newtype.restype:=tt;
  436. end;
  437. if assigned(tt.def) and not assigned(tt.def.typesym) then
  438. tt.def.typesym:=newtype;
  439. { KAZ: handle TGUID declaration in system unit }
  440. if (cs_compilesystem in aktmoduleswitches) and not assigned(rec_tguid) and
  441. (typename='TGUID') and { name: TGUID and size=16 bytes that is 128 bits }
  442. assigned(tt.def) and (tt.def.deftype=recorddef) and (tt.def.size=16) then
  443. rec_tguid:=trecorddef(tt.def);
  444. end;
  445. if assigned(tt.def) then
  446. begin
  447. case tt.def.deftype of
  448. pointerdef :
  449. begin
  450. consume(_SEMICOLON);
  451. if try_to_consume(_FAR) then
  452. begin
  453. tpointerdef(tt.def).is_far:=true;
  454. consume(_SEMICOLON);
  455. end;
  456. end;
  457. procvardef :
  458. begin
  459. { in case of type renaming, don't parse proc directives }
  460. if istyperenaming then
  461. consume(_SEMICOLON)
  462. else
  463. begin
  464. if not check_proc_directive(true) then
  465. consume(_SEMICOLON);
  466. parse_var_proc_directives(tsym(newtype));
  467. handle_calling_convention(tprocvardef(tt.def));
  468. end;
  469. end;
  470. objectdef,
  471. recorddef :
  472. begin
  473. try_consume_hintdirective(newtype.symoptions);
  474. consume(_SEMICOLON);
  475. end;
  476. else
  477. consume(_SEMICOLON);
  478. end;
  479. end;
  480. { Write tables if we are the typesym that defines
  481. this type. This will not be done for simple type renamings }
  482. if (tt.def.typesym=newtype) then
  483. begin
  484. { file position }
  485. oldfilepos:=aktfilepos;
  486. aktfilepos:=newtype.fileinfo;
  487. { generate persistent init/final tables when it's declared in the interface so it can
  488. be reused in other used }
  489. if current_module.in_interface and
  490. ((is_class(tt.def) and
  491. tobjectdef(tt.def).members_need_inittable) or
  492. tt.def.needs_inittable) then
  493. generate_inittable(newtype);
  494. { for objects we should write the vmt and interfaces.
  495. This need to be done after the rtti has been written, because
  496. it can contain a reference to that data (PFV)
  497. This is not for forward classes }
  498. if (tt.def.deftype=objectdef) then
  499. with Tobjectdef(tt.def) do
  500. begin
  501. if not(oo_is_forward in objectoptions) then
  502. begin
  503. ch:=tclassheader.create(tobjectdef(tt.def));
  504. { generate and check virtual methods, must be done
  505. before RTTI is written }
  506. ch.genvmt;
  507. { Generate RTTI for class }
  508. generate_rtti(newtype);
  509. if is_interface(tobjectdef(tt.def)) then
  510. ch.writeinterfaceids;
  511. if (oo_has_vmt in objectoptions) then
  512. ch.writevmt;
  513. ch.free;
  514. end;
  515. end
  516. else
  517. begin
  518. { Always generate RTTI info for all types. This is to have typeinfo() return
  519. the same pointer }
  520. generate_rtti(newtype);
  521. end;
  522. aktfilepos:=oldfilepos;
  523. end;
  524. until token<>_ID;
  525. typecanbeforward:=false;
  526. symtablestack.foreach_static(@resolve_type_forward,nil);
  527. block_type:=old_block_type;
  528. end;
  529. procedure var_dec;
  530. { parses variable declarations and inserts them in }
  531. { the top symbol table of symtablestack }
  532. begin
  533. consume(_VAR);
  534. read_var_decs(false,false,false);
  535. end;
  536. procedure property_dec;
  537. var
  538. old_block_type : tblock_type;
  539. begin
  540. consume(_PROPERTY);
  541. if not(symtablestack.symtabletype in [staticsymtable,globalsymtable]) then
  542. message(parser_e_resourcestring_only_sg);
  543. old_block_type:=block_type;
  544. block_type:=bt_const;
  545. repeat
  546. read_property_dec(nil);
  547. consume(_SEMICOLON);
  548. until token<>_ID;
  549. block_type:=old_block_type;
  550. end;
  551. procedure threadvar_dec;
  552. { parses thread variable declarations and inserts them in }
  553. { the top symbol table of symtablestack }
  554. begin
  555. consume(_THREADVAR);
  556. if not(symtablestack.symtabletype in [staticsymtable,globalsymtable]) then
  557. message(parser_e_threadvars_only_sg);
  558. read_var_decs(false,false,true);
  559. end;
  560. procedure resourcestring_dec;
  561. var
  562. orgname : stringid;
  563. p : tnode;
  564. dummysymoptions : tsymoptions;
  565. storetokenpos,filepos : tfileposinfo;
  566. old_block_type : tblock_type;
  567. sp : pchar;
  568. sym : tsym;
  569. begin
  570. consume(_RESOURCESTRING);
  571. if not(symtablestack.symtabletype in [staticsymtable,globalsymtable]) then
  572. message(parser_e_resourcestring_only_sg);
  573. old_block_type:=block_type;
  574. block_type:=bt_const;
  575. repeat
  576. orgname:=orgpattern;
  577. filepos:=akttokenpos;
  578. consume(_ID);
  579. case token of
  580. _EQUAL:
  581. begin
  582. consume(_EQUAL);
  583. p:=comp_expr(true);
  584. storetokenpos:=akttokenpos;
  585. akttokenpos:=filepos;
  586. sym:=nil;
  587. case p.nodetype of
  588. ordconstn:
  589. begin
  590. if is_constcharnode(p) then
  591. begin
  592. getmem(sp,2);
  593. sp[0]:=chr(tordconstnode(p).value);
  594. sp[1]:=#0;
  595. sym:=tconstsym.create_string(orgname,constresourcestring,sp,1);
  596. end
  597. else
  598. Message(parser_e_illegal_expression);
  599. end;
  600. stringconstn:
  601. with Tstringconstnode(p) do
  602. begin
  603. getmem(sp,len+1);
  604. move(value_str^,sp^,len+1);
  605. sym:=tconstsym.create_string(orgname,constresourcestring,sp,len);
  606. end;
  607. else
  608. Message(parser_e_illegal_expression);
  609. end;
  610. akttokenpos:=storetokenpos;
  611. { Support hint directives }
  612. dummysymoptions:=[];
  613. try_consume_hintdirective(dummysymoptions);
  614. if assigned(sym) then
  615. begin
  616. sym.symoptions:=sym.symoptions+dummysymoptions;
  617. symtablestack.insert(sym);
  618. end;
  619. consume(_SEMICOLON);
  620. p.free;
  621. end;
  622. else consume(_EQUAL);
  623. end;
  624. until token<>_ID;
  625. block_type:=old_block_type;
  626. end;
  627. end.