scandir.inc 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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=15;
  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_GOTO,
  30. _DIR_HINT,_DIR_HINTS,
  31. _DIR_I,_DIR_I386_ATT,_DIR_I386_DIRECT,_DIR_I386_INTEL,_DIR_IOCHECKS,
  32. _DIR_IF,_DIR_IFDEF,_DIR_IFNDEF,_DIR_IFOPT,_DIR_INCLUDE,_DIR_INCLUDEPATH,
  33. _DIR_INFO,_DIR_INLINE,
  34. _DIR_L,_DIR_LIBRARYPATH,_DIR_LINK,_DIR_LINKLIB,_DIR_LOCALSYMBOLS,
  35. _DIR_LONGSTRINGS,
  36. _DIR_M,_DIR_MACRO,_DIR_MEMORY,_DIR_MESSAGE,_DIR_MINENUMSIZE,_DIR_MMX,_DIR_MODE,
  37. _DIR_NOTE,_DIR_NOTES,
  38. _DIR_OBJECTPATH,_DIR_OPENSTRINGS,_DIR_OUTPUT_FORMAT,_DIR_OVERFLOWCHECKS,
  39. _DIR_PACKENUM,_DIR_PACKRECORDS,
  40. _DIR_R,_DIR_RANGECHECKS,_DIR_REFERENCEINFO,
  41. _DIR_SATURATION,_DIR_SMARTLINK,_DIR_STACKFRAMES,_DIR_STATIC,_DIR_STOP,
  42. _DIR_TYPEDADDRESS,_DIR_TYPEINFO,
  43. _DIR_UNDEF,_DIR_UNITPATH,
  44. _DIR_VARSTRINGCHECKS,
  45. _DIR_WAIT,_DIR_WARNING,_DIR_WARNINGS,
  46. _DIR_Z1,_DIR_Z2,_DIR_Z4
  47. );
  48. const
  49. firstdirective=_DIR_NONE;
  50. lastdirective=_DIR_Z4;
  51. directive:array[tdirectivetoken] of directivestr=(
  52. {12345678901234567890 (To determine longest string.)}
  53. '',
  54. 'ALIGN',
  55. 'APPTYPE',
  56. 'ASMMODE',
  57. 'ASSERTIONS',
  58. 'BOOLEVAL',
  59. 'D',
  60. 'DEBUGINFO',
  61. 'DEFINE',
  62. 'DESCRIPTION',
  63. 'ELSE',
  64. 'ENDIF',
  65. 'ERROR',
  66. 'EXTENDEDSYNTAX',
  67. 'FATAL',
  68. 'GOTO',
  69. 'HINT',
  70. 'HINTS',
  71. 'I',
  72. {12345678901234567890 (To determine longest string.)}
  73. 'I386_ATT',
  74. 'I386_DIRECT',
  75. 'I386_INTEL',
  76. 'IOCHECKS',
  77. 'IF',
  78. 'IFDEF',
  79. 'IFNDEF',
  80. 'IFOPT',
  81. 'INCLUDE',
  82. 'INCLUDEPATH',
  83. 'INFO',
  84. 'INLINE',
  85. 'L',
  86. 'LIBRARYPATH',
  87. 'LINK',
  88. 'LINKLIB',
  89. 'LOCALSYMBOLS',
  90. 'LONGSTRINGS',
  91. 'M',
  92. {12345678901234567890 (To determine longest string.)}
  93. 'MACRO',
  94. 'MEMORY',
  95. 'MESSAGE',
  96. 'MINENUMSIZE',
  97. 'MMX',
  98. 'MODE',
  99. 'NOTE',
  100. 'NOTES',
  101. 'OBJECTPATH',
  102. 'OPENSTRINGS',
  103. 'OUTPUT_FORMAT',
  104. 'OVERFLOWCHECKS',
  105. 'PACKENUM',
  106. 'PACKRECORDS',
  107. 'R',
  108. 'RANGECHECKS',
  109. 'REFERENCEINFO',
  110. 'SATURATION',
  111. 'SMARTLINK',
  112. {12345678901234567890 (To determine longest string.)}
  113. 'STACKFRAMES',
  114. 'STATIC',
  115. 'STOP',
  116. 'TYPEDADDRESS',
  117. 'TYPEINFO',
  118. 'UNDEF',
  119. 'UNITPATH',
  120. 'VARSTRINGCHECKS',
  121. 'WAIT',
  122. 'WARNING',
  123. 'WARNINGS',
  124. 'Z1',
  125. 'Z2',
  126. 'Z4'
  127. );
  128. function Get_Directive(const hs:string):tdirectivetoken;
  129. var
  130. i : tdirectivetoken;
  131. begin
  132. for i:=firstdirective to lastdirective do
  133. if directive[i]=hs then
  134. begin
  135. Get_Directive:=i;
  136. exit;
  137. end;
  138. Get_Directive:=_DIR_NONE;
  139. end;
  140. {-------------------------------------------
  141. IF Conditional Handling
  142. -------------------------------------------}
  143. var
  144. preprocpat : string;
  145. preproc_token : ttoken;
  146. procedure preproc_consume(t : ttoken);
  147. begin
  148. if t<>preproc_token then
  149. Message(scan_e_preproc_syntax_error);
  150. preproc_token:=current_scanner^.readpreproc;
  151. end;
  152. function read_expr : string;forward;
  153. function read_factor : string;
  154. var
  155. hs : string;
  156. mac : pmacrosym;
  157. len : byte;
  158. begin
  159. if preproc_token=_ID then
  160. begin
  161. if preprocpat='NOT' then
  162. begin
  163. preproc_consume(_ID);
  164. hs:=read_expr;
  165. if hs='0' then
  166. read_factor:='1'
  167. else
  168. read_factor:='0';
  169. end
  170. else
  171. begin
  172. mac:=pmacrosym(macros^.search(hs));
  173. hs:=preprocpat;
  174. preproc_consume(_ID);
  175. if assigned(mac) then
  176. begin
  177. if mac^.defined and assigned(mac^.buftext) then
  178. begin
  179. if mac^.buflen>255 then
  180. begin
  181. len:=255;
  182. Message(scan_w_marco_cut_after_255_chars);
  183. end
  184. else
  185. len:=mac^.buflen;
  186. {$ifndef TP}
  187. {$ifopt H+}
  188. setlength(hs,len);
  189. {$else}
  190. hs[0]:=char(len);
  191. {$endif}
  192. {$else}
  193. hs[0]:=char(len);
  194. {$endif}
  195. move(mac^.buftext^,hs[1],len);
  196. end
  197. else
  198. read_factor:='';
  199. end
  200. else
  201. read_factor:=hs;
  202. end
  203. end
  204. else if preproc_token=_LKLAMMER then
  205. begin
  206. preproc_consume(_LKLAMMER);
  207. read_factor:=read_expr;
  208. preproc_consume(_RKLAMMER);
  209. end
  210. else
  211. Message(scan_e_error_in_preproc_expr);
  212. end;
  213. function read_term : string;
  214. var
  215. hs1,hs2 : string;
  216. begin
  217. hs1:=read_factor;
  218. while true do
  219. begin
  220. if (preproc_token=_ID) then
  221. begin
  222. if preprocpat='AND' then
  223. begin
  224. preproc_consume(_ID);
  225. hs2:=read_factor;
  226. if (hs1<>'0') and (hs2<>'0') then
  227. hs1:='1';
  228. end
  229. else
  230. break;
  231. end
  232. else
  233. break;
  234. end;
  235. read_term:=hs1;
  236. end;
  237. function read_simple_expr : string;
  238. var
  239. hs1,hs2 : string;
  240. begin
  241. hs1:=read_term;
  242. while true do
  243. begin
  244. if (preproc_token=_ID) then
  245. begin
  246. if preprocpat='OR' then
  247. begin
  248. preproc_consume(_ID);
  249. hs2:=read_term;
  250. if (hs1<>'0') or (hs2<>'0') then
  251. hs1:='1';
  252. end
  253. else
  254. break;
  255. end
  256. else
  257. break;
  258. end;
  259. read_simple_expr:=hs1;
  260. end;
  261. function read_expr : string;
  262. var
  263. hs1,hs2 : string;
  264. b : boolean;
  265. t : ttoken;
  266. w : integer;
  267. l1,l2 : longint;
  268. begin
  269. hs1:=read_simple_expr;
  270. t:=preproc_token;
  271. if not(t in [_EQUAL,_UNEQUAL,_LT,_GT,_LTE,_GTE]) then
  272. begin
  273. read_expr:=hs1;
  274. exit;
  275. end;
  276. preproc_consume(t);
  277. hs2:=read_simple_expr;
  278. if is_number(hs1) and is_number(hs2) then
  279. begin
  280. valint(hs1,l1,w);
  281. valint(hs2,l2,w);
  282. case t of
  283. _EQUAL : b:=l1=l2;
  284. _UNEQUAL : b:=l1<>l2;
  285. _LT : b:=l1<l2;
  286. _GT : b:=l1>l2;
  287. _GTE : b:=l1>=l2;
  288. _LTE : b:=l1<=l2;
  289. end;
  290. end
  291. else
  292. begin
  293. case t of
  294. _EQUAL : b:=hs1=hs2;
  295. _UNEQUAL : b:=hs1<>hs2;
  296. _LT : b:=hs1<hs2;
  297. _GT : b:=hs1>hs2;
  298. _GTE : b:=hs1>=hs2;
  299. _LTE : b:=hs1<=hs2;
  300. end;
  301. end;
  302. if b then
  303. read_expr:='1'
  304. else
  305. read_expr:='0';
  306. end;
  307. {-------------------------------------------
  308. Directives
  309. -------------------------------------------}
  310. function is_conditional(t:tdirectivetoken):boolean;
  311. begin
  312. is_conditional:=(t in [_DIR_ENDIF,_DIR_IFDEF,_DIR_IFNDEF,_DIR_IFOPT,_DIR_IF,_DIR_ELSE]);
  313. end;
  314. procedure dir_conditional(t:tdirectivetoken);
  315. var
  316. hs : string;
  317. mac : pmacrosym;
  318. found : boolean;
  319. state : char;
  320. oldaktfilepos : tfileposinfo;
  321. begin
  322. oldaktfilepos:=aktfilepos;
  323. while true do
  324. begin
  325. current_scanner^.gettokenpos;
  326. case t of
  327. _DIR_ENDIF : begin
  328. current_scanner^.poppreprocstack;
  329. end;
  330. _DIR_ELSE : begin
  331. current_scanner^.elsepreprocstack;
  332. end;
  333. _DIR_IFDEF : begin
  334. current_scanner^.skipspace;
  335. hs:=current_scanner^.readid;
  336. mac:=pmacrosym(macros^.search(hs));
  337. if assigned(mac) then
  338. mac^.is_used:=true;
  339. current_scanner^.addpreprocstack(pp_ifdef,assigned(mac) and mac^.defined,hs,scan_c_ifdef_found);
  340. end;
  341. _DIR_IFOPT : begin
  342. current_scanner^.skipspace;
  343. hs:=current_scanner^.readid;
  344. if (length(hs)>1) then
  345. Message(scan_w_illegal_switch)
  346. else
  347. begin
  348. state:=current_scanner^.ReadState;
  349. if state in ['-','+'] then
  350. found:=CheckSwitch(hs[1],state);
  351. end;
  352. current_scanner^.addpreprocstack(pp_ifopt,found,hs,scan_c_ifopt_found);
  353. end;
  354. _DIR_IF : begin
  355. current_scanner^.skipspace;
  356. { start preproc expression scanner }
  357. preproc_token:=current_scanner^.readpreproc;
  358. hs:=read_expr;
  359. current_scanner^.addpreprocstack(pp_if,hs<>'0',hs,scan_c_if_found);
  360. end;
  361. _DIR_IFNDEF : begin
  362. current_scanner^.skipspace;
  363. hs:=current_scanner^.readid;
  364. mac:=pmacrosym(macros^.search(hs));
  365. if assigned(mac) then
  366. mac^.is_used:=true;
  367. current_scanner^.addpreprocstack(pp_ifndef,not(assigned(mac) and mac^.defined),hs,scan_c_ifndef_found);
  368. end;
  369. end;
  370. { accept the text ? }
  371. if (current_scanner^.preprocstack=nil) or current_scanner^.preprocstack^.accept then
  372. break
  373. else
  374. begin
  375. current_scanner^.gettokenpos;
  376. Message(scan_c_skipping_until);
  377. repeat
  378. current_scanner^.skipuntildirective;
  379. t:=Get_Directive(current_scanner^.readid);
  380. until is_conditional(t);
  381. current_scanner^.gettokenpos;
  382. Message1(scan_d_handling_switch,'$'+directive[t]);
  383. end;
  384. end;
  385. oldaktfilepos:=aktfilepos;
  386. end;
  387. procedure dir_define(t:tdirectivetoken);
  388. var
  389. hs : string;
  390. bracketcount : longint;
  391. mac : pmacrosym;
  392. macropos : longint;
  393. macrobuffer : pmacrobuffer;
  394. begin
  395. current_scanner^.skipspace;
  396. hs:=current_scanner^.readid;
  397. mac:=pmacrosym(macros^.search(hs));
  398. if not assigned(mac) then
  399. begin
  400. mac:=new(pmacrosym,init(hs));
  401. mac^.defined:=true;
  402. Message1(parser_m_macro_defined,mac^.name);
  403. macros^.insert(mac);
  404. end
  405. else
  406. begin
  407. Message1(parser_m_macro_defined,mac^.name);
  408. mac^.defined:=true;
  409. { delete old definition }
  410. if assigned(mac^.buftext) then
  411. begin
  412. freemem(mac^.buftext,mac^.buflen);
  413. mac^.buftext:=nil;
  414. end;
  415. end;
  416. mac^.is_used:=true;
  417. if (cs_support_macro in aktmoduleswitches) then
  418. begin
  419. { key words are never substituted }
  420. if is_keyword(hs) then
  421. Message(scan_e_keyword_cant_be_a_macro);
  422. { !!!!!! handle macro params, need we this? }
  423. current_scanner^.skipspace;
  424. { may be a macro? }
  425. if c=':' then
  426. begin
  427. current_scanner^.readchar;
  428. if c='=' then
  429. begin
  430. new(macrobuffer);
  431. macropos:=0;
  432. { parse macro, brackets are counted so it's possible
  433. to have a $ifdef etc. in the macro }
  434. bracketcount:=0;
  435. repeat
  436. current_scanner^.readchar;
  437. case c of
  438. '}' :
  439. if (bracketcount=0) then
  440. break
  441. else
  442. dec(bracketcount);
  443. '{' :
  444. inc(bracketcount);
  445. #26 :
  446. current_scanner^.end_of_file;
  447. end;
  448. macrobuffer^[macropos]:=c;
  449. inc(macropos);
  450. if macropos>maxmacrolen then
  451. Message(scan_f_macro_buffer_overflow);
  452. until false;
  453. { free buffer of macro ?}
  454. if assigned(mac^.buftext) then
  455. freemem(mac^.buftext,mac^.buflen);
  456. { get new mem }
  457. getmem(mac^.buftext,macropos);
  458. mac^.buflen:=macropos;
  459. { copy the text }
  460. move(macrobuffer^,mac^.buftext^,macropos);
  461. dispose(macrobuffer);
  462. end;
  463. end;
  464. end;
  465. end;
  466. procedure dir_undef(t:tdirectivetoken);
  467. var
  468. hs : string;
  469. mac : pmacrosym;
  470. begin
  471. current_scanner^.skipspace;
  472. hs:=current_scanner^.readid;
  473. mac:=pmacrosym(macros^.search(hs));
  474. if not assigned(mac) then
  475. begin
  476. mac:=new(pmacrosym,init(hs));
  477. Message1(parser_m_macro_undefined,mac^.name);
  478. mac^.defined:=false;
  479. macros^.insert(mac);
  480. end
  481. else
  482. begin
  483. Message1(parser_m_macro_undefined,mac^.name);
  484. mac^.defined:=false;
  485. { delete old definition }
  486. if assigned(mac^.buftext) then
  487. begin
  488. freemem(mac^.buftext,mac^.buflen);
  489. mac^.buftext:=nil;
  490. end;
  491. end;
  492. mac^.is_used:=true;
  493. end;
  494. procedure dir_message(t:tdirectivetoken);
  495. var
  496. w : tmsgconst;
  497. begin
  498. case t of
  499. _DIR_STOP,
  500. _DIR_FATAL : w:=scan_f_user_defined;
  501. _DIR_ERROR : w:=scan_e_user_defined;
  502. _DIR_WARNING : w:=scan_w_user_defined;
  503. _DIR_HINT : w:=scan_h_user_defined;
  504. _DIR_NOTE : w:=scan_n_user_defined;
  505. _DIR_MESSAGE,
  506. _DIR_INFO : w:=scan_i_user_defined;
  507. end;
  508. current_scanner^.skipspace;
  509. Message1(w,current_scanner^.readcomment);
  510. end;
  511. procedure dir_moduleswitch(t:tdirectivetoken);
  512. var
  513. sw : tmoduleswitch;
  514. state : char;
  515. begin
  516. sw:=cs_modulenone;
  517. case t of
  518. _DIR_GOTO : sw:=cs_support_goto;
  519. _DIR_MACRO : sw:=cs_support_macro;
  520. _DIR_INLINE : sw:=cs_support_inline;
  521. _DIR_SMARTLINK : sw:=cs_create_smart;
  522. _DIR_STATIC : sw:=cs_static_keyword;
  523. end;
  524. state:=current_scanner^.readstate;
  525. if (sw<>cs_modulenone) and (state in ['-','+']) then
  526. begin
  527. if state='-' then
  528. aktmoduleswitches:=aktmoduleswitches-[sw]
  529. else
  530. aktmoduleswitches:=aktmoduleswitches+[sw];
  531. end;
  532. end;
  533. procedure dir_localswitch(t:tdirectivetoken);
  534. var
  535. sw : tlocalswitch;
  536. state : char;
  537. begin
  538. sw:=cs_localnone;
  539. {$ifdef SUPPORT_MMX}
  540. case t of
  541. _DIR_MMX : sw:=cs_mmx;
  542. _DIR_SATURATION : sw:=cs_mmx_saturation;
  543. end;
  544. {$endif}
  545. state:=current_scanner^.readstate;
  546. if (sw<>cs_localnone) and (state in ['-','+']) then
  547. begin
  548. if state='-' then
  549. aktlocalswitches:=aktlocalswitches-[sw]
  550. else
  551. aktlocalswitches:=aktlocalswitches+[sw];
  552. end;
  553. end;
  554. procedure dir_include(t:tdirectivetoken);
  555. var
  556. hs : string;
  557. path : dirstr;
  558. name : namestr;
  559. ext : extstr;
  560. hp : pinputfile;
  561. i : longint;
  562. found : boolean;
  563. begin
  564. current_scanner^.skipspace;
  565. hs:=current_scanner^.readcomment;
  566. i:=length(hs);
  567. while (i>0) and (hs[i]=' ') do
  568. dec(i);
  569. Delete(hs,i+1,length(hs)-i);
  570. if hs='' then
  571. exit;
  572. if (hs[1]='%') then
  573. begin
  574. { case insensitive }
  575. hs:=upper(hs);
  576. { remove %'s }
  577. Delete(hs,1,1);
  578. if hs[length(hs)]='%' then
  579. Delete(hs,length(hs),1);
  580. { save old }
  581. path:=hs;
  582. { first check for internal macros }
  583. if hs='TIME' then
  584. hs:=gettimestr
  585. else
  586. if hs='DATE' then
  587. hs:=getdatestr
  588. else
  589. if hs='FILE' then
  590. hs:=current_module^.sourcefiles^.get_file_name(aktfilepos.fileindex)
  591. else
  592. if hs='LINE' then
  593. hs:=tostr(aktfilepos.line)
  594. else
  595. if hs='FPCVERSION' then
  596. hs:=version_string
  597. else
  598. if hs='FPCTARGET' then
  599. hs:=target_cpu_string
  600. else
  601. hs:=getenv(hs);
  602. if hs='' then
  603. Message1(scan_w_include_env_not_found,path);
  604. { make it a stringconst }
  605. hs:=''''+hs+'''';
  606. current_scanner^.insertmacro(path,@hs[1],length(hs));
  607. end
  608. else
  609. begin
  610. hs:=FixFileName(hs);
  611. fsplit(hs,path,name,ext);
  612. { look for the include file
  613. 1. specified path,path of current inputfile,current dir
  614. 2. local includepath
  615. 3. global includepath }
  616. found:=false;
  617. path:=FindFile(name+ext,path+';'+current_scanner^.inputfile^.path^+';.',found);
  618. if (not found) then
  619. path:=current_module^.localincludesearchpath.FindFile(name+ext,found);
  620. if (not found) then
  621. path:=includesearchpath.FindFile(name+ext,found);
  622. { shutdown current file }
  623. current_scanner^.tempcloseinputfile;
  624. { load new file }
  625. hp:=new(pinputfile,init(path+name+ext));
  626. current_scanner^.addfile(hp);
  627. if not current_scanner^.openinputfile then
  628. Message1(scan_f_cannot_open_includefile,hs);
  629. Message1(scan_t_start_include_file,current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^);
  630. current_scanner^.reload;
  631. { process first read char }
  632. case c of
  633. #26 : current_scanner^.reload;
  634. #10,
  635. #13 : current_scanner^.linebreak;
  636. end;
  637. { register for refs }
  638. current_module^.sourcefiles^.register_file(hp);
  639. end;
  640. end;
  641. procedure dir_description(t:tdirectivetoken);
  642. begin
  643. end;
  644. procedure dir_linkobject(t:tdirectivetoken);
  645. var
  646. s : string;
  647. begin
  648. current_scanner^.skipspace;
  649. s:=AddExtension(FixFileName(current_scanner^.readcomment),target_info.objext);
  650. current_module^.linkotherofiles.insert(s,link_allways);
  651. end;
  652. procedure dir_resource(t:tdirectivetoken);
  653. var
  654. s : string;
  655. begin
  656. current_scanner^.skipspace;
  657. s:=AddExtension(FixFileName(current_scanner^.readcomment),target_info.resext);
  658. if target_info.res<>res_none then
  659. current_module^.resourcefiles.insert(FixFileName(s))
  660. else
  661. Message(scan_e_resourcefiles_not_supported);
  662. end;
  663. procedure dir_linklib(t:tdirectivetoken);
  664. begin
  665. current_scanner^.skipspace;
  666. current_scanner^.readstring;
  667. current_module^.linkOtherSharedLibs.insert(orgpattern,link_allways);
  668. end;
  669. procedure dir_outputformat(t:tdirectivetoken);
  670. begin
  671. if not current_module^.in_global then
  672. Message(scan_w_switch_is_global)
  673. else
  674. begin
  675. current_scanner^.skipspace;
  676. if set_string_asm(current_scanner^.readid) then
  677. aktoutputformat:=target_asm.id
  678. else
  679. Message(scan_w_illegal_switch);
  680. end;
  681. end;
  682. procedure dir_unitpath(t:tdirectivetoken);
  683. begin
  684. if not current_module^.in_global then
  685. Message(scan_w_switch_is_global)
  686. else
  687. begin
  688. current_scanner^.skipspace;
  689. current_module^.localunitsearchpath.AddPath(current_scanner^.readcomment,false);
  690. end;
  691. end;
  692. procedure dir_includepath(t:tdirectivetoken);
  693. begin
  694. if not current_module^.in_global then
  695. Message(scan_w_switch_is_global)
  696. else
  697. begin
  698. current_scanner^.skipspace;
  699. current_module^.localincludesearchpath.AddPath(current_scanner^.readcomment,false);
  700. end;
  701. end;
  702. procedure dir_librarypath(t:tdirectivetoken);
  703. begin
  704. if not current_module^.in_global then
  705. Message(scan_w_switch_is_global)
  706. else
  707. begin
  708. current_scanner^.skipspace;
  709. current_module^.locallibrarysearchpath.AddPath(current_scanner^.readcomment,false);
  710. end;
  711. end;
  712. procedure dir_objectpath(t:tdirectivetoken);
  713. begin
  714. if not current_module^.in_global then
  715. Message(scan_w_switch_is_global)
  716. else
  717. begin
  718. current_scanner^.skipspace;
  719. current_module^.localobjectsearchpath.AddPath(current_scanner^.readcomment,false);
  720. end;
  721. end;
  722. procedure dir_mode(t:tdirectivetoken);
  723. begin
  724. if not current_module^.in_global then
  725. Message(scan_w_switch_is_global)
  726. else
  727. begin
  728. current_scanner^.skipspace;
  729. current_scanner^.readstring;
  730. if pattern='DEFAULT' then
  731. aktmodeswitches:=initmodeswitches
  732. else
  733. if pattern='DELPHI' then
  734. aktmodeswitches:=delphimodeswitches
  735. else
  736. if pattern='TP' then
  737. aktmodeswitches:=tpmodeswitches
  738. else
  739. if pattern='FPC' then
  740. aktmodeswitches:=fpcmodeswitches
  741. else
  742. if pattern='OBJFPC' then
  743. aktmodeswitches:=objfpcmodeswitches
  744. else
  745. if pattern='GPC' then
  746. aktmodeswitches:=gpcmodeswitches
  747. else
  748. Message(scan_w_illegal_switch);
  749. end;
  750. end;
  751. procedure dir_packrecords(t:tdirectivetoken);
  752. var
  753. hs : string;
  754. begin
  755. current_scanner^.skipspace;
  756. if not(c in ['0'..'9']) then
  757. begin
  758. hs:=current_scanner^.readid;
  759. if (hs='C') then
  760. aktpackrecords:=packrecord_C
  761. else
  762. if (hs='NORMAL') or (hs='DEFAULT') then
  763. aktpackrecords:=packrecord_2
  764. else
  765. Message(scan_w_only_pack_records);
  766. end
  767. else
  768. begin
  769. case current_scanner^.readval of
  770. 1 : aktpackrecords:=packrecord_1;
  771. 2 : aktpackrecords:=packrecord_2;
  772. 4 : aktpackrecords:=packrecord_4;
  773. 8 : aktpackrecords:=packrecord_8;
  774. 16 : aktpackrecords:=packrecord_16;
  775. 32 : aktpackrecords:=packrecord_32;
  776. else
  777. Message(scan_w_only_pack_records);
  778. end;
  779. end;
  780. end;
  781. procedure dir_packenum(t:tdirectivetoken);
  782. var
  783. hs : string;
  784. begin
  785. if t in [_DIR_Z1,_DIR_Z2,_DIR_Z4] then
  786. begin
  787. aktpackenum:=ord(pattern[2])-ord('0');
  788. exit;
  789. end;
  790. current_scanner^.skipspace;
  791. if not(c in ['0'..'9']) then
  792. begin
  793. hs:=current_scanner^.readid;
  794. if (hs='NORMAL') or (hs='DEFAULT') then
  795. aktpackenum:=4
  796. else
  797. Message(scan_w_only_pack_enum);
  798. end
  799. else
  800. begin
  801. case current_scanner^.readval of
  802. 1 : aktpackenum:=1;
  803. 2 : aktpackenum:=2;
  804. 4 : aktpackenum:=4;
  805. else
  806. Message(scan_w_only_pack_enum);
  807. end;
  808. end;
  809. end;
  810. procedure dir_apptype(t:tdirectivetoken);
  811. var
  812. hs : string;
  813. begin
  814. if target_info.target<>target_i386_win32 then
  815. Message(scan_w_app_type_not_support);
  816. if not current_module^.in_global then
  817. Message(scan_w_switch_is_global)
  818. else
  819. begin
  820. current_scanner^.skipspace;
  821. hs:=current_scanner^.readid;
  822. if hs='GUI' then
  823. apptype:=at_gui
  824. else if hs='CONSOLE' then
  825. apptype:=at_cui
  826. else
  827. Message1(scan_w_unsupported_app_type,hs);
  828. end;
  829. end;
  830. procedure dir_wait(t:tdirectivetoken);
  831. var had_info : boolean;
  832. begin
  833. had_info:=(status.verbosity and V_Info)<>0;
  834. { this message should allways appear !! }
  835. status.verbosity:=status.verbosity or V_Info;
  836. Message(scan_i_press_enter);
  837. readln;
  838. If not(had_info) then
  839. status.verbosity:=status.verbosity and (not V_Info);
  840. end;
  841. procedure dir_asmmode(t:tdirectivetoken);
  842. var
  843. s : string;
  844. begin
  845. If Inside_asm_statement then
  846. Message1(scan_w_no_asm_reader_switch_inside_asm,s);
  847. current_scanner^.skipspace;
  848. s:=current_scanner^.readid;
  849. if s='DEFAULT' then
  850. aktasmmode:=initasmmode
  851. else
  852. if not set_string_asmmode(s,aktasmmode) then
  853. Message1(scan_w_unsupported_asmmode_specifier,s);
  854. end;
  855. procedure dir_oldasmmode(t:tdirectivetoken);
  856. begin
  857. If Inside_asm_statement then
  858. Message1(scan_w_no_asm_reader_switch_inside_asm,directive[t]);
  859. {$ifdef i386}
  860. case t of
  861. _DIR_I386_ATT : aktasmmode:=asmmode_i386_att;
  862. _DIR_I386_DIRECT : aktasmmode:=asmmode_i386_direct;
  863. _DIR_I386_INTEL : aktasmmode:=asmmode_i386_intel;
  864. end;
  865. {$endif i386}
  866. end;
  867. procedure dir_delphiswitch(t:tdirectivetoken);
  868. var
  869. sw,state : char;
  870. begin
  871. case t of
  872. _DIR_ALIGN : sw:='A';
  873. _DIR_ASSERTIONS : sw:='C';
  874. _DIR_BOOLEVAL : sw:='B';
  875. _DIR_DEBUGINFO : sw:='D';
  876. _DIR_IOCHECKS : sw:='I';
  877. _DIR_LOCALSYMBOLS : sw:='L';
  878. _DIR_LONGSTRINGS : sw:='H';
  879. _DIR_OPENSTRINGS : sw:='P';
  880. _DIR_OVERFLOWCHECKS : sw:='Q';
  881. _DIR_RANGECHECKS : sw:='R';
  882. _DIR_REFERENCEINFO : sw:='Y';
  883. _DIR_STACKFRAMES : sw:='W';
  884. _DIR_TYPEDADDRESS : sw:='T';
  885. _DIR_TYPEINFO : sw:='M';
  886. _DIR_VARSTRINGCHECKS : sw:='V';
  887. else
  888. exit;
  889. end;
  890. { c contains the next char, a + or - would be fine }
  891. state:=current_scanner^.readstate;
  892. if state in ['-','+'] then
  893. HandleSwitch(sw,state);
  894. end;
  895. procedure dir_memory(t:tdirectivetoken);
  896. var
  897. l : longint;
  898. begin
  899. current_scanner^.skipspace;
  900. l:=current_scanner^.readval;
  901. if l>1024 then
  902. stacksize:=l;
  903. current_scanner^.skipspace;
  904. if c=',' then
  905. begin
  906. current_scanner^.readchar;
  907. current_scanner^.skipspace;
  908. l:=current_scanner^.readval;
  909. if l>1024 then
  910. heapsize:=l;
  911. end;
  912. if c=',' then
  913. begin
  914. current_scanner^.readchar;
  915. current_scanner^.skipspace;
  916. l:=current_scanner^.readval;
  917. { Ignore this value, because the limit is set by the OS
  918. info and shouldn't be changed by the user (PFV) }
  919. end;
  920. end;
  921. procedure dir_setverbose(t:tdirectivetoken);
  922. var
  923. flag,
  924. state : char;
  925. begin
  926. case t of
  927. _DIR_HINTS : flag:='H';
  928. _DIR_WARNINGS : flag:='W';
  929. _DIR_NOTES : flag:='N';
  930. else
  931. exit;
  932. end;
  933. { support ON/OFF }
  934. state:=current_scanner^.ReadState;
  935. SetVerbosity(flag+state);
  936. end;
  937. type
  938. tdirectiveproc=procedure(t:tdirectivetoken);
  939. const
  940. directiveproc:array[tdirectivetoken] of tdirectiveproc=(
  941. {_DIR_NONE} nil,
  942. {_DIR_ALIGN} dir_delphiswitch,
  943. {_DIR_APPTYPE} dir_apptype,
  944. {_DIR_ASMMODE} dir_asmmode,
  945. {_DIR_ASSERTION} dir_delphiswitch,
  946. {_DIR_BOOLEVAL} dir_delphiswitch,
  947. {_DIR_D} dir_description,
  948. {_DIR_DEBUGINFO} dir_delphiswitch,
  949. {_DIR_DEFINE} dir_define,
  950. {_DIR_DESCRIPTION} dir_description,
  951. {_DIR_ELSE} dir_conditional,
  952. {_DIR_ENDIF} dir_conditional,
  953. {_DIR_ERROR} dir_message,
  954. {_DIR_EXTENDEDSYNTAX} dir_delphiswitch,
  955. {_DIR_FATAL} dir_message,
  956. {_DIR_GOTO} dir_moduleswitch,
  957. {_DIR_HINT} dir_message,
  958. {_DIR_HINTS} dir_setverbose,
  959. {_DIR_I} dir_include,
  960. {_DIR_I386_ATT} dir_oldasmmode,
  961. {_DIR_I386_DIRECT} dir_oldasmmode,
  962. {_DIR_I386_INTEL} dir_oldasmmode,
  963. {_DIR_IOCHECKS} dir_delphiswitch,
  964. {_DIR_IF} dir_conditional,
  965. {_DIR_IFDEF} dir_conditional,
  966. {_DIR_IFNDEF} dir_conditional,
  967. {_DIR_IFOPT} dir_conditional,
  968. {_DIR_INCLUDE} dir_include,
  969. {_DIR_INCLUDEPATH} dir_includepath,
  970. {_DIR_INFO} dir_message,
  971. {_DIR_INLINE} dir_moduleswitch,
  972. {_DIR_L} dir_linkobject,
  973. {_DIR_LIBRARYPATH} dir_librarypath,
  974. {_DIR_LINK} dir_linkobject,
  975. {_DIR_LINKLIB} dir_linklib,
  976. {_DIR_LOCALSYMBOLS} dir_delphiswitch,
  977. {_DIR_LONGSTRINGS} dir_delphiswitch,
  978. {_DIR_M} dir_memory,
  979. {_DIR_MACRO} dir_moduleswitch,
  980. {_DIR_MEMORY} dir_memory,
  981. {_DIR_MESSAGE} dir_message,
  982. {_DIR_MINENUMSIZE} dir_packenum,
  983. {_DIR_MMX} dir_localswitch,
  984. {_DIR_MODE} dir_mode,
  985. {_DIR_NOTE} dir_message,
  986. {_DIR_NOTES} dir_setverbose,
  987. {_DIR_OBJECTPATH} dir_objectpath,
  988. {_DIR_OPENSTRINGS} dir_delphiswitch,
  989. {_DIR_OUTPUT_FORMAT} dir_outputformat,
  990. {_DIR_OVERFLOWCHECKS} dir_delphiswitch,
  991. {_DIR_PACKENUM} dir_packenum,
  992. {_DIR_PACKRECORDS} dir_packrecords,
  993. {_DIR_R} dir_resource,
  994. {_DIR_RANGECHECKS} dir_delphiswitch,
  995. {_DIR_REFERENCEINFO} dir_delphiswitch,
  996. {_DIR_SATURATION} dir_localswitch,
  997. {_DIR_SMARTLINK} dir_moduleswitch,
  998. {_DIR_STACKFRAMES} dir_delphiswitch,
  999. {_DIR_STATIC} dir_moduleswitch,
  1000. {_DIR_STOP} dir_message,
  1001. {_DIR_TYPEDADDRESS} dir_delphiswitch,
  1002. {_DIR_TYPEINFO} dir_delphiswitch,
  1003. {_DIR_UNDEF} dir_undef,
  1004. {_DIR_UNITPATH} dir_unitpath,
  1005. {_DIR_VARSTRINGCHECKS} dir_delphiswitch,
  1006. {_DIR_WAIT} dir_wait,
  1007. {_DIR_WARNING} dir_message,
  1008. {_DIR_WARNINGS} dir_setverbose,
  1009. {_DIR_Z1} dir_packenum,
  1010. {_DIR_Z2} dir_packenum,
  1011. {_DIR_Z4} dir_packenum
  1012. );
  1013. {-------------------------------------------
  1014. Main switches handling
  1015. -------------------------------------------}
  1016. procedure handledirectives;
  1017. var
  1018. t : tdirectivetoken;
  1019. p : tdirectiveproc;
  1020. hs : string;
  1021. begin
  1022. current_scanner^.gettokenpos;
  1023. current_scanner^.readchar; {Remove the $}
  1024. hs:=current_scanner^.readid;
  1025. Message1(scan_d_handling_switch,'$'+hs);
  1026. if hs='' then
  1027. Message1(scan_w_illegal_switch,'$'+hs);
  1028. { Check for compiler switches }
  1029. while (length(hs)=1) and (c in ['-','+']) do
  1030. begin
  1031. HandleSwitch(hs[1],c);
  1032. current_scanner^.readchar; {Remove + or -}
  1033. if c=',' then
  1034. begin
  1035. current_scanner^.readchar; {Remove , }
  1036. { read next switch, support $v+,$+}
  1037. hs:=current_scanner^.readid;
  1038. if (hs='') then
  1039. begin
  1040. if (c='$') and (m_fpc in aktmodeswitches) then
  1041. begin
  1042. current_scanner^.readchar; { skip $ }
  1043. hs:=current_scanner^.readid;
  1044. end;
  1045. if (hs='') then
  1046. Message1(scan_w_illegal_directive,'$'+c);
  1047. end
  1048. else
  1049. Message1(scan_d_handling_switch,'$'+hs);
  1050. end
  1051. else
  1052. hs:='';
  1053. end;
  1054. { directives may follow switches after a , }
  1055. if hs<>'' then
  1056. begin
  1057. t:=Get_Directive(hs);
  1058. if t<>_DIR_NONE then
  1059. begin
  1060. p:=directiveproc[t];
  1061. {$ifndef TP}
  1062. if assigned(p) then
  1063. {$else}
  1064. if @p<>nil then
  1065. {$endif}
  1066. p(t);
  1067. end
  1068. else
  1069. Message1(scan_w_illegal_directive,'$'+hs);
  1070. { conditionals already read the comment }
  1071. if (current_scanner^.comment_level>0) then
  1072. current_scanner^.readcomment;
  1073. { we've read the whole comment }
  1074. aktcommentstyle:=comment_none;
  1075. end;
  1076. end;
  1077. {
  1078. $Log$
  1079. Revision 1.67 1999-11-12 11:03:50 peter
  1080. * searchpaths changed to stringqueue object
  1081. Revision 1.66 1999/11/06 14:34:26 peter
  1082. * truncated log to 20 revs
  1083. Revision 1.65 1999/10/30 12:32:30 peter
  1084. * fixed line counter when the first line had #10 only. This was buggy
  1085. for both the main file as for include files
  1086. Revision 1.64 1999/09/27 23:38:17 peter
  1087. * bracket support for macro define
  1088. Revision 1.63 1999/09/20 16:39:02 peter
  1089. * cs_create_smart instead of cs_smartlink
  1090. * -CX is create smartlink
  1091. * -CD is create dynamic, but does nothing atm.
  1092. Revision 1.62 1999/09/03 10:00:49 peter
  1093. * included the 1.60 version of Pierre which was lost !
  1094. Revision 1.61 1999/09/02 18:47:46 daniel
  1095. * Could not compile with TP, some arrays moved to heap
  1096. * NOAG386BIN default for TP
  1097. * AG386* files were not compatible with TP, fixed.
  1098. Revision 1.60 1999/08/31 15:55:45 pierre
  1099. + tmacrosym.is_used set
  1100. Revision 1.59 1999/08/05 16:53:10 peter
  1101. * V_Fatal=1, all other V_ are also increased
  1102. * Check for local procedure when assigning procvar
  1103. * fixed comment parsing because directives
  1104. * oldtp mode directives better supported
  1105. * added some messages to errore.msg
  1106. Revision 1.58 1999/08/04 13:03:03 jonas
  1107. * all tokens now start with an underscore
  1108. * PowerPC compiles!!
  1109. Revision 1.57 1999/07/26 14:55:36 florian
  1110. * $mode gives now a warning if an unknown mode keyword follows
  1111. Revision 1.56 1999/07/23 16:05:27 peter
  1112. * alignment is now saved in the symtable
  1113. * C alignment added for records
  1114. * PPU version increased to solve .12 <-> .13 probs
  1115. Revision 1.55 1999/07/16 10:04:36 peter
  1116. * merged
  1117. Revision 1.54 1999/07/03 00:29:58 peter
  1118. * new link writing to the ppu, one .ppu is needed for all link types,
  1119. static (.o) is now always created also when smartlinking is used
  1120. Revision 1.53.2.1 1999/07/16 09:53:06 peter
  1121. * ignore maxheapsize
  1122. Revision 1.53 1999/06/02 22:44:18 pierre
  1123. * previous wrong log corrected
  1124. Revision 1.52 1999/06/02 22:25:48 pierre
  1125. * changed $ifdef FPC @ into $ifndef TP
  1126. Revision 1.51 1999/04/07 14:36:45 pierre
  1127. + better preproc stack checking and report
  1128. Revision 1.50 1999/03/31 13:55:20 peter
  1129. * assembler inlining working for ag386bin
  1130. Revision 1.49 1999/03/26 00:05:44 peter
  1131. * released valintern
  1132. + deffile is now removed when compiling is finished
  1133. * ^( compiles now correct
  1134. + static directive
  1135. * shrd fixed
  1136. Revision 1.48 1999/03/25 16:55:34 peter
  1137. + unitpath,librarypath,includepath,objectpath directives
  1138. Revision 1.47 1999/02/22 13:07:05 pierre
  1139. + -b and -bl options work !
  1140. + cs_local_browser ($L+) is disabled if cs_browser ($Y+)
  1141. is not enabled when quitting global section
  1142. * local vars and procedures are not yet stored into PPU
  1143. }