2
0

pbase.pas 17 KB

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