pbase.pas 18 KB

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