pbase.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Contains some helper routines for the parser
  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 pbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. tokens,globtype,
  23. symconst,symbase,symtype,symdef,symsym,symtable
  24. ;
  25. const
  26. { tokens that end a block or statement. And don't require
  27. a ; on the statement before }
  28. endtokens = [_SEMICOLON,_END,_ELSE,_UNTIL,_EXCEPT,_FINALLY];
  29. { true, if we are after an assignement }
  30. afterassignment : boolean = false;
  31. { true, if we are parsing arguments }
  32. in_args : boolean = false;
  33. { true, if we are parsing arguments allowing named parameters }
  34. named_args_allowed : boolean = false;
  35. { true, if we got an @ to get the address }
  36. got_addrn : boolean = false;
  37. { special for handling procedure vars }
  38. getprocvardef : tprocvardef = nil;
  39. { special for function reference vars }
  40. getfuncrefdef : tobjectdef = nil;
  41. var
  42. { for operators }
  43. optoken : ttoken;
  44. { true, if only routine headers should be parsed }
  45. parse_only : boolean;
  46. { true, if we found a name for a named arg }
  47. found_arg_name : boolean;
  48. { true, if we are parsing generic declaration }
  49. parse_generic : boolean;
  50. procedure identifier_not_found(const s:string);
  51. procedure identifier_not_found(const s:string;const filepos:tfileposinfo);
  52. { function tokenstring(i : ttoken):string;}
  53. { consumes token i, if the current token is unequal i }
  54. { a syntax error is written }
  55. procedure consume(i : ttoken);
  56. {Tries to consume the token i, and returns true if it was consumed:
  57. if token=i.}
  58. function try_to_consume(i:Ttoken):boolean;
  59. { consumes all tokens til atoken (for error recovering }
  60. procedure consume_all_until(atoken : ttoken);
  61. { consumes tokens while they are semicolons }
  62. procedure consume_emptystats;
  63. { reads a list of identifiers into a string list }
  64. { consume a symbol, if not found give an error and
  65. and return an errorsym }
  66. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  67. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  68. type
  69. tconsume_unitsym_flag = (
  70. cuf_consume_id,
  71. cuf_allow_specialize,
  72. cuf_check_attr_suffix
  73. );
  74. tconsume_unitsym_flags = set of tconsume_unitsym_flag;
  75. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;out is_specialize:boolean;sympattern:TSymStr):boolean;
  76. function try_consume_unitsym_no_specialize(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;sympattern:TSymStr):boolean;
  77. function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
  78. { just for an accurate position of the end of a procedure (PM) }
  79. var
  80. last_endtoken_filepos: tfileposinfo;
  81. implementation
  82. uses
  83. globals,scanner,verbose,fmodule;
  84. {****************************************************************************
  85. Token Parsing
  86. ****************************************************************************}
  87. procedure identifier_not_found(const s:string);
  88. begin
  89. Message1(sym_e_id_not_found,s);
  90. { show a fatal that you need -S2 or -Sd, but only
  91. if we just parsed the a token that has m_class }
  92. if not(m_class in current_settings.modeswitches) and
  93. (Upper(s)=pattern) and
  94. (m_class in tokeninfo^[idtoken].keyword) then
  95. Message(parser_f_need_objfpc_or_delphi_mode);
  96. end;
  97. procedure identifier_not_found(const s:string;const filepos:tfileposinfo);
  98. begin
  99. MessagePos1(filepos,sym_e_id_not_found,s);
  100. { show a fatal that you need -S2 or -Sd, but only
  101. if we just parsed the a token that has m_class }
  102. if not(m_class in current_settings.modeswitches) and
  103. (Upper(s)=pattern) and
  104. (m_class in tokeninfo^[idtoken].keyword) then
  105. MessagePos(filepos,parser_f_need_objfpc_or_delphi_mode);
  106. end;
  107. { consumes token i, write error if token is different }
  108. procedure consume(i : ttoken);
  109. begin
  110. if (token<>i) and (idtoken<>i) then
  111. if token=_id then
  112. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  113. else
  114. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  115. else
  116. begin
  117. if token=_END then
  118. last_endtoken_filepos:=current_tokenpos;
  119. current_scanner.readtoken(true);
  120. end;
  121. end;
  122. function try_to_consume(i:Ttoken):boolean;
  123. begin
  124. try_to_consume:=false;
  125. if (token=i) or (idtoken=i) then
  126. begin
  127. try_to_consume:=true;
  128. if token=_END then
  129. last_endtoken_filepos:=current_tokenpos;
  130. current_scanner.readtoken(true);
  131. end;
  132. end;
  133. procedure consume_all_until(atoken : ttoken);
  134. begin
  135. while (token<>atoken) and (idtoken<>atoken) do
  136. begin
  137. Consume(token);
  138. if token=_EOF then
  139. begin
  140. Consume(atoken);
  141. Message(scan_f_end_of_file);
  142. exit;
  143. end;
  144. end;
  145. end;
  146. procedure consume_emptystats;
  147. begin
  148. repeat
  149. until not try_to_consume(_SEMICOLON);
  150. end;
  151. { check if a symbol contains the hint directive, and if so gives out a hint
  152. if required.
  153. If this code is changed, it's likly that consume_sym_orgid and factor_read_id
  154. must be changed as well (FK)
  155. }
  156. function consume_sym(var srsym:tsym;var srsymtable:TSymtable):boolean;
  157. var
  158. t : ttoken;
  159. begin
  160. { first check for identifier }
  161. if token<>_ID then
  162. begin
  163. consume(_ID);
  164. srsym:=generrorsym;
  165. srsymtable:=nil;
  166. result:=false;
  167. exit;
  168. end;
  169. searchsym(pattern,srsym,srsymtable);
  170. { handle unit specification like System.Writeln }
  171. try_consume_unitsym_no_specialize(srsym,srsymtable,t,[cuf_consume_id],pattern);
  172. { if nothing found give error and return errorsym }
  173. if assigned(srsym) then
  174. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  175. else
  176. begin
  177. identifier_not_found(orgpattern);
  178. srsym:=generrorsym;
  179. srsymtable:=nil;
  180. end;
  181. consume(t);
  182. result:=assigned(srsym);
  183. end;
  184. { check if a symbol contains the hint directive, and if so gives out a hint
  185. if required and returns the id with it's original casing
  186. }
  187. function consume_sym_orgid(var srsym:tsym;var srsymtable:TSymtable;var s : string):boolean;
  188. var
  189. t : ttoken;
  190. begin
  191. { first check for identifier }
  192. if token<>_ID then
  193. begin
  194. consume(_ID);
  195. srsym:=generrorsym;
  196. srsymtable:=nil;
  197. result:=false;
  198. exit;
  199. end;
  200. searchsym(pattern,srsym,srsymtable);
  201. { handle unit specification like System.Writeln }
  202. try_consume_unitsym_no_specialize(srsym,srsymtable,t,[cuf_consume_id],pattern);
  203. { if nothing found give error and return errorsym }
  204. if assigned(srsym) then
  205. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  206. else
  207. begin
  208. identifier_not_found(orgpattern);
  209. srsym:=generrorsym;
  210. srsymtable:=nil;
  211. end;
  212. s:=orgpattern;
  213. consume(t);
  214. result:=assigned(srsym);
  215. end;
  216. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;out is_specialize:boolean;sympattern:TSymStr):boolean;
  217. var
  218. hmodule: tmodule;
  219. ns:ansistring;
  220. nssym:tsym;
  221. nsitem : TCmdStrListItem;
  222. procedure consume_namespace;
  223. begin
  224. while assigned(srsym) and (srsym.typ=namespacesym) do
  225. begin
  226. { we have a namespace. the next identifier should be either a namespace or a unit }
  227. searchsym_in_module(hmodule,ns+'.'+pattern,srsym,srsymtable);
  228. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  229. begin
  230. ns:=ns+'.'+pattern;
  231. nssym:=srsym;
  232. consume(_ID);
  233. consume(_POINT);
  234. end;
  235. end;
  236. { check if there is a hidden unit with this pattern in the namespace }
  237. if not assigned(srsym) and
  238. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  239. srsym:=tnamespacesym(nssym).unitsym;
  240. end;
  241. begin
  242. result:=false;
  243. tokentoconsume:=_ID;
  244. is_specialize:=false;
  245. if not assigned(srsym) and (pattern<>'') and (namespacelist.count>0) then
  246. begin
  247. hmodule:=get_module(current_filepos.moduleindex);
  248. if not assigned(hmodule) then
  249. internalerror(2018050301);
  250. nsitem:=TCmdStrListItem(namespacelist.first);
  251. while assigned(nsitem) do
  252. begin
  253. ns:=upper(nsitem.str)+'.'+sympattern;
  254. if searchsym_in_module(hmodule,ns,srsym,srsymtable) and
  255. (srsym.typ in [unitsym,namespacesym]) then
  256. break;
  257. nsitem:=TCmdStrListItem(nsitem.next);
  258. end;
  259. end;
  260. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  261. begin
  262. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  263. internalerror(2005011503);
  264. { only allow unit.symbol access if the name was
  265. found in the current module
  266. we can use iscurrentunit because generic specializations does not
  267. change current_unit variable }
  268. hmodule:=find_module_from_symtable(srsym.Owner);
  269. if not Assigned(hmodule) then
  270. internalerror(2010011201);
  271. if hmodule.unit_index=current_filepos.moduleindex then
  272. begin
  273. if cuf_consume_id in flags then
  274. consume(_ID);
  275. consume(_POINT);
  276. if srsym.typ=namespacesym then
  277. begin
  278. ns:=srsym.name;
  279. nssym:=srsym;
  280. consume_namespace;
  281. if not assigned(srsym) and (namespacelist.count>0) then
  282. begin
  283. nsitem:=TCmdStrListItem(namespacelist.first);
  284. while assigned(nsitem) do
  285. begin
  286. ns:=upper(nsitem.str)+'.'+nssym.name;
  287. if searchsym_in_module(hmodule,ns,srsym,srsymtable) and
  288. (srsym.typ in [unitsym,namespacesym]) then
  289. begin
  290. consume_namespace;
  291. break;
  292. end;
  293. nsitem:=TCmdStrListItem(nsitem.next);
  294. end;
  295. end;
  296. if assigned(srsym) and (srsym.typ<>unitsym) then
  297. internalerror(2011082601);
  298. if not assigned(srsym) then
  299. begin
  300. result:=true;
  301. srsymtable:=nil;
  302. exit;
  303. end;
  304. end;
  305. case token of
  306. _ID:
  307. begin
  308. if cuf_check_attr_suffix in flags then
  309. begin
  310. if searchsym_in_module(tunitsym(srsym).module,pattern+custom_attribute_suffix,srsym,srsymtable) then
  311. exit(true);
  312. end;
  313. { system.char? (char=widechar comes from the implicit
  314. uachar/uuchar unit -> override) }
  315. if (pattern='CHAR') and
  316. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  317. begin
  318. if m_default_unicodestring in current_settings.modeswitches then
  319. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  320. else
  321. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  322. end
  323. else
  324. if (cuf_allow_specialize in flags) and (idtoken=_SPECIALIZE) then
  325. begin
  326. consume(_ID);
  327. is_specialize:=true;
  328. if token=_ID then
  329. begin
  330. if (cuf_check_attr_suffix in flags) and
  331. searchsym_in_module(tunitsym(srsym).module,pattern+custom_attribute_suffix,srsym,srsymtable) then
  332. exit(true);
  333. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  334. end;
  335. end
  336. else
  337. searchsym_in_module(tunitsym(srsym).module,pattern,srsym,srsymtable);
  338. end;
  339. _STRING:
  340. begin
  341. if cs_compilesystem in current_settings.moduleswitches then
  342. Message(parser_e_nostringaliasinsystem);
  343. { system.string? }
  344. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  345. begin
  346. if cs_refcountedstrings in current_settings.localswitches then
  347. begin
  348. if m_default_unicodestring in current_settings.modeswitches then
  349. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  350. else
  351. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  352. end
  353. else
  354. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  355. tokentoconsume:=_STRING;
  356. end;
  357. end
  358. else
  359. ;
  360. end;
  361. end
  362. else
  363. begin
  364. srsym:=nil;
  365. srsymtable:=nil;
  366. end;
  367. result:=true;
  368. end;
  369. end;
  370. function try_consume_unitsym_no_specialize(var srsym:tsym;var srsymtable:TSymtable;var tokentoconsume:ttoken;flags:tconsume_unitsym_flags;sympattern:TSymStr):boolean;
  371. var
  372. dummy: Boolean;
  373. begin
  374. exclude(flags,cuf_allow_specialize);
  375. result:=try_consume_unitsym(srsym,srsymtable,tokentoconsume,flags,dummy,sympattern);
  376. end;
  377. function try_consume_hintdirective(var symopt:tsymoptions; var deprecatedmsg:pshortstring):boolean;
  378. var
  379. last_is_deprecated:boolean;
  380. begin
  381. try_consume_hintdirective:=false;
  382. if not(m_hintdirective in current_settings.modeswitches) then
  383. exit;
  384. repeat
  385. last_is_deprecated:=false;
  386. case idtoken of
  387. _LIBRARY:
  388. begin
  389. if sp_hint_library in symopt then
  390. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  391. else
  392. include(symopt,sp_hint_library);
  393. try_consume_hintdirective:=true;
  394. end;
  395. _DEPRECATED:
  396. begin
  397. if sp_hint_deprecated in symopt then
  398. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  399. else
  400. include(symopt,sp_hint_deprecated);
  401. try_consume_hintdirective:=true;
  402. last_is_deprecated:=true;
  403. end;
  404. _EXPERIMENTAL:
  405. begin
  406. if sp_hint_experimental in symopt then
  407. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  408. else
  409. include(symopt,sp_hint_experimental);
  410. try_consume_hintdirective:=true;
  411. end;
  412. _PLATFORM:
  413. begin
  414. if sp_hint_platform in symopt then
  415. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  416. else
  417. include(symopt,sp_hint_platform);
  418. try_consume_hintdirective:=true;
  419. end;
  420. _UNIMPLEMENTED:
  421. begin
  422. if sp_hint_unimplemented in symopt then
  423. Message1(parser_e_dir_not_allowed,arraytokeninfo[idtoken].str)
  424. else
  425. include(symopt,sp_hint_unimplemented);
  426. try_consume_hintdirective:=true;
  427. end;
  428. else
  429. break;
  430. end;
  431. consume(Token);
  432. { handle deprecated message }
  433. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  434. begin
  435. if not assigned(deprecatedmsg) then
  436. begin
  437. if token=_CSTRING then
  438. deprecatedmsg:=stringdup(cstringpattern)
  439. else
  440. deprecatedmsg:=stringdup(pattern);
  441. end;
  442. consume(token);
  443. include(symopt,sp_has_deprecated_msg);
  444. end;
  445. until false;
  446. end;
  447. end.