scanner.pas 182 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  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 scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,constexp,version,tokens,
  23. verbose,comphook,
  24. finput,
  25. widestr;
  26. const
  27. max_include_nesting=32;
  28. max_macro_nesting=16;
  29. preprocbufsize=32*1024;
  30. type
  31. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  32. tscannerfile = class;
  33. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  34. tpreprocstack = class
  35. typ : preproctyp;
  36. accept : boolean;
  37. next : tpreprocstack;
  38. name : TIDString;
  39. line_nb : longint;
  40. owner : tscannerfile;
  41. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  42. end;
  43. tdirectiveproc=procedure;
  44. tdirectiveitem = class(TFPHashObject)
  45. public
  46. is_conditional : boolean;
  47. proc : tdirectiveproc;
  48. constructor Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  49. constructor CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  50. end;
  51. // stack for replay buffers
  52. treplaystack = class
  53. token : ttoken;
  54. settings : tsettings;
  55. tokenbuf : tdynamicarray;
  56. next : treplaystack;
  57. constructor Create(atoken: ttoken;asettings:tsettings;
  58. atokenbuf:tdynamicarray;anext:treplaystack);
  59. end;
  60. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  61. tspecialgenerictoken =
  62. (ST_LOADSETTINGS,
  63. ST_LINE,
  64. ST_COLUMN,
  65. ST_FILEINDEX,
  66. ST_LOADMESSAGES);
  67. { tscannerfile }
  68. tscannerfile = class
  69. private
  70. procedure do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  71. procedure cachenexttokenpos;
  72. procedure setnexttoken;
  73. procedure savetokenpos;
  74. procedure restoretokenpos;
  75. procedure writetoken(t: ttoken);
  76. function readtoken : ttoken;
  77. public
  78. inputfile : tinputfile; { current inputfile list }
  79. inputfilecount : longint;
  80. inputbuffer, { input buffer }
  81. inputpointer : pchar;
  82. inputstart : longint;
  83. line_no, { line }
  84. lastlinepos : longint;
  85. lasttokenpos,
  86. nexttokenpos : longint; { token }
  87. lasttoken,
  88. nexttoken : ttoken;
  89. oldlasttokenpos : longint; { temporary saving/restoring tokenpos }
  90. oldcurrent_filepos,
  91. oldcurrent_tokenpos : tfileposinfo;
  92. replaytokenbuf,
  93. recordtokenbuf : tdynamicarray;
  94. { last settings we stored }
  95. last_settings : tsettings;
  96. last_message : pmessagestaterecord;
  97. { last filepos we stored }
  98. last_filepos,
  99. { if nexttoken<>NOTOKEN, then nexttokenpos holds its filepos }
  100. next_filepos : tfileposinfo;
  101. comment_level,
  102. yylexcount : longint;
  103. lastasmgetchar : char;
  104. ignoredirectives : TFPHashList; { ignore directives, used to give warnings only once }
  105. preprocstack : tpreprocstack;
  106. replaystack : treplaystack;
  107. in_asm_string : boolean;
  108. preproc_pattern : string;
  109. preproc_token : ttoken;
  110. constructor Create(const fn:string; is_macro: boolean = false);
  111. destructor Destroy;override;
  112. { File buffer things }
  113. function openinputfile:boolean;
  114. procedure closeinputfile;
  115. function tempopeninputfile:boolean;
  116. procedure tempcloseinputfile;
  117. procedure saveinputfile;
  118. procedure restoreinputfile;
  119. procedure firstfile;
  120. procedure nextfile;
  121. procedure addfile(hp:tinputfile);
  122. procedure reload;
  123. { replaces current token with the text in p }
  124. procedure substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  125. { Scanner things }
  126. procedure gettokenpos;
  127. procedure inc_comment_level;
  128. procedure dec_comment_level;
  129. procedure illegal_char(c:char);
  130. procedure end_of_file;
  131. procedure checkpreprocstack;
  132. procedure poppreprocstack;
  133. procedure ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  134. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  135. procedure elsepreprocstack;
  136. procedure popreplaystack;
  137. procedure handleconditional(p:tdirectiveitem);
  138. procedure handledirectives;
  139. procedure linebreak;
  140. procedure recordtoken;
  141. procedure startrecordtokens(buf:tdynamicarray);
  142. procedure stoprecordtokens;
  143. procedure replaytoken;
  144. procedure startreplaytokens(buf:tdynamicarray);
  145. { bit length asizeint is target depend }
  146. procedure tokenwritesizeint(val : asizeint);
  147. procedure tokenwritelongint(val : longint);
  148. procedure tokenwritelongword(val : longword);
  149. procedure tokenwriteword(val : word);
  150. procedure tokenwriteshortint(val : shortint);
  151. procedure tokenwriteset(var b;size : longint);
  152. procedure tokenwriteenum(var b;size : longint);
  153. function tokenreadsizeint : asizeint;
  154. procedure tokenwritesettings(var asettings : tsettings; var size : asizeint);
  155. { longword/longint are 32 bits on all targets }
  156. { word/smallint are 16-bits on all targest }
  157. function tokenreadlongword : longword;
  158. function tokenreadword : word;
  159. function tokenreadlongint : longint;
  160. function tokenreadsmallint : smallint;
  161. { short int is one a signed byte }
  162. function tokenreadshortint : shortint;
  163. function tokenreadbyte : byte;
  164. { This one takes the set size as an parameter }
  165. procedure tokenreadset(var b;size : longint);
  166. function tokenreadenum(size : longint) : longword;
  167. procedure tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  168. procedure readchar;
  169. procedure readstring;
  170. procedure readnumber;
  171. function readid:string;
  172. function readval:longint;
  173. function readcomment:string;
  174. function readquotedstring:string;
  175. function readstate:char;
  176. function readstatedefault:char;
  177. procedure skipspace;
  178. procedure skipuntildirective;
  179. procedure skipcomment;
  180. procedure skipdelphicomment;
  181. procedure skipoldtpcomment;
  182. procedure readtoken(allowrecordtoken:boolean);
  183. function readpreproc:ttoken;
  184. function asmgetcharstart : char;
  185. function asmgetchar:char;
  186. end;
  187. {$ifdef PREPROCWRITE}
  188. tpreprocfile=class
  189. f : text;
  190. buf : pointer;
  191. spacefound,
  192. eolfound : boolean;
  193. constructor create(const fn:string);
  194. destructor destroy;
  195. procedure Add(const s:string);
  196. procedure AddSpace;
  197. end;
  198. {$endif PREPROCWRITE}
  199. var
  200. { read strings }
  201. c : char;
  202. orgpattern,
  203. pattern : string;
  204. cstringpattern : ansistring;
  205. patternw : pcompilerwidestring;
  206. { token }
  207. token, { current token being parsed }
  208. idtoken : ttoken; { holds the token if the pattern is a known word }
  209. current_scanner : tscannerfile; { current scanner in use }
  210. aktcommentstyle : tcommentstyle; { needed to use read_comment from directives }
  211. {$ifdef PREPROCWRITE}
  212. preprocfile : tpreprocfile; { used with only preprocessing }
  213. {$endif PREPROCWRITE}
  214. type
  215. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  216. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  217. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  218. procedure InitScanner;
  219. procedure DoneScanner;
  220. { To be called when the language mode is finally determined }
  221. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  222. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  223. procedure SetAppType(NewAppType:tapptype);
  224. implementation
  225. uses
  226. SysUtils,
  227. cutils,cfileutl,
  228. systems,
  229. switches,
  230. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  231. { This is needed for tcputype }
  232. cpuinfo,
  233. fmodule
  234. {$if FPC_FULLVERSION<20700}
  235. ,ccharset
  236. {$endif}
  237. ;
  238. var
  239. { dictionaries with the supported directives }
  240. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  241. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  242. {*****************************************************************************
  243. Helper routines
  244. *****************************************************************************}
  245. const
  246. { use any special name that is an invalid file name to avoid problems }
  247. preprocstring : array [preproctyp] of string[7]
  248. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  249. function is_keyword(const s:string):boolean;
  250. var
  251. low,high,mid : longint;
  252. begin
  253. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  254. not (s[1] in ['a'..'z','A'..'Z']) then
  255. begin
  256. is_keyword:=false;
  257. exit;
  258. end;
  259. low:=ord(tokenidx^[length(s),s[1]].first);
  260. high:=ord(tokenidx^[length(s),s[1]].last);
  261. while low<high do
  262. begin
  263. mid:=(high+low+1) shr 1;
  264. if pattern<tokeninfo^[ttoken(mid)].str then
  265. high:=mid-1
  266. else
  267. low:=mid;
  268. end;
  269. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  270. ((tokeninfo^[ttoken(high)].keyword*current_settings.modeswitches)<>[]);
  271. end;
  272. Procedure HandleModeSwitches(switch: tmodeswitch; changeInit: boolean);
  273. begin
  274. { turn ansi/unicodestrings on by default ? (only change when this
  275. particular setting is changed, so that a random modeswitch won't
  276. change the state of $h+/$h-) }
  277. if switch in [m_none,m_default_ansistring,m_default_unicodestring] then
  278. begin
  279. if ([m_default_ansistring,m_default_unicodestring]*current_settings.modeswitches)<>[] then
  280. begin
  281. { can't have both ansistring and unicodestring as default }
  282. if switch=m_default_ansistring then
  283. begin
  284. exclude(current_settings.modeswitches,m_default_unicodestring);
  285. if changeinit then
  286. exclude(init_settings.modeswitches,m_default_unicodestring);
  287. end
  288. else if switch=m_default_unicodestring then
  289. begin
  290. exclude(current_settings.modeswitches,m_default_ansistring);
  291. if changeinit then
  292. exclude(init_settings.modeswitches,m_default_ansistring);
  293. end;
  294. { enable $h+ }
  295. include(current_settings.localswitches,cs_refcountedstrings);
  296. if changeinit then
  297. include(init_settings.localswitches,cs_refcountedstrings);
  298. if m_default_unicodestring in current_settings.modeswitches then
  299. begin
  300. def_system_macro('FPC_UNICODESTRINGS');
  301. def_system_macro('UNICODE');
  302. end;
  303. end
  304. else
  305. begin
  306. exclude(current_settings.localswitches,cs_refcountedstrings);
  307. if changeinit then
  308. exclude(init_settings.localswitches,cs_refcountedstrings);
  309. undef_system_macro('FPC_UNICODESTRINGS');
  310. undef_system_macro('UNICODE');
  311. end;
  312. end;
  313. { turn inline on by default ? }
  314. if switch in [m_none,m_default_inline] then
  315. begin
  316. if (m_default_inline in current_settings.modeswitches) then
  317. begin
  318. include(current_settings.localswitches,cs_do_inline);
  319. if changeinit then
  320. include(init_settings.localswitches,cs_do_inline);
  321. end
  322. else
  323. begin
  324. exclude(current_settings.localswitches,cs_do_inline);
  325. if changeinit then
  326. exclude(init_settings.localswitches,cs_do_inline);
  327. end;
  328. end;
  329. { turn on system codepage by default }
  330. if switch in [m_none,m_systemcodepage] then
  331. begin
  332. if m_systemcodepage in current_settings.modeswitches then
  333. begin
  334. current_settings.sourcecodepage:=DefaultSystemCodePage;
  335. if (current_settings.sourcecodepage<>CP_UTF8) and not cpavailable(current_settings.sourcecodepage) then
  336. begin
  337. Message2(scan_w_unavailable_system_codepage,IntToStr(current_settings.sourcecodepage),IntToStr(default_settings.sourcecodepage));
  338. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  339. end;
  340. include(current_settings.moduleswitches,cs_explicit_codepage);
  341. if changeinit then
  342. begin
  343. init_settings.sourcecodepage:=current_settings.sourcecodepage;
  344. include(init_settings.moduleswitches,cs_explicit_codepage);
  345. end;
  346. end
  347. else
  348. begin
  349. exclude(current_settings.moduleswitches,cs_explicit_codepage);
  350. if changeinit then
  351. exclude(init_settings.moduleswitches,cs_explicit_codepage);
  352. end;
  353. end;
  354. end;
  355. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  356. var
  357. b : boolean;
  358. oldmodeswitches : tmodeswitches;
  359. begin
  360. oldmodeswitches:=current_settings.modeswitches;
  361. b:=true;
  362. if s='DEFAULT' then
  363. current_settings.modeswitches:=fpcmodeswitches
  364. else
  365. if s='DELPHI' then
  366. current_settings.modeswitches:=delphimodeswitches
  367. else
  368. if s='DELPHIUNICODE' then
  369. current_settings.modeswitches:=delphiunicodemodeswitches
  370. else
  371. if s='TP' then
  372. current_settings.modeswitches:=tpmodeswitches
  373. else
  374. if s='FPC' then begin
  375. current_settings.modeswitches:=fpcmodeswitches;
  376. { TODO: enable this for 2.3/2.9 }
  377. // include(current_settings.localswitches, cs_typed_addresses);
  378. end else
  379. if s='OBJFPC' then begin
  380. current_settings.modeswitches:=objfpcmodeswitches;
  381. { TODO: enable this for 2.3/2.9 }
  382. // include(current_settings.localswitches, cs_typed_addresses);
  383. end
  384. {$ifdef gpc_mode}
  385. else if s='GPC' then
  386. current_settings.modeswitches:=gpcmodeswitches
  387. {$endif}
  388. else
  389. if s='MACPAS' then
  390. current_settings.modeswitches:=macmodeswitches
  391. else
  392. if s='ISO' then
  393. current_settings.modeswitches:=isomodeswitches
  394. else
  395. b:=false;
  396. {$ifdef jvm}
  397. { enable final fields by default for the JVM targets }
  398. include(current_settings.modeswitches,m_final_fields);
  399. {$endif jvm}
  400. if b and changeInit then
  401. init_settings.modeswitches := current_settings.modeswitches;
  402. if b then
  403. begin
  404. { resolve all postponed switch changes }
  405. flushpendingswitchesstate;
  406. HandleModeSwitches(m_none,changeinit);
  407. { turn on bitpacking for mode macpas and iso pascal }
  408. if ([m_mac,m_iso] * current_settings.modeswitches <> []) then
  409. begin
  410. include(current_settings.localswitches,cs_bitpacking);
  411. if changeinit then
  412. include(init_settings.localswitches,cs_bitpacking);
  413. end;
  414. { support goto/label by default in delphi/tp7/mac modes }
  415. if ([m_delphi,m_tp7,m_mac,m_iso] * current_settings.modeswitches <> []) then
  416. begin
  417. include(current_settings.moduleswitches,cs_support_goto);
  418. if changeinit then
  419. include(init_settings.moduleswitches,cs_support_goto);
  420. end;
  421. { support pointer math by default in fpc/objfpc modes }
  422. if ([m_fpc,m_objfpc] * current_settings.modeswitches <> []) then
  423. begin
  424. include(current_settings.localswitches,cs_pointermath);
  425. if changeinit then
  426. include(init_settings.localswitches,cs_pointermath);
  427. end
  428. else
  429. begin
  430. exclude(current_settings.localswitches,cs_pointermath);
  431. if changeinit then
  432. exclude(init_settings.localswitches,cs_pointermath);
  433. end;
  434. { Default enum and set packing for delphi/tp7 }
  435. if (m_tp7 in current_settings.modeswitches) or
  436. (m_delphi in current_settings.modeswitches) then
  437. begin
  438. current_settings.packenum:=1;
  439. current_settings.setalloc:=1;
  440. end
  441. else if (m_mac in current_settings.modeswitches) then
  442. { compatible with Metrowerks Pascal }
  443. current_settings.packenum:=2
  444. else
  445. current_settings.packenum:=4;
  446. if changeinit then
  447. begin
  448. init_settings.packenum:=current_settings.packenum;
  449. init_settings.setalloc:=current_settings.setalloc;
  450. end;
  451. {$if defined(i386) or defined(i8086)}
  452. { Default to intel assembler for delphi/tp7 on i386/i8086 }
  453. if (m_delphi in current_settings.modeswitches) or
  454. (m_tp7 in current_settings.modeswitches) then
  455. current_settings.asmmode:=asmmode_i386_intel;
  456. if changeinit then
  457. init_settings.asmmode:=current_settings.asmmode;
  458. {$endif i386 or i8086}
  459. { Exception support explicitly turned on (mainly for macpas, to }
  460. { compensate for lack of interprocedural goto support) }
  461. if (cs_support_exceptions in current_settings.globalswitches) then
  462. include(current_settings.modeswitches,m_except);
  463. { Default strict string var checking in TP/Delphi modes }
  464. if ([m_delphi,m_tp7] * current_settings.modeswitches <> []) then
  465. begin
  466. include(current_settings.localswitches,cs_strict_var_strings);
  467. if changeinit then
  468. include(init_settings.localswitches,cs_strict_var_strings);
  469. end;
  470. { Undefine old symbol }
  471. if (m_delphi in oldmodeswitches) then
  472. undef_system_macro('FPC_DELPHI')
  473. else if (m_tp7 in oldmodeswitches) then
  474. undef_system_macro('FPC_TP')
  475. else if (m_objfpc in oldmodeswitches) then
  476. undef_system_macro('FPC_OBJFPC')
  477. {$ifdef gpc_mode}
  478. else if (m_gpc in oldmodeswitches) then
  479. undef_system_macro('FPC_GPC')
  480. {$endif}
  481. else if (m_mac in oldmodeswitches) then
  482. undef_system_macro('FPC_MACPAS');
  483. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  484. if (m_delphi in current_settings.modeswitches) then
  485. def_system_macro('FPC_DELPHI')
  486. else if (m_tp7 in current_settings.modeswitches) then
  487. def_system_macro('FPC_TP')
  488. else if (m_objfpc in current_settings.modeswitches) then
  489. def_system_macro('FPC_OBJFPC')
  490. {$ifdef gpc_mode}
  491. else if (m_gpc in current_settings.modeswitches) then
  492. def_system_macro('FPC_GPC')
  493. {$endif}
  494. else if (m_mac in current_settings.modeswitches) then
  495. def_system_macro('FPC_MACPAS');
  496. end;
  497. SetCompileMode:=b;
  498. end;
  499. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  500. var
  501. i : tmodeswitch;
  502. doinclude : boolean;
  503. begin
  504. s:=upper(s);
  505. { on/off? }
  506. doinclude:=true;
  507. case s[length(s)] of
  508. '+':
  509. s:=copy(s,1,length(s)-1);
  510. '-':
  511. begin
  512. s:=copy(s,1,length(s)-1);
  513. doinclude:=false;
  514. end;
  515. end;
  516. Result:=false;
  517. for i:=m_class to high(tmodeswitch) do
  518. if s=modeswitchstr[i] then
  519. begin
  520. { Objective-C is currently only supported for Darwin targets }
  521. if doinclude and
  522. (i in [m_objectivec1,m_objectivec2]) and
  523. not(target_info.system in systems_objc_supported) then
  524. begin
  525. Message1(option_unsupported_target_for_feature,'Objective-C');
  526. break;
  527. end;
  528. if changeInit then
  529. current_settings.modeswitches:=init_settings.modeswitches;
  530. Result:=true;
  531. if doinclude then
  532. begin
  533. include(current_settings.modeswitches,i);
  534. { Objective-C 2.0 support implies 1.0 support }
  535. if (i=m_objectivec2) then
  536. include(current_settings.modeswitches,m_objectivec1);
  537. if (i in [m_objectivec1,m_objectivec2]) then
  538. include(current_settings.modeswitches,m_class);
  539. end
  540. else
  541. begin
  542. exclude(current_settings.modeswitches,i);
  543. { Objective-C 2.0 support implies 1.0 support }
  544. if (i=m_objectivec2) then
  545. exclude(current_settings.modeswitches,m_objectivec1);
  546. if (i in [m_objectivec1,m_objectivec2]) and
  547. ([m_delphi,m_objfpc]*current_settings.modeswitches=[]) then
  548. exclude(current_settings.modeswitches,m_class);
  549. end;
  550. { set other switches depending on changed mode switch }
  551. HandleModeSwitches(i,changeinit);
  552. if changeInit then
  553. init_settings.modeswitches:=current_settings.modeswitches;
  554. break;
  555. end;
  556. end;
  557. procedure SetAppType(NewAppType:tapptype);
  558. begin
  559. {$ifdef i8086}
  560. if (target_info.system=system_i8086_msdos) and (apptype<>NewAppType) then
  561. begin
  562. if NewAppType=app_com then
  563. begin
  564. targetinfos[system_i8086_msdos]^.exeext:='.com';
  565. target_info.exeext:='.com';
  566. end
  567. else
  568. begin
  569. targetinfos[system_i8086_msdos]^.exeext:='.exe';
  570. target_info.exeext:='.exe';
  571. end;
  572. end;
  573. {$endif i8086}
  574. if apptype in [app_cui,app_com] then
  575. undef_system_macro('CONSOLE');
  576. apptype:=NewAppType;
  577. if apptype in [app_cui,app_com] then
  578. def_system_macro('CONSOLE');
  579. end;
  580. {*****************************************************************************
  581. Conditional Directives
  582. *****************************************************************************}
  583. procedure dir_else;
  584. begin
  585. current_scanner.elsepreprocstack;
  586. end;
  587. procedure dir_endif;
  588. begin
  589. current_scanner.poppreprocstack;
  590. end;
  591. function isdef(var valuedescr: String): Boolean;
  592. var
  593. hs : string;
  594. begin
  595. current_scanner.skipspace;
  596. hs:=current_scanner.readid;
  597. valuedescr:= hs;
  598. if hs='' then
  599. Message(scan_e_error_in_preproc_expr);
  600. isdef:=defined_macro(hs);
  601. end;
  602. procedure dir_ifdef;
  603. begin
  604. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  605. end;
  606. function isnotdef(var valuedescr: String): Boolean;
  607. var
  608. hs : string;
  609. begin
  610. current_scanner.skipspace;
  611. hs:=current_scanner.readid;
  612. valuedescr:= hs;
  613. if hs='' then
  614. Message(scan_e_error_in_preproc_expr);
  615. isnotdef:=not defined_macro(hs);
  616. end;
  617. procedure dir_ifndef;
  618. begin
  619. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  620. end;
  621. function opt_check(var valuedescr: String): Boolean;
  622. var
  623. hs : string;
  624. state : char;
  625. begin
  626. opt_check:= false;
  627. current_scanner.skipspace;
  628. hs:=current_scanner.readid;
  629. valuedescr:= hs;
  630. if (length(hs)>1) then
  631. Message1(scan_w_illegal_switch,hs)
  632. else
  633. begin
  634. state:=current_scanner.ReadState;
  635. if state in ['-','+'] then
  636. opt_check:=CheckSwitch(hs[1],state)
  637. else
  638. Message(scan_e_error_in_preproc_expr);
  639. end;
  640. end;
  641. procedure dir_ifopt;
  642. begin
  643. flushpendingswitchesstate;
  644. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  645. end;
  646. procedure dir_libprefix;
  647. var
  648. s : string;
  649. begin
  650. current_scanner.skipspace;
  651. if c <> '''' then
  652. Message2(scan_f_syn_expected, '''', c);
  653. s := current_scanner.readquotedstring;
  654. stringdispose(outputprefix);
  655. outputprefix := stringdup(s);
  656. with current_module do
  657. setfilename(paramfn, paramallowoutput);
  658. end;
  659. procedure dir_libsuffix;
  660. var
  661. s : string;
  662. begin
  663. current_scanner.skipspace;
  664. if c <> '''' then
  665. Message2(scan_f_syn_expected, '''', c);
  666. s := current_scanner.readquotedstring;
  667. stringdispose(outputsuffix);
  668. outputsuffix := stringdup(s);
  669. with current_module do
  670. setfilename(paramfn, paramallowoutput);
  671. end;
  672. procedure dir_extension;
  673. var
  674. s : string;
  675. begin
  676. current_scanner.skipspace;
  677. if c <> '''' then
  678. Message2(scan_f_syn_expected, '''', c);
  679. s := current_scanner.readquotedstring;
  680. if OutputFileName='' then
  681. OutputFileName:=InputFileName;
  682. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  683. with current_module do
  684. setfilename(paramfn, paramallowoutput);
  685. end;
  686. {
  687. Compile time expression type check
  688. ----------------------------------
  689. Each subexpression returns its type to the caller, which then can
  690. do type check. Since data types of compile time expressions is
  691. not well defined, the type system does a best effort. The drawback is
  692. that some errors might not be detected.
  693. Instead of returning a particular data type, a set of possible data types
  694. are returned. This way ambigouos types can be handled. For instance a
  695. value of 1 can be both a boolean and and integer.
  696. Booleans
  697. --------
  698. The following forms of boolean values are supported:
  699. * C coded, that is 0 is false, non-zero is true.
  700. * TRUE/FALSE for mac style compile time variables
  701. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  702. When a compile time expression is evaluated, they are then translated
  703. to C coded booleans (0/1), to simplify for the expression evaluator.
  704. Note that this scheme then also of support mac compile time variables which
  705. are 0/1 but with a boolean meaning.
  706. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  707. means that units which is not recompiled, and thus stores
  708. compile time variables as the old format (0/1), continue to work.
  709. Short circuit evaluation
  710. ------------------------
  711. For this to work, the part of a compile time expression which is short
  712. circuited, should not be evaluated, while it still should be parsed.
  713. Therefor there is a parameter eval, telling whether evaluation is needed.
  714. In case not, the value returned can be arbitrary.
  715. }
  716. type
  717. { texprvalue }
  718. texprvalue = class
  719. private
  720. { we can't use built-in defs since they
  721. may be not created at the moment }
  722. class var
  723. sintdef,uintdef,booldef,strdef,setdef,realdef: tdef;
  724. class constructor createdefs;
  725. class destructor destroydefs;
  726. public
  727. consttyp: tconsttyp;
  728. value: tconstvalue;
  729. def: tdef;
  730. constructor create_const(c:tconstsym);
  731. constructor create_error;
  732. constructor create_ord(v: Tconstexprint);
  733. constructor create_int(v: int64);
  734. constructor create_uint(v: qword);
  735. constructor create_bool(b: boolean);
  736. constructor create_str(s: string);
  737. constructor create_set(ns: tnormalset);
  738. constructor create_real(r: bestreal);
  739. class function try_parse_number(s:string):texprvalue; static;
  740. class function try_parse_real(s:string):texprvalue; static;
  741. function evaluate(v:texprvalue;op:ttoken):texprvalue;
  742. procedure error(expecteddef, place: string);
  743. function isBoolean: Boolean;
  744. function asBool: Boolean;
  745. function asInt: Integer;
  746. function asStr: String;
  747. destructor destroy; override;
  748. end;
  749. class constructor texprvalue.createdefs;
  750. begin
  751. sintdef:=torddef.create(s64bit,low(int64),high(int64));
  752. uintdef:=torddef.create(u64bit,low(qword),high(qword));
  753. booldef:=torddef.create(pasbool8,0,1);
  754. strdef:=tstringdef.createansi(0);
  755. setdef:=tsetdef.create(sintdef,0,255);
  756. realdef:=tfloatdef.create(s80real);
  757. end;
  758. class destructor texprvalue.destroydefs;
  759. begin
  760. setdef.free;
  761. sintdef.free;
  762. uintdef.free;
  763. booldef.free;
  764. strdef.free;
  765. realdef.free;
  766. end;
  767. constructor texprvalue.create_const(c: tconstsym);
  768. begin
  769. consttyp:=c.consttyp;
  770. def:=c.constdef;
  771. case consttyp of
  772. conststring,
  773. constresourcestring:
  774. begin
  775. value.len:=c.value.len;
  776. getmem(value.valueptr,value.len+1);
  777. move(c.value.valueptr^,value.valueptr,value.len+1);
  778. end;
  779. constwstring:
  780. begin
  781. initwidestring(value.valueptr);
  782. copywidestring(c.value.valueptr,value.valueptr);
  783. end;
  784. constreal:
  785. begin
  786. new(pbestreal(value.valueptr));
  787. pbestreal(value.valueptr)^:=pbestreal(c.value.valueptr)^;
  788. end;
  789. constset:
  790. begin
  791. new(pnormalset(value.valueptr));
  792. pnormalset(value.valueptr)^:=pnormalset(c.value.valueptr)^;
  793. end;
  794. constguid:
  795. begin
  796. new(pguid(value.valueptr));
  797. pguid(value.valueptr)^:=pguid(c.value.valueptr)^;
  798. end;
  799. else
  800. value:=c.value;
  801. end;
  802. end;
  803. constructor texprvalue.create_error;
  804. begin
  805. fillchar(value,sizeof(value),#0);
  806. consttyp:=constnone;
  807. def:=generrordef;
  808. end;
  809. constructor texprvalue.create_ord(v: Tconstexprint);
  810. begin
  811. fillchar(value,sizeof(value),#0);
  812. consttyp:=constord;
  813. value.valueord:=v;
  814. if v.signed then
  815. def:=sintdef
  816. else
  817. def:=uintdef;
  818. end;
  819. constructor texprvalue.create_int(v: int64);
  820. begin
  821. fillchar(value,sizeof(value),#0);
  822. consttyp:=constord;
  823. value.valueord:=v;
  824. def:=sintdef;
  825. end;
  826. constructor texprvalue.create_uint(v: qword);
  827. begin
  828. fillchar(value,sizeof(value),#0);
  829. consttyp:=constord;
  830. value.valueord:=v;
  831. def:=uintdef;
  832. end;
  833. constructor texprvalue.create_bool(b: boolean);
  834. begin
  835. fillchar(value,sizeof(value),#0);
  836. consttyp:=constord;
  837. value.valueord:=ord(b);
  838. def:=booldef;
  839. end;
  840. constructor texprvalue.create_str(s: string);
  841. var
  842. sp: pansichar;
  843. len: integer;
  844. begin
  845. fillchar(value,sizeof(value),#0);
  846. consttyp:=conststring;
  847. len:=length(s);
  848. getmem(sp,len+1);
  849. move(s[1],sp^,len+1);
  850. value.valueptr:=sp;
  851. value.len:=length(s);
  852. def:=strdef;
  853. end;
  854. constructor texprvalue.create_set(ns: tnormalset);
  855. begin
  856. fillchar(value,sizeof(value),#0);
  857. consttyp:=constset;
  858. new(pnormalset(value.valueptr));
  859. pnormalset(value.valueptr)^:=ns;
  860. def:=setdef;
  861. end;
  862. constructor texprvalue.create_real(r: bestreal);
  863. begin
  864. fillchar(value,sizeof(value),#0);
  865. consttyp:=constreal;
  866. new(pbestreal(value.valueptr));
  867. pbestreal(value.valueptr)^:=r;
  868. def:=realdef;
  869. end;
  870. class function texprvalue.try_parse_number(s:string):texprvalue;
  871. var
  872. ic: int64;
  873. qc: qword;
  874. code: integer;
  875. begin
  876. { try int64 }
  877. val(s,ic,code);
  878. if code=0 then
  879. result:=texprvalue.create_int(ic)
  880. else
  881. begin
  882. { try qword }
  883. val(s,qc,code);
  884. if code=0 then
  885. result:=texprvalue.create_uint(qc)
  886. else
  887. result:=try_parse_real(s);
  888. end;
  889. end;
  890. class function texprvalue.try_parse_real(s:string):texprvalue;
  891. var
  892. d: bestreal;
  893. code: integer;
  894. begin
  895. val(s,d,code);
  896. if code=0 then
  897. result:=texprvalue.create_real(d)
  898. else
  899. result:=nil;
  900. end;
  901. function texprvalue.evaluate(v:texprvalue;op:ttoken):texprvalue;
  902. function check_compatbile: boolean;
  903. begin
  904. result:=(
  905. (is_ordinal(v.def) or is_fpu(v.def)) and
  906. (is_ordinal(def) or is_fpu(def))
  907. ) or
  908. (is_string(v.def) and is_string(def));
  909. if not result then
  910. Message2(type_e_incompatible_types,def.typename,v.def.typename);
  911. end;
  912. var
  913. lv,rv: tconstexprint;
  914. lvd,rvd: bestreal;
  915. lvs,rvs: string;
  916. begin
  917. case op of
  918. _OP_IN:
  919. begin
  920. if not is_set(v.def) then
  921. begin
  922. v.error('Set', 'IN');
  923. result:=texprvalue.create_error;
  924. end
  925. else
  926. if not is_ordinal(def) then
  927. begin
  928. error('Ordinal', 'IN');
  929. result:=texprvalue.create_error;
  930. end
  931. else
  932. if value.valueord.signed then
  933. result:=texprvalue.create_bool(value.valueord.svalue in pnormalset(v.value.valueptr)^)
  934. else
  935. result:=texprvalue.create_bool(value.valueord.uvalue in pnormalset(v.value.valueptr)^);
  936. end;
  937. _OP_NOT:
  938. begin
  939. if isBoolean then
  940. result:=texprvalue.create_bool(not asBool)
  941. else
  942. begin
  943. error('Boolean', 'NOT');
  944. result:=texprvalue.create_error;
  945. end;
  946. end;
  947. _OP_OR:
  948. begin
  949. if isBoolean then
  950. if v.isBoolean then
  951. result:=texprvalue.create_bool(asBool or v.asBool)
  952. else
  953. begin
  954. v.error('Boolean','OR');
  955. result:=texprvalue.create_error;
  956. end
  957. else
  958. begin
  959. error('Boolean','OR');
  960. result:=texprvalue.create_error;
  961. end;
  962. end;
  963. _OP_XOR:
  964. begin
  965. if isBoolean then
  966. if v.isBoolean then
  967. result:=texprvalue.create_bool(asBool xor v.asBool)
  968. else
  969. begin
  970. v.error('Boolean','XOR');
  971. result:=texprvalue.create_error;
  972. end
  973. else
  974. begin
  975. error('Boolean','XOR');
  976. result:=texprvalue.create_error;
  977. end;
  978. end;
  979. _OP_AND:
  980. begin
  981. if isBoolean then
  982. if v.isBoolean then
  983. result:=texprvalue.create_bool(asBool and v.asBool)
  984. else
  985. begin
  986. v.error('Boolean','AND');
  987. result:=texprvalue.create_error;
  988. end
  989. else
  990. begin
  991. error('Boolean','AND');
  992. result:=texprvalue.create_error;
  993. end;
  994. end;
  995. _EQ,_NE,_LT,_GT,_GTE,_LTE,_PLUS,_MINUS,_STAR,_SLASH,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR:
  996. if check_compatbile then
  997. begin
  998. if (is_ordinal(def) and is_ordinal(v.def)) then
  999. begin
  1000. lv:=value.valueord;
  1001. rv:=v.value.valueord;
  1002. case op of
  1003. _EQ:
  1004. result:=texprvalue.create_bool(lv=rv);
  1005. _NE:
  1006. result:=texprvalue.create_bool(lv<>rv);
  1007. _LT:
  1008. result:=texprvalue.create_bool(lv<rv);
  1009. _GT:
  1010. result:=texprvalue.create_bool(lv>rv);
  1011. _GTE:
  1012. result:=texprvalue.create_bool(lv>=rv);
  1013. _LTE:
  1014. result:=texprvalue.create_bool(lv<=rv);
  1015. _PLUS:
  1016. result:=texprvalue.create_ord(lv+rv);
  1017. _MINUS:
  1018. result:=texprvalue.create_ord(lv-rv);
  1019. _STAR:
  1020. result:=texprvalue.create_ord(lv*rv);
  1021. _SLASH:
  1022. result:=texprvalue.create_real(lv/rv);
  1023. _OP_DIV:
  1024. result:=texprvalue.create_ord(lv div rv);
  1025. _OP_MOD:
  1026. result:=texprvalue.create_ord(lv mod rv);
  1027. _OP_SHL:
  1028. result:=texprvalue.create_ord(lv shl rv);
  1029. _OP_SHR:
  1030. result:=texprvalue.create_ord(lv shr rv);
  1031. else
  1032. begin
  1033. { actually we should never get here but this avoids a warning }
  1034. Message(parser_e_illegal_expression);
  1035. result:=texprvalue.create_error;
  1036. end;
  1037. end;
  1038. end
  1039. else
  1040. if (is_fpu(def) or is_ordinal(def)) and
  1041. (is_fpu(v.def) or is_ordinal(v.def)) then
  1042. begin
  1043. if is_fpu(def) then
  1044. lvd:=pbestreal(value.valueptr)^
  1045. else
  1046. lvd:=value.valueord;
  1047. if is_fpu(v.def) then
  1048. rvd:=pbestreal(v.value.valueptr)^
  1049. else
  1050. rvd:=v.value.valueord;
  1051. case op of
  1052. _EQ:
  1053. result:=texprvalue.create_bool(lvd=rvd);
  1054. _NE:
  1055. result:=texprvalue.create_bool(lvd<>rvd);
  1056. _LT:
  1057. result:=texprvalue.create_bool(lvd<rvd);
  1058. _GT:
  1059. result:=texprvalue.create_bool(lvd>rvd);
  1060. _GTE:
  1061. result:=texprvalue.create_bool(lvd>=rvd);
  1062. _LTE:
  1063. result:=texprvalue.create_bool(lvd<=rvd);
  1064. _PLUS:
  1065. result:=texprvalue.create_real(lvd+rvd);
  1066. _MINUS:
  1067. result:=texprvalue.create_real(lvd-rvd);
  1068. _STAR:
  1069. result:=texprvalue.create_real(lvd*rvd);
  1070. _SLASH:
  1071. result:=texprvalue.create_real(lvd/rvd);
  1072. else
  1073. begin
  1074. Message(parser_e_illegal_expression);
  1075. result:=texprvalue.create_error;
  1076. end;
  1077. end;
  1078. end
  1079. else
  1080. begin
  1081. lvs:=asStr;
  1082. rvs:=v.asStr;
  1083. case op of
  1084. _EQ:
  1085. result:=texprvalue.create_bool(lvs=rvs);
  1086. _NE:
  1087. result:=texprvalue.create_bool(lvs<>rvs);
  1088. _LT:
  1089. result:=texprvalue.create_bool(lvs<rvs);
  1090. _GT:
  1091. result:=texprvalue.create_bool(lvs>rvs);
  1092. _GTE:
  1093. result:=texprvalue.create_bool(lvs>=rvs);
  1094. _LTE:
  1095. result:=texprvalue.create_bool(lvs<=rvs);
  1096. _PLUS:
  1097. result:=texprvalue.create_str(lvs+rvs);
  1098. else
  1099. begin
  1100. Message(parser_e_illegal_expression);
  1101. result:=texprvalue.create_error;
  1102. end;
  1103. end;
  1104. end;
  1105. end
  1106. else
  1107. result:=texprvalue.create_error;
  1108. else
  1109. result:=texprvalue.create_error;
  1110. end;
  1111. end;
  1112. procedure texprvalue.error(expecteddef, place: string);
  1113. begin
  1114. Message3(scan_e_compile_time_typeerror,
  1115. expecteddef,
  1116. def.typename,
  1117. place
  1118. );
  1119. end;
  1120. function texprvalue.isBoolean: Boolean;
  1121. var
  1122. i: integer;
  1123. begin
  1124. result:=is_boolean(def);
  1125. if not result and is_integer(def) then
  1126. begin
  1127. i:=asInt;
  1128. result:=(i=0)or(i=1);
  1129. end;
  1130. end;
  1131. function texprvalue.asBool: Boolean;
  1132. begin
  1133. result:=value.valueord<>0;
  1134. end;
  1135. function texprvalue.asInt: Integer;
  1136. begin
  1137. result:=value.valueord.svalue;
  1138. end;
  1139. function texprvalue.asStr: String;
  1140. var
  1141. b:byte;
  1142. begin
  1143. case consttyp of
  1144. constord:
  1145. result:=tostr(value.valueord);
  1146. conststring,
  1147. constresourcestring:
  1148. SetString(result,pchar(value.valueptr),value.len);
  1149. constreal:
  1150. str(pbestreal(value.valueptr)^,result);
  1151. constset:
  1152. begin
  1153. result:=',';
  1154. for b:=0 to 255 do
  1155. if b in pconstset(value.valueptr)^ then
  1156. result:=result+tostr(b)+',';
  1157. end;
  1158. else
  1159. internalerror(2013112801);
  1160. end;
  1161. end;
  1162. destructor texprvalue.destroy;
  1163. begin
  1164. case consttyp of
  1165. conststring,
  1166. constresourcestring :
  1167. freemem(pchar(value.valueptr),value.len+1);
  1168. constwstring :
  1169. donewidestring(pcompilerwidestring(value.valueptr));
  1170. constreal :
  1171. dispose(pbestreal(value.valueptr));
  1172. constset :
  1173. dispose(pnormalset(value.valueptr));
  1174. constguid :
  1175. dispose(pguid(value.valueptr));
  1176. constord,
  1177. { error values }
  1178. constnone:
  1179. ;
  1180. else
  1181. internalerror(2013112802);
  1182. end;
  1183. inherited destroy;
  1184. end;
  1185. const
  1186. preproc_operators=[_EQ,_NE,_LT,_GT,_LTE,_GTE,_MINUS,_PLUS,_STAR,_SLASH,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR,_OP_IN,_OP_AND,_OP_OR,_OP_XOR];
  1187. function preproc_comp_expr:texprvalue;
  1188. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean):texprvalue; forward;
  1189. procedure preproc_consume(t:ttoken);
  1190. begin
  1191. if t<>current_scanner.preproc_token then
  1192. Message(scan_e_preproc_syntax_error);
  1193. current_scanner.preproc_token:=current_scanner.readpreproc;
  1194. end;
  1195. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;out tokentoconsume:ttoken):boolean;
  1196. var
  1197. hmodule: tmodule;
  1198. ns:ansistring;
  1199. nssym:tsym;
  1200. begin
  1201. result:=false;
  1202. tokentoconsume:=_ID;
  1203. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  1204. begin
  1205. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  1206. internalerror(200501154);
  1207. { only allow unit.symbol access if the name was
  1208. found in the current module
  1209. we can use iscurrentunit because generic specializations does not
  1210. change current_unit variable }
  1211. hmodule:=find_module_from_symtable(srsym.Owner);
  1212. if not Assigned(hmodule) then
  1213. internalerror(201001120);
  1214. if hmodule.unit_index=current_filepos.moduleindex then
  1215. begin
  1216. preproc_consume(_POINT);
  1217. current_scanner.skipspace;
  1218. if srsym.typ=namespacesym then
  1219. begin
  1220. ns:=srsym.name;
  1221. nssym:=srsym;
  1222. while assigned(srsym) and (srsym.typ=namespacesym) do
  1223. begin
  1224. { we have a namespace. the next identifier should be either a namespace or a unit }
  1225. searchsym_in_module(hmodule,ns+'.'+current_scanner.preproc_pattern,srsym,srsymtable);
  1226. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  1227. begin
  1228. ns:=ns+'.'+current_scanner.preproc_pattern;
  1229. nssym:=srsym;
  1230. preproc_consume(_ID);
  1231. current_scanner.skipspace;
  1232. preproc_consume(_POINT);
  1233. current_scanner.skipspace;
  1234. end;
  1235. end;
  1236. { check if there is a hidden unit with this pattern in the namespace }
  1237. if not assigned(srsym) and
  1238. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  1239. srsym:=tnamespacesym(nssym).unitsym;
  1240. if assigned(srsym) and (srsym.typ<>unitsym) then
  1241. internalerror(201108260);
  1242. if not assigned(srsym) then
  1243. begin
  1244. result:=true;
  1245. srsymtable:=nil;
  1246. exit;
  1247. end;
  1248. end;
  1249. case current_scanner.preproc_token of
  1250. _ID:
  1251. { system.char? (char=widechar comes from the implicit
  1252. uuchar unit -> override) }
  1253. if (current_scanner.preproc_pattern='CHAR') and
  1254. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  1255. begin
  1256. if m_default_unicodestring in current_settings.modeswitches then
  1257. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  1258. else
  1259. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  1260. end
  1261. else
  1262. searchsym_in_module(tunitsym(srsym).module,current_scanner.preproc_pattern,srsym,srsymtable);
  1263. _STRING:
  1264. begin
  1265. { system.string? }
  1266. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  1267. begin
  1268. if cs_refcountedstrings in current_settings.localswitches then
  1269. begin
  1270. if m_default_unicodestring in current_settings.modeswitches then
  1271. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  1272. else
  1273. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  1274. end
  1275. else
  1276. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  1277. tokentoconsume:=_STRING;
  1278. end;
  1279. end
  1280. end;
  1281. end
  1282. else
  1283. begin
  1284. srsym:=nil;
  1285. srsymtable:=nil;
  1286. end;
  1287. result:=true;
  1288. end;
  1289. end;
  1290. procedure try_consume_nestedsym(var srsym:tsym;var srsymtable:TSymtable);
  1291. var
  1292. def:tdef;
  1293. tokentoconsume:ttoken;
  1294. found:boolean;
  1295. begin
  1296. found:=try_consume_unitsym(srsym,srsymtable,tokentoconsume);
  1297. if found then
  1298. begin
  1299. preproc_consume(tokentoconsume);
  1300. current_scanner.skipspace;
  1301. end;
  1302. while (current_scanner.preproc_token=_POINT) do
  1303. begin
  1304. if assigned(srsym)and(srsym.typ=typesym) then
  1305. begin
  1306. def:=ttypesym(srsym).typedef;
  1307. if is_class_or_object(def) or is_record(def) or is_java_class_or_interface(def) then
  1308. begin
  1309. preproc_consume(_POINT);
  1310. current_scanner.skipspace;
  1311. if def.typ=objectdef then
  1312. found:=searchsym_in_class(tobjectdef(def),tobjectdef(def),current_scanner.preproc_pattern,srsym,srsymtable,[ssf_search_helper])
  1313. else
  1314. found:=searchsym_in_record(trecorddef(def),current_scanner.preproc_pattern,srsym,srsymtable);
  1315. if not found then
  1316. begin
  1317. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  1318. exit;
  1319. end;
  1320. preproc_consume(_ID);
  1321. current_scanner.skipspace;
  1322. end
  1323. else
  1324. begin
  1325. Message(sym_e_type_must_be_rec_or_object_or_class);
  1326. exit;
  1327. end;
  1328. end
  1329. else
  1330. begin
  1331. Message(type_e_type_id_expected);
  1332. exit;
  1333. end;
  1334. end;
  1335. end;
  1336. function preproc_substitutedtoken(searchstr:string;eval:Boolean):texprvalue;
  1337. { Currently this parses identifiers as well as numbers.
  1338. The result from this procedure can either be that the token
  1339. itself is a value, or that it is a compile time variable/macro,
  1340. which then is substituted for another value (for macros
  1341. recursivelly substituted).}
  1342. var
  1343. hs: string;
  1344. mac: tmacro;
  1345. macrocount,
  1346. len: integer;
  1347. begin
  1348. if not eval then
  1349. begin
  1350. result:=texprvalue.create_str(searchstr);
  1351. exit;
  1352. end;
  1353. mac:=nil;
  1354. { Substitue macros and compiler variables with their content/value.
  1355. For real macros also do recursive substitution. }
  1356. macrocount:=0;
  1357. repeat
  1358. mac:=tmacro(search_macro(searchstr));
  1359. inc(macrocount);
  1360. if macrocount>max_macro_nesting then
  1361. begin
  1362. Message(scan_w_macro_too_deep);
  1363. break;
  1364. end;
  1365. if assigned(mac) and mac.defined then
  1366. if assigned(mac.buftext) then
  1367. begin
  1368. if mac.buflen>255 then
  1369. begin
  1370. len:=255;
  1371. Message(scan_w_macro_cut_after_255_chars);
  1372. end
  1373. else
  1374. len:=mac.buflen;
  1375. hs[0]:=char(len);
  1376. move(mac.buftext^,hs[1],len);
  1377. searchstr:=upcase(hs);
  1378. mac.is_used:=true;
  1379. end
  1380. else
  1381. begin
  1382. Message1(scan_e_error_macro_lacks_value,searchstr);
  1383. break;
  1384. end
  1385. else
  1386. break;
  1387. if mac.is_compiler_var then
  1388. break;
  1389. until false;
  1390. { At this point, result do contain the value. Do some decoding and
  1391. determine the type.}
  1392. result:=texprvalue.try_parse_number(searchstr);
  1393. if not assigned(result) then
  1394. begin
  1395. if assigned(mac) and (searchstr='FALSE') then
  1396. result:=texprvalue.create_bool(false)
  1397. else if assigned(mac) and (searchstr='TRUE') then
  1398. result:=texprvalue.create_bool(true)
  1399. else if (m_mac in current_settings.modeswitches) and
  1400. (not assigned(mac) or not mac.defined) and
  1401. (macrocount = 1) then
  1402. begin
  1403. {Errors in mode mac is issued here. For non macpas modes there is
  1404. more liberty, but the error will eventually be caught at a later stage.}
  1405. Message1(scan_e_error_macro_undefined,searchstr);
  1406. result:=texprvalue.create_str(searchstr); { just to have something }
  1407. end
  1408. else
  1409. result:=texprvalue.create_str(searchstr);
  1410. end;
  1411. end;
  1412. function preproc_factor(eval: Boolean):texprvalue;
  1413. var
  1414. hs,countstr,storedpattern: string;
  1415. mac: tmacro;
  1416. srsym : tsym;
  1417. srsymtable : TSymtable;
  1418. hdef : TDef;
  1419. l : longint;
  1420. hasKlammer: Boolean;
  1421. exprvalue:texprvalue;
  1422. ns:tnormalset;
  1423. begin
  1424. result:=nil;
  1425. hasKlammer:=false;
  1426. if current_scanner.preproc_token=_ID then
  1427. begin
  1428. if current_scanner.preproc_pattern='DEFINED' then
  1429. begin
  1430. preproc_consume(_ID);
  1431. current_scanner.skipspace;
  1432. if current_scanner.preproc_token =_LKLAMMER then
  1433. begin
  1434. preproc_consume(_LKLAMMER);
  1435. current_scanner.skipspace;
  1436. hasKlammer:= true;
  1437. end
  1438. else if (m_mac in current_settings.modeswitches) then
  1439. hasKlammer:= false
  1440. else
  1441. Message(scan_e_error_in_preproc_expr);
  1442. if current_scanner.preproc_token =_ID then
  1443. begin
  1444. hs := current_scanner.preproc_pattern;
  1445. mac := tmacro(search_macro(hs));
  1446. if assigned(mac) and mac.defined then
  1447. begin
  1448. result:=texprvalue.create_bool(true);
  1449. mac.is_used:=true;
  1450. end
  1451. else
  1452. result:=texprvalue.create_bool(false);
  1453. preproc_consume(_ID);
  1454. current_scanner.skipspace;
  1455. end
  1456. else
  1457. Message(scan_e_error_in_preproc_expr);
  1458. if hasKlammer then
  1459. if current_scanner.preproc_token =_RKLAMMER then
  1460. preproc_consume(_RKLAMMER)
  1461. else
  1462. Message(scan_e_error_in_preproc_expr);
  1463. end
  1464. else
  1465. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  1466. begin
  1467. preproc_consume(_ID);
  1468. current_scanner.skipspace;
  1469. if current_scanner.preproc_token =_ID then
  1470. begin
  1471. hs := current_scanner.preproc_pattern;
  1472. mac := tmacro(search_macro(hs));
  1473. if assigned(mac) then
  1474. begin
  1475. result:=texprvalue.create_bool(false);
  1476. mac.is_used:=true;
  1477. end
  1478. else
  1479. result:=texprvalue.create_bool(true);
  1480. preproc_consume(_ID);
  1481. current_scanner.skipspace;
  1482. end
  1483. else
  1484. Message(scan_e_error_in_preproc_expr);
  1485. end
  1486. else
  1487. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  1488. begin
  1489. preproc_consume(_ID);
  1490. current_scanner.skipspace;
  1491. if current_scanner.preproc_token =_LKLAMMER then
  1492. begin
  1493. preproc_consume(_LKLAMMER);
  1494. current_scanner.skipspace;
  1495. end
  1496. else
  1497. Message(scan_e_error_in_preproc_expr);
  1498. if not (current_scanner.preproc_token = _ID) then
  1499. Message(scan_e_error_in_preproc_expr);
  1500. hs:=current_scanner.preproc_pattern;
  1501. if (length(hs) > 1) then
  1502. {This is allowed in Metrowerks Pascal}
  1503. Message(scan_e_error_in_preproc_expr)
  1504. else
  1505. begin
  1506. if CheckSwitch(hs[1],'+') then
  1507. result:=texprvalue.create_bool(true)
  1508. else
  1509. result:=texprvalue.create_bool(false);
  1510. end;
  1511. preproc_consume(_ID);
  1512. current_scanner.skipspace;
  1513. if current_scanner.preproc_token =_RKLAMMER then
  1514. preproc_consume(_RKLAMMER)
  1515. else
  1516. Message(scan_e_error_in_preproc_expr);
  1517. end
  1518. else
  1519. if current_scanner.preproc_pattern='SIZEOF' then
  1520. begin
  1521. preproc_consume(_ID);
  1522. current_scanner.skipspace;
  1523. if current_scanner.preproc_token =_LKLAMMER then
  1524. begin
  1525. preproc_consume(_LKLAMMER);
  1526. current_scanner.skipspace;
  1527. end
  1528. else
  1529. Message(scan_e_preproc_syntax_error);
  1530. storedpattern:=current_scanner.preproc_pattern;
  1531. preproc_consume(_ID);
  1532. current_scanner.skipspace;
  1533. if eval then
  1534. if searchsym(storedpattern,srsym,srsymtable) then
  1535. begin
  1536. try_consume_nestedsym(srsym,srsymtable);
  1537. l:=0;
  1538. if assigned(srsym) then
  1539. case srsym.typ of
  1540. staticvarsym,
  1541. localvarsym,
  1542. paravarsym :
  1543. l:=tabstractvarsym(srsym).getsize;
  1544. typesym:
  1545. l:=ttypesym(srsym).typedef.size;
  1546. else
  1547. Message(scan_e_error_in_preproc_expr);
  1548. end;
  1549. result:=texprvalue.create_int(l);
  1550. end
  1551. else
  1552. Message1(sym_e_id_not_found,storedpattern);
  1553. if current_scanner.preproc_token =_RKLAMMER then
  1554. preproc_consume(_RKLAMMER)
  1555. else
  1556. Message(scan_e_preproc_syntax_error);
  1557. end
  1558. else
  1559. if current_scanner.preproc_pattern='HIGH' then
  1560. begin
  1561. preproc_consume(_ID);
  1562. current_scanner.skipspace;
  1563. if current_scanner.preproc_token =_LKLAMMER then
  1564. begin
  1565. preproc_consume(_LKLAMMER);
  1566. current_scanner.skipspace;
  1567. end
  1568. else
  1569. Message(scan_e_preproc_syntax_error);
  1570. storedpattern:=current_scanner.preproc_pattern;
  1571. preproc_consume(_ID);
  1572. current_scanner.skipspace;
  1573. if eval then
  1574. if searchsym(storedpattern,srsym,srsymtable) then
  1575. begin
  1576. try_consume_nestedsym(srsym,srsymtable);
  1577. hdef:=nil;
  1578. hs:='';
  1579. l:=0;
  1580. if assigned(srsym) then
  1581. case srsym.typ of
  1582. staticvarsym,
  1583. localvarsym,
  1584. paravarsym :
  1585. hdef:=tabstractvarsym(srsym).vardef;
  1586. typesym:
  1587. hdef:=ttypesym(srsym).typedef;
  1588. else
  1589. Message(scan_e_error_in_preproc_expr);
  1590. end;
  1591. if assigned(hdef) then
  1592. begin
  1593. if hdef.typ=setdef then
  1594. hdef:=tsetdef(hdef).elementdef;
  1595. case hdef.typ of
  1596. orddef:
  1597. with torddef(hdef).high do
  1598. if signed then
  1599. result:=texprvalue.create_int(svalue)
  1600. else
  1601. result:=texprvalue.create_uint(uvalue);
  1602. enumdef:
  1603. result:=texprvalue.create_int(tenumdef(hdef).maxval);
  1604. arraydef:
  1605. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1606. Message(type_e_mismatch)
  1607. else
  1608. result:=texprvalue.create_int(tarraydef(hdef).highrange);
  1609. stringdef:
  1610. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1611. Message(type_e_mismatch)
  1612. else
  1613. result:=texprvalue.create_int(tstringdef(hdef).len);
  1614. else
  1615. Message(type_e_mismatch);
  1616. end;
  1617. end;
  1618. end
  1619. else
  1620. Message1(sym_e_id_not_found,storedpattern);
  1621. if current_scanner.preproc_token =_RKLAMMER then
  1622. preproc_consume(_RKLAMMER)
  1623. else
  1624. Message(scan_e_preproc_syntax_error);
  1625. end
  1626. else
  1627. if current_scanner.preproc_pattern='DECLARED' then
  1628. begin
  1629. preproc_consume(_ID);
  1630. current_scanner.skipspace;
  1631. if current_scanner.preproc_token =_LKLAMMER then
  1632. begin
  1633. preproc_consume(_LKLAMMER);
  1634. current_scanner.skipspace;
  1635. end
  1636. else
  1637. Message(scan_e_error_in_preproc_expr);
  1638. if current_scanner.preproc_token =_ID then
  1639. begin
  1640. hs := upper(current_scanner.preproc_pattern);
  1641. preproc_consume(_ID);
  1642. current_scanner.skipspace;
  1643. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  1644. begin
  1645. l:=1;
  1646. preproc_consume(current_scanner.preproc_token);
  1647. current_scanner.skipspace;
  1648. while current_scanner.preproc_token=_COMMA do
  1649. begin
  1650. inc(l);
  1651. preproc_consume(_COMMA);
  1652. current_scanner.skipspace;
  1653. end;
  1654. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  1655. Message(scan_e_error_in_preproc_expr)
  1656. else
  1657. preproc_consume(current_scanner.preproc_token);
  1658. str(l,countstr);
  1659. hs:=hs+'$'+countstr;
  1660. end
  1661. else
  1662. { special case: <> }
  1663. if current_scanner.preproc_token=_NE then
  1664. begin
  1665. hs:=hs+'$1';
  1666. preproc_consume(_NE);
  1667. end;
  1668. current_scanner.skipspace;
  1669. if searchsym(hs,srsym,srsymtable) then
  1670. begin
  1671. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  1672. if (sp_generic_dummy in srsym.symoptions) and
  1673. (srsym.typ=typesym) and
  1674. (
  1675. { mode delphi}
  1676. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  1677. { non-delphi modes }
  1678. (df_generic in ttypesym(srsym).typedef.defoptions)
  1679. ) then
  1680. result:=texprvalue.create_bool(false)
  1681. else
  1682. result:=texprvalue.create_bool(true);
  1683. end
  1684. else
  1685. result:=texprvalue.create_bool(false);
  1686. end
  1687. else
  1688. Message(scan_e_error_in_preproc_expr);
  1689. if current_scanner.preproc_token =_RKLAMMER then
  1690. preproc_consume(_RKLAMMER)
  1691. else
  1692. Message(scan_e_error_in_preproc_expr);
  1693. end
  1694. else
  1695. if current_scanner.preproc_pattern='ORD' then
  1696. begin
  1697. preproc_consume(_ID);
  1698. current_scanner.skipspace;
  1699. if current_scanner.preproc_token =_LKLAMMER then
  1700. begin
  1701. preproc_consume(_LKLAMMER);
  1702. current_scanner.skipspace;
  1703. end
  1704. else
  1705. Message(scan_e_preproc_syntax_error);
  1706. exprvalue:=preproc_factor(eval);
  1707. if eval then
  1708. begin
  1709. if is_ordinal(exprvalue.def) then
  1710. result:=texprvalue.create_int(exprvalue.asInt)
  1711. else
  1712. begin
  1713. exprvalue.error('Ordinal','ORD');
  1714. result:=texprvalue.create_int(0);
  1715. end;
  1716. end
  1717. else
  1718. result:=texprvalue.create_int(0);
  1719. exprvalue.free;
  1720. if current_scanner.preproc_token =_RKLAMMER then
  1721. preproc_consume(_RKLAMMER)
  1722. else
  1723. Message(scan_e_error_in_preproc_expr);
  1724. end
  1725. else
  1726. if current_scanner.preproc_pattern='NOT' then
  1727. begin
  1728. preproc_consume(_ID);
  1729. exprvalue:=preproc_factor(eval);
  1730. if eval then
  1731. result:=exprvalue.evaluate(nil,_OP_NOT)
  1732. else
  1733. result:=texprvalue.create_bool(false); {Just to have something}
  1734. exprvalue.free;
  1735. end
  1736. else
  1737. if (current_scanner.preproc_pattern='TRUE') then
  1738. begin
  1739. result:=texprvalue.create_bool(true);
  1740. preproc_consume(_ID);
  1741. end
  1742. else
  1743. if (current_scanner.preproc_pattern='FALSE') then
  1744. begin
  1745. result:=texprvalue.create_bool(false);
  1746. preproc_consume(_ID);
  1747. end
  1748. else
  1749. begin
  1750. storedpattern:=current_scanner.preproc_pattern;
  1751. preproc_consume(_ID);
  1752. current_scanner.skipspace;
  1753. { first look for a macros/int/float }
  1754. result:=preproc_substitutedtoken(storedpattern,eval);
  1755. if eval and (result.consttyp=conststring) then
  1756. if searchsym(storedpattern,srsym,srsymtable) then
  1757. begin
  1758. try_consume_nestedsym(srsym,srsymtable);
  1759. if assigned(srsym) then
  1760. case srsym.typ of
  1761. constsym:
  1762. begin
  1763. result.free;
  1764. result:=texprvalue.create_const(tconstsym(srsym));
  1765. end;
  1766. enumsym:
  1767. begin
  1768. result.free;
  1769. result:=texprvalue.create_int(tenumsym(srsym).value);
  1770. end;
  1771. end;
  1772. end;
  1773. end
  1774. end
  1775. else if current_scanner.preproc_token =_LKLAMMER then
  1776. begin
  1777. preproc_consume(_LKLAMMER);
  1778. result:=preproc_sub_expr(opcompare,eval);
  1779. preproc_consume(_RKLAMMER);
  1780. end
  1781. else if current_scanner.preproc_token = _LECKKLAMMER then
  1782. begin
  1783. preproc_consume(_LECKKLAMMER);
  1784. ns:=[];
  1785. while current_scanner.preproc_token in [_ID,_INTCONST] do
  1786. begin
  1787. exprvalue:=preproc_factor(eval);
  1788. include(ns,exprvalue.asInt);
  1789. if current_scanner.preproc_token = _COMMA then
  1790. preproc_consume(_COMMA);
  1791. end;
  1792. // TODO Add check of setElemType
  1793. preproc_consume(_RECKKLAMMER);
  1794. result:=texprvalue.create_set(ns);
  1795. end
  1796. else if current_scanner.preproc_token = _INTCONST then
  1797. begin
  1798. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  1799. if not assigned(result) then
  1800. begin
  1801. Message(parser_e_invalid_integer);
  1802. result:=texprvalue.create_int(1);
  1803. end;
  1804. preproc_consume(_INTCONST);
  1805. end
  1806. else if current_scanner.preproc_token = _REALNUMBER then
  1807. begin
  1808. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  1809. if not assigned(result) then
  1810. begin
  1811. Message(parser_e_error_in_real);
  1812. result:=texprvalue.create_real(1.0);
  1813. end;
  1814. preproc_consume(_REALNUMBER);
  1815. end
  1816. else
  1817. Message(scan_e_error_in_preproc_expr);
  1818. if not assigned(result) then
  1819. result:=texprvalue.create_error;
  1820. end;
  1821. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  1822. var
  1823. hs1,hs2: texprvalue;
  1824. op: ttoken;
  1825. begin
  1826. if pred_level=highest_precedence then
  1827. result:=preproc_factor(eval)
  1828. else
  1829. result:=preproc_sub_expr(succ(pred_level),eval);
  1830. repeat
  1831. op:=current_scanner.preproc_token;
  1832. if (op in preproc_operators) and
  1833. (op in operator_levels[pred_level]) then
  1834. begin
  1835. hs1:=result;
  1836. preproc_consume(op);
  1837. if (op=_OP_OR) and hs1.isBoolean and hs1.asBool then
  1838. begin
  1839. { stop evaluation the rest of expression }
  1840. result:=texprvalue.create_bool(true);
  1841. if pred_level=highest_precedence then
  1842. hs2:=preproc_factor(false)
  1843. else
  1844. hs2:=preproc_sub_expr(succ(pred_level),false);
  1845. end
  1846. else if (op=_OP_AND) and hs1.isBoolean and not hs1.asBool then
  1847. begin
  1848. { stop evaluation the rest of expression }
  1849. result:=texprvalue.create_bool(false);
  1850. if pred_level=highest_precedence then
  1851. hs2:=preproc_factor(false)
  1852. else
  1853. hs2:=preproc_sub_expr(succ(pred_level),false);
  1854. end
  1855. else
  1856. begin
  1857. if pred_level=highest_precedence then
  1858. hs2:=preproc_factor(eval)
  1859. else
  1860. hs2:=preproc_sub_expr(succ(pred_level),eval);
  1861. if eval then
  1862. result:=hs1.evaluate(hs2,op)
  1863. else
  1864. result:=texprvalue.create_bool(false); {Just to have something}
  1865. end;
  1866. hs1.free;
  1867. hs2.free;
  1868. end
  1869. else
  1870. break;
  1871. until false;
  1872. end;
  1873. begin
  1874. current_scanner.skipspace;
  1875. { start preproc expression scanner }
  1876. current_scanner.preproc_token:=current_scanner.readpreproc;
  1877. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  1878. end;
  1879. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  1880. var
  1881. hs: texprvalue;
  1882. begin
  1883. hs:=preproc_comp_expr;
  1884. if hs.isBoolean then
  1885. result:=hs.asBool
  1886. else
  1887. begin
  1888. hs.error('Boolean', 'IF or ELSEIF');
  1889. result:=false;
  1890. end;
  1891. valuedescr:=hs.asStr;
  1892. hs.free;
  1893. end;
  1894. procedure dir_if;
  1895. begin
  1896. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1897. end;
  1898. procedure dir_elseif;
  1899. begin
  1900. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1901. end;
  1902. procedure dir_define_impl(macstyle: boolean);
  1903. var
  1904. hs : string;
  1905. bracketcount : longint;
  1906. mac : tmacro;
  1907. macropos : longint;
  1908. macrobuffer : pmacrobuffer;
  1909. begin
  1910. current_scanner.skipspace;
  1911. hs:=current_scanner.readid;
  1912. mac:=tmacro(search_macro(hs));
  1913. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  1914. begin
  1915. mac:=tmacro.create(hs);
  1916. mac.defined:=true;
  1917. current_module.localmacrosymtable.insert(mac);
  1918. end
  1919. else
  1920. begin
  1921. mac.defined:=true;
  1922. mac.is_compiler_var:=false;
  1923. { delete old definition }
  1924. if assigned(mac.buftext) then
  1925. begin
  1926. freemem(mac.buftext,mac.buflen);
  1927. mac.buftext:=nil;
  1928. end;
  1929. end;
  1930. Message1(parser_c_macro_defined,mac.name);
  1931. mac.is_used:=true;
  1932. if (cs_support_macro in current_settings.moduleswitches) then
  1933. begin
  1934. current_scanner.skipspace;
  1935. if not macstyle then
  1936. begin
  1937. { may be a macro? }
  1938. if c <> ':' then
  1939. exit;
  1940. current_scanner.readchar;
  1941. if c <> '=' then
  1942. exit;
  1943. current_scanner.readchar;
  1944. current_scanner.skipspace;
  1945. end;
  1946. { key words are never substituted }
  1947. if is_keyword(hs) then
  1948. Message(scan_e_keyword_cant_be_a_macro);
  1949. new(macrobuffer);
  1950. macropos:=0;
  1951. { parse macro, brackets are counted so it's possible
  1952. to have a $ifdef etc. in the macro }
  1953. bracketcount:=0;
  1954. repeat
  1955. case c of
  1956. '}' :
  1957. if (bracketcount=0) then
  1958. break
  1959. else
  1960. dec(bracketcount);
  1961. '{' :
  1962. inc(bracketcount);
  1963. #10,#13 :
  1964. current_scanner.linebreak;
  1965. #26 :
  1966. current_scanner.end_of_file;
  1967. end;
  1968. macrobuffer^[macropos]:=c;
  1969. inc(macropos);
  1970. if macropos>=maxmacrolen then
  1971. Message(scan_f_macro_buffer_overflow);
  1972. current_scanner.readchar;
  1973. until false;
  1974. { free buffer of macro ?}
  1975. if assigned(mac.buftext) then
  1976. freemem(mac.buftext,mac.buflen);
  1977. { get new mem }
  1978. getmem(mac.buftext,macropos);
  1979. mac.buflen:=macropos;
  1980. { copy the text }
  1981. move(macrobuffer^,mac.buftext^,macropos);
  1982. dispose(macrobuffer);
  1983. end
  1984. else
  1985. begin
  1986. { check if there is an assignment, then we need to give a
  1987. warning }
  1988. current_scanner.skipspace;
  1989. if c=':' then
  1990. begin
  1991. current_scanner.readchar;
  1992. if c='=' then
  1993. Message(scan_w_macro_support_turned_off);
  1994. end;
  1995. end;
  1996. end;
  1997. procedure dir_define;
  1998. begin
  1999. dir_define_impl(false);
  2000. end;
  2001. procedure dir_definec;
  2002. begin
  2003. dir_define_impl(true);
  2004. end;
  2005. procedure dir_setc;
  2006. var
  2007. hs : string;
  2008. mac : tmacro;
  2009. l : longint;
  2010. w : integer;
  2011. exprvalue: texprvalue;
  2012. begin
  2013. current_scanner.skipspace;
  2014. hs:=current_scanner.readid;
  2015. mac:=tmacro(search_macro(hs));
  2016. if not assigned(mac) or
  2017. (mac.owner <> current_module.localmacrosymtable) then
  2018. begin
  2019. mac:=tmacro.create(hs);
  2020. mac.defined:=true;
  2021. mac.is_compiler_var:=true;
  2022. current_module.localmacrosymtable.insert(mac);
  2023. end
  2024. else
  2025. begin
  2026. mac.defined:=true;
  2027. mac.is_compiler_var:=true;
  2028. { delete old definition }
  2029. if assigned(mac.buftext) then
  2030. begin
  2031. freemem(mac.buftext,mac.buflen);
  2032. mac.buftext:=nil;
  2033. end;
  2034. end;
  2035. Message1(parser_c_macro_defined,mac.name);
  2036. mac.is_used:=true;
  2037. { key words are never substituted }
  2038. if is_keyword(hs) then
  2039. Message(scan_e_keyword_cant_be_a_macro);
  2040. { macro assignment can be both := and = }
  2041. current_scanner.skipspace;
  2042. if c=':' then
  2043. current_scanner.readchar;
  2044. if c='=' then
  2045. begin
  2046. current_scanner.readchar;
  2047. exprvalue:=preproc_comp_expr;
  2048. if not is_boolean(exprvalue.def) and
  2049. not is_integer(exprvalue.def) then
  2050. exprvalue.error('Boolean, Integer', 'SETC');
  2051. hs:=exprvalue.asStr;
  2052. if length(hs) <> 0 then
  2053. begin
  2054. {If we are absolutely shure it is boolean, translate
  2055. to TRUE/FALSE to increase possibility to do future type check}
  2056. if exprvalue.isBoolean then
  2057. begin
  2058. if exprvalue.asBool then
  2059. hs:='TRUE'
  2060. else
  2061. hs:='FALSE';
  2062. end;
  2063. Message2(parser_c_macro_set_to,mac.name,hs);
  2064. { free buffer of macro ?}
  2065. if assigned(mac.buftext) then
  2066. freemem(mac.buftext,mac.buflen);
  2067. { get new mem }
  2068. getmem(mac.buftext,length(hs));
  2069. mac.buflen:=length(hs);
  2070. { copy the text }
  2071. move(hs[1],mac.buftext^,mac.buflen);
  2072. end
  2073. else
  2074. Message(scan_e_preproc_syntax_error);
  2075. exprvalue.free;
  2076. end
  2077. else
  2078. Message(scan_e_preproc_syntax_error);
  2079. end;
  2080. procedure dir_undef;
  2081. var
  2082. hs : string;
  2083. mac : tmacro;
  2084. begin
  2085. current_scanner.skipspace;
  2086. hs:=current_scanner.readid;
  2087. mac:=tmacro(search_macro(hs));
  2088. if not assigned(mac) or
  2089. (mac.owner <> current_module.localmacrosymtable) then
  2090. begin
  2091. mac:=tmacro.create(hs);
  2092. mac.defined:=false;
  2093. current_module.localmacrosymtable.insert(mac);
  2094. end
  2095. else
  2096. begin
  2097. mac.defined:=false;
  2098. mac.is_compiler_var:=false;
  2099. { delete old definition }
  2100. if assigned(mac.buftext) then
  2101. begin
  2102. freemem(mac.buftext,mac.buflen);
  2103. mac.buftext:=nil;
  2104. end;
  2105. end;
  2106. Message1(parser_c_macro_undefined,mac.name);
  2107. mac.is_used:=true;
  2108. end;
  2109. procedure dir_include;
  2110. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  2111. var
  2112. found : boolean;
  2113. hpath : TCmdStr;
  2114. begin
  2115. (* look for the include file
  2116. If path was absolute and specified as part of {$I } then
  2117. 1. specified path
  2118. else
  2119. 1. path of current inputfile,current dir
  2120. 2. local includepath
  2121. 3. global includepath
  2122. -- Check mantis #13461 before changing this *)
  2123. found:=false;
  2124. foundfile:='';
  2125. hpath:='';
  2126. if path_absolute(path) then
  2127. begin
  2128. found:=FindFile(name,path,true,foundfile);
  2129. end
  2130. else
  2131. begin
  2132. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  2133. found:=FindFile(path+name, hpath,true,foundfile);
  2134. if not found then
  2135. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  2136. if not found then
  2137. found:=includesearchpath.FindFile(path+name,true,foundfile);
  2138. end;
  2139. result:=found;
  2140. end;
  2141. var
  2142. foundfile : TCmdStr;
  2143. path,
  2144. name,
  2145. hs : tpathstr;
  2146. args : string;
  2147. hp : tinputfile;
  2148. found : boolean;
  2149. macroIsString : boolean;
  2150. begin
  2151. current_scanner.skipspace;
  2152. args:=current_scanner.readcomment;
  2153. hs:=GetToken(args,' ');
  2154. if hs='' then
  2155. exit;
  2156. if (hs[1]='%') then
  2157. begin
  2158. { case insensitive }
  2159. hs:=upper(hs);
  2160. { remove %'s }
  2161. Delete(hs,1,1);
  2162. if hs[length(hs)]='%' then
  2163. Delete(hs,length(hs),1);
  2164. { save old }
  2165. path:=hs;
  2166. { first check for internal macros }
  2167. macroIsString:=true;
  2168. if hs='TIME' then
  2169. hs:=gettimestr
  2170. else
  2171. if hs='DATE' then
  2172. hs:=getdatestr
  2173. else
  2174. if hs='FILE' then
  2175. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  2176. else
  2177. if hs='LINE' then
  2178. hs:=tostr(current_filepos.line)
  2179. else
  2180. if hs='LINENUM' then
  2181. begin
  2182. hs:=tostr(current_filepos.line);
  2183. macroIsString:=false;
  2184. end
  2185. else
  2186. if hs='FPCVERSION' then
  2187. hs:=version_string
  2188. else
  2189. if hs='FPCDATE' then
  2190. hs:=date_string
  2191. else
  2192. if hs='FPCTARGET' then
  2193. hs:=target_cpu_string
  2194. else
  2195. if hs='FPCTARGETCPU' then
  2196. hs:=target_cpu_string
  2197. else
  2198. if hs='FPCTARGETOS' then
  2199. hs:=target_info.shortname
  2200. else
  2201. hs:=GetEnvironmentVariable(hs);
  2202. if hs='' then
  2203. Message1(scan_w_include_env_not_found,path);
  2204. { make it a stringconst }
  2205. if macroIsString then
  2206. hs:=''''+hs+'''';
  2207. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2208. current_scanner.line_no,current_scanner.inputfile.ref_index);
  2209. end
  2210. else
  2211. begin
  2212. hs:=FixFileName(hs);
  2213. path:=ExtractFilePath(hs);
  2214. name:=ExtractFileName(hs);
  2215. { Special case for Delphi compatibility: '*' has to be replaced
  2216. by the file name of the current source file. }
  2217. if (length(name)>=1) and
  2218. (name[1]='*') then
  2219. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2220. { try to find the file }
  2221. found:=findincludefile(path,name,foundfile);
  2222. if (not found) and (ExtractFileExt(name)='') then
  2223. begin
  2224. { try default extensions .inc , .pp and .pas }
  2225. if (not found) then
  2226. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2227. if (not found) then
  2228. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2229. if (not found) then
  2230. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2231. end;
  2232. if current_scanner.inputfilecount<max_include_nesting then
  2233. begin
  2234. inc(current_scanner.inputfilecount);
  2235. { we need to reread the current char }
  2236. dec(current_scanner.inputpointer);
  2237. { shutdown current file }
  2238. current_scanner.tempcloseinputfile;
  2239. { load new file }
  2240. hp:=do_openinputfile(foundfile);
  2241. hp.inc_path:=path;
  2242. current_scanner.addfile(hp);
  2243. current_module.sourcefiles.register_file(hp);
  2244. if (not found) then
  2245. Message1(scan_f_cannot_open_includefile,hs);
  2246. if (not current_scanner.openinputfile) then
  2247. Message1(scan_f_cannot_open_includefile,hs);
  2248. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2249. current_scanner.reload;
  2250. end
  2251. else
  2252. Message(scan_f_include_deep_ten);
  2253. end;
  2254. end;
  2255. {*****************************************************************************
  2256. Preprocessor writing
  2257. *****************************************************************************}
  2258. {$ifdef PREPROCWRITE}
  2259. constructor tpreprocfile.create(const fn:string);
  2260. begin
  2261. { open outputfile }
  2262. assign(f,fn);
  2263. {$push}{$I-}
  2264. rewrite(f);
  2265. {$pop}
  2266. if ioresult<>0 then
  2267. Comment(V_Fatal,'can''t create file '+fn);
  2268. getmem(buf,preprocbufsize);
  2269. settextbuf(f,buf^,preprocbufsize);
  2270. { reset }
  2271. eolfound:=false;
  2272. spacefound:=false;
  2273. end;
  2274. destructor tpreprocfile.destroy;
  2275. begin
  2276. close(f);
  2277. freemem(buf,preprocbufsize);
  2278. end;
  2279. procedure tpreprocfile.add(const s:string);
  2280. begin
  2281. write(f,s);
  2282. end;
  2283. procedure tpreprocfile.addspace;
  2284. begin
  2285. if eolfound then
  2286. begin
  2287. writeln(f,'');
  2288. eolfound:=false;
  2289. spacefound:=false;
  2290. end
  2291. else
  2292. if spacefound then
  2293. begin
  2294. write(f,' ');
  2295. spacefound:=false;
  2296. end;
  2297. end;
  2298. {$endif PREPROCWRITE}
  2299. {*****************************************************************************
  2300. TPreProcStack
  2301. *****************************************************************************}
  2302. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2303. begin
  2304. accept:=a;
  2305. typ:=atyp;
  2306. next:=n;
  2307. end;
  2308. {*****************************************************************************
  2309. TReplayStack
  2310. *****************************************************************************}
  2311. constructor treplaystack.Create(atoken:ttoken;asettings:tsettings;
  2312. atokenbuf:tdynamicarray;anext:treplaystack);
  2313. begin
  2314. token:=atoken;
  2315. settings:=asettings;
  2316. tokenbuf:=atokenbuf;
  2317. next:=anext;
  2318. end;
  2319. {*****************************************************************************
  2320. TDirectiveItem
  2321. *****************************************************************************}
  2322. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2323. begin
  2324. inherited Create(AList,n);
  2325. is_conditional:=false;
  2326. proc:=p;
  2327. end;
  2328. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2329. begin
  2330. inherited Create(AList,n);
  2331. is_conditional:=true;
  2332. proc:=p;
  2333. end;
  2334. {****************************************************************************
  2335. TSCANNERFILE
  2336. ****************************************************************************}
  2337. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  2338. begin
  2339. inputfile:=do_openinputfile(fn);
  2340. if is_macro then
  2341. inputfile.is_macro:=true;
  2342. if assigned(current_module) then
  2343. current_module.sourcefiles.register_file(inputfile);
  2344. { reset localinput }
  2345. c:=#0;
  2346. inputbuffer:=nil;
  2347. inputpointer:=nil;
  2348. inputstart:=0;
  2349. { reset scanner }
  2350. preprocstack:=nil;
  2351. replaystack:=nil;
  2352. comment_level:=0;
  2353. yylexcount:=0;
  2354. block_type:=bt_general;
  2355. line_no:=0;
  2356. lastlinepos:=0;
  2357. lasttokenpos:=0;
  2358. nexttokenpos:=0;
  2359. lasttoken:=NOTOKEN;
  2360. nexttoken:=NOTOKEN;
  2361. lastasmgetchar:=#0;
  2362. ignoredirectives:=TFPHashList.Create;
  2363. in_asm_string:=false;
  2364. end;
  2365. procedure tscannerfile.firstfile;
  2366. begin
  2367. { load block }
  2368. if not openinputfile then
  2369. Message1(scan_f_cannot_open_input,inputfile.name);
  2370. reload;
  2371. end;
  2372. destructor tscannerfile.destroy;
  2373. begin
  2374. if assigned(current_module) and
  2375. (current_module.state=ms_compiled) and
  2376. (status.errorcount=0) then
  2377. checkpreprocstack
  2378. else
  2379. begin
  2380. while assigned(preprocstack) do
  2381. poppreprocstack;
  2382. end;
  2383. while assigned(replaystack) do
  2384. popreplaystack;
  2385. if not inputfile.closed then
  2386. closeinputfile;
  2387. if inputfile.is_macro then
  2388. inputfile.free;
  2389. ignoredirectives.free;
  2390. end;
  2391. function tscannerfile.openinputfile:boolean;
  2392. begin
  2393. openinputfile:=inputfile.open;
  2394. { load buffer }
  2395. inputbuffer:=inputfile.buf;
  2396. inputpointer:=inputfile.buf;
  2397. inputstart:=inputfile.bufstart;
  2398. { line }
  2399. line_no:=0;
  2400. lastlinepos:=0;
  2401. lasttokenpos:=0;
  2402. nexttokenpos:=0;
  2403. end;
  2404. procedure tscannerfile.closeinputfile;
  2405. begin
  2406. inputfile.close;
  2407. { reset buffer }
  2408. inputbuffer:=nil;
  2409. inputpointer:=nil;
  2410. inputstart:=0;
  2411. { reset line }
  2412. line_no:=0;
  2413. lastlinepos:=0;
  2414. lasttokenpos:=0;
  2415. nexttokenpos:=0;
  2416. end;
  2417. function tscannerfile.tempopeninputfile:boolean;
  2418. begin
  2419. tempopeninputfile:=false;
  2420. if inputfile.is_macro then
  2421. exit;
  2422. tempopeninputfile:=inputfile.tempopen;
  2423. { reload buffer }
  2424. inputbuffer:=inputfile.buf;
  2425. inputpointer:=inputfile.buf;
  2426. inputstart:=inputfile.bufstart;
  2427. end;
  2428. procedure tscannerfile.tempcloseinputfile;
  2429. begin
  2430. if inputfile.closed or inputfile.is_macro then
  2431. exit;
  2432. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2433. inputfile.tempclose;
  2434. { reset buffer }
  2435. inputbuffer:=nil;
  2436. inputpointer:=nil;
  2437. inputstart:=0;
  2438. end;
  2439. procedure tscannerfile.saveinputfile;
  2440. begin
  2441. inputfile.saveinputpointer:=inputpointer;
  2442. inputfile.savelastlinepos:=lastlinepos;
  2443. inputfile.saveline_no:=line_no;
  2444. end;
  2445. procedure tscannerfile.restoreinputfile;
  2446. begin
  2447. inputbuffer:=inputfile.buf;
  2448. inputpointer:=inputfile.saveinputpointer;
  2449. lastlinepos:=inputfile.savelastlinepos;
  2450. line_no:=inputfile.saveline_no;
  2451. if not inputfile.is_macro then
  2452. parser_current_file:=inputfile.name;
  2453. end;
  2454. procedure tscannerfile.nextfile;
  2455. var
  2456. to_dispose : tinputfile;
  2457. begin
  2458. if assigned(inputfile.next) then
  2459. begin
  2460. if inputfile.is_macro then
  2461. to_dispose:=inputfile
  2462. else
  2463. begin
  2464. to_dispose:=nil;
  2465. dec(inputfilecount);
  2466. end;
  2467. { we can allways close the file, no ? }
  2468. inputfile.close;
  2469. inputfile:=inputfile.next;
  2470. if assigned(to_dispose) then
  2471. to_dispose.free;
  2472. restoreinputfile;
  2473. end;
  2474. end;
  2475. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  2476. begin
  2477. if not assigned(buf) then
  2478. internalerror(200511172);
  2479. if assigned(recordtokenbuf) then
  2480. internalerror(200511173);
  2481. recordtokenbuf:=buf;
  2482. fillchar(last_settings,sizeof(last_settings),0);
  2483. last_message:=nil;
  2484. fillchar(last_filepos,sizeof(last_filepos),0);
  2485. end;
  2486. procedure tscannerfile.stoprecordtokens;
  2487. begin
  2488. if not assigned(recordtokenbuf) then
  2489. internalerror(200511174);
  2490. recordtokenbuf:=nil;
  2491. end;
  2492. procedure tscannerfile.writetoken(t : ttoken);
  2493. var
  2494. b : byte;
  2495. begin
  2496. if ord(t)>$7f then
  2497. begin
  2498. b:=(ord(t) shr 8) or $80;
  2499. recordtokenbuf.write(b,1);
  2500. end;
  2501. b:=ord(t) and $ff;
  2502. recordtokenbuf.write(b,1);
  2503. end;
  2504. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  2505. begin
  2506. {$ifdef FPC_BIG_ENDIAN}
  2507. val:=swapendian(val);
  2508. {$endif}
  2509. recordtokenbuf.write(val,sizeof(asizeint));
  2510. end;
  2511. procedure tscannerfile.tokenwritelongint(val : longint);
  2512. begin
  2513. {$ifdef FPC_BIG_ENDIAN}
  2514. val:=swapendian(val);
  2515. {$endif}
  2516. recordtokenbuf.write(val,sizeof(longint));
  2517. end;
  2518. procedure tscannerfile.tokenwriteshortint(val : shortint);
  2519. begin
  2520. recordtokenbuf.write(val,sizeof(shortint));
  2521. end;
  2522. procedure tscannerfile.tokenwriteword(val : word);
  2523. begin
  2524. {$ifdef FPC_BIG_ENDIAN}
  2525. val:=swapendian(val);
  2526. {$endif}
  2527. recordtokenbuf.write(val,sizeof(word));
  2528. end;
  2529. procedure tscannerfile.tokenwritelongword(val : longword);
  2530. begin
  2531. {$ifdef FPC_BIG_ENDIAN}
  2532. val:=swapendian(val);
  2533. {$endif}
  2534. recordtokenbuf.write(val,sizeof(longword));
  2535. end;
  2536. function tscannerfile.tokenreadsizeint : asizeint;
  2537. var
  2538. val : asizeint;
  2539. begin
  2540. replaytokenbuf.read(val,sizeof(asizeint));
  2541. {$ifdef FPC_BIG_ENDIAN}
  2542. val:=swapendian(val);
  2543. {$endif}
  2544. result:=val;
  2545. end;
  2546. function tscannerfile.tokenreadlongword : longword;
  2547. var
  2548. val : longword;
  2549. begin
  2550. replaytokenbuf.read(val,sizeof(longword));
  2551. {$ifdef FPC_BIG_ENDIAN}
  2552. val:=swapendian(val);
  2553. {$endif}
  2554. result:=val;
  2555. end;
  2556. function tscannerfile.tokenreadlongint : longint;
  2557. var
  2558. val : longint;
  2559. begin
  2560. replaytokenbuf.read(val,sizeof(longint));
  2561. {$ifdef FPC_BIG_ENDIAN}
  2562. val:=swapendian(val);
  2563. {$endif}
  2564. result:=val;
  2565. end;
  2566. function tscannerfile.tokenreadshortint : shortint;
  2567. var
  2568. val : shortint;
  2569. begin
  2570. replaytokenbuf.read(val,sizeof(shortint));
  2571. result:=val;
  2572. end;
  2573. function tscannerfile.tokenreadbyte : byte;
  2574. var
  2575. val : byte;
  2576. begin
  2577. replaytokenbuf.read(val,sizeof(byte));
  2578. result:=val;
  2579. end;
  2580. function tscannerfile.tokenreadsmallint : smallint;
  2581. var
  2582. val : smallint;
  2583. begin
  2584. replaytokenbuf.read(val,sizeof(smallint));
  2585. {$ifdef FPC_BIG_ENDIAN}
  2586. val:=swapendian(val);
  2587. {$endif}
  2588. result:=val;
  2589. end;
  2590. function tscannerfile.tokenreadword : word;
  2591. var
  2592. val : word;
  2593. begin
  2594. replaytokenbuf.read(val,sizeof(word));
  2595. {$ifdef FPC_BIG_ENDIAN}
  2596. val:=swapendian(val);
  2597. {$endif}
  2598. result:=val;
  2599. end;
  2600. function tscannerfile.tokenreadenum(size : longint) : longword;
  2601. begin
  2602. if size=1 then
  2603. result:=tokenreadbyte
  2604. else if size=2 then
  2605. result:=tokenreadword
  2606. else if size=4 then
  2607. result:=tokenreadlongword
  2608. else
  2609. internalerror(2013112901);
  2610. end;
  2611. procedure tscannerfile.tokenreadset(var b;size : longint);
  2612. {$ifdef FPC_BIG_ENDIAN}
  2613. var
  2614. i : longint;
  2615. {$endif}
  2616. begin
  2617. replaytokenbuf.read(b,size);
  2618. {$ifdef FPC_BIG_ENDIAN}
  2619. for i:=0 to size-1 do
  2620. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2621. {$endif}
  2622. end;
  2623. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2624. begin
  2625. recordtokenbuf.write(b,size);
  2626. end;
  2627. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2628. {$ifdef FPC_BIG_ENDIAN}
  2629. var
  2630. i: longint;
  2631. tmpset: array[0..31] of byte;
  2632. {$endif}
  2633. begin
  2634. {$ifdef FPC_BIG_ENDIAN}
  2635. { satisfy DFA because it assumes that size may be 0 and doesn't know that
  2636. recordtokenbuf.write wouldn't use tmpset in that case }
  2637. tmpset[0]:=0;
  2638. for i:=0 to size-1 do
  2639. tmpset[i]:=reverse_byte(Pbyte(@b)[i]);
  2640. recordtokenbuf.write(tmpset,size);
  2641. {$else}
  2642. recordtokenbuf.write(b,size);
  2643. {$endif}
  2644. end;
  2645. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2646. { This procedure
  2647. needs to be changed whenever
  2648. globals.tsettings type is changed,
  2649. the problem is that no error will appear
  2650. before tests with generics are tested. PM }
  2651. var
  2652. startpos, endpos : longword;
  2653. begin
  2654. { WARNING all those fields need to be in the correct
  2655. order otherwise cross_endian PPU reading will fail }
  2656. startpos:=replaytokenbuf.pos;
  2657. with asettings do
  2658. begin
  2659. alignment.procalign:=tokenreadlongint;
  2660. alignment.loopalign:=tokenreadlongint;
  2661. alignment.jumpalign:=tokenreadlongint;
  2662. alignment.constalignmin:=tokenreadlongint;
  2663. alignment.constalignmax:=tokenreadlongint;
  2664. alignment.varalignmin:=tokenreadlongint;
  2665. alignment.varalignmax:=tokenreadlongint;
  2666. alignment.localalignmin:=tokenreadlongint;
  2667. alignment.localalignmax:=tokenreadlongint;
  2668. alignment.recordalignmin:=tokenreadlongint;
  2669. alignment.recordalignmax:=tokenreadlongint;
  2670. alignment.maxCrecordalign:=tokenreadlongint;
  2671. tokenreadset(globalswitches,sizeof(globalswitches));
  2672. tokenreadset(targetswitches,sizeof(targetswitches));
  2673. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2674. tokenreadset(localswitches,sizeof(localswitches));
  2675. tokenreadset(modeswitches,sizeof(modeswitches));
  2676. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2677. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2678. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2679. tokenreadset(debugswitches,sizeof(debugswitches));
  2680. { 0: old behaviour for sets <=256 elements
  2681. >0: round to this size }
  2682. setalloc:=tokenreadshortint;
  2683. packenum:=tokenreadshortint;
  2684. packrecords:=tokenreadshortint;
  2685. maxfpuregisters:=tokenreadshortint;
  2686. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2687. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2688. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2689. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2690. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2691. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2692. { tstringencoding is word type,
  2693. thus this should be OK here }
  2694. sourcecodepage:=tstringEncoding(tokenreadword);
  2695. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2696. disabledircache:=boolean(tokenreadbyte);
  2697. {$if defined(ARM) or defined(AVR) or defined(MIPSEL)}
  2698. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)));
  2699. {$endif defined(ARM) or defined(AVR) or DEFINED(MIPSEL)}
  2700. endpos:=replaytokenbuf.pos;
  2701. if endpos-startpos<>expected_size then
  2702. Comment(V_Error,'Wrong size of Settings read-in');
  2703. end;
  2704. end;
  2705. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2706. { This procedure
  2707. needs to be changed whenever
  2708. globals.tsettings type is changed,
  2709. the problem is that no error will appear
  2710. before tests with generics are tested. PM }
  2711. var
  2712. sizepos, startpos, endpos : longword;
  2713. begin
  2714. { WARNING all those fields need to be in the correct
  2715. order otherwise cross_endian PPU reading will fail }
  2716. sizepos:=recordtokenbuf.pos;
  2717. size:=0;
  2718. tokenwritesizeint(size);
  2719. startpos:=recordtokenbuf.pos;
  2720. with asettings do
  2721. begin
  2722. tokenwritelongint(alignment.procalign);
  2723. tokenwritelongint(alignment.loopalign);
  2724. tokenwritelongint(alignment.jumpalign);
  2725. tokenwritelongint(alignment.constalignmin);
  2726. tokenwritelongint(alignment.constalignmax);
  2727. tokenwritelongint(alignment.varalignmin);
  2728. tokenwritelongint(alignment.varalignmax);
  2729. tokenwritelongint(alignment.localalignmin);
  2730. tokenwritelongint(alignment.localalignmax);
  2731. tokenwritelongint(alignment.recordalignmin);
  2732. tokenwritelongint(alignment.recordalignmax);
  2733. tokenwritelongint(alignment.maxCrecordalign);
  2734. tokenwriteset(globalswitches,sizeof(globalswitches));
  2735. tokenwriteset(targetswitches,sizeof(targetswitches));
  2736. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  2737. tokenwriteset(localswitches,sizeof(localswitches));
  2738. tokenwriteset(modeswitches,sizeof(modeswitches));
  2739. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  2740. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2741. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2742. tokenwriteset(debugswitches,sizeof(debugswitches));
  2743. { 0: old behaviour for sets <=256 elements
  2744. >0: round to this size }
  2745. tokenwriteshortint(setalloc);
  2746. tokenwriteshortint(packenum);
  2747. tokenwriteshortint(packrecords);
  2748. tokenwriteshortint(maxfpuregisters);
  2749. tokenwriteenum(cputype,sizeof(tcputype));
  2750. tokenwriteenum(optimizecputype,sizeof(tcputype));
  2751. tokenwriteenum(fputype,sizeof(tfputype));
  2752. tokenwriteenum(asmmode,sizeof(tasmmode));
  2753. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  2754. tokenwriteenum(defproccall,sizeof(tproccalloption));
  2755. { tstringencoding is word type,
  2756. thus this should be OK here }
  2757. tokenwriteword(sourcecodepage);
  2758. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  2759. recordtokenbuf.write(byte(disabledircache),1);
  2760. {$if defined(ARM) or defined(AVR) or defined(MIPSEL)}
  2761. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  2762. {$endif defined(ARM) or defined(AVR) or defined(MIPSEL)}
  2763. endpos:=recordtokenbuf.pos;
  2764. size:=endpos-startpos;
  2765. recordtokenbuf.seek(sizepos);
  2766. tokenwritesizeint(size);
  2767. recordtokenbuf.seek(endpos);
  2768. end;
  2769. end;
  2770. procedure tscannerfile.recordtoken;
  2771. var
  2772. t : ttoken;
  2773. s : tspecialgenerictoken;
  2774. len,msgnb,copy_size : asizeint;
  2775. val : longint;
  2776. b : byte;
  2777. pmsg : pmessagestaterecord;
  2778. begin
  2779. if not assigned(recordtokenbuf) then
  2780. internalerror(200511176);
  2781. t:=_GENERICSPECIALTOKEN;
  2782. { settings changed? }
  2783. { last field pmessage is handled separately below in
  2784. ST_LOADMESSAGES }
  2785. if CompareByte(current_settings,last_settings,
  2786. sizeof(current_settings)-sizeof(pointer))<>0 then
  2787. begin
  2788. { use a special token to record it }
  2789. s:=ST_LOADSETTINGS;
  2790. writetoken(t);
  2791. recordtokenbuf.write(s,1);
  2792. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2793. tokenwritesettings(current_settings,copy_size);
  2794. last_settings:=current_settings;
  2795. end;
  2796. if current_settings.pmessage<>last_message then
  2797. begin
  2798. { use a special token to record it }
  2799. s:=ST_LOADMESSAGES;
  2800. writetoken(t);
  2801. recordtokenbuf.write(s,1);
  2802. msgnb:=0;
  2803. pmsg:=current_settings.pmessage;
  2804. while assigned(pmsg) do
  2805. begin
  2806. if msgnb=high(asizeint) then
  2807. { Too many messages }
  2808. internalerror(2011090401);
  2809. inc(msgnb);
  2810. pmsg:=pmsg^.next;
  2811. end;
  2812. tokenwritesizeint(msgnb);
  2813. pmsg:=current_settings.pmessage;
  2814. while assigned(pmsg) do
  2815. begin
  2816. { What about endianess here?}
  2817. { SB: this is handled by tokenreadlongint }
  2818. val:=pmsg^.value;
  2819. tokenwritelongint(val);
  2820. val:=ord(pmsg^.state);
  2821. tokenwritelongint(val);
  2822. pmsg:=pmsg^.next;
  2823. end;
  2824. last_message:=current_settings.pmessage;
  2825. end;
  2826. { file pos changes? }
  2827. if current_tokenpos.line<>last_filepos.line then
  2828. begin
  2829. s:=ST_LINE;
  2830. writetoken(t);
  2831. recordtokenbuf.write(s,1);
  2832. tokenwritelongint(current_tokenpos.line);
  2833. last_filepos.line:=current_tokenpos.line;
  2834. end;
  2835. if current_tokenpos.column<>last_filepos.column then
  2836. begin
  2837. s:=ST_COLUMN;
  2838. writetoken(t);
  2839. { can the column be written packed? }
  2840. if current_tokenpos.column<$80 then
  2841. begin
  2842. b:=$80 or current_tokenpos.column;
  2843. recordtokenbuf.write(b,1);
  2844. end
  2845. else
  2846. begin
  2847. recordtokenbuf.write(s,1);
  2848. tokenwriteword(current_tokenpos.column);
  2849. end;
  2850. last_filepos.column:=current_tokenpos.column;
  2851. end;
  2852. if current_tokenpos.fileindex<>last_filepos.fileindex then
  2853. begin
  2854. s:=ST_FILEINDEX;
  2855. writetoken(t);
  2856. recordtokenbuf.write(s,1);
  2857. tokenwriteword(current_tokenpos.fileindex);
  2858. last_filepos.fileindex:=current_tokenpos.fileindex;
  2859. end;
  2860. writetoken(token);
  2861. if token<>_GENERICSPECIALTOKEN then
  2862. writetoken(idtoken);
  2863. case token of
  2864. _CWCHAR,
  2865. _CWSTRING :
  2866. begin
  2867. tokenwritesizeint(patternw^.len);
  2868. if patternw^.len>0 then
  2869. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2870. end;
  2871. _CSTRING:
  2872. begin
  2873. len:=length(cstringpattern);
  2874. tokenwritesizeint(len);
  2875. if len>0 then
  2876. recordtokenbuf.write(cstringpattern[1],len);
  2877. end;
  2878. _CCHAR,
  2879. _INTCONST,
  2880. _REALNUMBER :
  2881. begin
  2882. { pexpr.pas messes with pattern in case of negative integer consts,
  2883. see around line 2562 the comment of JM; remove the - before recording it
  2884. (FK)
  2885. }
  2886. if (token=_INTCONST) and (pattern[1]='-') then
  2887. delete(pattern,1,1);
  2888. recordtokenbuf.write(pattern[0],1);
  2889. recordtokenbuf.write(pattern[1],length(pattern));
  2890. end;
  2891. _ID :
  2892. begin
  2893. recordtokenbuf.write(orgpattern[0],1);
  2894. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  2895. end;
  2896. end;
  2897. end;
  2898. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  2899. begin
  2900. if not assigned(buf) then
  2901. internalerror(200511175);
  2902. { save current token }
  2903. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  2904. internalerror(200511178);
  2905. replaystack:=treplaystack.create(token,current_settings,
  2906. replaytokenbuf,replaystack);
  2907. if assigned(inputpointer) then
  2908. dec(inputpointer);
  2909. { install buffer }
  2910. replaytokenbuf:=buf;
  2911. { reload next token }
  2912. replaytokenbuf.seek(0);
  2913. replaytoken;
  2914. end;
  2915. function tscannerfile.readtoken: ttoken;
  2916. var
  2917. b,b2 : byte;
  2918. begin
  2919. replaytokenbuf.read(b,1);
  2920. if (b and $80)<>0 then
  2921. begin
  2922. replaytokenbuf.read(b2,1);
  2923. result:=ttoken(((b and $7f) shl 8) or b2);
  2924. end
  2925. else
  2926. result:=ttoken(b);
  2927. end;
  2928. procedure tscannerfile.replaytoken;
  2929. var
  2930. wlen,mesgnb,copy_size : asizeint;
  2931. specialtoken : tspecialgenerictoken;
  2932. i : byte;
  2933. pmsg,prevmsg : pmessagestaterecord;
  2934. begin
  2935. if not assigned(replaytokenbuf) then
  2936. internalerror(200511177);
  2937. { End of replay buffer? Then load the next char from the file again }
  2938. if replaytokenbuf.pos>=replaytokenbuf.size then
  2939. begin
  2940. token:=replaystack.token;
  2941. replaytokenbuf:=replaystack.tokenbuf;
  2942. { restore compiler settings }
  2943. current_settings:=replaystack.settings;
  2944. popreplaystack;
  2945. if assigned(inputpointer) then
  2946. begin
  2947. c:=inputpointer^;
  2948. inc(inputpointer);
  2949. end;
  2950. exit;
  2951. end;
  2952. repeat
  2953. { load token from the buffer }
  2954. token:=readtoken;
  2955. if token<>_GENERICSPECIALTOKEN then
  2956. idtoken:=readtoken
  2957. else
  2958. idtoken:=_NOID;
  2959. case token of
  2960. _CWCHAR,
  2961. _CWSTRING :
  2962. begin
  2963. wlen:=tokenreadsizeint;
  2964. setlengthwidestring(patternw,wlen);
  2965. if wlen>0 then
  2966. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2967. orgpattern:='';
  2968. pattern:='';
  2969. cstringpattern:='';
  2970. end;
  2971. _CSTRING:
  2972. begin
  2973. wlen:=tokenreadsizeint;
  2974. if wlen>0 then
  2975. begin
  2976. setlength(cstringpattern,wlen);
  2977. replaytokenbuf.read(cstringpattern[1],wlen);
  2978. end
  2979. else
  2980. cstringpattern:='';
  2981. orgpattern:='';
  2982. pattern:='';
  2983. end;
  2984. _CCHAR,
  2985. _INTCONST,
  2986. _REALNUMBER :
  2987. begin
  2988. replaytokenbuf.read(pattern[0],1);
  2989. replaytokenbuf.read(pattern[1],length(pattern));
  2990. orgpattern:='';
  2991. end;
  2992. _ID :
  2993. begin
  2994. replaytokenbuf.read(orgpattern[0],1);
  2995. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  2996. pattern:=upper(orgpattern);
  2997. end;
  2998. _GENERICSPECIALTOKEN:
  2999. begin
  3000. replaytokenbuf.read(specialtoken,1);
  3001. { packed column? }
  3002. if (ord(specialtoken) and $80)<>0 then
  3003. begin
  3004. current_tokenpos.column:=ord(specialtoken) and $7f;
  3005. current_filepos:=current_tokenpos;
  3006. end
  3007. else
  3008. case specialtoken of
  3009. ST_LOADSETTINGS:
  3010. begin
  3011. copy_size:=tokenreadsizeint;
  3012. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3013. // internalerror(2011090501);
  3014. {
  3015. replaytokenbuf.read(current_settings,copy_size);
  3016. }
  3017. tokenreadsettings(current_settings,copy_size);
  3018. end;
  3019. ST_LOADMESSAGES:
  3020. begin
  3021. current_settings.pmessage:=nil;
  3022. mesgnb:=tokenreadsizeint;
  3023. if mesgnb>0 then
  3024. Comment(V_Error,'Message recordind not yet supported');
  3025. prevmsg:=nil;
  3026. for i:=1 to mesgnb do
  3027. begin
  3028. new(pmsg);
  3029. if i=1 then
  3030. current_settings.pmessage:=pmsg
  3031. else
  3032. prevmsg^.next:=pmsg;
  3033. pmsg^.value:=tokenreadlongint;
  3034. pmsg^.state:=tmsgstate(tokenreadlongint);
  3035. pmsg^.next:=nil;
  3036. prevmsg:=pmsg;
  3037. end;
  3038. end;
  3039. ST_LINE:
  3040. begin
  3041. current_tokenpos.line:=tokenreadlongint;
  3042. current_filepos:=current_tokenpos;
  3043. end;
  3044. ST_COLUMN:
  3045. begin
  3046. current_tokenpos.column:=tokenreadword;
  3047. current_filepos:=current_tokenpos;
  3048. end;
  3049. ST_FILEINDEX:
  3050. begin
  3051. current_tokenpos.fileindex:=tokenreadword;
  3052. current_filepos:=current_tokenpos;
  3053. end;
  3054. else
  3055. internalerror(2006103010);
  3056. end;
  3057. continue;
  3058. end;
  3059. end;
  3060. break;
  3061. until false;
  3062. end;
  3063. procedure tscannerfile.addfile(hp:tinputfile);
  3064. begin
  3065. saveinputfile;
  3066. { add to list }
  3067. hp.next:=inputfile;
  3068. inputfile:=hp;
  3069. { load new inputfile }
  3070. restoreinputfile;
  3071. end;
  3072. procedure tscannerfile.reload;
  3073. begin
  3074. with inputfile do
  3075. begin
  3076. { when nothing more to read then leave immediatly, so we
  3077. don't change the current_filepos and leave it point to the last
  3078. char }
  3079. if (c=#26) and (not assigned(next)) then
  3080. exit;
  3081. repeat
  3082. { still more to read?, then change the #0 to a space so its seen
  3083. as a seperator, this can't be used for macro's which can change
  3084. the place of the #0 in the buffer with tempopen }
  3085. if (c=#0) and (bufsize>0) and
  3086. not(inputfile.is_macro) and
  3087. (inputpointer-inputbuffer<bufsize) then
  3088. begin
  3089. c:=' ';
  3090. inc(inputpointer);
  3091. exit;
  3092. end;
  3093. { can we read more from this file ? }
  3094. if (c<>#26) and (not endoffile) then
  3095. begin
  3096. readbuf;
  3097. inputpointer:=buf;
  3098. inputbuffer:=buf;
  3099. inputstart:=bufstart;
  3100. { first line? }
  3101. if line_no=0 then
  3102. begin
  3103. c:=inputpointer^;
  3104. { eat utf-8 signature? }
  3105. if (ord(inputpointer^)=$ef) and
  3106. (ord((inputpointer+1)^)=$bb) and
  3107. (ord((inputpointer+2)^)=$bf) then
  3108. begin
  3109. (* we don't support including files with an UTF-8 bom
  3110. inside another file that wasn't encoded as UTF-8
  3111. already (we don't support {$codepage xxx} switches in
  3112. the middle of a file either) *)
  3113. if (current_settings.sourcecodepage<>CP_UTF8) and
  3114. not current_module.in_global then
  3115. Message(scanner_f_illegal_utf8_bom);
  3116. inc(inputpointer,3);
  3117. message(scan_c_switching_to_utf8);
  3118. current_settings.sourcecodepage:=CP_UTF8;
  3119. include(current_settings.moduleswitches,cs_explicit_codepage);
  3120. end;
  3121. line_no:=1;
  3122. if cs_asm_source in current_settings.globalswitches then
  3123. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3124. end;
  3125. end
  3126. else
  3127. begin
  3128. { load eof position in tokenpos/current_filepos }
  3129. gettokenpos;
  3130. { close file }
  3131. closeinputfile;
  3132. { no next module, than EOF }
  3133. if not assigned(inputfile.next) then
  3134. begin
  3135. c:=#26;
  3136. exit;
  3137. end;
  3138. { load next file and reopen it }
  3139. nextfile;
  3140. tempopeninputfile;
  3141. { status }
  3142. Message1(scan_t_back_in,inputfile.name);
  3143. end;
  3144. { load next char }
  3145. c:=inputpointer^;
  3146. inc(inputpointer);
  3147. until c<>#0; { if also end, then reload again }
  3148. end;
  3149. end;
  3150. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  3151. var
  3152. hp : tinputfile;
  3153. begin
  3154. { save old postion }
  3155. dec(inputpointer);
  3156. tempcloseinputfile;
  3157. { create macro 'file' }
  3158. { use special name to dispose after !! }
  3159. hp:=do_openinputfile('_Macro_.'+macname);
  3160. addfile(hp);
  3161. with inputfile do
  3162. begin
  3163. setmacro(p,len);
  3164. { local buffer }
  3165. inputbuffer:=buf;
  3166. inputpointer:=buf;
  3167. inputstart:=bufstart;
  3168. ref_index:=fileindex;
  3169. end;
  3170. { reset line }
  3171. line_no:=line;
  3172. lastlinepos:=0;
  3173. lasttokenpos:=0;
  3174. nexttokenpos:=0;
  3175. { load new c }
  3176. c:=inputpointer^;
  3177. inc(inputpointer);
  3178. end;
  3179. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3180. begin
  3181. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3182. filepos.line:=line_no;
  3183. filepos.column:=tokenpos-lastlinepos;
  3184. filepos.fileindex:=inputfile.ref_index;
  3185. filepos.moduleindex:=current_module.unit_index;
  3186. end;
  3187. procedure tscannerfile.gettokenpos;
  3188. { load the values of tokenpos and lasttokenpos }
  3189. begin
  3190. do_gettokenpos(lasttokenpos,current_tokenpos);
  3191. current_filepos:=current_tokenpos;
  3192. end;
  3193. procedure tscannerfile.cachenexttokenpos;
  3194. begin
  3195. do_gettokenpos(nexttokenpos,next_filepos);
  3196. end;
  3197. procedure tscannerfile.setnexttoken;
  3198. begin
  3199. token:=nexttoken;
  3200. nexttoken:=NOTOKEN;
  3201. lasttokenpos:=nexttokenpos;
  3202. current_tokenpos:=next_filepos;
  3203. current_filepos:=current_tokenpos;
  3204. nexttokenpos:=0;
  3205. end;
  3206. procedure tscannerfile.savetokenpos;
  3207. begin
  3208. oldlasttokenpos:=lasttokenpos;
  3209. oldcurrent_filepos:=current_filepos;
  3210. oldcurrent_tokenpos:=current_tokenpos;
  3211. end;
  3212. procedure tscannerfile.restoretokenpos;
  3213. begin
  3214. lasttokenpos:=oldlasttokenpos;
  3215. current_filepos:=oldcurrent_filepos;
  3216. current_tokenpos:=oldcurrent_tokenpos;
  3217. end;
  3218. procedure tscannerfile.inc_comment_level;
  3219. begin
  3220. if (m_nested_comment in current_settings.modeswitches) then
  3221. inc(comment_level)
  3222. else
  3223. comment_level:=1;
  3224. if (comment_level>1) then
  3225. begin
  3226. savetokenpos;
  3227. gettokenpos; { update for warning }
  3228. Message1(scan_w_comment_level,tostr(comment_level));
  3229. restoretokenpos;
  3230. end;
  3231. end;
  3232. procedure tscannerfile.dec_comment_level;
  3233. begin
  3234. if (m_nested_comment in current_settings.modeswitches) then
  3235. dec(comment_level)
  3236. else
  3237. comment_level:=0;
  3238. end;
  3239. procedure tscannerfile.linebreak;
  3240. var
  3241. cur : char;
  3242. begin
  3243. with inputfile do
  3244. begin
  3245. if (byte(inputpointer^)=0) and not(endoffile) then
  3246. begin
  3247. cur:=c;
  3248. reload;
  3249. if byte(cur)+byte(c)<>23 then
  3250. dec(inputpointer);
  3251. end
  3252. else
  3253. begin
  3254. { Support all combination of #10 and #13 as line break }
  3255. if (byte(inputpointer^)+byte(c)=23) then
  3256. inc(inputpointer);
  3257. end;
  3258. { Always return #10 as line break }
  3259. c:=#10;
  3260. { increase line counters }
  3261. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3262. inc(line_no);
  3263. { update linebuffer }
  3264. if cs_asm_source in current_settings.globalswitches then
  3265. inputfile.setline(line_no,lastlinepos);
  3266. { update for status and call the show status routine,
  3267. but don't touch current_filepos ! }
  3268. savetokenpos;
  3269. gettokenpos; { update for v_status }
  3270. inc(status.compiledlines);
  3271. ShowStatus;
  3272. restoretokenpos;
  3273. end;
  3274. end;
  3275. procedure tscannerfile.illegal_char(c:char);
  3276. var
  3277. s : string;
  3278. begin
  3279. if c in [#32..#255] then
  3280. s:=''''+c+''''
  3281. else
  3282. s:='#'+tostr(ord(c));
  3283. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3284. end;
  3285. procedure tscannerfile.end_of_file;
  3286. begin
  3287. checkpreprocstack;
  3288. Message(scan_f_end_of_file);
  3289. end;
  3290. {-------------------------------------------
  3291. IF Conditional Handling
  3292. -------------------------------------------}
  3293. procedure tscannerfile.checkpreprocstack;
  3294. begin
  3295. { check for missing ifdefs }
  3296. while assigned(preprocstack) do
  3297. begin
  3298. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3299. preprocstack.owner.inputfile.name,tostr(preprocstack.line_nb));
  3300. poppreprocstack;
  3301. end;
  3302. end;
  3303. procedure tscannerfile.poppreprocstack;
  3304. var
  3305. hp : tpreprocstack;
  3306. begin
  3307. if assigned(preprocstack) then
  3308. begin
  3309. Message1(scan_c_endif_found,preprocstack.name);
  3310. hp:=preprocstack.next;
  3311. preprocstack.free;
  3312. preprocstack:=hp;
  3313. end
  3314. else
  3315. Message(scan_e_endif_without_if);
  3316. end;
  3317. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3318. var
  3319. condition: Boolean;
  3320. valuedescr: String;
  3321. begin
  3322. if (preprocstack=nil) or preprocstack.accept then
  3323. condition:=compile_time_predicate(valuedescr)
  3324. else
  3325. begin
  3326. condition:= false;
  3327. valuedescr:= '';
  3328. end;
  3329. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3330. preprocstack.name:=valuedescr;
  3331. preprocstack.line_nb:=line_no;
  3332. preprocstack.owner:=self;
  3333. if preprocstack.accept then
  3334. Message2(messid,preprocstack.name,'accepted')
  3335. else
  3336. Message2(messid,preprocstack.name,'rejected');
  3337. end;
  3338. procedure tscannerfile.elsepreprocstack;
  3339. begin
  3340. if assigned(preprocstack) and
  3341. (preprocstack.typ<>pp_else) then
  3342. begin
  3343. if (preprocstack.typ=pp_elseif) then
  3344. preprocstack.accept:=false
  3345. else
  3346. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3347. preprocstack.accept:=not preprocstack.accept;
  3348. preprocstack.typ:=pp_else;
  3349. preprocstack.line_nb:=line_no;
  3350. if preprocstack.accept then
  3351. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3352. else
  3353. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3354. end
  3355. else
  3356. Message(scan_e_endif_without_if);
  3357. end;
  3358. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3359. var
  3360. valuedescr: String;
  3361. begin
  3362. if assigned(preprocstack) and
  3363. (preprocstack.typ in [pp_if,pp_elseif]) then
  3364. begin
  3365. { when the branch is accepted we use pp_elseif so we know that
  3366. all the next branches need to be rejected. when this branch is still
  3367. not accepted then leave it at pp_if }
  3368. if (preprocstack.typ=pp_elseif) then
  3369. preprocstack.accept:=false
  3370. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  3371. begin
  3372. preprocstack.accept:=false;
  3373. preprocstack.typ:=pp_elseif;
  3374. end
  3375. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  3376. and compile_time_predicate(valuedescr) then
  3377. begin
  3378. preprocstack.name:=valuedescr;
  3379. preprocstack.accept:=true;
  3380. preprocstack.typ:=pp_elseif;
  3381. end;
  3382. preprocstack.line_nb:=line_no;
  3383. if preprocstack.accept then
  3384. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3385. else
  3386. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3387. end
  3388. else
  3389. Message(scan_e_endif_without_if);
  3390. end;
  3391. procedure tscannerfile.popreplaystack;
  3392. var
  3393. hp : treplaystack;
  3394. begin
  3395. if assigned(replaystack) then
  3396. begin
  3397. hp:=replaystack.next;
  3398. replaystack.free;
  3399. replaystack:=hp;
  3400. end;
  3401. end;
  3402. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  3403. begin
  3404. savetokenpos;
  3405. repeat
  3406. current_scanner.gettokenpos;
  3407. Message1(scan_d_handling_switch,'$'+p.name);
  3408. p.proc();
  3409. { accept the text ? }
  3410. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  3411. break
  3412. else
  3413. begin
  3414. current_scanner.gettokenpos;
  3415. Message(scan_c_skipping_until);
  3416. repeat
  3417. current_scanner.skipuntildirective;
  3418. if not (m_mac in current_settings.modeswitches) then
  3419. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  3420. else
  3421. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  3422. until assigned(p) and (p.is_conditional);
  3423. current_scanner.gettokenpos;
  3424. end;
  3425. until false;
  3426. restoretokenpos;
  3427. end;
  3428. procedure tscannerfile.handledirectives;
  3429. var
  3430. t : tdirectiveitem;
  3431. hs : string;
  3432. begin
  3433. gettokenpos;
  3434. readchar; {Remove the $}
  3435. hs:=readid;
  3436. { handle empty directive }
  3437. if hs='' then
  3438. begin
  3439. Message1(scan_w_illegal_switch,'$');
  3440. exit;
  3441. end;
  3442. {$ifdef PREPROCWRITE}
  3443. if parapreprocess then
  3444. begin
  3445. t:=Get_Directive(hs);
  3446. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  3447. begin
  3448. preprocfile^.AddSpace;
  3449. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  3450. exit;
  3451. end;
  3452. end;
  3453. {$endif PREPROCWRITE}
  3454. { skip this directive? }
  3455. if (ignoredirectives.find(hs)<>nil) then
  3456. begin
  3457. if (comment_level>0) then
  3458. readcomment;
  3459. { we've read the whole comment }
  3460. aktcommentstyle:=comment_none;
  3461. exit;
  3462. end;
  3463. { Check for compiler switches }
  3464. while (length(hs)=1) and (c in ['-','+']) do
  3465. begin
  3466. Message1(scan_d_handling_switch,'$'+hs+c);
  3467. HandleSwitch(hs[1],c);
  3468. current_scanner.readchar; {Remove + or -}
  3469. if c=',' then
  3470. begin
  3471. current_scanner.readchar; {Remove , }
  3472. { read next switch, support $v+,$+}
  3473. hs:=current_scanner.readid;
  3474. if (hs='') then
  3475. begin
  3476. if (c='$') and (m_fpc in current_settings.modeswitches) then
  3477. begin
  3478. current_scanner.readchar; { skip $ }
  3479. hs:=current_scanner.readid;
  3480. end;
  3481. if (hs='') then
  3482. Message1(scan_w_illegal_directive,'$'+c);
  3483. end;
  3484. end
  3485. else
  3486. hs:='';
  3487. end;
  3488. { directives may follow switches after a , }
  3489. if hs<>'' then
  3490. begin
  3491. if not (m_mac in current_settings.modeswitches) then
  3492. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3493. else
  3494. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3495. if assigned(t) then
  3496. begin
  3497. if t.is_conditional then
  3498. handleconditional(t)
  3499. else
  3500. begin
  3501. Message1(scan_d_handling_switch,'$'+hs);
  3502. t.proc();
  3503. end;
  3504. end
  3505. else
  3506. begin
  3507. current_scanner.ignoredirectives.Add(hs,nil);
  3508. Message1(scan_w_illegal_directive,'$'+hs);
  3509. end;
  3510. { conditionals already read the comment }
  3511. if (current_scanner.comment_level>0) then
  3512. current_scanner.readcomment;
  3513. { we've read the whole comment }
  3514. aktcommentstyle:=comment_none;
  3515. end;
  3516. end;
  3517. procedure tscannerfile.readchar;
  3518. begin
  3519. c:=inputpointer^;
  3520. if c=#0 then
  3521. reload
  3522. else
  3523. inc(inputpointer);
  3524. end;
  3525. procedure tscannerfile.readstring;
  3526. var
  3527. i : longint;
  3528. err : boolean;
  3529. begin
  3530. err:=false;
  3531. i:=0;
  3532. repeat
  3533. case c of
  3534. '_',
  3535. '0'..'9',
  3536. 'A'..'Z' :
  3537. begin
  3538. if i<255 then
  3539. begin
  3540. inc(i);
  3541. orgpattern[i]:=c;
  3542. pattern[i]:=c;
  3543. end
  3544. else
  3545. begin
  3546. if not err then
  3547. begin
  3548. Message(scan_e_string_exceeds_255_chars);
  3549. err:=true;
  3550. end;
  3551. end;
  3552. c:=inputpointer^;
  3553. inc(inputpointer);
  3554. end;
  3555. 'a'..'z' :
  3556. begin
  3557. if i<255 then
  3558. begin
  3559. inc(i);
  3560. orgpattern[i]:=c;
  3561. pattern[i]:=chr(ord(c)-32)
  3562. end
  3563. else
  3564. begin
  3565. if not err then
  3566. begin
  3567. Message(scan_e_string_exceeds_255_chars);
  3568. err:=true;
  3569. end;
  3570. end;
  3571. c:=inputpointer^;
  3572. inc(inputpointer);
  3573. end;
  3574. #0 :
  3575. reload;
  3576. else
  3577. break;
  3578. end;
  3579. until false;
  3580. orgpattern[0]:=chr(i);
  3581. pattern[0]:=chr(i);
  3582. end;
  3583. procedure tscannerfile.readnumber;
  3584. var
  3585. base,
  3586. i : longint;
  3587. begin
  3588. case c of
  3589. '%' :
  3590. begin
  3591. readchar;
  3592. base:=2;
  3593. pattern[1]:='%';
  3594. i:=1;
  3595. end;
  3596. '&' :
  3597. begin
  3598. readchar;
  3599. base:=8;
  3600. pattern[1]:='&';
  3601. i:=1;
  3602. end;
  3603. '$' :
  3604. begin
  3605. readchar;
  3606. base:=16;
  3607. pattern[1]:='$';
  3608. i:=1;
  3609. end;
  3610. else
  3611. begin
  3612. base:=10;
  3613. i:=0;
  3614. end;
  3615. end;
  3616. while ((base>=10) and (c in ['0'..'9'])) or
  3617. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3618. ((base=8) and (c in ['0'..'7'])) or
  3619. ((base=2) and (c in ['0'..'1'])) do
  3620. begin
  3621. if i<255 then
  3622. begin
  3623. inc(i);
  3624. pattern[i]:=c;
  3625. end;
  3626. readchar;
  3627. end;
  3628. pattern[0]:=chr(i);
  3629. end;
  3630. function tscannerfile.readid:string;
  3631. begin
  3632. readstring;
  3633. readid:=pattern;
  3634. end;
  3635. function tscannerfile.readval:longint;
  3636. var
  3637. l : longint;
  3638. w : integer;
  3639. begin
  3640. readnumber;
  3641. val(pattern,l,w);
  3642. readval:=l;
  3643. end;
  3644. function tscannerfile.readcomment:string;
  3645. var
  3646. i : longint;
  3647. begin
  3648. i:=0;
  3649. repeat
  3650. case c of
  3651. '{' :
  3652. begin
  3653. if aktcommentstyle=comment_tp then
  3654. inc_comment_level;
  3655. end;
  3656. '}' :
  3657. begin
  3658. if aktcommentstyle=comment_tp then
  3659. begin
  3660. readchar;
  3661. dec_comment_level;
  3662. if comment_level=0 then
  3663. break
  3664. else
  3665. continue;
  3666. end;
  3667. end;
  3668. '*' :
  3669. begin
  3670. if aktcommentstyle=comment_oldtp then
  3671. begin
  3672. readchar;
  3673. if c=')' then
  3674. begin
  3675. readchar;
  3676. dec_comment_level;
  3677. break;
  3678. end
  3679. else
  3680. { Add both characters !!}
  3681. if (i<255) then
  3682. begin
  3683. inc(i);
  3684. readcomment[i]:='*';
  3685. if (i<255) then
  3686. begin
  3687. inc(i);
  3688. readcomment[i]:=c;
  3689. end;
  3690. end;
  3691. end
  3692. else
  3693. { Not old TP comment, so add...}
  3694. begin
  3695. if (i<255) then
  3696. begin
  3697. inc(i);
  3698. readcomment[i]:='*';
  3699. end;
  3700. end;
  3701. end;
  3702. #10,#13 :
  3703. linebreak;
  3704. #26 :
  3705. end_of_file;
  3706. else
  3707. begin
  3708. if (i<255) then
  3709. begin
  3710. inc(i);
  3711. readcomment[i]:=c;
  3712. end;
  3713. end;
  3714. end;
  3715. readchar;
  3716. until false;
  3717. readcomment[0]:=chr(i);
  3718. end;
  3719. function tscannerfile.readquotedstring:string;
  3720. var
  3721. i : longint;
  3722. msgwritten : boolean;
  3723. begin
  3724. i:=0;
  3725. msgwritten:=false;
  3726. if (c='''') then
  3727. begin
  3728. repeat
  3729. readchar;
  3730. case c of
  3731. #26 :
  3732. end_of_file;
  3733. #10,#13 :
  3734. Message(scan_f_string_exceeds_line);
  3735. '''' :
  3736. begin
  3737. readchar;
  3738. if c<>'''' then
  3739. break;
  3740. end;
  3741. end;
  3742. if i<255 then
  3743. begin
  3744. inc(i);
  3745. result[i]:=c;
  3746. end
  3747. else
  3748. begin
  3749. if not msgwritten then
  3750. begin
  3751. Message(scan_e_string_exceeds_255_chars);
  3752. msgwritten:=true;
  3753. end;
  3754. end;
  3755. until false;
  3756. end;
  3757. result[0]:=chr(i);
  3758. end;
  3759. function tscannerfile.readstate:char;
  3760. var
  3761. state : char;
  3762. begin
  3763. state:=' ';
  3764. if c=' ' then
  3765. begin
  3766. current_scanner.skipspace;
  3767. current_scanner.readid;
  3768. if pattern='ON' then
  3769. state:='+'
  3770. else
  3771. if pattern='OFF' then
  3772. state:='-';
  3773. end
  3774. else
  3775. state:=c;
  3776. if not (state in ['+','-']) then
  3777. Message(scan_e_wrong_switch_toggle);
  3778. readstate:=state;
  3779. end;
  3780. function tscannerfile.readstatedefault:char;
  3781. var
  3782. state : char;
  3783. begin
  3784. state:=' ';
  3785. if c=' ' then
  3786. begin
  3787. current_scanner.skipspace;
  3788. current_scanner.readid;
  3789. if pattern='ON' then
  3790. state:='+'
  3791. else
  3792. if pattern='OFF' then
  3793. state:='-'
  3794. else
  3795. if pattern='DEFAULT' then
  3796. state:='*';
  3797. end
  3798. else
  3799. state:=c;
  3800. if not (state in ['+','-','*']) then
  3801. Message(scan_e_wrong_switch_toggle_default);
  3802. readstatedefault:=state;
  3803. end;
  3804. procedure tscannerfile.skipspace;
  3805. begin
  3806. repeat
  3807. case c of
  3808. #26 :
  3809. begin
  3810. reload;
  3811. if (c=#26) and not assigned(inputfile.next) then
  3812. break;
  3813. continue;
  3814. end;
  3815. #10,
  3816. #13 :
  3817. linebreak;
  3818. #9,#11,#12,' ' :
  3819. ;
  3820. else
  3821. break;
  3822. end;
  3823. readchar;
  3824. until false;
  3825. end;
  3826. procedure tscannerfile.skipuntildirective;
  3827. var
  3828. found : longint;
  3829. next_char_loaded : boolean;
  3830. begin
  3831. found:=0;
  3832. next_char_loaded:=false;
  3833. repeat
  3834. case c of
  3835. #10,
  3836. #13 :
  3837. linebreak;
  3838. #26 :
  3839. begin
  3840. reload;
  3841. if (c=#26) and not assigned(inputfile.next) then
  3842. end_of_file;
  3843. continue;
  3844. end;
  3845. '{' :
  3846. begin
  3847. if (aktcommentstyle in [comment_tp,comment_none]) then
  3848. begin
  3849. aktcommentstyle:=comment_tp;
  3850. if (comment_level=0) then
  3851. found:=1;
  3852. inc_comment_level;
  3853. end;
  3854. end;
  3855. '*' :
  3856. begin
  3857. if (aktcommentstyle=comment_oldtp) then
  3858. begin
  3859. readchar;
  3860. if c=')' then
  3861. begin
  3862. dec_comment_level;
  3863. found:=0;
  3864. aktcommentstyle:=comment_none;
  3865. end
  3866. else
  3867. next_char_loaded:=true;
  3868. end
  3869. else
  3870. found := 0;
  3871. end;
  3872. '}' :
  3873. begin
  3874. if (aktcommentstyle=comment_tp) then
  3875. begin
  3876. dec_comment_level;
  3877. if (comment_level=0) then
  3878. aktcommentstyle:=comment_none;
  3879. found:=0;
  3880. end;
  3881. end;
  3882. '$' :
  3883. begin
  3884. if found=1 then
  3885. found:=2;
  3886. end;
  3887. '''' :
  3888. if (aktcommentstyle=comment_none) then
  3889. begin
  3890. repeat
  3891. readchar;
  3892. case c of
  3893. #26 :
  3894. end_of_file;
  3895. #10,#13 :
  3896. break;
  3897. '''' :
  3898. begin
  3899. readchar;
  3900. if c<>'''' then
  3901. begin
  3902. next_char_loaded:=true;
  3903. break;
  3904. end;
  3905. end;
  3906. end;
  3907. until false;
  3908. end;
  3909. '(' :
  3910. begin
  3911. if (aktcommentstyle=comment_none) then
  3912. begin
  3913. readchar;
  3914. if c='*' then
  3915. begin
  3916. readchar;
  3917. if c='$' then
  3918. begin
  3919. found:=2;
  3920. inc_comment_level;
  3921. aktcommentstyle:=comment_oldtp;
  3922. end
  3923. else
  3924. begin
  3925. skipoldtpcomment;
  3926. next_char_loaded:=true;
  3927. end;
  3928. end
  3929. else
  3930. next_char_loaded:=true;
  3931. end
  3932. else
  3933. found:=0;
  3934. end;
  3935. '/' :
  3936. begin
  3937. if (aktcommentstyle=comment_none) then
  3938. begin
  3939. readchar;
  3940. if c='/' then
  3941. skipdelphicomment;
  3942. next_char_loaded:=true;
  3943. end
  3944. else
  3945. found:=0;
  3946. end;
  3947. else
  3948. found:=0;
  3949. end;
  3950. if next_char_loaded then
  3951. next_char_loaded:=false
  3952. else
  3953. readchar;
  3954. until (found=2);
  3955. end;
  3956. {****************************************************************************
  3957. Comment Handling
  3958. ****************************************************************************}
  3959. procedure tscannerfile.skipcomment;
  3960. begin
  3961. aktcommentstyle:=comment_tp;
  3962. readchar;
  3963. inc_comment_level;
  3964. { handle compiler switches }
  3965. if (c='$') then
  3966. handledirectives;
  3967. { handle_switches can dec comment_level, }
  3968. while (comment_level>0) do
  3969. begin
  3970. case c of
  3971. '{' :
  3972. inc_comment_level;
  3973. '}' :
  3974. dec_comment_level;
  3975. #10,#13 :
  3976. linebreak;
  3977. #26 :
  3978. begin
  3979. reload;
  3980. if (c=#26) and not assigned(inputfile.next) then
  3981. end_of_file;
  3982. continue;
  3983. end;
  3984. end;
  3985. readchar;
  3986. end;
  3987. aktcommentstyle:=comment_none;
  3988. end;
  3989. procedure tscannerfile.skipdelphicomment;
  3990. begin
  3991. aktcommentstyle:=comment_delphi;
  3992. inc_comment_level;
  3993. readchar;
  3994. { this is not supported }
  3995. if c='$' then
  3996. Message(scan_w_wrong_styled_switch);
  3997. { skip comment }
  3998. while not (c in [#10,#13,#26]) do
  3999. readchar;
  4000. dec_comment_level;
  4001. aktcommentstyle:=comment_none;
  4002. end;
  4003. procedure tscannerfile.skipoldtpcomment;
  4004. var
  4005. found : longint;
  4006. begin
  4007. aktcommentstyle:=comment_oldtp;
  4008. inc_comment_level;
  4009. { only load a char if last already processed,
  4010. was cause of bug1634 PM }
  4011. if c=#0 then
  4012. readchar;
  4013. { this is now supported }
  4014. if (c='$') then
  4015. handledirectives;
  4016. { skip comment }
  4017. while (comment_level>0) do
  4018. begin
  4019. found:=0;
  4020. repeat
  4021. case c of
  4022. #26 :
  4023. begin
  4024. reload;
  4025. if (c=#26) and not assigned(inputfile.next) then
  4026. end_of_file;
  4027. continue;
  4028. end;
  4029. #10,#13 :
  4030. begin
  4031. if found=4 then
  4032. inc_comment_level;
  4033. linebreak;
  4034. found:=0;
  4035. end;
  4036. '*' :
  4037. begin
  4038. if found=3 then
  4039. found:=4
  4040. else
  4041. found:=1;
  4042. end;
  4043. ')' :
  4044. begin
  4045. if found in [1,4] then
  4046. begin
  4047. dec_comment_level;
  4048. if comment_level=0 then
  4049. found:=2
  4050. else
  4051. found:=0;
  4052. end
  4053. else
  4054. found:=0;
  4055. end;
  4056. '(' :
  4057. begin
  4058. if found=4 then
  4059. inc_comment_level;
  4060. found:=3;
  4061. end;
  4062. else
  4063. begin
  4064. if found=4 then
  4065. inc_comment_level;
  4066. found:=0;
  4067. end;
  4068. end;
  4069. readchar;
  4070. until (found=2);
  4071. end;
  4072. aktcommentstyle:=comment_none;
  4073. end;
  4074. {****************************************************************************
  4075. Token Scanner
  4076. ****************************************************************************}
  4077. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4078. var
  4079. code : integer;
  4080. len,
  4081. low,high,mid : longint;
  4082. w : word;
  4083. m : longint;
  4084. mac : tmacro;
  4085. asciinr : string[33];
  4086. iswidestring : boolean;
  4087. label
  4088. exit_label;
  4089. begin
  4090. flushpendingswitchesstate;
  4091. { record tokens? }
  4092. if allowrecordtoken and
  4093. assigned(recordtokenbuf) then
  4094. recordtoken;
  4095. { replay tokens? }
  4096. if assigned(replaytokenbuf) then
  4097. begin
  4098. replaytoken;
  4099. goto exit_label;
  4100. end;
  4101. { was there already a token read, then return that token }
  4102. if nexttoken<>NOTOKEN then
  4103. begin
  4104. setnexttoken;
  4105. goto exit_label;
  4106. end;
  4107. { Skip all spaces and comments }
  4108. repeat
  4109. case c of
  4110. '{' :
  4111. skipcomment;
  4112. #26 :
  4113. begin
  4114. reload;
  4115. if (c=#26) and not assigned(inputfile.next) then
  4116. break;
  4117. end;
  4118. ' ',#9..#13 :
  4119. begin
  4120. {$ifdef PREPROCWRITE}
  4121. if parapreprocess then
  4122. begin
  4123. if c=#10 then
  4124. preprocfile.eolfound:=true
  4125. else
  4126. preprocfile.spacefound:=true;
  4127. end;
  4128. {$endif PREPROCWRITE}
  4129. skipspace;
  4130. end
  4131. else
  4132. break;
  4133. end;
  4134. until false;
  4135. { Save current token position, for EOF its already loaded }
  4136. if c<>#26 then
  4137. gettokenpos;
  4138. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4139. if c in ['A'..'Z','a'..'z','_'] then
  4140. begin
  4141. readstring;
  4142. token:=_ID;
  4143. idtoken:=_ID;
  4144. { keyword or any other known token,
  4145. pattern is always uppercased }
  4146. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4147. begin
  4148. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4149. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4150. while low<high do
  4151. begin
  4152. mid:=(high+low+1) shr 1;
  4153. if pattern<tokeninfo^[ttoken(mid)].str then
  4154. high:=mid-1
  4155. else
  4156. low:=mid;
  4157. end;
  4158. with tokeninfo^[ttoken(high)] do
  4159. if pattern=str then
  4160. begin
  4161. if (keyword*current_settings.modeswitches)<>[] then
  4162. if op=NOTOKEN then
  4163. token:=ttoken(high)
  4164. else
  4165. token:=op;
  4166. idtoken:=ttoken(high);
  4167. end;
  4168. end;
  4169. { Only process identifiers and not keywords }
  4170. if token=_ID then
  4171. begin
  4172. { this takes some time ... }
  4173. if (cs_support_macro in current_settings.moduleswitches) then
  4174. begin
  4175. mac:=tmacro(search_macro(pattern));
  4176. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4177. begin
  4178. if yylexcount<max_macro_nesting then
  4179. begin
  4180. mac.is_used:=true;
  4181. inc(yylexcount);
  4182. substitutemacro(pattern,mac.buftext,mac.buflen,
  4183. mac.fileinfo.line,mac.fileinfo.fileindex);
  4184. { handle empty macros }
  4185. if c=#0 then
  4186. reload;
  4187. readtoken(false);
  4188. { that's all folks }
  4189. dec(yylexcount);
  4190. exit;
  4191. end
  4192. else
  4193. Message(scan_w_macro_too_deep);
  4194. end;
  4195. end;
  4196. end;
  4197. { return token }
  4198. goto exit_label;
  4199. end
  4200. else
  4201. begin
  4202. idtoken:=_NOID;
  4203. case c of
  4204. '$' :
  4205. begin
  4206. readnumber;
  4207. token:=_INTCONST;
  4208. goto exit_label;
  4209. end;
  4210. '%' :
  4211. begin
  4212. if not(m_fpc in current_settings.modeswitches) then
  4213. Illegal_Char(c)
  4214. else
  4215. begin
  4216. readnumber;
  4217. token:=_INTCONST;
  4218. goto exit_label;
  4219. end;
  4220. end;
  4221. '&' :
  4222. begin
  4223. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4224. begin
  4225. readnumber;
  4226. if length(pattern)=1 then
  4227. begin
  4228. { does really an identifier follow? }
  4229. if not (c in ['_','A'..'Z','a'..'z']) then
  4230. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4231. readstring;
  4232. token:=_ID;
  4233. idtoken:=_ID;
  4234. end
  4235. else
  4236. token:=_INTCONST;
  4237. goto exit_label;
  4238. end
  4239. else if m_mac in current_settings.modeswitches then
  4240. begin
  4241. readchar;
  4242. token:=_AMPERSAND;
  4243. goto exit_label;
  4244. end
  4245. else
  4246. Illegal_Char(c);
  4247. end;
  4248. '0'..'9' :
  4249. begin
  4250. readnumber;
  4251. if (c in ['.','e','E']) then
  4252. begin
  4253. { first check for a . }
  4254. if c='.' then
  4255. begin
  4256. cachenexttokenpos;
  4257. readchar;
  4258. { is it a .. from a range? }
  4259. case c of
  4260. '.' :
  4261. begin
  4262. readchar;
  4263. token:=_INTCONST;
  4264. nexttoken:=_POINTPOINT;
  4265. goto exit_label;
  4266. end;
  4267. ')' :
  4268. begin
  4269. readchar;
  4270. token:=_INTCONST;
  4271. nexttoken:=_RECKKLAMMER;
  4272. goto exit_label;
  4273. end;
  4274. '0'..'9' :
  4275. begin
  4276. { insert the number after the . }
  4277. pattern:=pattern+'.';
  4278. while c in ['0'..'9'] do
  4279. begin
  4280. pattern:=pattern+c;
  4281. readchar;
  4282. end;
  4283. end;
  4284. else
  4285. begin
  4286. token:=_INTCONST;
  4287. nexttoken:=_POINT;
  4288. goto exit_label;
  4289. end;
  4290. end;
  4291. end;
  4292. { E can also follow after a point is scanned }
  4293. if c in ['e','E'] then
  4294. begin
  4295. pattern:=pattern+'E';
  4296. readchar;
  4297. if c in ['-','+'] then
  4298. begin
  4299. pattern:=pattern+c;
  4300. readchar;
  4301. end;
  4302. if not(c in ['0'..'9']) then
  4303. Illegal_Char(c);
  4304. while c in ['0'..'9'] do
  4305. begin
  4306. pattern:=pattern+c;
  4307. readchar;
  4308. end;
  4309. end;
  4310. token:=_REALNUMBER;
  4311. goto exit_label;
  4312. end;
  4313. token:=_INTCONST;
  4314. goto exit_label;
  4315. end;
  4316. ';' :
  4317. begin
  4318. readchar;
  4319. token:=_SEMICOLON;
  4320. goto exit_label;
  4321. end;
  4322. '[' :
  4323. begin
  4324. readchar;
  4325. token:=_LECKKLAMMER;
  4326. goto exit_label;
  4327. end;
  4328. ']' :
  4329. begin
  4330. readchar;
  4331. token:=_RECKKLAMMER;
  4332. goto exit_label;
  4333. end;
  4334. '(' :
  4335. begin
  4336. readchar;
  4337. case c of
  4338. '*' :
  4339. begin
  4340. c:=#0;{Signal skipoldtpcomment to reload a char }
  4341. skipoldtpcomment;
  4342. readtoken(false);
  4343. exit;
  4344. end;
  4345. '.' :
  4346. begin
  4347. readchar;
  4348. token:=_LECKKLAMMER;
  4349. goto exit_label;
  4350. end;
  4351. end;
  4352. token:=_LKLAMMER;
  4353. goto exit_label;
  4354. end;
  4355. ')' :
  4356. begin
  4357. readchar;
  4358. token:=_RKLAMMER;
  4359. goto exit_label;
  4360. end;
  4361. '+' :
  4362. begin
  4363. readchar;
  4364. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4365. begin
  4366. readchar;
  4367. token:=_PLUSASN;
  4368. goto exit_label;
  4369. end;
  4370. token:=_PLUS;
  4371. goto exit_label;
  4372. end;
  4373. '-' :
  4374. begin
  4375. readchar;
  4376. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4377. begin
  4378. readchar;
  4379. token:=_MINUSASN;
  4380. goto exit_label;
  4381. end;
  4382. token:=_MINUS;
  4383. goto exit_label;
  4384. end;
  4385. ':' :
  4386. begin
  4387. readchar;
  4388. if c='=' then
  4389. begin
  4390. readchar;
  4391. token:=_ASSIGNMENT;
  4392. goto exit_label;
  4393. end;
  4394. token:=_COLON;
  4395. goto exit_label;
  4396. end;
  4397. '*' :
  4398. begin
  4399. readchar;
  4400. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4401. begin
  4402. readchar;
  4403. token:=_STARASN;
  4404. end
  4405. else
  4406. if c='*' then
  4407. begin
  4408. readchar;
  4409. token:=_STARSTAR;
  4410. end
  4411. else
  4412. token:=_STAR;
  4413. goto exit_label;
  4414. end;
  4415. '/' :
  4416. begin
  4417. readchar;
  4418. case c of
  4419. '=' :
  4420. begin
  4421. if (cs_support_c_operators in current_settings.moduleswitches) then
  4422. begin
  4423. readchar;
  4424. token:=_SLASHASN;
  4425. goto exit_label;
  4426. end;
  4427. end;
  4428. '/' :
  4429. begin
  4430. skipdelphicomment;
  4431. readtoken(false);
  4432. exit;
  4433. end;
  4434. end;
  4435. token:=_SLASH;
  4436. goto exit_label;
  4437. end;
  4438. '|' :
  4439. if m_mac in current_settings.modeswitches then
  4440. begin
  4441. readchar;
  4442. token:=_PIPE;
  4443. goto exit_label;
  4444. end
  4445. else
  4446. Illegal_Char(c);
  4447. '=' :
  4448. begin
  4449. readchar;
  4450. token:=_EQ;
  4451. goto exit_label;
  4452. end;
  4453. '.' :
  4454. begin
  4455. readchar;
  4456. case c of
  4457. '.' :
  4458. begin
  4459. readchar;
  4460. case c of
  4461. '.' :
  4462. begin
  4463. readchar;
  4464. token:=_POINTPOINTPOINT;
  4465. goto exit_label;
  4466. end;
  4467. else
  4468. begin
  4469. token:=_POINTPOINT;
  4470. goto exit_label;
  4471. end;
  4472. end;
  4473. end;
  4474. ')' :
  4475. begin
  4476. readchar;
  4477. token:=_RECKKLAMMER;
  4478. goto exit_label;
  4479. end;
  4480. end;
  4481. token:=_POINT;
  4482. goto exit_label;
  4483. end;
  4484. '@' :
  4485. begin
  4486. readchar;
  4487. token:=_KLAMMERAFFE;
  4488. goto exit_label;
  4489. end;
  4490. ',' :
  4491. begin
  4492. readchar;
  4493. token:=_COMMA;
  4494. goto exit_label;
  4495. end;
  4496. '''','#','^' :
  4497. begin
  4498. len:=0;
  4499. cstringpattern:='';
  4500. iswidestring:=false;
  4501. if c='^' then
  4502. begin
  4503. readchar;
  4504. c:=upcase(c);
  4505. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4506. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4507. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4508. begin
  4509. token:=_CARET;
  4510. goto exit_label;
  4511. end
  4512. else
  4513. begin
  4514. inc(len);
  4515. setlength(cstringpattern,256);
  4516. if c<#64 then
  4517. cstringpattern[len]:=chr(ord(c)+64)
  4518. else
  4519. cstringpattern[len]:=chr(ord(c)-64);
  4520. readchar;
  4521. end;
  4522. end;
  4523. repeat
  4524. case c of
  4525. '#' :
  4526. begin
  4527. readchar; { read # }
  4528. case c of
  4529. '$':
  4530. begin
  4531. readchar; { read leading $ }
  4532. asciinr:='$';
  4533. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  4534. begin
  4535. asciinr:=asciinr+c;
  4536. readchar;
  4537. end;
  4538. end;
  4539. '&':
  4540. begin
  4541. readchar; { read leading $ }
  4542. asciinr:='&';
  4543. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  4544. begin
  4545. asciinr:=asciinr+c;
  4546. readchar;
  4547. end;
  4548. end;
  4549. '%':
  4550. begin
  4551. readchar; { read leading $ }
  4552. asciinr:='%';
  4553. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  4554. begin
  4555. asciinr:=asciinr+c;
  4556. readchar;
  4557. end;
  4558. end;
  4559. else
  4560. begin
  4561. asciinr:='';
  4562. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  4563. begin
  4564. asciinr:=asciinr+c;
  4565. readchar;
  4566. end;
  4567. end;
  4568. end;
  4569. val(asciinr,m,code);
  4570. if (asciinr='') or (code<>0) then
  4571. Message(scan_e_illegal_char_const)
  4572. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4573. begin
  4574. if (m>=0) and (m<=65535) then
  4575. begin
  4576. if not iswidestring then
  4577. begin
  4578. if len>0 then
  4579. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4580. else
  4581. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4582. iswidestring:=true;
  4583. len:=0;
  4584. end;
  4585. concatwidestringchar(patternw,tcompilerwidechar(m));
  4586. end
  4587. else
  4588. Message(scan_e_illegal_char_const)
  4589. end
  4590. else if iswidestring then
  4591. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4592. else
  4593. begin
  4594. if len>=length(cstringpattern) then
  4595. setlength(cstringpattern,length(cstringpattern)+256);
  4596. inc(len);
  4597. cstringpattern[len]:=chr(m);
  4598. end;
  4599. end;
  4600. '''' :
  4601. begin
  4602. repeat
  4603. readchar;
  4604. case c of
  4605. #26 :
  4606. end_of_file;
  4607. #10,#13 :
  4608. Message(scan_f_string_exceeds_line);
  4609. '''' :
  4610. begin
  4611. readchar;
  4612. if c<>'''' then
  4613. break;
  4614. end;
  4615. end;
  4616. { interpret as utf-8 string? }
  4617. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4618. begin
  4619. { convert existing string to an utf-8 string }
  4620. if not iswidestring then
  4621. begin
  4622. if len>0 then
  4623. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4624. else
  4625. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4626. iswidestring:=true;
  4627. len:=0;
  4628. end;
  4629. { four or more chars aren't handled }
  4630. if (ord(c) and $f0)=$f0 then
  4631. message(scan_e_utf8_bigger_than_65535)
  4632. { three chars }
  4633. else if (ord(c) and $e0)=$e0 then
  4634. begin
  4635. w:=ord(c) and $f;
  4636. readchar;
  4637. if (ord(c) and $c0)<>$80 then
  4638. message(scan_e_utf8_malformed);
  4639. w:=(w shl 6) or (ord(c) and $3f);
  4640. readchar;
  4641. if (ord(c) and $c0)<>$80 then
  4642. message(scan_e_utf8_malformed);
  4643. w:=(w shl 6) or (ord(c) and $3f);
  4644. concatwidestringchar(patternw,w);
  4645. end
  4646. { two chars }
  4647. else if (ord(c) and $c0)<>0 then
  4648. begin
  4649. w:=ord(c) and $1f;
  4650. readchar;
  4651. if (ord(c) and $c0)<>$80 then
  4652. message(scan_e_utf8_malformed);
  4653. w:=(w shl 6) or (ord(c) and $3f);
  4654. concatwidestringchar(patternw,w);
  4655. end
  4656. { illegal }
  4657. else if (ord(c) and $80)<>0 then
  4658. message(scan_e_utf8_malformed)
  4659. else
  4660. concatwidestringchar(patternw,tcompilerwidechar(c))
  4661. end
  4662. else if iswidestring then
  4663. begin
  4664. if current_settings.sourcecodepage=CP_UTF8 then
  4665. concatwidestringchar(patternw,ord(c))
  4666. else
  4667. concatwidestringchar(patternw,asciichar2unicode(c))
  4668. end
  4669. else
  4670. begin
  4671. if len>=length(cstringpattern) then
  4672. setlength(cstringpattern,length(cstringpattern)+256);
  4673. inc(len);
  4674. cstringpattern[len]:=c;
  4675. end;
  4676. until false;
  4677. end;
  4678. '^' :
  4679. begin
  4680. readchar;
  4681. c:=upcase(c);
  4682. if c<#64 then
  4683. c:=chr(ord(c)+64)
  4684. else
  4685. c:=chr(ord(c)-64);
  4686. if iswidestring then
  4687. concatwidestringchar(patternw,asciichar2unicode(c))
  4688. else
  4689. begin
  4690. if len>=length(cstringpattern) then
  4691. setlength(cstringpattern,length(cstringpattern)+256);
  4692. inc(len);
  4693. cstringpattern[len]:=c;
  4694. end;
  4695. readchar;
  4696. end;
  4697. else
  4698. break;
  4699. end;
  4700. until false;
  4701. { strings with length 1 become const chars }
  4702. if iswidestring then
  4703. begin
  4704. if patternw^.len=1 then
  4705. token:=_CWCHAR
  4706. else
  4707. token:=_CWSTRING;
  4708. end
  4709. else
  4710. begin
  4711. setlength(cstringpattern,len);
  4712. if length(cstringpattern)=1 then
  4713. begin
  4714. token:=_CCHAR;
  4715. pattern:=cstringpattern;
  4716. end
  4717. else
  4718. token:=_CSTRING;
  4719. end;
  4720. goto exit_label;
  4721. end;
  4722. '>' :
  4723. begin
  4724. readchar;
  4725. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4726. token:=_RSHARPBRACKET
  4727. else
  4728. begin
  4729. case c of
  4730. '=' :
  4731. begin
  4732. readchar;
  4733. token:=_GTE;
  4734. goto exit_label;
  4735. end;
  4736. '>' :
  4737. begin
  4738. readchar;
  4739. token:=_OP_SHR;
  4740. goto exit_label;
  4741. end;
  4742. '<' :
  4743. begin { >< is for a symetric diff for sets }
  4744. readchar;
  4745. token:=_SYMDIF;
  4746. goto exit_label;
  4747. end;
  4748. end;
  4749. token:=_GT;
  4750. end;
  4751. goto exit_label;
  4752. end;
  4753. '<' :
  4754. begin
  4755. readchar;
  4756. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4757. token:=_LSHARPBRACKET
  4758. else
  4759. begin
  4760. case c of
  4761. '>' :
  4762. begin
  4763. readchar;
  4764. token:=_NE;
  4765. goto exit_label;
  4766. end;
  4767. '=' :
  4768. begin
  4769. readchar;
  4770. token:=_LTE;
  4771. goto exit_label;
  4772. end;
  4773. '<' :
  4774. begin
  4775. readchar;
  4776. token:=_OP_SHL;
  4777. goto exit_label;
  4778. end;
  4779. end;
  4780. token:=_LT;
  4781. end;
  4782. goto exit_label;
  4783. end;
  4784. #26 :
  4785. begin
  4786. token:=_EOF;
  4787. checkpreprocstack;
  4788. goto exit_label;
  4789. end;
  4790. else
  4791. Illegal_Char(c);
  4792. end;
  4793. end;
  4794. exit_label:
  4795. lasttoken:=token;
  4796. end;
  4797. function tscannerfile.readpreproc:ttoken;
  4798. var
  4799. low,high,mid: longint;
  4800. optoken: ttoken;
  4801. begin
  4802. skipspace;
  4803. case c of
  4804. '_',
  4805. 'A'..'Z',
  4806. 'a'..'z' :
  4807. begin
  4808. readstring;
  4809. optoken:=_ID;
  4810. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4811. begin
  4812. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4813. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4814. while low<high do
  4815. begin
  4816. mid:=(high+low+1) shr 1;
  4817. if pattern<tokeninfo^[ttoken(mid)].str then
  4818. high:=mid-1
  4819. else
  4820. low:=mid;
  4821. end;
  4822. with tokeninfo^[ttoken(high)] do
  4823. if pattern=str then
  4824. begin
  4825. if (keyword*current_settings.modeswitches)<>[] then
  4826. if op=NOTOKEN then
  4827. optoken:=ttoken(high)
  4828. else
  4829. optoken:=op;
  4830. end;
  4831. if not (optoken in preproc_operators) then
  4832. optoken:=_ID;
  4833. end;
  4834. current_scanner.preproc_pattern:=pattern;
  4835. readpreproc:=optoken;
  4836. end;
  4837. '0'..'9' :
  4838. begin
  4839. readnumber;
  4840. if (c in ['.','e','E']) then
  4841. begin
  4842. { first check for a . }
  4843. if c='.' then
  4844. begin
  4845. readchar;
  4846. if c in ['0'..'9'] then
  4847. begin
  4848. { insert the number after the . }
  4849. pattern:=pattern+'.';
  4850. while c in ['0'..'9'] do
  4851. begin
  4852. pattern:=pattern+c;
  4853. readchar;
  4854. end;
  4855. end
  4856. else
  4857. Illegal_Char(c);
  4858. end;
  4859. { E can also follow after a point is scanned }
  4860. if c in ['e','E'] then
  4861. begin
  4862. pattern:=pattern+'E';
  4863. readchar;
  4864. if c in ['-','+'] then
  4865. begin
  4866. pattern:=pattern+c;
  4867. readchar;
  4868. end;
  4869. if not(c in ['0'..'9']) then
  4870. Illegal_Char(c);
  4871. while c in ['0'..'9'] do
  4872. begin
  4873. pattern:=pattern+c;
  4874. readchar;
  4875. end;
  4876. end;
  4877. readpreproc:=_REALNUMBER;
  4878. end
  4879. else
  4880. readpreproc:=_INTCONST;
  4881. current_scanner.preproc_pattern:=pattern;
  4882. end;
  4883. '$','%':
  4884. begin
  4885. readnumber;
  4886. current_scanner.preproc_pattern:=pattern;
  4887. readpreproc:=_INTCONST;
  4888. end;
  4889. '&' :
  4890. begin
  4891. readnumber;
  4892. if length(pattern)=1 then
  4893. begin
  4894. readstring;
  4895. readpreproc:=_ID;
  4896. end
  4897. else
  4898. readpreproc:=_INTCONST;
  4899. current_scanner.preproc_pattern:=pattern;
  4900. end;
  4901. '.' :
  4902. begin
  4903. readchar;
  4904. readpreproc:=_POINT;
  4905. end;
  4906. ',' :
  4907. begin
  4908. readchar;
  4909. readpreproc:=_COMMA;
  4910. end;
  4911. '}' :
  4912. begin
  4913. readpreproc:=_END;
  4914. end;
  4915. '(' :
  4916. begin
  4917. readchar;
  4918. readpreproc:=_LKLAMMER;
  4919. end;
  4920. ')' :
  4921. begin
  4922. readchar;
  4923. readpreproc:=_RKLAMMER;
  4924. end;
  4925. '[' :
  4926. begin
  4927. readchar;
  4928. readpreproc:=_LECKKLAMMER;
  4929. end;
  4930. ']' :
  4931. begin
  4932. readchar;
  4933. readpreproc:=_RECKKLAMMER;
  4934. end;
  4935. '+' :
  4936. begin
  4937. readchar;
  4938. readpreproc:=_PLUS;
  4939. end;
  4940. '-' :
  4941. begin
  4942. readchar;
  4943. readpreproc:=_MINUS;
  4944. end;
  4945. '*' :
  4946. begin
  4947. readchar;
  4948. readpreproc:=_STAR;
  4949. end;
  4950. '/' :
  4951. begin
  4952. readchar;
  4953. readpreproc:=_SLASH;
  4954. end;
  4955. '=' :
  4956. begin
  4957. readchar;
  4958. readpreproc:=_EQ;
  4959. end;
  4960. '>' :
  4961. begin
  4962. readchar;
  4963. if c='=' then
  4964. begin
  4965. readchar;
  4966. readpreproc:=_GTE;
  4967. end
  4968. else
  4969. readpreproc:=_GT;
  4970. end;
  4971. '<' :
  4972. begin
  4973. readchar;
  4974. case c of
  4975. '>' :
  4976. begin
  4977. readchar;
  4978. readpreproc:=_NE;
  4979. end;
  4980. '=' :
  4981. begin
  4982. readchar;
  4983. readpreproc:=_LTE;
  4984. end;
  4985. else
  4986. readpreproc:=_LT;
  4987. end;
  4988. end;
  4989. #26 :
  4990. begin
  4991. readpreproc:=_EOF;
  4992. checkpreprocstack;
  4993. end;
  4994. else
  4995. begin
  4996. Illegal_Char(c);
  4997. readpreproc:=NOTOKEN;
  4998. end;
  4999. end;
  5000. end;
  5001. function tscannerfile.asmgetcharstart : char;
  5002. begin
  5003. { return first the character already
  5004. available in c }
  5005. lastasmgetchar:=c;
  5006. result:=asmgetchar;
  5007. end;
  5008. function tscannerfile.asmgetchar : char;
  5009. begin
  5010. if lastasmgetchar<>#0 then
  5011. begin
  5012. c:=lastasmgetchar;
  5013. lastasmgetchar:=#0;
  5014. end
  5015. else
  5016. readchar;
  5017. if in_asm_string then
  5018. begin
  5019. asmgetchar:=c;
  5020. exit;
  5021. end;
  5022. repeat
  5023. case c of
  5024. // the { ... } is used in ARM assembler to define register sets, so we can't used
  5025. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  5026. // But compiler directives {$...} are allowed in ARM assembler.
  5027. '{' :
  5028. begin
  5029. {$ifdef arm}
  5030. readchar;
  5031. dec(inputpointer);
  5032. if c<>'$' then
  5033. begin
  5034. asmgetchar:='{';
  5035. exit;
  5036. end
  5037. else
  5038. {$endif arm}
  5039. skipcomment;
  5040. end;
  5041. #10,#13 :
  5042. begin
  5043. linebreak;
  5044. asmgetchar:=c;
  5045. exit;
  5046. end;
  5047. #26 :
  5048. begin
  5049. reload;
  5050. if (c=#26) and not assigned(inputfile.next) then
  5051. end_of_file;
  5052. continue;
  5053. end;
  5054. '/' :
  5055. begin
  5056. readchar;
  5057. if c='/' then
  5058. skipdelphicomment
  5059. else
  5060. begin
  5061. asmgetchar:='/';
  5062. lastasmgetchar:=c;
  5063. exit;
  5064. end;
  5065. end;
  5066. '(' :
  5067. begin
  5068. readchar;
  5069. if c='*' then
  5070. begin
  5071. c:=#0;{Signal skipoldtpcomment to reload a char }
  5072. skipoldtpcomment;
  5073. end
  5074. else
  5075. begin
  5076. asmgetchar:='(';
  5077. lastasmgetchar:=c;
  5078. exit;
  5079. end;
  5080. end;
  5081. else
  5082. begin
  5083. asmgetchar:=c;
  5084. exit;
  5085. end;
  5086. end;
  5087. until false;
  5088. end;
  5089. {*****************************************************************************
  5090. Helpers
  5091. *****************************************************************************}
  5092. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5093. begin
  5094. if dm in [directive_all, directive_turbo] then
  5095. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5096. if dm in [directive_all, directive_mac] then
  5097. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5098. end;
  5099. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5100. begin
  5101. if dm in [directive_all, directive_turbo] then
  5102. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5103. if dm in [directive_all, directive_mac] then
  5104. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5105. end;
  5106. {*****************************************************************************
  5107. Initialization
  5108. *****************************************************************************}
  5109. procedure InitScanner;
  5110. begin
  5111. InitWideString(patternw);
  5112. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5113. mac_scannerdirectives:=TFPHashObjectList.Create;
  5114. { Common directives and conditionals }
  5115. AddDirective('I',directive_all, @dir_include);
  5116. AddDirective('DEFINE',directive_all, @dir_define);
  5117. AddDirective('UNDEF',directive_all, @dir_undef);
  5118. AddConditional('IF',directive_all, @dir_if);
  5119. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5120. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5121. AddConditional('ELSE',directive_all, @dir_else);
  5122. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5123. AddConditional('ENDIF',directive_all, @dir_endif);
  5124. { Directives and conditionals for all modes except mode macpas}
  5125. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5126. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5127. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5128. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5129. AddConditional('IFEND',directive_turbo, @dir_endif);
  5130. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5131. { Directives and conditionals for mode macpas: }
  5132. AddDirective('SETC',directive_mac, @dir_setc);
  5133. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5134. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5135. AddConditional('IFC',directive_mac, @dir_if);
  5136. AddConditional('ELSEC',directive_mac, @dir_else);
  5137. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5138. AddConditional('ENDC',directive_mac, @dir_endif);
  5139. end;
  5140. procedure DoneScanner;
  5141. begin
  5142. turbo_scannerdirectives.Free;
  5143. mac_scannerdirectives.Free;
  5144. DoneWideString(patternw);
  5145. end;
  5146. end.