scandir.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Peter Vreman
  4. This unit implements directive parsing for the scanner
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. const
  19. directivelen=16;
  20. type
  21. directivestr=string[directivelen];
  22. tdirectivetoken=(
  23. _DIR_NONE,
  24. _DIR_ALIGN,_DIR_APPTYPE,_DIR_ASMMODE,_DIR_ASSERTIONS,
  25. _DIR_BOOLEVAL,
  26. _DIR_D,_DIR_DEBUGINFO,_DIR_DEFINE,_DIR_DESCRIPTION,
  27. _DIR_ELSE,_DIR_ENDIF,_DIR_ERROR,_DIR_EXTENDEDSYNTAX,
  28. _DIR_FATAL,
  29. _DIR_HINT,_DIR_HINTS,
  30. _DIR_I,_DIR_I386_ATT,_DIR_I386_DIRECT,_DIR_I386_INTEL,_DIR_IOCHECKS,
  31. _DIR_IF,_DIR_IFDEF,_DIR_IFNDEF,_DIR_IFOPT,_DIR_INCLUDE,_DIR_INFO,
  32. _DIR_L,_DIR_LINK,_DIR_LINKLIB,_DIR_LOCALSYMBOLS,_DIR_LONGSTRINGS,
  33. _DIR_M,_DIR_MEMORY,_DIR_MESSAGE,_DIR_MINENUMSIZE,_DIR_MMX,_DIR_MODE,
  34. _DIR_NOTE,_DIR_NOTES,
  35. _DIR_OPENSTRINGS,_DIR_OUTPUT_FORMAT,_DIR_OVERFLOWCHECKS,
  36. _DIR_PACKENUM,_DIR_PACKRECORDS,
  37. _DIR_R,_DIR_RANGECHECKS,_DIR_REFERENCEINFO,
  38. _DIR_SATURATION,_DIR_SMARTLINK,_DIR_STACKFRAMES,_DIR_STOP,
  39. _DIR_TYPEDADDRESS,_DIR_TYPEINFO,
  40. _DIR_UNDEF,
  41. _DIR_VARSTRINGCHECKS,
  42. _DIR_WAIT,_DIR_WARNING,_DIR_WARNINGS,
  43. _DIR_Z1,_DIR_Z2,_DIR_Z4
  44. );
  45. const
  46. firstdirective=_DIR_NONE;
  47. lastdirective=_DIR_Z4;
  48. directive:array[tdirectivetoken] of directivestr=(
  49. '',
  50. 'ALIGN','APPTYPE','ASMMODE','ASSERTIONS',
  51. 'BOOLEVAL',
  52. 'D','DEBUGINFO','DEFINE','DESCRIPTION',
  53. 'ELSE','ENDIF','ERROR','EXTENDEDSYNTAX',
  54. 'FATAL',
  55. 'HINT','HINTS',
  56. 'I','I386_ATT','I386_DIRECT','I386_INTEL','IOCHECKS',
  57. 'IF','IFDEF','IFNDEF','IFOPT','INCLUDE','INFO',
  58. 'L','LINK','LINKLIB','LOCALSYMBOLS','LONGSTRINGS',
  59. 'M','MEMORY','MESSAGE','MINENUMSIZE','MMX','MODE',
  60. 'NOTE','NOTES',
  61. 'OPENSTRINGS','OUTPUT_FORMAT','OVERFLOWCHECKS',
  62. 'PACKENUM','PACKRECORDS',
  63. 'R','RANGECHECKS','REFERENCEINFO',
  64. 'SATURATION','SMARTLINK','STACKFRAMES','STOP',
  65. 'TYPEDADDRESS','TYPEINFO',
  66. 'UNDEF',
  67. 'VARSTRINGCHECKS',
  68. 'WAIT','WARNING','WARNINGS',
  69. 'Z1','Z2','Z4'
  70. );
  71. function Get_Directive(const hs:string):tdirectivetoken;
  72. var
  73. i : tdirectivetoken;
  74. begin
  75. for i:=firstdirective to lastdirective do
  76. if directive[i]=hs then
  77. begin
  78. Get_Directive:=i;
  79. exit;
  80. end;
  81. Get_Directive:=_DIR_NONE;
  82. end;
  83. {-------------------------------------------
  84. IF Conditional Handling
  85. -------------------------------------------}
  86. var
  87. preprocpat : string;
  88. preproc_token : ttoken;
  89. procedure preproc_consume(t : ttoken);
  90. begin
  91. if t<>preproc_token then
  92. Message(scan_e_preproc_syntax_error);
  93. preproc_token:=current_scanner^.readpreproc;
  94. end;
  95. function read_expr : string;forward;
  96. function read_factor : string;
  97. var
  98. hs : string;
  99. mac : pmacrosym;
  100. len : byte;
  101. begin
  102. if preproc_token=ID then
  103. begin
  104. if preprocpat='NOT' then
  105. begin
  106. preproc_consume(ID);
  107. hs:=read_expr;
  108. if hs='0' then
  109. read_factor:='1'
  110. else
  111. read_factor:='0';
  112. end
  113. else
  114. begin
  115. mac:=pmacrosym(macros^.search(hs));
  116. hs:=preprocpat;
  117. preproc_consume(ID);
  118. if assigned(mac) then
  119. begin
  120. if mac^.defined and assigned(mac^.buftext) then
  121. begin
  122. if mac^.buflen>255 then
  123. begin
  124. len:=255;
  125. Message(scan_w_marco_cut_after_255_chars);
  126. end
  127. else
  128. len:=mac^.buflen;
  129. {$ifndef TP}
  130. {$ifopt H+}
  131. setlength(hs,len);
  132. {$else}
  133. hs[0]:=char(len);
  134. {$endif}
  135. {$else}
  136. hs[0]:=char(len);
  137. {$endif}
  138. move(mac^.buftext^,hs[1],len);
  139. end
  140. else
  141. read_factor:='';
  142. end
  143. else
  144. read_factor:=hs;
  145. end
  146. end
  147. else if preproc_token=LKLAMMER then
  148. begin
  149. preproc_consume(LKLAMMER);
  150. read_factor:=read_expr;
  151. preproc_consume(RKLAMMER);
  152. end
  153. else
  154. Message(scan_e_error_in_preproc_expr);
  155. end;
  156. function read_term : string;
  157. var
  158. hs1,hs2 : string;
  159. begin
  160. hs1:=read_factor;
  161. while true do
  162. begin
  163. if (preproc_token=ID) then
  164. begin
  165. if preprocpat='AND' then
  166. begin
  167. preproc_consume(ID);
  168. hs2:=read_factor;
  169. if (hs1<>'0') and (hs2<>'0') then
  170. hs1:='1';
  171. end
  172. else
  173. break;
  174. end
  175. else
  176. break;
  177. end;
  178. read_term:=hs1;
  179. end;
  180. function read_simple_expr : string;
  181. var
  182. hs1,hs2 : string;
  183. begin
  184. hs1:=read_term;
  185. while true do
  186. begin
  187. if (preproc_token=ID) then
  188. begin
  189. if preprocpat='OR' then
  190. begin
  191. preproc_consume(ID);
  192. hs2:=read_term;
  193. if (hs1<>'0') or (hs2<>'0') then
  194. hs1:='1';
  195. end
  196. else
  197. break;
  198. end
  199. else
  200. break;
  201. end;
  202. read_simple_expr:=hs1;
  203. end;
  204. function read_expr : string;
  205. var
  206. hs1,hs2 : string;
  207. b : boolean;
  208. t : ttoken;
  209. w : integer;
  210. l1,l2 : longint;
  211. begin
  212. hs1:=read_simple_expr;
  213. t:=preproc_token;
  214. if not(t in [EQUAL,UNEQUAL,LT,GT,LTE,GTE]) then
  215. begin
  216. read_expr:=hs1;
  217. exit;
  218. end;
  219. preproc_consume(t);
  220. hs2:=read_simple_expr;
  221. if is_number(hs1) and is_number(hs2) then
  222. begin
  223. valint(hs1,l1,w);
  224. valint(hs2,l2,w);
  225. case t of
  226. EQUAL : b:=l1=l2;
  227. UNEQUAL : b:=l1<>l2;
  228. LT : b:=l1<l2;
  229. GT : b:=l1>l2;
  230. GTE : b:=l1>=l2;
  231. LTE : b:=l1<=l2;
  232. end;
  233. end
  234. else
  235. begin
  236. case t of
  237. EQUAL : b:=hs1=hs2;
  238. UNEQUAL : b:=hs1<>hs2;
  239. LT : b:=hs1<hs2;
  240. GT : b:=hs1>hs2;
  241. GTE : b:=hs1>=hs2;
  242. LTE : b:=hs1<=hs2;
  243. end;
  244. end;
  245. if b then
  246. read_expr:='1'
  247. else
  248. read_expr:='0';
  249. end;
  250. {-------------------------------------------
  251. Directives
  252. -------------------------------------------}
  253. function is_conditional(t:tdirectivetoken):boolean;
  254. begin
  255. is_conditional:=(t in [_DIR_ENDIF,_DIR_IFDEF,_DIR_IFNDEF,_DIR_IFOPT,_DIR_IF,_DIR_ELSE]);
  256. end;
  257. procedure dir_conditional(t:tdirectivetoken);
  258. var
  259. hs : string;
  260. mac : pmacrosym;
  261. found : boolean;
  262. state : char;
  263. oldaktfilepos : tfileposinfo;
  264. begin
  265. oldaktfilepos:=aktfilepos;
  266. while true do
  267. begin
  268. current_scanner^.gettokenpos;
  269. case t of
  270. _DIR_ENDIF : begin
  271. current_scanner^.poppreprocstack;
  272. end;
  273. _DIR_ELSE : begin
  274. current_scanner^.elsepreprocstack;
  275. end;
  276. _DIR_IFDEF : begin
  277. current_scanner^.skipspace;
  278. hs:=current_scanner^.readid;
  279. mac:=pmacrosym(macros^.search(hs));
  280. current_scanner^.addpreprocstack(assigned(mac) and mac^.defined,hs,scan_c_ifdef_found);
  281. end;
  282. _DIR_IFOPT : begin
  283. current_scanner^.skipspace;
  284. hs:=current_scanner^.readid;
  285. if (length(hs)>1) then
  286. Message(scan_w_illegal_switch)
  287. else
  288. begin
  289. state:=current_scanner^.ReadState;
  290. if state in ['-','+'] then
  291. found:=CheckSwitch(hs[1],state);
  292. end;
  293. current_scanner^.addpreprocstack(found,hs,scan_c_ifopt_found);
  294. end;
  295. _DIR_IF : begin
  296. current_scanner^.skipspace;
  297. { start preproc expression scanner }
  298. preproc_token:=current_scanner^.readpreproc;
  299. hs:=read_expr;
  300. current_scanner^.addpreprocstack(hs<>'0',hs,scan_c_if_found);
  301. end;
  302. _DIR_IFNDEF : begin
  303. current_scanner^.skipspace;
  304. hs:=current_scanner^.readid;
  305. mac:=pmacrosym(macros^.search(hs));
  306. current_scanner^.addpreprocstack(not(assigned(mac) and mac^.defined),hs,scan_c_ifndef_found);
  307. end;
  308. end;
  309. { accept the text ? }
  310. if (current_scanner^.preprocstack=nil) or current_scanner^.preprocstack^.accept then
  311. break
  312. else
  313. begin
  314. current_scanner^.gettokenpos;
  315. Message(scan_c_skipping_until);
  316. repeat
  317. current_scanner^.skipuntildirective;
  318. t:=Get_Directive(current_scanner^.readid);
  319. until is_conditional(t);
  320. current_scanner^.gettokenpos;
  321. Message1(scan_d_handling_switch,'$'+directive[t]);
  322. end;
  323. end;
  324. oldaktfilepos:=aktfilepos;
  325. end;
  326. procedure dir_define(t:tdirectivetoken);
  327. var
  328. hs : string;
  329. mac : pmacrosym;
  330. macropos : longint;
  331. macrobuffer : pmacrobuffer;
  332. begin
  333. current_scanner^.skipspace;
  334. hs:=current_scanner^.readid;
  335. mac:=pmacrosym(macros^.search(hs));
  336. if not assigned(mac) then
  337. begin
  338. mac:=new(pmacrosym,init(hs));
  339. mac^.defined:=true;
  340. Message1(parser_m_macro_defined,mac^.name);
  341. macros^.insert(mac);
  342. end
  343. else
  344. begin
  345. Message1(parser_m_macro_defined,mac^.name);
  346. mac^.defined:=true;
  347. { delete old definition }
  348. if assigned(mac^.buftext) then
  349. begin
  350. freemem(mac^.buftext,mac^.buflen);
  351. mac^.buftext:=nil;
  352. end;
  353. end;
  354. if (cs_support_macro in aktmoduleswitches) then
  355. begin
  356. { key words are never substituted }
  357. if is_keyword(hs) then
  358. Message(scan_e_keyword_cant_be_a_macro);
  359. { !!!!!! handle macro params, need we this? }
  360. current_scanner^.skipspace;
  361. { may be a macro? }
  362. if c=':' then
  363. begin
  364. current_scanner^.readchar;
  365. if c='=' then
  366. begin
  367. new(macrobuffer);
  368. macropos:=0;
  369. { first char }
  370. current_scanner^.readchar;
  371. while (c<>'}') do
  372. begin
  373. macrobuffer^[macropos]:=c;
  374. current_scanner^.readchar;
  375. if c=#26 then Message(scan_f_end_of_file);
  376. inc(macropos);
  377. if macropos>maxmacrolen then
  378. Message(scan_f_macro_buffer_overflow);
  379. end;
  380. { free buffer of macro ?}
  381. if assigned(mac^.buftext) then
  382. freemem(mac^.buftext,mac^.buflen);
  383. { get new mem }
  384. getmem(mac^.buftext,macropos);
  385. mac^.buflen:=macropos;
  386. { copy the text }
  387. move(macrobuffer^,mac^.buftext^,macropos);
  388. dispose(macrobuffer);
  389. end;
  390. end;
  391. end;
  392. end;
  393. procedure dir_undef(t:tdirectivetoken);
  394. var
  395. hs : string;
  396. mac : pmacrosym;
  397. begin
  398. current_scanner^.skipspace;
  399. hs:=current_scanner^.readid;
  400. mac:=pmacrosym(macros^.search(hs));
  401. if not assigned(mac) then
  402. begin
  403. mac:=new(pmacrosym,init(hs));
  404. Message1(parser_m_macro_undefined,mac^.name);
  405. mac^.defined:=false;
  406. macros^.insert(mac);
  407. end
  408. else
  409. begin
  410. Message1(parser_m_macro_undefined,mac^.name);
  411. mac^.defined:=false;
  412. { delete old definition }
  413. if assigned(mac^.buftext) then
  414. begin
  415. freemem(mac^.buftext,mac^.buflen);
  416. mac^.buftext:=nil;
  417. end;
  418. end;
  419. end;
  420. procedure dir_message(t:tdirectivetoken);
  421. var
  422. w : tmsgconst;
  423. begin
  424. case t of
  425. _DIR_STOP,
  426. _DIR_FATAL : w:=scan_f_user_defined;
  427. _DIR_ERROR : w:=scan_e_user_defined;
  428. _DIR_WARNING : w:=scan_w_user_defined;
  429. _DIR_HINT : w:=scan_h_user_defined;
  430. _DIR_NOTE : w:=scan_n_user_defined;
  431. _DIR_MESSAGE,
  432. _DIR_INFO : w:=scan_i_user_defined;
  433. end;
  434. current_scanner^.skipspace;
  435. Message1(w,current_scanner^.readcomment);
  436. end;
  437. procedure dir_moduleswitch(t:tdirectivetoken);
  438. var
  439. sw : tmoduleswitch;
  440. state : char;
  441. begin
  442. sw:=cs_modulenone;
  443. case t of
  444. _DIR_SMARTLINK : sw:=cs_smartlink;
  445. end;
  446. state:=current_scanner^.readstate;
  447. if (sw<>cs_modulenone) and (state in ['-','+']) then
  448. begin
  449. if state='-' then
  450. aktmoduleswitches:=aktmoduleswitches-[sw]
  451. else
  452. aktmoduleswitches:=aktmoduleswitches+[sw];
  453. end;
  454. end;
  455. procedure dir_localswitch(t:tdirectivetoken);
  456. var
  457. sw : tlocalswitch;
  458. state : char;
  459. begin
  460. sw:=cs_localnone;
  461. {$ifdef SUPPORT_MMX}
  462. case t of
  463. _DIR_MMX : sw:=cs_mmx;
  464. _DIR_SATURATION : sw:=cs_mmx_saturation;
  465. end;
  466. {$endif}
  467. state:=current_scanner^.readstate;
  468. if (sw<>cs_localnone) and (state in ['-','+']) then
  469. begin
  470. if state='-' then
  471. aktlocalswitches:=aktlocalswitches-[sw]
  472. else
  473. aktlocalswitches:=aktlocalswitches+[sw];
  474. end;
  475. end;
  476. procedure dir_include(t:tdirectivetoken);
  477. var
  478. hs : string;
  479. path : dirstr;
  480. name : namestr;
  481. ext : extstr;
  482. hp : pinputfile;
  483. i : longint;
  484. found : boolean;
  485. begin
  486. current_scanner^.skipspace;
  487. hs:=current_scanner^.readcomment;
  488. i:=length(hs);
  489. while (i>0) and (hs[i]=' ') do
  490. dec(i);
  491. Delete(hs,i+1,length(hs)-i);
  492. if hs='' then
  493. exit;
  494. if (hs[1]='%') then
  495. begin
  496. { case insensitive }
  497. hs:=upper(hs);
  498. { save old }
  499. path:=hs;
  500. { remove %'s }
  501. Delete(hs,1,1);
  502. if hs[length(hs)]='%' then
  503. Delete(hs,length(hs),1);
  504. { first check for internal macros }
  505. if hs='TIME' then
  506. hs:=gettimestr
  507. else
  508. if hs='DATE' then
  509. hs:=getdatestr
  510. else
  511. if hs='FILE' then
  512. hs:=current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex)
  513. else
  514. if hs='LINE' then
  515. hs:=tostr(aktfilepos.line)
  516. else
  517. if hs='FPCVERSION' then
  518. hs:=version_string
  519. else
  520. if hs='FPCTARGET' then
  521. hs:=target_cpu_string
  522. else
  523. hs:=getenv(hs);
  524. if hs='' then
  525. Comment(V_Warning,'Include environment '+path+' not found in environment')
  526. else
  527. begin
  528. { make it a stringconst }
  529. hs:=''''+hs+'''';
  530. current_scanner^.insertmacro(@hs[1],length(hs));
  531. end;
  532. end
  533. else
  534. begin
  535. hs:=FixFileName(hs);
  536. fsplit(hs,path,name,ext);
  537. { first look in the path of _d then currentmodule }
  538. path:=search(name+ext,path+';'+current_scanner^.inputfile^.path^+';'+includesearchpath,found);
  539. { shutdown current file }
  540. current_scanner^.tempcloseinputfile;
  541. { load new file }
  542. hp:=new(pinputfile,init(path+name+ext));
  543. current_scanner^.addfile(hp);
  544. if not current_scanner^.openinputfile then
  545. Message1(scan_f_cannot_open_includefile,hs);
  546. Message1(scan_t_start_include_file,current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^);
  547. current_scanner^.reload;
  548. { register for refs }
  549. current_module^.sourcefiles^.register_file(hp);
  550. end;
  551. end;
  552. procedure dir_description(t:tdirectivetoken);
  553. begin
  554. end;
  555. procedure dir_linkobject(t:tdirectivetoken);
  556. var
  557. s : string;
  558. begin
  559. current_scanner^.skipspace;
  560. s:=AddExtension(FixFileName(current_scanner^.readcomment),target_info.objext);
  561. current_module^.linkofiles.insert(s);
  562. end;
  563. procedure dir_resource(t:tdirectivetoken);
  564. var
  565. s : string;
  566. begin
  567. current_scanner^.skipspace;
  568. s:=AddExtension(FixFileName(current_scanner^.readcomment),target_info.resext);
  569. if target_info.res<>res_none then
  570. current_module^.resourcefiles.insert(FixFileName(s))
  571. else
  572. Message(scan_e_resourcefiles_not_supported);
  573. end;
  574. procedure dir_linklib(t:tdirectivetoken);
  575. begin
  576. current_scanner^.skipspace;
  577. current_scanner^.readstring;
  578. current_module^.linkSharedLibs.insert(orgpattern);
  579. end;
  580. procedure dir_outputformat(t:tdirectivetoken);
  581. begin
  582. if not current_module^.in_global then
  583. Message(scan_w_switch_is_global)
  584. else
  585. begin
  586. current_scanner^.skipspace;
  587. if set_string_asm(current_scanner^.readid) then
  588. aktoutputformat:=target_asm.id
  589. else
  590. Message(scan_w_illegal_switch);
  591. end;
  592. end;
  593. procedure dir_mode(t:tdirectivetoken);
  594. begin
  595. if not current_module^.in_global then
  596. Message(scan_w_switch_is_global)
  597. else
  598. begin
  599. current_scanner^.skipspace;
  600. current_scanner^.readstring;
  601. if pattern='DEFAULT' then
  602. aktmodeswitches:=initmodeswitches
  603. else
  604. if pattern='DELPHI' then
  605. aktmodeswitches:=delphimodeswitches
  606. else
  607. if pattern='TP' then
  608. aktmodeswitches:=tpmodeswitches
  609. else
  610. if pattern='FPC' then
  611. aktmodeswitches:=fpcmodeswitches
  612. else
  613. if pattern='OBJFPC' then
  614. aktmodeswitches:=objfpcmodeswitches
  615. else
  616. if pattern='GPC' then
  617. aktmodeswitches:=gpcmodeswitches;
  618. end;
  619. end;
  620. procedure dir_packrecords(t:tdirectivetoken);
  621. var
  622. hs : string;
  623. begin
  624. current_scanner^.skipspace;
  625. if not(c in ['0'..'9']) then
  626. begin
  627. hs:=current_scanner^.readid;
  628. if (hs='NORMAL') or (hs='DEFAULT') then
  629. aktpackrecords:=2
  630. else
  631. Message(scan_w_only_pack_records);
  632. end
  633. else
  634. begin
  635. case current_scanner^.readval of
  636. 1 : aktpackrecords:=1;
  637. 2 : aktpackrecords:=2;
  638. 4 : aktpackrecords:=4;
  639. 8 : aktpackrecords:=8;
  640. 16 : aktpackrecords:=16;
  641. 32 : aktpackrecords:=32;
  642. else
  643. Message(scan_w_only_pack_records);
  644. end;
  645. end;
  646. end;
  647. procedure dir_packenum(t:tdirectivetoken);
  648. var
  649. hs : string;
  650. begin
  651. if t in [_DIR_Z1,_DIR_Z2,_DIR_Z4] then
  652. begin
  653. aktpackenum:=ord(pattern[2])-ord('0');
  654. exit;
  655. end;
  656. current_scanner^.skipspace;
  657. if not(c in ['0'..'9']) then
  658. begin
  659. hs:=current_scanner^.readid;
  660. if (hs='NORMAL') or (hs='DEFAULT') then
  661. aktpackenum:=4
  662. else
  663. Message(scan_w_only_pack_enum);
  664. end
  665. else
  666. begin
  667. case current_scanner^.readval of
  668. 1 : aktpackenum:=1;
  669. 2 : aktpackenum:=2;
  670. 4 : aktpackenum:=4;
  671. else
  672. Message(scan_w_only_pack_enum);
  673. end;
  674. end;
  675. end;
  676. procedure dir_apptype(t:tdirectivetoken);
  677. var
  678. hs : string;
  679. begin
  680. if target_info.target<>target_i386_win32 then
  681. Message(scan_w_app_type_not_support);
  682. if not current_module^.in_global then
  683. Message(scan_w_switch_is_global)
  684. else
  685. begin
  686. current_scanner^.skipspace;
  687. hs:=current_scanner^.readid;
  688. if hs='GUI' then
  689. apptype:=at_gui
  690. else if hs='CONSOLE' then
  691. apptype:=at_cui
  692. else
  693. Message1(scan_w_unsupported_app_type,hs);
  694. end;
  695. end;
  696. procedure dir_wait(t:tdirectivetoken);
  697. var had_info : boolean;
  698. begin
  699. had_info:=(status.verbosity and V_Info)<>0;
  700. { this message should allways appear !! }
  701. status.verbosity:=status.verbosity or V_Info;
  702. Message(scan_i_press_enter);
  703. readln;
  704. If not(had_info) then
  705. status.verbosity:=status.verbosity and (not V_Info);
  706. end;
  707. procedure dir_asmmode(t:tdirectivetoken);
  708. var
  709. s : string;
  710. begin
  711. If Inside_asm_statement then
  712. Message1(scan_w_no_asm_reader_switch_inside_asm,s);
  713. current_scanner^.skipspace;
  714. s:=current_scanner^.readid;
  715. if s='DEFAULT' then
  716. aktasmmode:=initasmmode
  717. else
  718. if not set_string_asmmode(s,aktasmmode) then
  719. Message1(scan_w_unsupported_asmmode_specifier,s);
  720. end;
  721. procedure dir_oldasmmode(t:tdirectivetoken);
  722. begin
  723. If Inside_asm_statement then
  724. Message1(scan_w_no_asm_reader_switch_inside_asm,directive[t]);
  725. {$ifdef i386}
  726. case t of
  727. _DIR_I386_ATT : aktasmmode:=asmmode_i386_att;
  728. _DIR_I386_DIRECT : aktasmmode:=asmmode_i386_direct;
  729. _DIR_I386_INTEL : aktasmmode:=asmmode_i386_intel;
  730. end;
  731. {$endif i386}
  732. end;
  733. procedure dir_delphiswitch(t:tdirectivetoken);
  734. var
  735. sw,state : char;
  736. begin
  737. case t of
  738. _DIR_ALIGN : sw:='A';
  739. _DIR_ASSERTIONS : sw:='C';
  740. _DIR_BOOLEVAL : sw:='B';
  741. _DIR_DEBUGINFO : sw:='D';
  742. _DIR_IOCHECKS : sw:='I';
  743. _DIR_LOCALSYMBOLS : sw:='L';
  744. _DIR_LONGSTRINGS : sw:='H';
  745. _DIR_OPENSTRINGS : sw:='P';
  746. _DIR_OVERFLOWCHECKS : sw:='Q';
  747. _DIR_RANGECHECKS : sw:='R';
  748. _DIR_REFERENCEINFO : sw:='Y';
  749. _DIR_STACKFRAMES : sw:='W';
  750. _DIR_TYPEDADDRESS : sw:='T';
  751. _DIR_TYPEINFO : sw:='M';
  752. _DIR_VARSTRINGCHECKS : sw:='V';
  753. else
  754. exit;
  755. end;
  756. { c contains the next char, a + or - would be fine }
  757. state:=current_scanner^.readstate;
  758. if state in ['-','+'] then
  759. HandleSwitch(sw,state);
  760. end;
  761. procedure dir_memory(t:tdirectivetoken);
  762. var
  763. l : longint;
  764. begin
  765. current_scanner^.skipspace;
  766. l:=current_scanner^.readval;
  767. if l>1024 then
  768. stacksize:=l;
  769. current_scanner^.skipspace;
  770. if c=',' then
  771. begin
  772. current_scanner^.readchar;
  773. current_scanner^.skipspace;
  774. l:=current_scanner^.readval;
  775. if l>1024 then
  776. heapsize:=l;
  777. end;
  778. if c=',' then
  779. begin
  780. current_scanner^.readchar;
  781. current_scanner^.skipspace;
  782. l:=current_scanner^.readval;
  783. if l>1024 then
  784. maxheapsize:=l;
  785. end;
  786. if heapsize>maxheapsize then
  787. message(scan_w_illegal_switch);
  788. end;
  789. procedure dir_setverbose(t:tdirectivetoken);
  790. var
  791. flag,
  792. state : char;
  793. begin
  794. case t of
  795. _DIR_HINTS : flag:='H';
  796. _DIR_WARNINGS : flag:='W';
  797. _DIR_NOTES : flag:='N';
  798. else
  799. exit;
  800. end;
  801. { support ON/OFF }
  802. state:=current_scanner^.ReadState;
  803. SetVerbosity(flag+state);
  804. end;
  805. type
  806. tdirectiveproc=procedure(t:tdirectivetoken);
  807. const
  808. directiveproc:array[tdirectivetoken] of tdirectiveproc=(
  809. {_DIR_NONE} nil,
  810. {_DIR_ALIGN} dir_delphiswitch,
  811. {_DIR_APPTYPE} dir_apptype,
  812. {_DIR_ASMMODE} dir_asmmode,
  813. {_DIR_ASSERTION} dir_delphiswitch,
  814. {_DIR_BOOLEVAL} dir_delphiswitch,
  815. {_DIR_D} dir_description,
  816. {_DIR_DEBUGINFO} dir_delphiswitch,
  817. {_DIR_DEFINE} dir_define,
  818. {_DIR_DESCRIPTION} dir_description,
  819. {_DIR_ELSE} dir_conditional,
  820. {_DIR_ENDIF} dir_conditional,
  821. {_DIR_ERROR} dir_message,
  822. {_DIR_EXTENDEDSYNTAX} dir_delphiswitch,
  823. {_DIR_FATAL} dir_message,
  824. {_DIR_HINT} dir_message,
  825. {_DIR_HINTS} dir_setverbose,
  826. {_DIR_I} dir_include,
  827. {_DIR_I386_ATT} dir_oldasmmode,
  828. {_DIR_I386_DIRECT} dir_oldasmmode,
  829. {_DIR_I386_INTEL} dir_oldasmmode,
  830. {_DIR_IOCHECKS} dir_delphiswitch,
  831. {_DIR_IF} dir_conditional,
  832. {_DIR_IFDEF} dir_conditional,
  833. {_DIR_IFNDEF} dir_conditional,
  834. {_DIR_IFOPT} dir_conditional,
  835. {_DIR_INCLUDE} dir_include,
  836. {_DIR_INFO} dir_message,
  837. {_DIR_L} dir_linkobject,
  838. {_DIR_LINK} dir_linkobject,
  839. {_DIR_LINKLIB} dir_linklib,
  840. {_DIR_LOCALSYMBOLS} dir_delphiswitch,
  841. {_DIR_LONGSTRINGS} dir_delphiswitch,
  842. {_DIR_M} dir_memory,
  843. {_DIR_MEMORY} dir_memory,
  844. {_DIR_MESSAGE} dir_message,
  845. {_DIR_MINENUMSIZE} dir_packenum,
  846. {_DIR_MMX} dir_localswitch,
  847. {_DIR_MODE} dir_mode,
  848. {_DIR_NOTE} dir_message,
  849. {_DIR_NOTES} dir_setverbose,
  850. {_DIR_OPENSTRINGS} dir_delphiswitch,
  851. {_DIR_OUTPUT_FORMAT} dir_outputformat,
  852. {_DIR_OVERFLOWCHECKS} dir_delphiswitch,
  853. {_DIR_PACKENUM} dir_packenum,
  854. {_DIR_PACKRECORDS} dir_packrecords,
  855. {_DIR_R} dir_resource,
  856. {_DIR_RANGECHECKS} dir_delphiswitch,
  857. {_DIR_REFERENCEINFO} dir_delphiswitch,
  858. {_DIR_SATURATION} dir_localswitch,
  859. {_DIR_SMARTLINK} dir_moduleswitch,
  860. {_DIR_STACKFRAMES} dir_delphiswitch,
  861. {_DIR_STOP} dir_message,
  862. {_DIR_TYPEDADDRESS} dir_delphiswitch,
  863. {_DIR_TYPEINFO} dir_delphiswitch,
  864. {_DIR_UNDEF} dir_undef,
  865. {_DIR_VARSTRINGCHECKS} dir_delphiswitch,
  866. {_DIR_WAIT} dir_wait,
  867. {_DIR_WARNING} dir_message,
  868. {_DIR_WARNINGS} dir_setverbose,
  869. {_DIR_Z1} dir_packenum,
  870. {_DIR_Z2} dir_packenum,
  871. {_DIR_Z4} dir_packenum
  872. );
  873. {-------------------------------------------
  874. Main switches handling
  875. -------------------------------------------}
  876. procedure handledirectives;
  877. var
  878. t : tdirectivetoken;
  879. p : tdirectiveproc;
  880. hs : string;
  881. begin
  882. current_scanner^.gettokenpos;
  883. current_scanner^.readchar; {Remove the $}
  884. hs:=current_scanner^.readid;
  885. Message1(scan_d_handling_switch,'$'+hs);
  886. if hs='' then
  887. Message1(scan_w_illegal_switch,'$'+hs);
  888. { Check for compiler switches }
  889. while (length(hs)=1) and (c in ['-','+']) do
  890. begin
  891. HandleSwitch(hs[1],c);
  892. current_scanner^.readchar; {Remove + or -}
  893. if c=',' then
  894. begin
  895. current_scanner^.readchar; {Remove , }
  896. { read next switch, support $v+,$+}
  897. hs:=current_scanner^.readid;
  898. if (hs='') then
  899. begin
  900. if (c='$') and (m_fpc in aktmodeswitches) then
  901. begin
  902. current_scanner^.readchar; { skip $ }
  903. hs:=current_scanner^.readid;
  904. end;
  905. if (hs='') then
  906. Message1(scan_w_illegal_directive,'$'+c);
  907. end
  908. else
  909. Message1(scan_d_handling_switch,'$'+hs);
  910. end
  911. else
  912. hs:='';
  913. end;
  914. { directives may follow switches after a , }
  915. if hs<>'' then
  916. begin
  917. t:=Get_Directive(hs);
  918. if t<>_DIR_NONE then
  919. begin
  920. p:=directiveproc[t];
  921. {$ifdef FPC}
  922. if assigned(p) then
  923. {$else}
  924. if @p<>nil then
  925. {$endif}
  926. p(t);
  927. end
  928. else
  929. Message1(scan_w_illegal_directive,'$'+hs);
  930. { conditionals already read the comment }
  931. if (current_scanner^.comment_level>0) then
  932. current_scanner^.readcomment;
  933. end;
  934. end;
  935. {
  936. $Log$
  937. Revision 1.47 1999-02-22 13:07:05 pierre
  938. + -b and -bl options work !
  939. + cs_local_browser ($L+) is disabled if cs_browser ($Y+)
  940. is not enabled when quitting global section
  941. * local vars and procedures are not yet stored into PPU
  942. Revision 1.46 1999/02/05 08:51:16 pierre
  943. * $L default extension from resext to objext
  944. Revision 1.45 1998/12/28 23:26:25 peter
  945. + resource file handling ($R directive) for Win32
  946. Revision 1.44 1998/12/15 10:23:29 peter
  947. + -iSO, -iSP, -iTO, -iTP
  948. Revision 1.43 1998/11/16 15:41:43 peter
  949. * tp7 didn't like my ifopt H+ :(
  950. Revision 1.42 1998/11/16 12:18:04 peter
  951. * H+ fixes
  952. Revision 1.41 1998/10/22 17:54:08 florian
  953. + switch $APPTYPE for win32 added
  954. Revision 1.40 1998/10/21 20:16:04 peter
  955. * beter line info for conditionals
  956. Revision 1.39 1998/10/13 16:50:21 pierre
  957. * undid some changes of Peter that made the compiler wrong
  958. for m68k (I had to reinsert some ifdefs)
  959. * removed several memory leaks under m68k
  960. * removed the meory leaks for assembler readers
  961. * cross compiling shoud work again better
  962. ( crosscompiling sysamiga works
  963. but as68k still complain about some code !)
  964. Revision 1.38 1998/10/13 13:10:28 peter
  965. * new style for m68k/i386 infos and enums
  966. Revision 1.37 1998/10/08 23:29:04 peter
  967. * -vu shows unit info, -vt shows tried/used files
  968. Revision 1.36 1998/10/08 17:17:29 pierre
  969. * current_module old scanner tagged as invalid if unit is recompiled
  970. + added ppheap for better info on tracegetmem of heaptrc
  971. (adds line column and file index)
  972. * several memory leaks removed ith help of heaptrc !!
  973. Revision 1.35 1998/10/08 13:44:39 peter
  974. * fixed $I %date% not in uppercase
  975. Revision 1.34 1998/09/28 16:57:24 pierre
  976. * changed all length(p^.value_str^) into str_length(p)
  977. to get it work with and without ansistrings
  978. * changed sourcefiles field of tmodule to a pointer
  979. Revision 1.33 1998/09/26 17:45:40 peter
  980. + idtoken and only one token table
  981. Revision 1.32 1998/09/24 23:49:19 peter
  982. + aktmodeswitches
  983. Revision 1.31 1998/09/18 16:03:44 florian
  984. * some changes to compile with Delphi
  985. Revision 1.30 1998/09/16 16:41:47 peter
  986. * merged fixes
  987. Revision 1.28.2.1 1998/09/16 16:09:51 peter
  988. * on/off support also for the local/module switches
  989. Revision 1.29 1998/09/11 09:19:53 daniel
  990. * Removed tabs.
  991. Revision 1.28 1998/09/10 15:25:36 daniel
  992. + Added maxheapsize.
  993. * Corrected semi-bug in calling the assembler and the linker
  994. Revision 1.27 1998/09/09 15:33:59 peter
  995. * removed warnings
  996. Revision 1.26 1998/09/03 11:24:02 peter
  997. * moved more inputfile things from tscannerfile to tinputfile
  998. * changed ifdef Sourceline to cs_asm_source
  999. Revision 1.25 1998/09/02 15:13:31 peter
  1000. * fixed typo in directive table
  1001. Revision 1.24 1998/09/01 12:52:06 peter
  1002. + a lot of delphi switches
  1003. Revision 1.23 1998/08/26 15:35:34 peter
  1004. * fixed scannerfiles for macros
  1005. + $I %<environment>%
  1006. Revision 1.22 1998/08/19 14:57:50 peter
  1007. * small fix for aktfilepos
  1008. Revision 1.20 1998/08/18 15:11:52 peter
  1009. * recompiles again
  1010. Revision 1.19 1998/08/18 09:24:44 pierre
  1011. * small warning position bug fixed
  1012. * support_mmx switches splitting was missing
  1013. * rhide error and warning output corrected
  1014. Revision 1.18 1998/08/10 14:50:25 peter
  1015. + localswitches, moduleswitches, globalswitches splitting
  1016. Revision 1.17 1998/08/10 09:56:04 peter
  1017. * path to the include file is also written to the debug output
  1018. Revision 1.16 1998/08/04 22:03:44 michael
  1019. + fixed dir_include search() call
  1020. Revision 1.15 1998/07/14 21:46:55 peter
  1021. * updated messages file
  1022. Revision 1.14 1998/07/14 14:47:03 peter
  1023. * released NEWINPUT
  1024. Revision 1.13 1998/07/07 12:32:54 peter
  1025. * status.currentsource is now calculated in verbose (more accurated)
  1026. Revision 1.12 1998/07/07 11:20:10 peter
  1027. + NEWINPUT for a better inputfile and scanner object
  1028. Revision 1.11 1998/06/04 23:51:59 peter
  1029. * m68k compiles
  1030. + .def file creation moved to gendef.pas so it could also be used
  1031. for win32
  1032. Revision 1.10 1998/05/30 14:31:10 peter
  1033. + $ASMMODE
  1034. Revision 1.9 1998/05/23 01:21:28 peter
  1035. + aktasmmode, aktoptprocessor, aktoutputformat
  1036. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1037. + $LIBNAME to set the library name where the unit will be put in
  1038. * splitted cgi386 a bit (codeseg to large for bp7)
  1039. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1040. Revision 1.8 1998/05/11 13:07:57 peter
  1041. + $ifdef NEWPPU for the new ppuformat
  1042. + $define GDB not longer required
  1043. * removed all warnings and stripped some log comments
  1044. * no findfirst/findnext anymore to remove smartlink *.o files
  1045. Revision 1.7 1998/05/08 09:21:20 michael
  1046. * Added missing -Fl message to messages file.
  1047. * Corrected mangling of file names when doing Linklib
  1048. * -Fl now actually WORKS.
  1049. * Librarysearchpath is now a field in linker object.
  1050. Revision 1.6 1998/05/04 17:54:28 peter
  1051. + smartlinking works (only case jumptable left todo)
  1052. * redesign of systems.pas to support assemblers and linkers
  1053. + Unitname is now also in the PPU-file, increased version to 14
  1054. Revision 1.5 1998/04/30 15:59:42 pierre
  1055. * GDB works again better :
  1056. correct type info in one pass
  1057. + UseTokenInfo for better source position
  1058. * fixed one remaining bug in scanner for line counts
  1059. * several little fixes
  1060. Revision 1.4 1998/04/29 13:42:27 peter
  1061. + $IOCHECKS and $ALIGN to test already, other will follow soon
  1062. * fixed the wrong linecounting with comments
  1063. Revision 1.3 1998/04/28 11:45:53 florian
  1064. * make it compilable with TP
  1065. + small COM problems solved to compile classes.pp
  1066. Revision 1.2 1998/04/28 10:09:54 pierre
  1067. * typo error in asm style reading corrected
  1068. Revision 1.1 1998/04/27 23:13:53 peter
  1069. + the new files for the scanner
  1070. }