pdecl.pas 24 KB

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