scanner.pas 182 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526
  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. ;
  1178. else
  1179. internalerror(2013112802);
  1180. end;
  1181. inherited destroy;
  1182. end;
  1183. const
  1184. 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];
  1185. function preproc_comp_expr:texprvalue;
  1186. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean):texprvalue; forward;
  1187. procedure preproc_consume(t:ttoken);
  1188. begin
  1189. if t<>current_scanner.preproc_token then
  1190. Message(scan_e_preproc_syntax_error);
  1191. current_scanner.preproc_token:=current_scanner.readpreproc;
  1192. end;
  1193. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;out tokentoconsume:ttoken):boolean;
  1194. var
  1195. hmodule: tmodule;
  1196. ns:ansistring;
  1197. nssym:tsym;
  1198. begin
  1199. result:=false;
  1200. tokentoconsume:=_ID;
  1201. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  1202. begin
  1203. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  1204. internalerror(200501154);
  1205. { only allow unit.symbol access if the name was
  1206. found in the current module
  1207. we can use iscurrentunit because generic specializations does not
  1208. change current_unit variable }
  1209. hmodule:=find_module_from_symtable(srsym.Owner);
  1210. if not Assigned(hmodule) then
  1211. internalerror(201001120);
  1212. if hmodule.unit_index=current_filepos.moduleindex then
  1213. begin
  1214. preproc_consume(_POINT);
  1215. current_scanner.skipspace;
  1216. if srsym.typ=namespacesym then
  1217. begin
  1218. ns:=srsym.name;
  1219. nssym:=srsym;
  1220. while assigned(srsym) and (srsym.typ=namespacesym) do
  1221. begin
  1222. { we have a namespace. the next identifier should be either a namespace or a unit }
  1223. searchsym_in_module(hmodule,ns+'.'+current_scanner.preproc_pattern,srsym,srsymtable);
  1224. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  1225. begin
  1226. ns:=ns+'.'+current_scanner.preproc_pattern;
  1227. nssym:=srsym;
  1228. preproc_consume(_ID);
  1229. current_scanner.skipspace;
  1230. preproc_consume(_POINT);
  1231. current_scanner.skipspace;
  1232. end;
  1233. end;
  1234. { check if there is a hidden unit with this pattern in the namespace }
  1235. if not assigned(srsym) and
  1236. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  1237. srsym:=tnamespacesym(nssym).unitsym;
  1238. if assigned(srsym) and (srsym.typ<>unitsym) then
  1239. internalerror(201108260);
  1240. if not assigned(srsym) then
  1241. begin
  1242. result:=true;
  1243. srsymtable:=nil;
  1244. exit;
  1245. end;
  1246. end;
  1247. case current_scanner.preproc_token of
  1248. _ID:
  1249. { system.char? (char=widechar comes from the implicit
  1250. uuchar unit -> override) }
  1251. if (current_scanner.preproc_pattern='CHAR') and
  1252. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  1253. begin
  1254. if m_default_unicodestring in current_settings.modeswitches then
  1255. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  1256. else
  1257. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  1258. end
  1259. else
  1260. searchsym_in_module(tunitsym(srsym).module,current_scanner.preproc_pattern,srsym,srsymtable);
  1261. _STRING:
  1262. begin
  1263. { system.string? }
  1264. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  1265. begin
  1266. if cs_refcountedstrings in current_settings.localswitches then
  1267. begin
  1268. if m_default_unicodestring in current_settings.modeswitches then
  1269. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  1270. else
  1271. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  1272. end
  1273. else
  1274. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  1275. tokentoconsume:=_STRING;
  1276. end;
  1277. end
  1278. end;
  1279. end
  1280. else
  1281. begin
  1282. srsym:=nil;
  1283. srsymtable:=nil;
  1284. end;
  1285. result:=true;
  1286. end;
  1287. end;
  1288. procedure try_consume_nestedsym(var srsym:tsym;var srsymtable:TSymtable);
  1289. var
  1290. def:tdef;
  1291. tokentoconsume:ttoken;
  1292. found:boolean;
  1293. begin
  1294. found:=try_consume_unitsym(srsym,srsymtable,tokentoconsume);
  1295. if found then
  1296. begin
  1297. preproc_consume(tokentoconsume);
  1298. current_scanner.skipspace;
  1299. end;
  1300. while (current_scanner.preproc_token=_POINT) do
  1301. begin
  1302. if assigned(srsym)and(srsym.typ=typesym) then
  1303. begin
  1304. def:=ttypesym(srsym).typedef;
  1305. if is_class_or_object(def) or is_record(def) or is_java_class_or_interface(def) then
  1306. begin
  1307. preproc_consume(_POINT);
  1308. current_scanner.skipspace;
  1309. if def.typ=objectdef then
  1310. found:=searchsym_in_class(tobjectdef(def),tobjectdef(def),current_scanner.preproc_pattern,srsym,srsymtable,[ssf_search_helper])
  1311. else
  1312. found:=searchsym_in_record(trecorddef(def),current_scanner.preproc_pattern,srsym,srsymtable);
  1313. if not found then
  1314. begin
  1315. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  1316. exit;
  1317. end;
  1318. preproc_consume(_ID);
  1319. current_scanner.skipspace;
  1320. end
  1321. else
  1322. begin
  1323. Message(sym_e_type_must_be_rec_or_object_or_class);
  1324. exit;
  1325. end;
  1326. end
  1327. else
  1328. begin
  1329. Message(type_e_type_id_expected);
  1330. exit;
  1331. end;
  1332. end;
  1333. end;
  1334. function preproc_substitutedtoken(searchstr:string;eval:Boolean):texprvalue;
  1335. { Currently this parses identifiers as well as numbers.
  1336. The result from this procedure can either be that the token
  1337. itself is a value, or that it is a compile time variable/macro,
  1338. which then is substituted for another value (for macros
  1339. recursivelly substituted).}
  1340. var
  1341. hs: string;
  1342. mac: tmacro;
  1343. macrocount,
  1344. len: integer;
  1345. begin
  1346. if not eval then
  1347. begin
  1348. result:=texprvalue.create_str(searchstr);
  1349. exit;
  1350. end;
  1351. mac:=nil;
  1352. { Substitue macros and compiler variables with their content/value.
  1353. For real macros also do recursive substitution. }
  1354. macrocount:=0;
  1355. repeat
  1356. mac:=tmacro(search_macro(searchstr));
  1357. inc(macrocount);
  1358. if macrocount>max_macro_nesting then
  1359. begin
  1360. Message(scan_w_macro_too_deep);
  1361. break;
  1362. end;
  1363. if assigned(mac) and mac.defined then
  1364. if assigned(mac.buftext) then
  1365. begin
  1366. if mac.buflen>255 then
  1367. begin
  1368. len:=255;
  1369. Message(scan_w_macro_cut_after_255_chars);
  1370. end
  1371. else
  1372. len:=mac.buflen;
  1373. hs[0]:=char(len);
  1374. move(mac.buftext^,hs[1],len);
  1375. searchstr:=upcase(hs);
  1376. mac.is_used:=true;
  1377. end
  1378. else
  1379. begin
  1380. Message1(scan_e_error_macro_lacks_value,searchstr);
  1381. break;
  1382. end
  1383. else
  1384. break;
  1385. if mac.is_compiler_var then
  1386. break;
  1387. until false;
  1388. { At this point, result do contain the value. Do some decoding and
  1389. determine the type.}
  1390. result:=texprvalue.try_parse_number(searchstr);
  1391. if not assigned(result) then
  1392. begin
  1393. if assigned(mac) and (searchstr='FALSE') then
  1394. result:=texprvalue.create_bool(false)
  1395. else if assigned(mac) and (searchstr='TRUE') then
  1396. result:=texprvalue.create_bool(true)
  1397. else if (m_mac in current_settings.modeswitches) and
  1398. (not assigned(mac) or not mac.defined) and
  1399. (macrocount = 1) then
  1400. begin
  1401. {Errors in mode mac is issued here. For non macpas modes there is
  1402. more liberty, but the error will eventually be caught at a later stage.}
  1403. Message1(scan_e_error_macro_undefined,searchstr);
  1404. result:=texprvalue.create_str(searchstr); { just to have something }
  1405. end
  1406. else
  1407. result:=texprvalue.create_str(searchstr);
  1408. end;
  1409. end;
  1410. function preproc_factor(eval: Boolean):texprvalue;
  1411. var
  1412. hs,countstr,storedpattern: string;
  1413. mac: tmacro;
  1414. srsym : tsym;
  1415. srsymtable : TSymtable;
  1416. hdef : TDef;
  1417. l : longint;
  1418. hasKlammer: Boolean;
  1419. exprvalue:texprvalue;
  1420. ns:tnormalset;
  1421. begin
  1422. result:=nil;
  1423. hasKlammer:=false;
  1424. if current_scanner.preproc_token=_ID then
  1425. begin
  1426. if current_scanner.preproc_pattern='DEFINED' then
  1427. begin
  1428. preproc_consume(_ID);
  1429. current_scanner.skipspace;
  1430. if current_scanner.preproc_token =_LKLAMMER then
  1431. begin
  1432. preproc_consume(_LKLAMMER);
  1433. current_scanner.skipspace;
  1434. hasKlammer:= true;
  1435. end
  1436. else if (m_mac in current_settings.modeswitches) then
  1437. hasKlammer:= false
  1438. else
  1439. Message(scan_e_error_in_preproc_expr);
  1440. if current_scanner.preproc_token =_ID then
  1441. begin
  1442. hs := current_scanner.preproc_pattern;
  1443. mac := tmacro(search_macro(hs));
  1444. if assigned(mac) and mac.defined then
  1445. begin
  1446. result:=texprvalue.create_bool(true);
  1447. mac.is_used:=true;
  1448. end
  1449. else
  1450. result:=texprvalue.create_bool(false);
  1451. preproc_consume(_ID);
  1452. current_scanner.skipspace;
  1453. end
  1454. else
  1455. Message(scan_e_error_in_preproc_expr);
  1456. if hasKlammer then
  1457. if current_scanner.preproc_token =_RKLAMMER then
  1458. preproc_consume(_RKLAMMER)
  1459. else
  1460. Message(scan_e_error_in_preproc_expr);
  1461. end
  1462. else
  1463. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  1464. begin
  1465. preproc_consume(_ID);
  1466. current_scanner.skipspace;
  1467. if current_scanner.preproc_token =_ID then
  1468. begin
  1469. hs := current_scanner.preproc_pattern;
  1470. mac := tmacro(search_macro(hs));
  1471. if assigned(mac) then
  1472. begin
  1473. result:=texprvalue.create_bool(false);
  1474. mac.is_used:=true;
  1475. end
  1476. else
  1477. result:=texprvalue.create_bool(true);
  1478. preproc_consume(_ID);
  1479. current_scanner.skipspace;
  1480. end
  1481. else
  1482. Message(scan_e_error_in_preproc_expr);
  1483. end
  1484. else
  1485. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  1486. begin
  1487. preproc_consume(_ID);
  1488. current_scanner.skipspace;
  1489. if current_scanner.preproc_token =_LKLAMMER then
  1490. begin
  1491. preproc_consume(_LKLAMMER);
  1492. current_scanner.skipspace;
  1493. end
  1494. else
  1495. Message(scan_e_error_in_preproc_expr);
  1496. if not (current_scanner.preproc_token = _ID) then
  1497. Message(scan_e_error_in_preproc_expr);
  1498. hs:=current_scanner.preproc_pattern;
  1499. if (length(hs) > 1) then
  1500. {This is allowed in Metrowerks Pascal}
  1501. Message(scan_e_error_in_preproc_expr)
  1502. else
  1503. begin
  1504. if CheckSwitch(hs[1],'+') then
  1505. result:=texprvalue.create_bool(true)
  1506. else
  1507. result:=texprvalue.create_bool(false);
  1508. end;
  1509. preproc_consume(_ID);
  1510. current_scanner.skipspace;
  1511. if current_scanner.preproc_token =_RKLAMMER then
  1512. preproc_consume(_RKLAMMER)
  1513. else
  1514. Message(scan_e_error_in_preproc_expr);
  1515. end
  1516. else
  1517. if current_scanner.preproc_pattern='SIZEOF' then
  1518. begin
  1519. preproc_consume(_ID);
  1520. current_scanner.skipspace;
  1521. if current_scanner.preproc_token =_LKLAMMER then
  1522. begin
  1523. preproc_consume(_LKLAMMER);
  1524. current_scanner.skipspace;
  1525. end
  1526. else
  1527. Message(scan_e_preproc_syntax_error);
  1528. storedpattern:=current_scanner.preproc_pattern;
  1529. preproc_consume(_ID);
  1530. current_scanner.skipspace;
  1531. if eval then
  1532. if searchsym(storedpattern,srsym,srsymtable) then
  1533. begin
  1534. try_consume_nestedsym(srsym,srsymtable);
  1535. l:=0;
  1536. if assigned(srsym) then
  1537. case srsym.typ of
  1538. staticvarsym,
  1539. localvarsym,
  1540. paravarsym :
  1541. l:=tabstractvarsym(srsym).getsize;
  1542. typesym:
  1543. l:=ttypesym(srsym).typedef.size;
  1544. else
  1545. Message(scan_e_error_in_preproc_expr);
  1546. end;
  1547. result:=texprvalue.create_int(l);
  1548. end
  1549. else
  1550. Message1(sym_e_id_not_found,storedpattern);
  1551. if current_scanner.preproc_token =_RKLAMMER then
  1552. preproc_consume(_RKLAMMER)
  1553. else
  1554. Message(scan_e_preproc_syntax_error);
  1555. end
  1556. else
  1557. if current_scanner.preproc_pattern='HIGH' then
  1558. begin
  1559. preproc_consume(_ID);
  1560. current_scanner.skipspace;
  1561. if current_scanner.preproc_token =_LKLAMMER then
  1562. begin
  1563. preproc_consume(_LKLAMMER);
  1564. current_scanner.skipspace;
  1565. end
  1566. else
  1567. Message(scan_e_preproc_syntax_error);
  1568. storedpattern:=current_scanner.preproc_pattern;
  1569. preproc_consume(_ID);
  1570. current_scanner.skipspace;
  1571. if eval then
  1572. if searchsym(storedpattern,srsym,srsymtable) then
  1573. begin
  1574. try_consume_nestedsym(srsym,srsymtable);
  1575. hdef:=nil;
  1576. hs:='';
  1577. l:=0;
  1578. if assigned(srsym) then
  1579. case srsym.typ of
  1580. staticvarsym,
  1581. localvarsym,
  1582. paravarsym :
  1583. hdef:=tabstractvarsym(srsym).vardef;
  1584. typesym:
  1585. hdef:=ttypesym(srsym).typedef;
  1586. else
  1587. Message(scan_e_error_in_preproc_expr);
  1588. end;
  1589. if assigned(hdef) then
  1590. begin
  1591. if hdef.typ=setdef then
  1592. hdef:=tsetdef(hdef).elementdef;
  1593. case hdef.typ of
  1594. orddef:
  1595. with torddef(hdef).high do
  1596. if signed then
  1597. result:=texprvalue.create_int(svalue)
  1598. else
  1599. result:=texprvalue.create_uint(uvalue);
  1600. enumdef:
  1601. result:=texprvalue.create_int(tenumdef(hdef).maxval);
  1602. arraydef:
  1603. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1604. Message(type_e_mismatch)
  1605. else
  1606. result:=texprvalue.create_int(tarraydef(hdef).highrange);
  1607. stringdef:
  1608. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1609. Message(type_e_mismatch)
  1610. else
  1611. result:=texprvalue.create_int(tstringdef(hdef).len);
  1612. else
  1613. Message(type_e_mismatch);
  1614. end;
  1615. end;
  1616. end
  1617. else
  1618. Message1(sym_e_id_not_found,storedpattern);
  1619. if current_scanner.preproc_token =_RKLAMMER then
  1620. preproc_consume(_RKLAMMER)
  1621. else
  1622. Message(scan_e_preproc_syntax_error);
  1623. end
  1624. else
  1625. if current_scanner.preproc_pattern='DECLARED' then
  1626. begin
  1627. preproc_consume(_ID);
  1628. current_scanner.skipspace;
  1629. if current_scanner.preproc_token =_LKLAMMER then
  1630. begin
  1631. preproc_consume(_LKLAMMER);
  1632. current_scanner.skipspace;
  1633. end
  1634. else
  1635. Message(scan_e_error_in_preproc_expr);
  1636. if current_scanner.preproc_token =_ID then
  1637. begin
  1638. hs := upper(current_scanner.preproc_pattern);
  1639. preproc_consume(_ID);
  1640. current_scanner.skipspace;
  1641. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  1642. begin
  1643. l:=1;
  1644. preproc_consume(current_scanner.preproc_token);
  1645. current_scanner.skipspace;
  1646. while current_scanner.preproc_token=_COMMA do
  1647. begin
  1648. inc(l);
  1649. preproc_consume(_COMMA);
  1650. current_scanner.skipspace;
  1651. end;
  1652. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  1653. Message(scan_e_error_in_preproc_expr)
  1654. else
  1655. preproc_consume(current_scanner.preproc_token);
  1656. str(l,countstr);
  1657. hs:=hs+'$'+countstr;
  1658. end
  1659. else
  1660. { special case: <> }
  1661. if current_scanner.preproc_token=_NE then
  1662. begin
  1663. hs:=hs+'$1';
  1664. preproc_consume(_NE);
  1665. end;
  1666. current_scanner.skipspace;
  1667. if searchsym(hs,srsym,srsymtable) then
  1668. begin
  1669. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  1670. if (sp_generic_dummy in srsym.symoptions) and
  1671. (srsym.typ=typesym) and
  1672. (
  1673. { mode delphi}
  1674. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  1675. { non-delphi modes }
  1676. (df_generic in ttypesym(srsym).typedef.defoptions)
  1677. ) then
  1678. result:=texprvalue.create_bool(false)
  1679. else
  1680. result:=texprvalue.create_bool(true);
  1681. end
  1682. else
  1683. result:=texprvalue.create_bool(false);
  1684. end
  1685. else
  1686. Message(scan_e_error_in_preproc_expr);
  1687. if current_scanner.preproc_token =_RKLAMMER then
  1688. preproc_consume(_RKLAMMER)
  1689. else
  1690. Message(scan_e_error_in_preproc_expr);
  1691. end
  1692. else
  1693. if current_scanner.preproc_pattern='ORD' then
  1694. begin
  1695. preproc_consume(_ID);
  1696. current_scanner.skipspace;
  1697. if current_scanner.preproc_token =_LKLAMMER then
  1698. begin
  1699. preproc_consume(_LKLAMMER);
  1700. current_scanner.skipspace;
  1701. end
  1702. else
  1703. Message(scan_e_preproc_syntax_error);
  1704. exprvalue:=preproc_factor(eval);
  1705. if eval then
  1706. begin
  1707. if is_ordinal(exprvalue.def) then
  1708. result:=texprvalue.create_int(exprvalue.asInt)
  1709. else
  1710. begin
  1711. exprvalue.error('Ordinal','ORD');
  1712. result:=texprvalue.create_int(0);
  1713. end;
  1714. end
  1715. else
  1716. result:=texprvalue.create_int(0);
  1717. exprvalue.free;
  1718. if current_scanner.preproc_token =_RKLAMMER then
  1719. preproc_consume(_RKLAMMER)
  1720. else
  1721. Message(scan_e_error_in_preproc_expr);
  1722. end
  1723. else
  1724. if current_scanner.preproc_pattern='NOT' then
  1725. begin
  1726. preproc_consume(_ID);
  1727. exprvalue:=preproc_factor(eval);
  1728. if eval then
  1729. result:=exprvalue.evaluate(nil,_OP_NOT)
  1730. else
  1731. result:=texprvalue.create_bool(false); {Just to have something}
  1732. exprvalue.free;
  1733. end
  1734. else
  1735. if (current_scanner.preproc_pattern='TRUE') then
  1736. begin
  1737. result:=texprvalue.create_bool(true);
  1738. preproc_consume(_ID);
  1739. end
  1740. else
  1741. if (current_scanner.preproc_pattern='FALSE') then
  1742. begin
  1743. result:=texprvalue.create_bool(false);
  1744. preproc_consume(_ID);
  1745. end
  1746. else
  1747. begin
  1748. storedpattern:=current_scanner.preproc_pattern;
  1749. preproc_consume(_ID);
  1750. current_scanner.skipspace;
  1751. { first look for a macros/int/float }
  1752. result:=preproc_substitutedtoken(storedpattern,eval);
  1753. if eval and (result.consttyp=conststring) then
  1754. if searchsym(storedpattern,srsym,srsymtable) then
  1755. begin
  1756. try_consume_nestedsym(srsym,srsymtable);
  1757. if assigned(srsym) then
  1758. case srsym.typ of
  1759. constsym:
  1760. begin
  1761. result.free;
  1762. result:=texprvalue.create_const(tconstsym(srsym));
  1763. end;
  1764. enumsym:
  1765. begin
  1766. result.free;
  1767. result:=texprvalue.create_int(tenumsym(srsym).value);
  1768. end;
  1769. end;
  1770. end;
  1771. end
  1772. end
  1773. else if current_scanner.preproc_token =_LKLAMMER then
  1774. begin
  1775. preproc_consume(_LKLAMMER);
  1776. result:=preproc_sub_expr(opcompare,true);
  1777. preproc_consume(_RKLAMMER);
  1778. end
  1779. else if current_scanner.preproc_token = _LECKKLAMMER then
  1780. begin
  1781. preproc_consume(_LECKKLAMMER);
  1782. ns:=[];
  1783. while current_scanner.preproc_token in [_ID,_INTCONST] do
  1784. begin
  1785. exprvalue:=preproc_factor(eval);
  1786. include(ns,exprvalue.asInt);
  1787. if current_scanner.preproc_token = _COMMA then
  1788. preproc_consume(_COMMA);
  1789. end;
  1790. // TODO Add check of setElemType
  1791. preproc_consume(_RECKKLAMMER);
  1792. result:=texprvalue.create_set(ns);
  1793. end
  1794. else if current_scanner.preproc_token = _INTCONST then
  1795. begin
  1796. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  1797. if not assigned(result) then
  1798. begin
  1799. Message(parser_e_invalid_integer);
  1800. result:=texprvalue.create_int(1);
  1801. end;
  1802. preproc_consume(_INTCONST);
  1803. end
  1804. else if current_scanner.preproc_token = _REALNUMBER then
  1805. begin
  1806. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  1807. if not assigned(result) then
  1808. begin
  1809. Message(parser_e_error_in_real);
  1810. result:=texprvalue.create_real(1.0);
  1811. end;
  1812. preproc_consume(_REALNUMBER);
  1813. end
  1814. else
  1815. Message(scan_e_error_in_preproc_expr);
  1816. if not assigned(result) then
  1817. result:=texprvalue.create_error;
  1818. end;
  1819. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  1820. var
  1821. hs1,hs2: texprvalue;
  1822. op: ttoken;
  1823. begin
  1824. if pred_level=highest_precedence then
  1825. result:=preproc_factor(eval)
  1826. else
  1827. result:=preproc_sub_expr(succ(pred_level),eval);
  1828. repeat
  1829. op:=current_scanner.preproc_token;
  1830. if (op in preproc_operators) and
  1831. (op in operator_levels[pred_level]) then
  1832. begin
  1833. hs1:=result;
  1834. preproc_consume(op);
  1835. if (op=_OP_OR)and hs1.isBoolean and hs1.asBool then
  1836. begin
  1837. { stop evaluation the rest of expression }
  1838. result:=texprvalue.create_bool(true);
  1839. if pred_level=highest_precedence then
  1840. hs2:=preproc_factor(false)
  1841. else
  1842. hs2:=preproc_sub_expr(succ(pred_level),false);
  1843. end
  1844. else if (op=_OP_AND)and hs1.isBoolean and not hs1.asBool then
  1845. begin
  1846. { stop evaluation the rest of expression }
  1847. result:=texprvalue.create_bool(false);
  1848. if pred_level=highest_precedence then
  1849. hs2:=preproc_factor(false)
  1850. else
  1851. hs2:=preproc_sub_expr(succ(pred_level),false);
  1852. end
  1853. else
  1854. begin
  1855. if pred_level=highest_precedence then
  1856. hs2:=preproc_factor(eval)
  1857. else
  1858. hs2:=preproc_sub_expr(succ(pred_level),eval);
  1859. if eval then
  1860. result:=hs1.evaluate(hs2,op)
  1861. else
  1862. result:=texprvalue.create_bool(false); {Just to have something}
  1863. end;
  1864. hs1.free;
  1865. hs2.free;
  1866. end
  1867. else
  1868. break;
  1869. until false;
  1870. end;
  1871. begin
  1872. current_scanner.skipspace;
  1873. { start preproc expression scanner }
  1874. current_scanner.preproc_token:=current_scanner.readpreproc;
  1875. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  1876. end;
  1877. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  1878. var
  1879. hs: texprvalue;
  1880. begin
  1881. hs:=preproc_comp_expr;
  1882. if hs.isBoolean then
  1883. result:=hs.asBool
  1884. else
  1885. begin
  1886. hs.error('Boolean', 'IF or ELSEIF');
  1887. result:=false;
  1888. end;
  1889. valuedescr:=hs.asStr;
  1890. hs.free;
  1891. end;
  1892. procedure dir_if;
  1893. begin
  1894. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  1895. end;
  1896. procedure dir_elseif;
  1897. begin
  1898. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  1899. end;
  1900. procedure dir_define_impl(macstyle: boolean);
  1901. var
  1902. hs : string;
  1903. bracketcount : longint;
  1904. mac : tmacro;
  1905. macropos : longint;
  1906. macrobuffer : pmacrobuffer;
  1907. begin
  1908. current_scanner.skipspace;
  1909. hs:=current_scanner.readid;
  1910. mac:=tmacro(search_macro(hs));
  1911. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  1912. begin
  1913. mac:=tmacro.create(hs);
  1914. mac.defined:=true;
  1915. current_module.localmacrosymtable.insert(mac);
  1916. end
  1917. else
  1918. begin
  1919. mac.defined:=true;
  1920. mac.is_compiler_var:=false;
  1921. { delete old definition }
  1922. if assigned(mac.buftext) then
  1923. begin
  1924. freemem(mac.buftext,mac.buflen);
  1925. mac.buftext:=nil;
  1926. end;
  1927. end;
  1928. Message1(parser_c_macro_defined,mac.name);
  1929. mac.is_used:=true;
  1930. if (cs_support_macro in current_settings.moduleswitches) then
  1931. begin
  1932. current_scanner.skipspace;
  1933. if not macstyle then
  1934. begin
  1935. { may be a macro? }
  1936. if c <> ':' then
  1937. exit;
  1938. current_scanner.readchar;
  1939. if c <> '=' then
  1940. exit;
  1941. current_scanner.readchar;
  1942. current_scanner.skipspace;
  1943. end;
  1944. { key words are never substituted }
  1945. if is_keyword(hs) then
  1946. Message(scan_e_keyword_cant_be_a_macro);
  1947. new(macrobuffer);
  1948. macropos:=0;
  1949. { parse macro, brackets are counted so it's possible
  1950. to have a $ifdef etc. in the macro }
  1951. bracketcount:=0;
  1952. repeat
  1953. case c of
  1954. '}' :
  1955. if (bracketcount=0) then
  1956. break
  1957. else
  1958. dec(bracketcount);
  1959. '{' :
  1960. inc(bracketcount);
  1961. #10,#13 :
  1962. current_scanner.linebreak;
  1963. #26 :
  1964. current_scanner.end_of_file;
  1965. end;
  1966. macrobuffer^[macropos]:=c;
  1967. inc(macropos);
  1968. if macropos>=maxmacrolen then
  1969. Message(scan_f_macro_buffer_overflow);
  1970. current_scanner.readchar;
  1971. until false;
  1972. { free buffer of macro ?}
  1973. if assigned(mac.buftext) then
  1974. freemem(mac.buftext,mac.buflen);
  1975. { get new mem }
  1976. getmem(mac.buftext,macropos);
  1977. mac.buflen:=macropos;
  1978. { copy the text }
  1979. move(macrobuffer^,mac.buftext^,macropos);
  1980. dispose(macrobuffer);
  1981. end
  1982. else
  1983. begin
  1984. { check if there is an assignment, then we need to give a
  1985. warning }
  1986. current_scanner.skipspace;
  1987. if c=':' then
  1988. begin
  1989. current_scanner.readchar;
  1990. if c='=' then
  1991. Message(scan_w_macro_support_turned_off);
  1992. end;
  1993. end;
  1994. end;
  1995. procedure dir_define;
  1996. begin
  1997. dir_define_impl(false);
  1998. end;
  1999. procedure dir_definec;
  2000. begin
  2001. dir_define_impl(true);
  2002. end;
  2003. procedure dir_setc;
  2004. var
  2005. hs : string;
  2006. mac : tmacro;
  2007. l : longint;
  2008. w : integer;
  2009. exprvalue: texprvalue;
  2010. begin
  2011. current_scanner.skipspace;
  2012. hs:=current_scanner.readid;
  2013. mac:=tmacro(search_macro(hs));
  2014. if not assigned(mac) or
  2015. (mac.owner <> current_module.localmacrosymtable) then
  2016. begin
  2017. mac:=tmacro.create(hs);
  2018. mac.defined:=true;
  2019. mac.is_compiler_var:=true;
  2020. current_module.localmacrosymtable.insert(mac);
  2021. end
  2022. else
  2023. begin
  2024. mac.defined:=true;
  2025. mac.is_compiler_var:=true;
  2026. { delete old definition }
  2027. if assigned(mac.buftext) then
  2028. begin
  2029. freemem(mac.buftext,mac.buflen);
  2030. mac.buftext:=nil;
  2031. end;
  2032. end;
  2033. Message1(parser_c_macro_defined,mac.name);
  2034. mac.is_used:=true;
  2035. { key words are never substituted }
  2036. if is_keyword(hs) then
  2037. Message(scan_e_keyword_cant_be_a_macro);
  2038. { macro assignment can be both := and = }
  2039. current_scanner.skipspace;
  2040. if c=':' then
  2041. current_scanner.readchar;
  2042. if c='=' then
  2043. begin
  2044. current_scanner.readchar;
  2045. exprvalue:=preproc_comp_expr;
  2046. if not is_boolean(exprvalue.def) and
  2047. not is_integer(exprvalue.def) then
  2048. exprvalue.error('Boolean, Integer', 'SETC');
  2049. hs:=exprvalue.asStr;
  2050. if length(hs) <> 0 then
  2051. begin
  2052. {If we are absolutely shure it is boolean, translate
  2053. to TRUE/FALSE to increase possibility to do future type check}
  2054. if exprvalue.isBoolean then
  2055. begin
  2056. if exprvalue.asBool then
  2057. hs:='TRUE'
  2058. else
  2059. hs:='FALSE';
  2060. end;
  2061. Message2(parser_c_macro_set_to,mac.name,hs);
  2062. { free buffer of macro ?}
  2063. if assigned(mac.buftext) then
  2064. freemem(mac.buftext,mac.buflen);
  2065. { get new mem }
  2066. getmem(mac.buftext,length(hs));
  2067. mac.buflen:=length(hs);
  2068. { copy the text }
  2069. move(hs[1],mac.buftext^,mac.buflen);
  2070. end
  2071. else
  2072. Message(scan_e_preproc_syntax_error);
  2073. exprvalue.free;
  2074. end
  2075. else
  2076. Message(scan_e_preproc_syntax_error);
  2077. end;
  2078. procedure dir_undef;
  2079. var
  2080. hs : string;
  2081. mac : tmacro;
  2082. begin
  2083. current_scanner.skipspace;
  2084. hs:=current_scanner.readid;
  2085. mac:=tmacro(search_macro(hs));
  2086. if not assigned(mac) or
  2087. (mac.owner <> current_module.localmacrosymtable) then
  2088. begin
  2089. mac:=tmacro.create(hs);
  2090. mac.defined:=false;
  2091. current_module.localmacrosymtable.insert(mac);
  2092. end
  2093. else
  2094. begin
  2095. mac.defined:=false;
  2096. mac.is_compiler_var:=false;
  2097. { delete old definition }
  2098. if assigned(mac.buftext) then
  2099. begin
  2100. freemem(mac.buftext,mac.buflen);
  2101. mac.buftext:=nil;
  2102. end;
  2103. end;
  2104. Message1(parser_c_macro_undefined,mac.name);
  2105. mac.is_used:=true;
  2106. end;
  2107. procedure dir_include;
  2108. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  2109. var
  2110. found : boolean;
  2111. hpath : TCmdStr;
  2112. begin
  2113. (* look for the include file
  2114. If path was absolute and specified as part of {$I } then
  2115. 1. specified path
  2116. else
  2117. 1. path of current inputfile,current dir
  2118. 2. local includepath
  2119. 3. global includepath
  2120. -- Check mantis #13461 before changing this *)
  2121. found:=false;
  2122. foundfile:='';
  2123. hpath:='';
  2124. if path_absolute(path) then
  2125. begin
  2126. found:=FindFile(name,path,true,foundfile);
  2127. end
  2128. else
  2129. begin
  2130. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  2131. found:=FindFile(path+name, hpath,true,foundfile);
  2132. if not found then
  2133. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  2134. if not found then
  2135. found:=includesearchpath.FindFile(path+name,true,foundfile);
  2136. end;
  2137. result:=found;
  2138. end;
  2139. var
  2140. foundfile : TCmdStr;
  2141. path,
  2142. name,
  2143. hs : tpathstr;
  2144. args : string;
  2145. hp : tinputfile;
  2146. found : boolean;
  2147. macroIsString : boolean;
  2148. begin
  2149. current_scanner.skipspace;
  2150. args:=current_scanner.readcomment;
  2151. hs:=GetToken(args,' ');
  2152. if hs='' then
  2153. exit;
  2154. if (hs[1]='%') then
  2155. begin
  2156. { case insensitive }
  2157. hs:=upper(hs);
  2158. { remove %'s }
  2159. Delete(hs,1,1);
  2160. if hs[length(hs)]='%' then
  2161. Delete(hs,length(hs),1);
  2162. { save old }
  2163. path:=hs;
  2164. { first check for internal macros }
  2165. macroIsString:=true;
  2166. if hs='TIME' then
  2167. hs:=gettimestr
  2168. else
  2169. if hs='DATE' then
  2170. hs:=getdatestr
  2171. else
  2172. if hs='FILE' then
  2173. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex)
  2174. else
  2175. if hs='LINE' then
  2176. hs:=tostr(current_filepos.line)
  2177. else
  2178. if hs='LINENUM' then
  2179. begin
  2180. hs:=tostr(current_filepos.line);
  2181. macroIsString:=false;
  2182. end
  2183. else
  2184. if hs='FPCVERSION' then
  2185. hs:=version_string
  2186. else
  2187. if hs='FPCDATE' then
  2188. hs:=date_string
  2189. else
  2190. if hs='FPCTARGET' then
  2191. hs:=target_cpu_string
  2192. else
  2193. if hs='FPCTARGETCPU' then
  2194. hs:=target_cpu_string
  2195. else
  2196. if hs='FPCTARGETOS' then
  2197. hs:=target_info.shortname
  2198. else
  2199. hs:=GetEnvironmentVariable(hs);
  2200. if hs='' then
  2201. Message1(scan_w_include_env_not_found,path);
  2202. { make it a stringconst }
  2203. if macroIsString then
  2204. hs:=''''+hs+'''';
  2205. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2206. current_scanner.line_no,current_scanner.inputfile.ref_index);
  2207. end
  2208. else
  2209. begin
  2210. hs:=FixFileName(hs);
  2211. path:=ExtractFilePath(hs);
  2212. name:=ExtractFileName(hs);
  2213. { Special case for Delphi compatibility: '*' has to be replaced
  2214. by the file name of the current source file. }
  2215. if (length(name)>=1) and
  2216. (name[1]='*') then
  2217. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2218. { try to find the file }
  2219. found:=findincludefile(path,name,foundfile);
  2220. if (not found) and (ExtractFileExt(name)='') then
  2221. begin
  2222. { try default extensions .inc , .pp and .pas }
  2223. if (not found) then
  2224. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2225. if (not found) then
  2226. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2227. if (not found) then
  2228. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2229. end;
  2230. if current_scanner.inputfilecount<max_include_nesting then
  2231. begin
  2232. inc(current_scanner.inputfilecount);
  2233. { we need to reread the current char }
  2234. dec(current_scanner.inputpointer);
  2235. { shutdown current file }
  2236. current_scanner.tempcloseinputfile;
  2237. { load new file }
  2238. hp:=do_openinputfile(foundfile);
  2239. hp.inc_path:=path;
  2240. current_scanner.addfile(hp);
  2241. current_module.sourcefiles.register_file(hp);
  2242. if (not found) then
  2243. Message1(scan_f_cannot_open_includefile,hs);
  2244. if (not current_scanner.openinputfile) then
  2245. Message1(scan_f_cannot_open_includefile,hs);
  2246. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2247. current_scanner.reload;
  2248. end
  2249. else
  2250. Message(scan_f_include_deep_ten);
  2251. end;
  2252. end;
  2253. {*****************************************************************************
  2254. Preprocessor writing
  2255. *****************************************************************************}
  2256. {$ifdef PREPROCWRITE}
  2257. constructor tpreprocfile.create(const fn:string);
  2258. begin
  2259. { open outputfile }
  2260. assign(f,fn);
  2261. {$push}{$I-}
  2262. rewrite(f);
  2263. {$pop}
  2264. if ioresult<>0 then
  2265. Comment(V_Fatal,'can''t create file '+fn);
  2266. getmem(buf,preprocbufsize);
  2267. settextbuf(f,buf^,preprocbufsize);
  2268. { reset }
  2269. eolfound:=false;
  2270. spacefound:=false;
  2271. end;
  2272. destructor tpreprocfile.destroy;
  2273. begin
  2274. close(f);
  2275. freemem(buf,preprocbufsize);
  2276. end;
  2277. procedure tpreprocfile.add(const s:string);
  2278. begin
  2279. write(f,s);
  2280. end;
  2281. procedure tpreprocfile.addspace;
  2282. begin
  2283. if eolfound then
  2284. begin
  2285. writeln(f,'');
  2286. eolfound:=false;
  2287. spacefound:=false;
  2288. end
  2289. else
  2290. if spacefound then
  2291. begin
  2292. write(f,' ');
  2293. spacefound:=false;
  2294. end;
  2295. end;
  2296. {$endif PREPROCWRITE}
  2297. {*****************************************************************************
  2298. TPreProcStack
  2299. *****************************************************************************}
  2300. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2301. begin
  2302. accept:=a;
  2303. typ:=atyp;
  2304. next:=n;
  2305. end;
  2306. {*****************************************************************************
  2307. TReplayStack
  2308. *****************************************************************************}
  2309. constructor treplaystack.Create(atoken:ttoken;asettings:tsettings;
  2310. atokenbuf:tdynamicarray;anext:treplaystack);
  2311. begin
  2312. token:=atoken;
  2313. settings:=asettings;
  2314. tokenbuf:=atokenbuf;
  2315. next:=anext;
  2316. end;
  2317. {*****************************************************************************
  2318. TDirectiveItem
  2319. *****************************************************************************}
  2320. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2321. begin
  2322. inherited Create(AList,n);
  2323. is_conditional:=false;
  2324. proc:=p;
  2325. end;
  2326. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2327. begin
  2328. inherited Create(AList,n);
  2329. is_conditional:=true;
  2330. proc:=p;
  2331. end;
  2332. {****************************************************************************
  2333. TSCANNERFILE
  2334. ****************************************************************************}
  2335. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  2336. begin
  2337. inputfile:=do_openinputfile(fn);
  2338. if is_macro then
  2339. inputfile.is_macro:=true;
  2340. if assigned(current_module) then
  2341. current_module.sourcefiles.register_file(inputfile);
  2342. { reset localinput }
  2343. c:=#0;
  2344. inputbuffer:=nil;
  2345. inputpointer:=nil;
  2346. inputstart:=0;
  2347. { reset scanner }
  2348. preprocstack:=nil;
  2349. replaystack:=nil;
  2350. comment_level:=0;
  2351. yylexcount:=0;
  2352. block_type:=bt_general;
  2353. line_no:=0;
  2354. lastlinepos:=0;
  2355. lasttokenpos:=0;
  2356. nexttokenpos:=0;
  2357. lasttoken:=NOTOKEN;
  2358. nexttoken:=NOTOKEN;
  2359. lastasmgetchar:=#0;
  2360. ignoredirectives:=TFPHashList.Create;
  2361. in_asm_string:=false;
  2362. end;
  2363. procedure tscannerfile.firstfile;
  2364. begin
  2365. { load block }
  2366. if not openinputfile then
  2367. Message1(scan_f_cannot_open_input,inputfile.name);
  2368. reload;
  2369. end;
  2370. destructor tscannerfile.destroy;
  2371. begin
  2372. if assigned(current_module) and
  2373. (current_module.state=ms_compiled) and
  2374. (status.errorcount=0) then
  2375. checkpreprocstack
  2376. else
  2377. begin
  2378. while assigned(preprocstack) do
  2379. poppreprocstack;
  2380. end;
  2381. while assigned(replaystack) do
  2382. popreplaystack;
  2383. if not inputfile.closed then
  2384. closeinputfile;
  2385. if inputfile.is_macro then
  2386. inputfile.free;
  2387. ignoredirectives.free;
  2388. end;
  2389. function tscannerfile.openinputfile:boolean;
  2390. begin
  2391. openinputfile:=inputfile.open;
  2392. { load buffer }
  2393. inputbuffer:=inputfile.buf;
  2394. inputpointer:=inputfile.buf;
  2395. inputstart:=inputfile.bufstart;
  2396. { line }
  2397. line_no:=0;
  2398. lastlinepos:=0;
  2399. lasttokenpos:=0;
  2400. nexttokenpos:=0;
  2401. end;
  2402. procedure tscannerfile.closeinputfile;
  2403. begin
  2404. inputfile.close;
  2405. { reset buffer }
  2406. inputbuffer:=nil;
  2407. inputpointer:=nil;
  2408. inputstart:=0;
  2409. { reset line }
  2410. line_no:=0;
  2411. lastlinepos:=0;
  2412. lasttokenpos:=0;
  2413. nexttokenpos:=0;
  2414. end;
  2415. function tscannerfile.tempopeninputfile:boolean;
  2416. begin
  2417. tempopeninputfile:=false;
  2418. if inputfile.is_macro then
  2419. exit;
  2420. tempopeninputfile:=inputfile.tempopen;
  2421. { reload buffer }
  2422. inputbuffer:=inputfile.buf;
  2423. inputpointer:=inputfile.buf;
  2424. inputstart:=inputfile.bufstart;
  2425. end;
  2426. procedure tscannerfile.tempcloseinputfile;
  2427. begin
  2428. if inputfile.closed or inputfile.is_macro then
  2429. exit;
  2430. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2431. inputfile.tempclose;
  2432. { reset buffer }
  2433. inputbuffer:=nil;
  2434. inputpointer:=nil;
  2435. inputstart:=0;
  2436. end;
  2437. procedure tscannerfile.saveinputfile;
  2438. begin
  2439. inputfile.saveinputpointer:=inputpointer;
  2440. inputfile.savelastlinepos:=lastlinepos;
  2441. inputfile.saveline_no:=line_no;
  2442. end;
  2443. procedure tscannerfile.restoreinputfile;
  2444. begin
  2445. inputbuffer:=inputfile.buf;
  2446. inputpointer:=inputfile.saveinputpointer;
  2447. lastlinepos:=inputfile.savelastlinepos;
  2448. line_no:=inputfile.saveline_no;
  2449. if not inputfile.is_macro then
  2450. parser_current_file:=inputfile.name;
  2451. end;
  2452. procedure tscannerfile.nextfile;
  2453. var
  2454. to_dispose : tinputfile;
  2455. begin
  2456. if assigned(inputfile.next) then
  2457. begin
  2458. if inputfile.is_macro then
  2459. to_dispose:=inputfile
  2460. else
  2461. begin
  2462. to_dispose:=nil;
  2463. dec(inputfilecount);
  2464. end;
  2465. { we can allways close the file, no ? }
  2466. inputfile.close;
  2467. inputfile:=inputfile.next;
  2468. if assigned(to_dispose) then
  2469. to_dispose.free;
  2470. restoreinputfile;
  2471. end;
  2472. end;
  2473. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  2474. begin
  2475. if not assigned(buf) then
  2476. internalerror(200511172);
  2477. if assigned(recordtokenbuf) then
  2478. internalerror(200511173);
  2479. recordtokenbuf:=buf;
  2480. fillchar(last_settings,sizeof(last_settings),0);
  2481. last_message:=nil;
  2482. fillchar(last_filepos,sizeof(last_filepos),0);
  2483. end;
  2484. procedure tscannerfile.stoprecordtokens;
  2485. begin
  2486. if not assigned(recordtokenbuf) then
  2487. internalerror(200511174);
  2488. recordtokenbuf:=nil;
  2489. end;
  2490. procedure tscannerfile.writetoken(t : ttoken);
  2491. var
  2492. b : byte;
  2493. begin
  2494. if ord(t)>$7f then
  2495. begin
  2496. b:=(ord(t) shr 8) or $80;
  2497. recordtokenbuf.write(b,1);
  2498. end;
  2499. b:=ord(t) and $ff;
  2500. recordtokenbuf.write(b,1);
  2501. end;
  2502. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  2503. begin
  2504. {$ifdef FPC_BIG_ENDIAN}
  2505. val:=swapendian(val);
  2506. {$endif}
  2507. recordtokenbuf.write(val,sizeof(asizeint));
  2508. end;
  2509. procedure tscannerfile.tokenwritelongint(val : longint);
  2510. begin
  2511. {$ifdef FPC_BIG_ENDIAN}
  2512. val:=swapendian(val);
  2513. {$endif}
  2514. recordtokenbuf.write(val,sizeof(longint));
  2515. end;
  2516. procedure tscannerfile.tokenwriteshortint(val : shortint);
  2517. begin
  2518. recordtokenbuf.write(val,sizeof(shortint));
  2519. end;
  2520. procedure tscannerfile.tokenwriteword(val : word);
  2521. begin
  2522. {$ifdef FPC_BIG_ENDIAN}
  2523. val:=swapendian(val);
  2524. {$endif}
  2525. recordtokenbuf.write(val,sizeof(word));
  2526. end;
  2527. procedure tscannerfile.tokenwritelongword(val : longword);
  2528. begin
  2529. {$ifdef FPC_BIG_ENDIAN}
  2530. val:=swapendian(val);
  2531. {$endif}
  2532. recordtokenbuf.write(val,sizeof(longword));
  2533. end;
  2534. function tscannerfile.tokenreadsizeint : asizeint;
  2535. var
  2536. val : asizeint;
  2537. begin
  2538. replaytokenbuf.read(val,sizeof(asizeint));
  2539. {$ifdef FPC_BIG_ENDIAN}
  2540. val:=swapendian(val);
  2541. {$endif}
  2542. result:=val;
  2543. end;
  2544. function tscannerfile.tokenreadlongword : longword;
  2545. var
  2546. val : longword;
  2547. begin
  2548. replaytokenbuf.read(val,sizeof(longword));
  2549. {$ifdef FPC_BIG_ENDIAN}
  2550. val:=swapendian(val);
  2551. {$endif}
  2552. result:=val;
  2553. end;
  2554. function tscannerfile.tokenreadlongint : longint;
  2555. var
  2556. val : longint;
  2557. begin
  2558. replaytokenbuf.read(val,sizeof(longint));
  2559. {$ifdef FPC_BIG_ENDIAN}
  2560. val:=swapendian(val);
  2561. {$endif}
  2562. result:=val;
  2563. end;
  2564. function tscannerfile.tokenreadshortint : shortint;
  2565. var
  2566. val : shortint;
  2567. begin
  2568. replaytokenbuf.read(val,sizeof(shortint));
  2569. result:=val;
  2570. end;
  2571. function tscannerfile.tokenreadbyte : byte;
  2572. var
  2573. val : byte;
  2574. begin
  2575. replaytokenbuf.read(val,sizeof(byte));
  2576. result:=val;
  2577. end;
  2578. function tscannerfile.tokenreadsmallint : smallint;
  2579. var
  2580. val : smallint;
  2581. begin
  2582. replaytokenbuf.read(val,sizeof(smallint));
  2583. {$ifdef FPC_BIG_ENDIAN}
  2584. val:=swapendian(val);
  2585. {$endif}
  2586. result:=val;
  2587. end;
  2588. function tscannerfile.tokenreadword : word;
  2589. var
  2590. val : word;
  2591. begin
  2592. replaytokenbuf.read(val,sizeof(word));
  2593. {$ifdef FPC_BIG_ENDIAN}
  2594. val:=swapendian(val);
  2595. {$endif}
  2596. result:=val;
  2597. end;
  2598. function tscannerfile.tokenreadenum(size : longint) : longword;
  2599. begin
  2600. if size=1 then
  2601. result:=tokenreadbyte
  2602. else if size=2 then
  2603. result:=tokenreadword
  2604. else if size=4 then
  2605. result:=tokenreadlongword
  2606. else
  2607. internalerror(2013112901);
  2608. end;
  2609. procedure tscannerfile.tokenreadset(var b;size : longint);
  2610. {$ifdef FPC_BIG_ENDIAN}
  2611. var
  2612. i : longint;
  2613. {$endif}
  2614. begin
  2615. replaytokenbuf.read(b,size);
  2616. {$ifdef FPC_BIG_ENDIAN}
  2617. for i:=0 to size-1 do
  2618. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2619. {$endif}
  2620. end;
  2621. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2622. begin
  2623. recordtokenbuf.write(b,size);
  2624. end;
  2625. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2626. {$ifdef FPC_BIG_ENDIAN}
  2627. var
  2628. i: longint;
  2629. tmpset: array[0..31] of byte;
  2630. {$endif}
  2631. begin
  2632. {$ifdef FPC_BIG_ENDIAN}
  2633. for i:=0 to size-1 do
  2634. tmpset[i]:=reverse_byte(Pbyte(@b)[i]);
  2635. recordtokenbuf.write(tmpset,size);
  2636. {$else}
  2637. recordtokenbuf.write(b,size);
  2638. {$endif}
  2639. end;
  2640. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2641. { This procedure
  2642. needs to be changed whenever
  2643. globals.tsettings type is changed,
  2644. the problem is that no error will appear
  2645. before tests with generics are tested. PM }
  2646. var
  2647. startpos, endpos : longword;
  2648. begin
  2649. { WARNING all those fields need to be in the correct
  2650. order otherwise cross_endian PPU reading will fail }
  2651. startpos:=replaytokenbuf.pos;
  2652. with asettings do
  2653. begin
  2654. alignment.procalign:=tokenreadlongint;
  2655. alignment.loopalign:=tokenreadlongint;
  2656. alignment.jumpalign:=tokenreadlongint;
  2657. alignment.constalignmin:=tokenreadlongint;
  2658. alignment.constalignmax:=tokenreadlongint;
  2659. alignment.varalignmin:=tokenreadlongint;
  2660. alignment.varalignmax:=tokenreadlongint;
  2661. alignment.localalignmin:=tokenreadlongint;
  2662. alignment.localalignmax:=tokenreadlongint;
  2663. alignment.recordalignmin:=tokenreadlongint;
  2664. alignment.recordalignmax:=tokenreadlongint;
  2665. alignment.maxCrecordalign:=tokenreadlongint;
  2666. tokenreadset(globalswitches,sizeof(globalswitches));
  2667. tokenreadset(targetswitches,sizeof(targetswitches));
  2668. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2669. tokenreadset(localswitches,sizeof(localswitches));
  2670. tokenreadset(modeswitches,sizeof(modeswitches));
  2671. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2672. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2673. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2674. tokenreadset(debugswitches,sizeof(debugswitches));
  2675. { 0: old behaviour for sets <=256 elements
  2676. >0: round to this size }
  2677. setalloc:=tokenreadshortint;
  2678. packenum:=tokenreadshortint;
  2679. packrecords:=tokenreadshortint;
  2680. maxfpuregisters:=tokenreadshortint;
  2681. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2682. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2683. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2684. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2685. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2686. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2687. { tstringencoding is word type,
  2688. thus this should be OK here }
  2689. sourcecodepage:=tstringEncoding(tokenreadword);
  2690. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2691. disabledircache:=boolean(tokenreadbyte);
  2692. {$if defined(ARM) or defined(AVR)}
  2693. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)));
  2694. {$endif defined(ARM) or defined(AVR)}
  2695. endpos:=replaytokenbuf.pos;
  2696. if endpos-startpos<>expected_size then
  2697. Comment(V_Error,'Wrong size of Settings read-in');
  2698. end;
  2699. end;
  2700. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2701. { This procedure
  2702. needs to be changed whenever
  2703. globals.tsettings type is changed,
  2704. the problem is that no error will appear
  2705. before tests with generics are tested. PM }
  2706. var
  2707. sizepos, startpos, endpos : longword;
  2708. begin
  2709. { WARNING all those fields need to be in the correct
  2710. order otherwise cross_endian PPU reading will fail }
  2711. sizepos:=recordtokenbuf.pos;
  2712. size:=0;
  2713. tokenwritesizeint(size);
  2714. startpos:=recordtokenbuf.pos;
  2715. with asettings do
  2716. begin
  2717. tokenwritelongint(alignment.procalign);
  2718. tokenwritelongint(alignment.loopalign);
  2719. tokenwritelongint(alignment.jumpalign);
  2720. tokenwritelongint(alignment.constalignmin);
  2721. tokenwritelongint(alignment.constalignmax);
  2722. tokenwritelongint(alignment.varalignmin);
  2723. tokenwritelongint(alignment.varalignmax);
  2724. tokenwritelongint(alignment.localalignmin);
  2725. tokenwritelongint(alignment.localalignmax);
  2726. tokenwritelongint(alignment.recordalignmin);
  2727. tokenwritelongint(alignment.recordalignmax);
  2728. tokenwritelongint(alignment.maxCrecordalign);
  2729. tokenwriteset(globalswitches,sizeof(globalswitches));
  2730. tokenwriteset(targetswitches,sizeof(targetswitches));
  2731. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  2732. tokenwriteset(localswitches,sizeof(localswitches));
  2733. tokenwriteset(modeswitches,sizeof(modeswitches));
  2734. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  2735. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2736. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2737. tokenwriteset(debugswitches,sizeof(debugswitches));
  2738. { 0: old behaviour for sets <=256 elements
  2739. >0: round to this size }
  2740. tokenwriteshortint(setalloc);
  2741. tokenwriteshortint(packenum);
  2742. tokenwriteshortint(packrecords);
  2743. tokenwriteshortint(maxfpuregisters);
  2744. tokenwriteenum(cputype,sizeof(tcputype));
  2745. tokenwriteenum(optimizecputype,sizeof(tcputype));
  2746. tokenwriteenum(fputype,sizeof(tfputype));
  2747. tokenwriteenum(asmmode,sizeof(tasmmode));
  2748. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  2749. tokenwriteenum(defproccall,sizeof(tproccalloption));
  2750. { tstringencoding is word type,
  2751. thus this should be OK here }
  2752. tokenwriteword(sourcecodepage);
  2753. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  2754. recordtokenbuf.write(byte(disabledircache),1);
  2755. {$if defined(ARM) or defined(AVR)}
  2756. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  2757. {$endif defined(ARM) or defined(AVR)}
  2758. endpos:=recordtokenbuf.pos;
  2759. size:=endpos-startpos;
  2760. recordtokenbuf.seek(sizepos);
  2761. tokenwritesizeint(size);
  2762. recordtokenbuf.seek(endpos);
  2763. end;
  2764. end;
  2765. procedure tscannerfile.recordtoken;
  2766. var
  2767. t : ttoken;
  2768. s : tspecialgenerictoken;
  2769. len,msgnb,copy_size : asizeint;
  2770. val : longint;
  2771. b : byte;
  2772. pmsg : pmessagestaterecord;
  2773. begin
  2774. if not assigned(recordtokenbuf) then
  2775. internalerror(200511176);
  2776. t:=_GENERICSPECIALTOKEN;
  2777. { settings changed? }
  2778. { last field pmessage is handled separately below in
  2779. ST_LOADMESSAGES }
  2780. if CompareByte(current_settings,last_settings,
  2781. sizeof(current_settings)-sizeof(pointer))<>0 then
  2782. begin
  2783. { use a special token to record it }
  2784. s:=ST_LOADSETTINGS;
  2785. writetoken(t);
  2786. recordtokenbuf.write(s,1);
  2787. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2788. tokenwritesettings(current_settings,copy_size);
  2789. last_settings:=current_settings;
  2790. end;
  2791. if current_settings.pmessage<>last_message then
  2792. begin
  2793. { use a special token to record it }
  2794. s:=ST_LOADMESSAGES;
  2795. writetoken(t);
  2796. recordtokenbuf.write(s,1);
  2797. msgnb:=0;
  2798. pmsg:=current_settings.pmessage;
  2799. while assigned(pmsg) do
  2800. begin
  2801. if msgnb=high(asizeint) then
  2802. { Too many messages }
  2803. internalerror(2011090401);
  2804. inc(msgnb);
  2805. pmsg:=pmsg^.next;
  2806. end;
  2807. tokenwritesizeint(msgnb);
  2808. pmsg:=current_settings.pmessage;
  2809. while assigned(pmsg) do
  2810. begin
  2811. { What about endianess here?}
  2812. { SB: this is handled by tokenreadlongint }
  2813. val:=pmsg^.value;
  2814. tokenwritelongint(val);
  2815. val:=ord(pmsg^.state);
  2816. tokenwritelongint(val);
  2817. pmsg:=pmsg^.next;
  2818. end;
  2819. last_message:=current_settings.pmessage;
  2820. end;
  2821. { file pos changes? }
  2822. if current_tokenpos.line<>last_filepos.line then
  2823. begin
  2824. s:=ST_LINE;
  2825. writetoken(t);
  2826. recordtokenbuf.write(s,1);
  2827. tokenwritelongint(current_tokenpos.line);
  2828. last_filepos.line:=current_tokenpos.line;
  2829. end;
  2830. if current_tokenpos.column<>last_filepos.column then
  2831. begin
  2832. s:=ST_COLUMN;
  2833. writetoken(t);
  2834. { can the column be written packed? }
  2835. if current_tokenpos.column<$80 then
  2836. begin
  2837. b:=$80 or current_tokenpos.column;
  2838. recordtokenbuf.write(b,1);
  2839. end
  2840. else
  2841. begin
  2842. recordtokenbuf.write(s,1);
  2843. tokenwriteword(current_tokenpos.column);
  2844. end;
  2845. last_filepos.column:=current_tokenpos.column;
  2846. end;
  2847. if current_tokenpos.fileindex<>last_filepos.fileindex then
  2848. begin
  2849. s:=ST_FILEINDEX;
  2850. writetoken(t);
  2851. recordtokenbuf.write(s,1);
  2852. tokenwriteword(current_tokenpos.fileindex);
  2853. last_filepos.fileindex:=current_tokenpos.fileindex;
  2854. end;
  2855. writetoken(token);
  2856. if token<>_GENERICSPECIALTOKEN then
  2857. writetoken(idtoken);
  2858. case token of
  2859. _CWCHAR,
  2860. _CWSTRING :
  2861. begin
  2862. tokenwritesizeint(patternw^.len);
  2863. if patternw^.len>0 then
  2864. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2865. end;
  2866. _CSTRING:
  2867. begin
  2868. len:=length(cstringpattern);
  2869. tokenwritesizeint(len);
  2870. if len>0 then
  2871. recordtokenbuf.write(cstringpattern[1],len);
  2872. end;
  2873. _CCHAR,
  2874. _INTCONST,
  2875. _REALNUMBER :
  2876. begin
  2877. { pexpr.pas messes with pattern in case of negative integer consts,
  2878. see around line 2562 the comment of JM; remove the - before recording it
  2879. (FK)
  2880. }
  2881. if (token=_INTCONST) and (pattern[1]='-') then
  2882. delete(pattern,1,1);
  2883. recordtokenbuf.write(pattern[0],1);
  2884. recordtokenbuf.write(pattern[1],length(pattern));
  2885. end;
  2886. _ID :
  2887. begin
  2888. recordtokenbuf.write(orgpattern[0],1);
  2889. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  2890. end;
  2891. end;
  2892. end;
  2893. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  2894. begin
  2895. if not assigned(buf) then
  2896. internalerror(200511175);
  2897. { save current token }
  2898. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  2899. internalerror(200511178);
  2900. replaystack:=treplaystack.create(token,current_settings,
  2901. replaytokenbuf,replaystack);
  2902. if assigned(inputpointer) then
  2903. dec(inputpointer);
  2904. { install buffer }
  2905. replaytokenbuf:=buf;
  2906. { reload next token }
  2907. replaytokenbuf.seek(0);
  2908. replaytoken;
  2909. end;
  2910. function tscannerfile.readtoken: ttoken;
  2911. var
  2912. b,b2 : byte;
  2913. begin
  2914. replaytokenbuf.read(b,1);
  2915. if (b and $80)<>0 then
  2916. begin
  2917. replaytokenbuf.read(b2,1);
  2918. result:=ttoken(((b and $7f) shl 8) or b2);
  2919. end
  2920. else
  2921. result:=ttoken(b);
  2922. end;
  2923. procedure tscannerfile.replaytoken;
  2924. var
  2925. wlen,mesgnb,copy_size : asizeint;
  2926. specialtoken : tspecialgenerictoken;
  2927. i : byte;
  2928. pmsg,prevmsg : pmessagestaterecord;
  2929. begin
  2930. if not assigned(replaytokenbuf) then
  2931. internalerror(200511177);
  2932. { End of replay buffer? Then load the next char from the file again }
  2933. if replaytokenbuf.pos>=replaytokenbuf.size then
  2934. begin
  2935. token:=replaystack.token;
  2936. replaytokenbuf:=replaystack.tokenbuf;
  2937. { restore compiler settings }
  2938. current_settings:=replaystack.settings;
  2939. popreplaystack;
  2940. if assigned(inputpointer) then
  2941. begin
  2942. c:=inputpointer^;
  2943. inc(inputpointer);
  2944. end;
  2945. exit;
  2946. end;
  2947. repeat
  2948. { load token from the buffer }
  2949. token:=readtoken;
  2950. if token<>_GENERICSPECIALTOKEN then
  2951. idtoken:=readtoken
  2952. else
  2953. idtoken:=_NOID;
  2954. case token of
  2955. _CWCHAR,
  2956. _CWSTRING :
  2957. begin
  2958. wlen:=tokenreadsizeint;
  2959. setlengthwidestring(patternw,wlen);
  2960. if wlen>0 then
  2961. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2962. orgpattern:='';
  2963. pattern:='';
  2964. cstringpattern:='';
  2965. end;
  2966. _CSTRING:
  2967. begin
  2968. wlen:=tokenreadsizeint;
  2969. if wlen>0 then
  2970. begin
  2971. setlength(cstringpattern,wlen);
  2972. replaytokenbuf.read(cstringpattern[1],wlen);
  2973. end
  2974. else
  2975. cstringpattern:='';
  2976. orgpattern:='';
  2977. pattern:='';
  2978. end;
  2979. _CCHAR,
  2980. _INTCONST,
  2981. _REALNUMBER :
  2982. begin
  2983. replaytokenbuf.read(pattern[0],1);
  2984. replaytokenbuf.read(pattern[1],length(pattern));
  2985. orgpattern:='';
  2986. end;
  2987. _ID :
  2988. begin
  2989. replaytokenbuf.read(orgpattern[0],1);
  2990. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  2991. pattern:=upper(orgpattern);
  2992. end;
  2993. _GENERICSPECIALTOKEN:
  2994. begin
  2995. replaytokenbuf.read(specialtoken,1);
  2996. { packed column? }
  2997. if (ord(specialtoken) and $80)<>0 then
  2998. begin
  2999. current_tokenpos.column:=ord(specialtoken) and $7f;
  3000. current_filepos:=current_tokenpos;
  3001. end
  3002. else
  3003. case specialtoken of
  3004. ST_LOADSETTINGS:
  3005. begin
  3006. copy_size:=tokenreadsizeint;
  3007. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3008. // internalerror(2011090501);
  3009. {
  3010. replaytokenbuf.read(current_settings,copy_size);
  3011. }
  3012. tokenreadsettings(current_settings,copy_size);
  3013. end;
  3014. ST_LOADMESSAGES:
  3015. begin
  3016. current_settings.pmessage:=nil;
  3017. mesgnb:=tokenreadsizeint;
  3018. if mesgnb>0 then
  3019. Comment(V_Error,'Message recordind not yet supported');
  3020. prevmsg:=nil;
  3021. for i:=1 to mesgnb do
  3022. begin
  3023. new(pmsg);
  3024. if i=1 then
  3025. current_settings.pmessage:=pmsg
  3026. else
  3027. prevmsg^.next:=pmsg;
  3028. pmsg^.value:=tokenreadlongint;
  3029. pmsg^.state:=tmsgstate(tokenreadlongint);
  3030. pmsg^.next:=nil;
  3031. prevmsg:=pmsg;
  3032. end;
  3033. end;
  3034. ST_LINE:
  3035. begin
  3036. current_tokenpos.line:=tokenreadlongint;
  3037. current_filepos:=current_tokenpos;
  3038. end;
  3039. ST_COLUMN:
  3040. begin
  3041. current_tokenpos.column:=tokenreadword;
  3042. current_filepos:=current_tokenpos;
  3043. end;
  3044. ST_FILEINDEX:
  3045. begin
  3046. current_tokenpos.fileindex:=tokenreadword;
  3047. current_filepos:=current_tokenpos;
  3048. end;
  3049. else
  3050. internalerror(2006103010);
  3051. end;
  3052. continue;
  3053. end;
  3054. end;
  3055. break;
  3056. until false;
  3057. end;
  3058. procedure tscannerfile.addfile(hp:tinputfile);
  3059. begin
  3060. saveinputfile;
  3061. { add to list }
  3062. hp.next:=inputfile;
  3063. inputfile:=hp;
  3064. { load new inputfile }
  3065. restoreinputfile;
  3066. end;
  3067. procedure tscannerfile.reload;
  3068. begin
  3069. with inputfile do
  3070. begin
  3071. { when nothing more to read then leave immediatly, so we
  3072. don't change the current_filepos and leave it point to the last
  3073. char }
  3074. if (c=#26) and (not assigned(next)) then
  3075. exit;
  3076. repeat
  3077. { still more to read?, then change the #0 to a space so its seen
  3078. as a seperator, this can't be used for macro's which can change
  3079. the place of the #0 in the buffer with tempopen }
  3080. if (c=#0) and (bufsize>0) and
  3081. not(inputfile.is_macro) and
  3082. (inputpointer-inputbuffer<bufsize) then
  3083. begin
  3084. c:=' ';
  3085. inc(inputpointer);
  3086. exit;
  3087. end;
  3088. { can we read more from this file ? }
  3089. if (c<>#26) and (not endoffile) then
  3090. begin
  3091. readbuf;
  3092. inputpointer:=buf;
  3093. inputbuffer:=buf;
  3094. inputstart:=bufstart;
  3095. { first line? }
  3096. if line_no=0 then
  3097. begin
  3098. c:=inputpointer^;
  3099. { eat utf-8 signature? }
  3100. if (ord(inputpointer^)=$ef) and
  3101. (ord((inputpointer+1)^)=$bb) and
  3102. (ord((inputpointer+2)^)=$bf) then
  3103. begin
  3104. (* we don't support including files with an UTF-8 bom
  3105. inside another file that wasn't encoded as UTF-8
  3106. already (we don't support {$codepage xxx} switches in
  3107. the middle of a file either) *)
  3108. if (current_settings.sourcecodepage<>CP_UTF8) and
  3109. not current_module.in_global then
  3110. Message(scanner_f_illegal_utf8_bom);
  3111. inc(inputpointer,3);
  3112. message(scan_c_switching_to_utf8);
  3113. current_settings.sourcecodepage:=CP_UTF8;
  3114. include(current_settings.moduleswitches,cs_explicit_codepage);
  3115. end;
  3116. line_no:=1;
  3117. if cs_asm_source in current_settings.globalswitches then
  3118. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3119. end;
  3120. end
  3121. else
  3122. begin
  3123. { load eof position in tokenpos/current_filepos }
  3124. gettokenpos;
  3125. { close file }
  3126. closeinputfile;
  3127. { no next module, than EOF }
  3128. if not assigned(inputfile.next) then
  3129. begin
  3130. c:=#26;
  3131. exit;
  3132. end;
  3133. { load next file and reopen it }
  3134. nextfile;
  3135. tempopeninputfile;
  3136. { status }
  3137. Message1(scan_t_back_in,inputfile.name);
  3138. end;
  3139. { load next char }
  3140. c:=inputpointer^;
  3141. inc(inputpointer);
  3142. until c<>#0; { if also end, then reload again }
  3143. end;
  3144. end;
  3145. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  3146. var
  3147. hp : tinputfile;
  3148. begin
  3149. { save old postion }
  3150. dec(inputpointer);
  3151. tempcloseinputfile;
  3152. { create macro 'file' }
  3153. { use special name to dispose after !! }
  3154. hp:=do_openinputfile('_Macro_.'+macname);
  3155. addfile(hp);
  3156. with inputfile do
  3157. begin
  3158. setmacro(p,len);
  3159. { local buffer }
  3160. inputbuffer:=buf;
  3161. inputpointer:=buf;
  3162. inputstart:=bufstart;
  3163. ref_index:=fileindex;
  3164. end;
  3165. { reset line }
  3166. line_no:=line;
  3167. lastlinepos:=0;
  3168. lasttokenpos:=0;
  3169. nexttokenpos:=0;
  3170. { load new c }
  3171. c:=inputpointer^;
  3172. inc(inputpointer);
  3173. end;
  3174. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3175. begin
  3176. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3177. filepos.line:=line_no;
  3178. filepos.column:=tokenpos-lastlinepos;
  3179. filepos.fileindex:=inputfile.ref_index;
  3180. filepos.moduleindex:=current_module.unit_index;
  3181. end;
  3182. procedure tscannerfile.gettokenpos;
  3183. { load the values of tokenpos and lasttokenpos }
  3184. begin
  3185. do_gettokenpos(lasttokenpos,current_tokenpos);
  3186. current_filepos:=current_tokenpos;
  3187. end;
  3188. procedure tscannerfile.cachenexttokenpos;
  3189. begin
  3190. do_gettokenpos(nexttokenpos,next_filepos);
  3191. end;
  3192. procedure tscannerfile.setnexttoken;
  3193. begin
  3194. token:=nexttoken;
  3195. nexttoken:=NOTOKEN;
  3196. lasttokenpos:=nexttokenpos;
  3197. current_tokenpos:=next_filepos;
  3198. current_filepos:=current_tokenpos;
  3199. nexttokenpos:=0;
  3200. end;
  3201. procedure tscannerfile.savetokenpos;
  3202. begin
  3203. oldlasttokenpos:=lasttokenpos;
  3204. oldcurrent_filepos:=current_filepos;
  3205. oldcurrent_tokenpos:=current_tokenpos;
  3206. end;
  3207. procedure tscannerfile.restoretokenpos;
  3208. begin
  3209. lasttokenpos:=oldlasttokenpos;
  3210. current_filepos:=oldcurrent_filepos;
  3211. current_tokenpos:=oldcurrent_tokenpos;
  3212. end;
  3213. procedure tscannerfile.inc_comment_level;
  3214. begin
  3215. if (m_nested_comment in current_settings.modeswitches) then
  3216. inc(comment_level)
  3217. else
  3218. comment_level:=1;
  3219. if (comment_level>1) then
  3220. begin
  3221. savetokenpos;
  3222. gettokenpos; { update for warning }
  3223. Message1(scan_w_comment_level,tostr(comment_level));
  3224. restoretokenpos;
  3225. end;
  3226. end;
  3227. procedure tscannerfile.dec_comment_level;
  3228. begin
  3229. if (m_nested_comment in current_settings.modeswitches) then
  3230. dec(comment_level)
  3231. else
  3232. comment_level:=0;
  3233. end;
  3234. procedure tscannerfile.linebreak;
  3235. var
  3236. cur : char;
  3237. begin
  3238. with inputfile do
  3239. begin
  3240. if (byte(inputpointer^)=0) and not(endoffile) then
  3241. begin
  3242. cur:=c;
  3243. reload;
  3244. if byte(cur)+byte(c)<>23 then
  3245. dec(inputpointer);
  3246. end
  3247. else
  3248. begin
  3249. { Support all combination of #10 and #13 as line break }
  3250. if (byte(inputpointer^)+byte(c)=23) then
  3251. inc(inputpointer);
  3252. end;
  3253. { Always return #10 as line break }
  3254. c:=#10;
  3255. { increase line counters }
  3256. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3257. inc(line_no);
  3258. { update linebuffer }
  3259. if cs_asm_source in current_settings.globalswitches then
  3260. inputfile.setline(line_no,lastlinepos);
  3261. { update for status and call the show status routine,
  3262. but don't touch current_filepos ! }
  3263. savetokenpos;
  3264. gettokenpos; { update for v_status }
  3265. inc(status.compiledlines);
  3266. ShowStatus;
  3267. restoretokenpos;
  3268. end;
  3269. end;
  3270. procedure tscannerfile.illegal_char(c:char);
  3271. var
  3272. s : string;
  3273. begin
  3274. if c in [#32..#255] then
  3275. s:=''''+c+''''
  3276. else
  3277. s:='#'+tostr(ord(c));
  3278. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3279. end;
  3280. procedure tscannerfile.end_of_file;
  3281. begin
  3282. checkpreprocstack;
  3283. Message(scan_f_end_of_file);
  3284. end;
  3285. {-------------------------------------------
  3286. IF Conditional Handling
  3287. -------------------------------------------}
  3288. procedure tscannerfile.checkpreprocstack;
  3289. begin
  3290. { check for missing ifdefs }
  3291. while assigned(preprocstack) do
  3292. begin
  3293. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3294. preprocstack.owner.inputfile.name,tostr(preprocstack.line_nb));
  3295. poppreprocstack;
  3296. end;
  3297. end;
  3298. procedure tscannerfile.poppreprocstack;
  3299. var
  3300. hp : tpreprocstack;
  3301. begin
  3302. if assigned(preprocstack) then
  3303. begin
  3304. Message1(scan_c_endif_found,preprocstack.name);
  3305. hp:=preprocstack.next;
  3306. preprocstack.free;
  3307. preprocstack:=hp;
  3308. end
  3309. else
  3310. Message(scan_e_endif_without_if);
  3311. end;
  3312. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3313. var
  3314. condition: Boolean;
  3315. valuedescr: String;
  3316. begin
  3317. if (preprocstack=nil) or preprocstack.accept then
  3318. condition:=compile_time_predicate(valuedescr)
  3319. else
  3320. begin
  3321. condition:= false;
  3322. valuedescr:= '';
  3323. end;
  3324. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3325. preprocstack.name:=valuedescr;
  3326. preprocstack.line_nb:=line_no;
  3327. preprocstack.owner:=self;
  3328. if preprocstack.accept then
  3329. Message2(messid,preprocstack.name,'accepted')
  3330. else
  3331. Message2(messid,preprocstack.name,'rejected');
  3332. end;
  3333. procedure tscannerfile.elsepreprocstack;
  3334. begin
  3335. if assigned(preprocstack) and
  3336. (preprocstack.typ<>pp_else) then
  3337. begin
  3338. if (preprocstack.typ=pp_elseif) then
  3339. preprocstack.accept:=false
  3340. else
  3341. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3342. preprocstack.accept:=not preprocstack.accept;
  3343. preprocstack.typ:=pp_else;
  3344. preprocstack.line_nb:=line_no;
  3345. if preprocstack.accept then
  3346. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3347. else
  3348. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3349. end
  3350. else
  3351. Message(scan_e_endif_without_if);
  3352. end;
  3353. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3354. var
  3355. valuedescr: String;
  3356. begin
  3357. if assigned(preprocstack) and
  3358. (preprocstack.typ in [pp_if,pp_elseif]) then
  3359. begin
  3360. { when the branch is accepted we use pp_elseif so we know that
  3361. all the next branches need to be rejected. when this branch is still
  3362. not accepted then leave it at pp_if }
  3363. if (preprocstack.typ=pp_elseif) then
  3364. preprocstack.accept:=false
  3365. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  3366. begin
  3367. preprocstack.accept:=false;
  3368. preprocstack.typ:=pp_elseif;
  3369. end
  3370. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  3371. and compile_time_predicate(valuedescr) then
  3372. begin
  3373. preprocstack.name:=valuedescr;
  3374. preprocstack.accept:=true;
  3375. preprocstack.typ:=pp_elseif;
  3376. end;
  3377. preprocstack.line_nb:=line_no;
  3378. if preprocstack.accept then
  3379. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3380. else
  3381. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3382. end
  3383. else
  3384. Message(scan_e_endif_without_if);
  3385. end;
  3386. procedure tscannerfile.popreplaystack;
  3387. var
  3388. hp : treplaystack;
  3389. begin
  3390. if assigned(replaystack) then
  3391. begin
  3392. hp:=replaystack.next;
  3393. replaystack.free;
  3394. replaystack:=hp;
  3395. end;
  3396. end;
  3397. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  3398. begin
  3399. savetokenpos;
  3400. repeat
  3401. current_scanner.gettokenpos;
  3402. Message1(scan_d_handling_switch,'$'+p.name);
  3403. p.proc();
  3404. { accept the text ? }
  3405. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  3406. break
  3407. else
  3408. begin
  3409. current_scanner.gettokenpos;
  3410. Message(scan_c_skipping_until);
  3411. repeat
  3412. current_scanner.skipuntildirective;
  3413. if not (m_mac in current_settings.modeswitches) then
  3414. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  3415. else
  3416. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  3417. until assigned(p) and (p.is_conditional);
  3418. current_scanner.gettokenpos;
  3419. end;
  3420. until false;
  3421. restoretokenpos;
  3422. end;
  3423. procedure tscannerfile.handledirectives;
  3424. var
  3425. t : tdirectiveitem;
  3426. hs : string;
  3427. begin
  3428. gettokenpos;
  3429. readchar; {Remove the $}
  3430. hs:=readid;
  3431. { handle empty directive }
  3432. if hs='' then
  3433. begin
  3434. Message1(scan_w_illegal_switch,'$');
  3435. exit;
  3436. end;
  3437. {$ifdef PREPROCWRITE}
  3438. if parapreprocess then
  3439. begin
  3440. t:=Get_Directive(hs);
  3441. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  3442. begin
  3443. preprocfile^.AddSpace;
  3444. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  3445. exit;
  3446. end;
  3447. end;
  3448. {$endif PREPROCWRITE}
  3449. { skip this directive? }
  3450. if (ignoredirectives.find(hs)<>nil) then
  3451. begin
  3452. if (comment_level>0) then
  3453. readcomment;
  3454. { we've read the whole comment }
  3455. aktcommentstyle:=comment_none;
  3456. exit;
  3457. end;
  3458. { Check for compiler switches }
  3459. while (length(hs)=1) and (c in ['-','+']) do
  3460. begin
  3461. Message1(scan_d_handling_switch,'$'+hs+c);
  3462. HandleSwitch(hs[1],c);
  3463. current_scanner.readchar; {Remove + or -}
  3464. if c=',' then
  3465. begin
  3466. current_scanner.readchar; {Remove , }
  3467. { read next switch, support $v+,$+}
  3468. hs:=current_scanner.readid;
  3469. if (hs='') then
  3470. begin
  3471. if (c='$') and (m_fpc in current_settings.modeswitches) then
  3472. begin
  3473. current_scanner.readchar; { skip $ }
  3474. hs:=current_scanner.readid;
  3475. end;
  3476. if (hs='') then
  3477. Message1(scan_w_illegal_directive,'$'+c);
  3478. end;
  3479. end
  3480. else
  3481. hs:='';
  3482. end;
  3483. { directives may follow switches after a , }
  3484. if hs<>'' then
  3485. begin
  3486. if not (m_mac in current_settings.modeswitches) then
  3487. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3488. else
  3489. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3490. if assigned(t) then
  3491. begin
  3492. if t.is_conditional then
  3493. handleconditional(t)
  3494. else
  3495. begin
  3496. Message1(scan_d_handling_switch,'$'+hs);
  3497. t.proc();
  3498. end;
  3499. end
  3500. else
  3501. begin
  3502. current_scanner.ignoredirectives.Add(hs,nil);
  3503. Message1(scan_w_illegal_directive,'$'+hs);
  3504. end;
  3505. { conditionals already read the comment }
  3506. if (current_scanner.comment_level>0) then
  3507. current_scanner.readcomment;
  3508. { we've read the whole comment }
  3509. aktcommentstyle:=comment_none;
  3510. end;
  3511. end;
  3512. procedure tscannerfile.readchar;
  3513. begin
  3514. c:=inputpointer^;
  3515. if c=#0 then
  3516. reload
  3517. else
  3518. inc(inputpointer);
  3519. end;
  3520. procedure tscannerfile.readstring;
  3521. var
  3522. i : longint;
  3523. err : boolean;
  3524. begin
  3525. err:=false;
  3526. i:=0;
  3527. repeat
  3528. case c of
  3529. '_',
  3530. '0'..'9',
  3531. 'A'..'Z' :
  3532. begin
  3533. if i<255 then
  3534. begin
  3535. inc(i);
  3536. orgpattern[i]:=c;
  3537. pattern[i]:=c;
  3538. end
  3539. else
  3540. begin
  3541. if not err then
  3542. begin
  3543. Message(scan_e_string_exceeds_255_chars);
  3544. err:=true;
  3545. end;
  3546. end;
  3547. c:=inputpointer^;
  3548. inc(inputpointer);
  3549. end;
  3550. 'a'..'z' :
  3551. begin
  3552. if i<255 then
  3553. begin
  3554. inc(i);
  3555. orgpattern[i]:=c;
  3556. pattern[i]:=chr(ord(c)-32)
  3557. end
  3558. else
  3559. begin
  3560. if not err then
  3561. begin
  3562. Message(scan_e_string_exceeds_255_chars);
  3563. err:=true;
  3564. end;
  3565. end;
  3566. c:=inputpointer^;
  3567. inc(inputpointer);
  3568. end;
  3569. #0 :
  3570. reload;
  3571. else
  3572. break;
  3573. end;
  3574. until false;
  3575. orgpattern[0]:=chr(i);
  3576. pattern[0]:=chr(i);
  3577. end;
  3578. procedure tscannerfile.readnumber;
  3579. var
  3580. base,
  3581. i : longint;
  3582. begin
  3583. case c of
  3584. '%' :
  3585. begin
  3586. readchar;
  3587. base:=2;
  3588. pattern[1]:='%';
  3589. i:=1;
  3590. end;
  3591. '&' :
  3592. begin
  3593. readchar;
  3594. base:=8;
  3595. pattern[1]:='&';
  3596. i:=1;
  3597. end;
  3598. '$' :
  3599. begin
  3600. readchar;
  3601. base:=16;
  3602. pattern[1]:='$';
  3603. i:=1;
  3604. end;
  3605. else
  3606. begin
  3607. base:=10;
  3608. i:=0;
  3609. end;
  3610. end;
  3611. while ((base>=10) and (c in ['0'..'9'])) or
  3612. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3613. ((base=8) and (c in ['0'..'7'])) or
  3614. ((base=2) and (c in ['0'..'1'])) do
  3615. begin
  3616. if i<255 then
  3617. begin
  3618. inc(i);
  3619. pattern[i]:=c;
  3620. end;
  3621. readchar;
  3622. end;
  3623. pattern[0]:=chr(i);
  3624. end;
  3625. function tscannerfile.readid:string;
  3626. begin
  3627. readstring;
  3628. readid:=pattern;
  3629. end;
  3630. function tscannerfile.readval:longint;
  3631. var
  3632. l : longint;
  3633. w : integer;
  3634. begin
  3635. readnumber;
  3636. val(pattern,l,w);
  3637. readval:=l;
  3638. end;
  3639. function tscannerfile.readcomment:string;
  3640. var
  3641. i : longint;
  3642. begin
  3643. i:=0;
  3644. repeat
  3645. case c of
  3646. '{' :
  3647. begin
  3648. if aktcommentstyle=comment_tp then
  3649. inc_comment_level;
  3650. end;
  3651. '}' :
  3652. begin
  3653. if aktcommentstyle=comment_tp then
  3654. begin
  3655. readchar;
  3656. dec_comment_level;
  3657. if comment_level=0 then
  3658. break
  3659. else
  3660. continue;
  3661. end;
  3662. end;
  3663. '*' :
  3664. begin
  3665. if aktcommentstyle=comment_oldtp then
  3666. begin
  3667. readchar;
  3668. if c=')' then
  3669. begin
  3670. readchar;
  3671. dec_comment_level;
  3672. break;
  3673. end
  3674. else
  3675. { Add both characters !!}
  3676. if (i<255) then
  3677. begin
  3678. inc(i);
  3679. readcomment[i]:='*';
  3680. if (i<255) then
  3681. begin
  3682. inc(i);
  3683. readcomment[i]:=c;
  3684. end;
  3685. end;
  3686. end
  3687. else
  3688. { Not old TP comment, so add...}
  3689. begin
  3690. if (i<255) then
  3691. begin
  3692. inc(i);
  3693. readcomment[i]:='*';
  3694. end;
  3695. end;
  3696. end;
  3697. #10,#13 :
  3698. linebreak;
  3699. #26 :
  3700. end_of_file;
  3701. else
  3702. begin
  3703. if (i<255) then
  3704. begin
  3705. inc(i);
  3706. readcomment[i]:=c;
  3707. end;
  3708. end;
  3709. end;
  3710. readchar;
  3711. until false;
  3712. readcomment[0]:=chr(i);
  3713. end;
  3714. function tscannerfile.readquotedstring:string;
  3715. var
  3716. i : longint;
  3717. msgwritten : boolean;
  3718. begin
  3719. i:=0;
  3720. msgwritten:=false;
  3721. if (c='''') then
  3722. begin
  3723. repeat
  3724. readchar;
  3725. case c of
  3726. #26 :
  3727. end_of_file;
  3728. #10,#13 :
  3729. Message(scan_f_string_exceeds_line);
  3730. '''' :
  3731. begin
  3732. readchar;
  3733. if c<>'''' then
  3734. break;
  3735. end;
  3736. end;
  3737. if i<255 then
  3738. begin
  3739. inc(i);
  3740. result[i]:=c;
  3741. end
  3742. else
  3743. begin
  3744. if not msgwritten then
  3745. begin
  3746. Message(scan_e_string_exceeds_255_chars);
  3747. msgwritten:=true;
  3748. end;
  3749. end;
  3750. until false;
  3751. end;
  3752. result[0]:=chr(i);
  3753. end;
  3754. function tscannerfile.readstate:char;
  3755. var
  3756. state : char;
  3757. begin
  3758. state:=' ';
  3759. if c=' ' then
  3760. begin
  3761. current_scanner.skipspace;
  3762. current_scanner.readid;
  3763. if pattern='ON' then
  3764. state:='+'
  3765. else
  3766. if pattern='OFF' then
  3767. state:='-';
  3768. end
  3769. else
  3770. state:=c;
  3771. if not (state in ['+','-']) then
  3772. Message(scan_e_wrong_switch_toggle);
  3773. readstate:=state;
  3774. end;
  3775. function tscannerfile.readstatedefault:char;
  3776. var
  3777. state : char;
  3778. begin
  3779. state:=' ';
  3780. if c=' ' then
  3781. begin
  3782. current_scanner.skipspace;
  3783. current_scanner.readid;
  3784. if pattern='ON' then
  3785. state:='+'
  3786. else
  3787. if pattern='OFF' then
  3788. state:='-'
  3789. else
  3790. if pattern='DEFAULT' then
  3791. state:='*';
  3792. end
  3793. else
  3794. state:=c;
  3795. if not (state in ['+','-','*']) then
  3796. Message(scan_e_wrong_switch_toggle_default);
  3797. readstatedefault:=state;
  3798. end;
  3799. procedure tscannerfile.skipspace;
  3800. begin
  3801. repeat
  3802. case c of
  3803. #26 :
  3804. begin
  3805. reload;
  3806. if (c=#26) and not assigned(inputfile.next) then
  3807. break;
  3808. continue;
  3809. end;
  3810. #10,
  3811. #13 :
  3812. linebreak;
  3813. #9,#11,#12,' ' :
  3814. ;
  3815. else
  3816. break;
  3817. end;
  3818. readchar;
  3819. until false;
  3820. end;
  3821. procedure tscannerfile.skipuntildirective;
  3822. var
  3823. found : longint;
  3824. next_char_loaded : boolean;
  3825. begin
  3826. found:=0;
  3827. next_char_loaded:=false;
  3828. repeat
  3829. case c of
  3830. #10,
  3831. #13 :
  3832. linebreak;
  3833. #26 :
  3834. begin
  3835. reload;
  3836. if (c=#26) and not assigned(inputfile.next) then
  3837. end_of_file;
  3838. continue;
  3839. end;
  3840. '{' :
  3841. begin
  3842. if (aktcommentstyle in [comment_tp,comment_none]) then
  3843. begin
  3844. aktcommentstyle:=comment_tp;
  3845. if (comment_level=0) then
  3846. found:=1;
  3847. inc_comment_level;
  3848. end;
  3849. end;
  3850. '*' :
  3851. begin
  3852. if (aktcommentstyle=comment_oldtp) then
  3853. begin
  3854. readchar;
  3855. if c=')' then
  3856. begin
  3857. dec_comment_level;
  3858. found:=0;
  3859. aktcommentstyle:=comment_none;
  3860. end
  3861. else
  3862. next_char_loaded:=true;
  3863. end
  3864. else
  3865. found := 0;
  3866. end;
  3867. '}' :
  3868. begin
  3869. if (aktcommentstyle=comment_tp) then
  3870. begin
  3871. dec_comment_level;
  3872. if (comment_level=0) then
  3873. aktcommentstyle:=comment_none;
  3874. found:=0;
  3875. end;
  3876. end;
  3877. '$' :
  3878. begin
  3879. if found=1 then
  3880. found:=2;
  3881. end;
  3882. '''' :
  3883. if (aktcommentstyle=comment_none) then
  3884. begin
  3885. repeat
  3886. readchar;
  3887. case c of
  3888. #26 :
  3889. end_of_file;
  3890. #10,#13 :
  3891. break;
  3892. '''' :
  3893. begin
  3894. readchar;
  3895. if c<>'''' then
  3896. begin
  3897. next_char_loaded:=true;
  3898. break;
  3899. end;
  3900. end;
  3901. end;
  3902. until false;
  3903. end;
  3904. '(' :
  3905. begin
  3906. if (aktcommentstyle=comment_none) then
  3907. begin
  3908. readchar;
  3909. if c='*' then
  3910. begin
  3911. readchar;
  3912. if c='$' then
  3913. begin
  3914. found:=2;
  3915. inc_comment_level;
  3916. aktcommentstyle:=comment_oldtp;
  3917. end
  3918. else
  3919. begin
  3920. skipoldtpcomment;
  3921. next_char_loaded:=true;
  3922. end;
  3923. end
  3924. else
  3925. next_char_loaded:=true;
  3926. end
  3927. else
  3928. found:=0;
  3929. end;
  3930. '/' :
  3931. begin
  3932. if (aktcommentstyle=comment_none) then
  3933. begin
  3934. readchar;
  3935. if c='/' then
  3936. skipdelphicomment;
  3937. next_char_loaded:=true;
  3938. end
  3939. else
  3940. found:=0;
  3941. end;
  3942. else
  3943. found:=0;
  3944. end;
  3945. if next_char_loaded then
  3946. next_char_loaded:=false
  3947. else
  3948. readchar;
  3949. until (found=2);
  3950. end;
  3951. {****************************************************************************
  3952. Comment Handling
  3953. ****************************************************************************}
  3954. procedure tscannerfile.skipcomment;
  3955. begin
  3956. aktcommentstyle:=comment_tp;
  3957. readchar;
  3958. inc_comment_level;
  3959. { handle compiler switches }
  3960. if (c='$') then
  3961. handledirectives;
  3962. { handle_switches can dec comment_level, }
  3963. while (comment_level>0) do
  3964. begin
  3965. case c of
  3966. '{' :
  3967. inc_comment_level;
  3968. '}' :
  3969. dec_comment_level;
  3970. #10,#13 :
  3971. linebreak;
  3972. #26 :
  3973. begin
  3974. reload;
  3975. if (c=#26) and not assigned(inputfile.next) then
  3976. end_of_file;
  3977. continue;
  3978. end;
  3979. end;
  3980. readchar;
  3981. end;
  3982. aktcommentstyle:=comment_none;
  3983. end;
  3984. procedure tscannerfile.skipdelphicomment;
  3985. begin
  3986. aktcommentstyle:=comment_delphi;
  3987. inc_comment_level;
  3988. readchar;
  3989. { this is not supported }
  3990. if c='$' then
  3991. Message(scan_w_wrong_styled_switch);
  3992. { skip comment }
  3993. while not (c in [#10,#13,#26]) do
  3994. readchar;
  3995. dec_comment_level;
  3996. aktcommentstyle:=comment_none;
  3997. end;
  3998. procedure tscannerfile.skipoldtpcomment;
  3999. var
  4000. found : longint;
  4001. begin
  4002. aktcommentstyle:=comment_oldtp;
  4003. inc_comment_level;
  4004. { only load a char if last already processed,
  4005. was cause of bug1634 PM }
  4006. if c=#0 then
  4007. readchar;
  4008. { this is now supported }
  4009. if (c='$') then
  4010. handledirectives;
  4011. { skip comment }
  4012. while (comment_level>0) do
  4013. begin
  4014. found:=0;
  4015. repeat
  4016. case c of
  4017. #26 :
  4018. begin
  4019. reload;
  4020. if (c=#26) and not assigned(inputfile.next) then
  4021. end_of_file;
  4022. continue;
  4023. end;
  4024. #10,#13 :
  4025. begin
  4026. if found=4 then
  4027. inc_comment_level;
  4028. linebreak;
  4029. found:=0;
  4030. end;
  4031. '*' :
  4032. begin
  4033. if found=3 then
  4034. found:=4
  4035. else
  4036. found:=1;
  4037. end;
  4038. ')' :
  4039. begin
  4040. if found in [1,4] then
  4041. begin
  4042. dec_comment_level;
  4043. if comment_level=0 then
  4044. found:=2
  4045. else
  4046. found:=0;
  4047. end
  4048. else
  4049. found:=0;
  4050. end;
  4051. '(' :
  4052. begin
  4053. if found=4 then
  4054. inc_comment_level;
  4055. found:=3;
  4056. end;
  4057. else
  4058. begin
  4059. if found=4 then
  4060. inc_comment_level;
  4061. found:=0;
  4062. end;
  4063. end;
  4064. readchar;
  4065. until (found=2);
  4066. end;
  4067. aktcommentstyle:=comment_none;
  4068. end;
  4069. {****************************************************************************
  4070. Token Scanner
  4071. ****************************************************************************}
  4072. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4073. var
  4074. code : integer;
  4075. len,
  4076. low,high,mid : longint;
  4077. w : word;
  4078. m : longint;
  4079. mac : tmacro;
  4080. asciinr : string[33];
  4081. iswidestring : boolean;
  4082. label
  4083. exit_label;
  4084. begin
  4085. flushpendingswitchesstate;
  4086. { record tokens? }
  4087. if allowrecordtoken and
  4088. assigned(recordtokenbuf) then
  4089. recordtoken;
  4090. { replay tokens? }
  4091. if assigned(replaytokenbuf) then
  4092. begin
  4093. replaytoken;
  4094. goto exit_label;
  4095. end;
  4096. { was there already a token read, then return that token }
  4097. if nexttoken<>NOTOKEN then
  4098. begin
  4099. setnexttoken;
  4100. goto exit_label;
  4101. end;
  4102. { Skip all spaces and comments }
  4103. repeat
  4104. case c of
  4105. '{' :
  4106. skipcomment;
  4107. #26 :
  4108. begin
  4109. reload;
  4110. if (c=#26) and not assigned(inputfile.next) then
  4111. break;
  4112. end;
  4113. ' ',#9..#13 :
  4114. begin
  4115. {$ifdef PREPROCWRITE}
  4116. if parapreprocess then
  4117. begin
  4118. if c=#10 then
  4119. preprocfile.eolfound:=true
  4120. else
  4121. preprocfile.spacefound:=true;
  4122. end;
  4123. {$endif PREPROCWRITE}
  4124. skipspace;
  4125. end
  4126. else
  4127. break;
  4128. end;
  4129. until false;
  4130. { Save current token position, for EOF its already loaded }
  4131. if c<>#26 then
  4132. gettokenpos;
  4133. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4134. if c in ['A'..'Z','a'..'z','_'] then
  4135. begin
  4136. readstring;
  4137. token:=_ID;
  4138. idtoken:=_ID;
  4139. { keyword or any other known token,
  4140. pattern is always uppercased }
  4141. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4142. begin
  4143. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4144. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4145. while low<high do
  4146. begin
  4147. mid:=(high+low+1) shr 1;
  4148. if pattern<tokeninfo^[ttoken(mid)].str then
  4149. high:=mid-1
  4150. else
  4151. low:=mid;
  4152. end;
  4153. with tokeninfo^[ttoken(high)] do
  4154. if pattern=str then
  4155. begin
  4156. if (keyword*current_settings.modeswitches)<>[] then
  4157. if op=NOTOKEN then
  4158. token:=ttoken(high)
  4159. else
  4160. token:=op;
  4161. idtoken:=ttoken(high);
  4162. end;
  4163. end;
  4164. { Only process identifiers and not keywords }
  4165. if token=_ID then
  4166. begin
  4167. { this takes some time ... }
  4168. if (cs_support_macro in current_settings.moduleswitches) then
  4169. begin
  4170. mac:=tmacro(search_macro(pattern));
  4171. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4172. begin
  4173. if yylexcount<max_macro_nesting then
  4174. begin
  4175. mac.is_used:=true;
  4176. inc(yylexcount);
  4177. substitutemacro(pattern,mac.buftext,mac.buflen,
  4178. mac.fileinfo.line,mac.fileinfo.fileindex);
  4179. { handle empty macros }
  4180. if c=#0 then
  4181. reload;
  4182. readtoken(false);
  4183. { that's all folks }
  4184. dec(yylexcount);
  4185. exit;
  4186. end
  4187. else
  4188. Message(scan_w_macro_too_deep);
  4189. end;
  4190. end;
  4191. end;
  4192. { return token }
  4193. goto exit_label;
  4194. end
  4195. else
  4196. begin
  4197. idtoken:=_NOID;
  4198. case c of
  4199. '$' :
  4200. begin
  4201. readnumber;
  4202. token:=_INTCONST;
  4203. goto exit_label;
  4204. end;
  4205. '%' :
  4206. begin
  4207. if not(m_fpc in current_settings.modeswitches) then
  4208. Illegal_Char(c)
  4209. else
  4210. begin
  4211. readnumber;
  4212. token:=_INTCONST;
  4213. goto exit_label;
  4214. end;
  4215. end;
  4216. '&' :
  4217. begin
  4218. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4219. begin
  4220. readnumber;
  4221. if length(pattern)=1 then
  4222. begin
  4223. { does really an identifier follow? }
  4224. if not (c in ['_','A'..'Z','a'..'z']) then
  4225. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4226. readstring;
  4227. token:=_ID;
  4228. idtoken:=_ID;
  4229. end
  4230. else
  4231. token:=_INTCONST;
  4232. goto exit_label;
  4233. end
  4234. else if m_mac in current_settings.modeswitches then
  4235. begin
  4236. readchar;
  4237. token:=_AMPERSAND;
  4238. goto exit_label;
  4239. end
  4240. else
  4241. Illegal_Char(c);
  4242. end;
  4243. '0'..'9' :
  4244. begin
  4245. readnumber;
  4246. if (c in ['.','e','E']) then
  4247. begin
  4248. { first check for a . }
  4249. if c='.' then
  4250. begin
  4251. cachenexttokenpos;
  4252. readchar;
  4253. { is it a .. from a range? }
  4254. case c of
  4255. '.' :
  4256. begin
  4257. readchar;
  4258. token:=_INTCONST;
  4259. nexttoken:=_POINTPOINT;
  4260. goto exit_label;
  4261. end;
  4262. ')' :
  4263. begin
  4264. readchar;
  4265. token:=_INTCONST;
  4266. nexttoken:=_RECKKLAMMER;
  4267. goto exit_label;
  4268. end;
  4269. '0'..'9' :
  4270. begin
  4271. { insert the number after the . }
  4272. pattern:=pattern+'.';
  4273. while c in ['0'..'9'] do
  4274. begin
  4275. pattern:=pattern+c;
  4276. readchar;
  4277. end;
  4278. end;
  4279. else
  4280. begin
  4281. token:=_INTCONST;
  4282. nexttoken:=_POINT;
  4283. goto exit_label;
  4284. end;
  4285. end;
  4286. end;
  4287. { E can also follow after a point is scanned }
  4288. if c in ['e','E'] then
  4289. begin
  4290. pattern:=pattern+'E';
  4291. readchar;
  4292. if c in ['-','+'] then
  4293. begin
  4294. pattern:=pattern+c;
  4295. readchar;
  4296. end;
  4297. if not(c in ['0'..'9']) then
  4298. Illegal_Char(c);
  4299. while c in ['0'..'9'] do
  4300. begin
  4301. pattern:=pattern+c;
  4302. readchar;
  4303. end;
  4304. end;
  4305. token:=_REALNUMBER;
  4306. goto exit_label;
  4307. end;
  4308. token:=_INTCONST;
  4309. goto exit_label;
  4310. end;
  4311. ';' :
  4312. begin
  4313. readchar;
  4314. token:=_SEMICOLON;
  4315. goto exit_label;
  4316. end;
  4317. '[' :
  4318. begin
  4319. readchar;
  4320. token:=_LECKKLAMMER;
  4321. goto exit_label;
  4322. end;
  4323. ']' :
  4324. begin
  4325. readchar;
  4326. token:=_RECKKLAMMER;
  4327. goto exit_label;
  4328. end;
  4329. '(' :
  4330. begin
  4331. readchar;
  4332. case c of
  4333. '*' :
  4334. begin
  4335. c:=#0;{Signal skipoldtpcomment to reload a char }
  4336. skipoldtpcomment;
  4337. readtoken(false);
  4338. exit;
  4339. end;
  4340. '.' :
  4341. begin
  4342. readchar;
  4343. token:=_LECKKLAMMER;
  4344. goto exit_label;
  4345. end;
  4346. end;
  4347. token:=_LKLAMMER;
  4348. goto exit_label;
  4349. end;
  4350. ')' :
  4351. begin
  4352. readchar;
  4353. token:=_RKLAMMER;
  4354. goto exit_label;
  4355. end;
  4356. '+' :
  4357. begin
  4358. readchar;
  4359. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4360. begin
  4361. readchar;
  4362. token:=_PLUSASN;
  4363. goto exit_label;
  4364. end;
  4365. token:=_PLUS;
  4366. goto exit_label;
  4367. end;
  4368. '-' :
  4369. begin
  4370. readchar;
  4371. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4372. begin
  4373. readchar;
  4374. token:=_MINUSASN;
  4375. goto exit_label;
  4376. end;
  4377. token:=_MINUS;
  4378. goto exit_label;
  4379. end;
  4380. ':' :
  4381. begin
  4382. readchar;
  4383. if c='=' then
  4384. begin
  4385. readchar;
  4386. token:=_ASSIGNMENT;
  4387. goto exit_label;
  4388. end;
  4389. token:=_COLON;
  4390. goto exit_label;
  4391. end;
  4392. '*' :
  4393. begin
  4394. readchar;
  4395. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4396. begin
  4397. readchar;
  4398. token:=_STARASN;
  4399. end
  4400. else
  4401. if c='*' then
  4402. begin
  4403. readchar;
  4404. token:=_STARSTAR;
  4405. end
  4406. else
  4407. token:=_STAR;
  4408. goto exit_label;
  4409. end;
  4410. '/' :
  4411. begin
  4412. readchar;
  4413. case c of
  4414. '=' :
  4415. begin
  4416. if (cs_support_c_operators in current_settings.moduleswitches) then
  4417. begin
  4418. readchar;
  4419. token:=_SLASHASN;
  4420. goto exit_label;
  4421. end;
  4422. end;
  4423. '/' :
  4424. begin
  4425. skipdelphicomment;
  4426. readtoken(false);
  4427. exit;
  4428. end;
  4429. end;
  4430. token:=_SLASH;
  4431. goto exit_label;
  4432. end;
  4433. '|' :
  4434. if m_mac in current_settings.modeswitches then
  4435. begin
  4436. readchar;
  4437. token:=_PIPE;
  4438. goto exit_label;
  4439. end
  4440. else
  4441. Illegal_Char(c);
  4442. '=' :
  4443. begin
  4444. readchar;
  4445. token:=_EQ;
  4446. goto exit_label;
  4447. end;
  4448. '.' :
  4449. begin
  4450. readchar;
  4451. case c of
  4452. '.' :
  4453. begin
  4454. readchar;
  4455. case c of
  4456. '.' :
  4457. begin
  4458. readchar;
  4459. token:=_POINTPOINTPOINT;
  4460. goto exit_label;
  4461. end;
  4462. else
  4463. begin
  4464. token:=_POINTPOINT;
  4465. goto exit_label;
  4466. end;
  4467. end;
  4468. end;
  4469. ')' :
  4470. begin
  4471. readchar;
  4472. token:=_RECKKLAMMER;
  4473. goto exit_label;
  4474. end;
  4475. end;
  4476. token:=_POINT;
  4477. goto exit_label;
  4478. end;
  4479. '@' :
  4480. begin
  4481. readchar;
  4482. token:=_KLAMMERAFFE;
  4483. goto exit_label;
  4484. end;
  4485. ',' :
  4486. begin
  4487. readchar;
  4488. token:=_COMMA;
  4489. goto exit_label;
  4490. end;
  4491. '''','#','^' :
  4492. begin
  4493. len:=0;
  4494. cstringpattern:='';
  4495. iswidestring:=false;
  4496. if c='^' then
  4497. begin
  4498. readchar;
  4499. c:=upcase(c);
  4500. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4501. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4502. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4503. begin
  4504. token:=_CARET;
  4505. goto exit_label;
  4506. end
  4507. else
  4508. begin
  4509. inc(len);
  4510. setlength(cstringpattern,256);
  4511. if c<#64 then
  4512. cstringpattern[len]:=chr(ord(c)+64)
  4513. else
  4514. cstringpattern[len]:=chr(ord(c)-64);
  4515. readchar;
  4516. end;
  4517. end;
  4518. repeat
  4519. case c of
  4520. '#' :
  4521. begin
  4522. readchar; { read # }
  4523. case c of
  4524. '$':
  4525. begin
  4526. readchar; { read leading $ }
  4527. asciinr:='$';
  4528. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  4529. begin
  4530. asciinr:=asciinr+c;
  4531. readchar;
  4532. end;
  4533. end;
  4534. '&':
  4535. begin
  4536. readchar; { read leading $ }
  4537. asciinr:='&';
  4538. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  4539. begin
  4540. asciinr:=asciinr+c;
  4541. readchar;
  4542. end;
  4543. end;
  4544. '%':
  4545. begin
  4546. readchar; { read leading $ }
  4547. asciinr:='%';
  4548. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  4549. begin
  4550. asciinr:=asciinr+c;
  4551. readchar;
  4552. end;
  4553. end;
  4554. else
  4555. begin
  4556. asciinr:='';
  4557. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  4558. begin
  4559. asciinr:=asciinr+c;
  4560. readchar;
  4561. end;
  4562. end;
  4563. end;
  4564. val(asciinr,m,code);
  4565. if (asciinr='') or (code<>0) then
  4566. Message(scan_e_illegal_char_const)
  4567. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4568. begin
  4569. if (m>=0) and (m<=65535) then
  4570. begin
  4571. if not iswidestring then
  4572. begin
  4573. if len>0 then
  4574. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4575. else
  4576. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4577. iswidestring:=true;
  4578. len:=0;
  4579. end;
  4580. concatwidestringchar(patternw,tcompilerwidechar(m));
  4581. end
  4582. else
  4583. Message(scan_e_illegal_char_const)
  4584. end
  4585. else if iswidestring then
  4586. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4587. else
  4588. begin
  4589. if len>=length(cstringpattern) then
  4590. setlength(cstringpattern,length(cstringpattern)+256);
  4591. inc(len);
  4592. cstringpattern[len]:=chr(m);
  4593. end;
  4594. end;
  4595. '''' :
  4596. begin
  4597. repeat
  4598. readchar;
  4599. case c of
  4600. #26 :
  4601. end_of_file;
  4602. #10,#13 :
  4603. Message(scan_f_string_exceeds_line);
  4604. '''' :
  4605. begin
  4606. readchar;
  4607. if c<>'''' then
  4608. break;
  4609. end;
  4610. end;
  4611. { interpret as utf-8 string? }
  4612. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4613. begin
  4614. { convert existing string to an utf-8 string }
  4615. if not iswidestring then
  4616. begin
  4617. if len>0 then
  4618. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4619. else
  4620. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4621. iswidestring:=true;
  4622. len:=0;
  4623. end;
  4624. { four or more chars aren't handled }
  4625. if (ord(c) and $f0)=$f0 then
  4626. message(scan_e_utf8_bigger_than_65535)
  4627. { three chars }
  4628. else if (ord(c) and $e0)=$e0 then
  4629. begin
  4630. w:=ord(c) and $f;
  4631. readchar;
  4632. if (ord(c) and $c0)<>$80 then
  4633. message(scan_e_utf8_malformed);
  4634. w:=(w shl 6) or (ord(c) and $3f);
  4635. readchar;
  4636. if (ord(c) and $c0)<>$80 then
  4637. message(scan_e_utf8_malformed);
  4638. w:=(w shl 6) or (ord(c) and $3f);
  4639. concatwidestringchar(patternw,w);
  4640. end
  4641. { two chars }
  4642. else if (ord(c) and $c0)<>0 then
  4643. begin
  4644. w:=ord(c) and $1f;
  4645. readchar;
  4646. if (ord(c) and $c0)<>$80 then
  4647. message(scan_e_utf8_malformed);
  4648. w:=(w shl 6) or (ord(c) and $3f);
  4649. concatwidestringchar(patternw,w);
  4650. end
  4651. { illegal }
  4652. else if (ord(c) and $80)<>0 then
  4653. message(scan_e_utf8_malformed)
  4654. else
  4655. concatwidestringchar(patternw,tcompilerwidechar(c))
  4656. end
  4657. else if iswidestring then
  4658. begin
  4659. if current_settings.sourcecodepage=CP_UTF8 then
  4660. concatwidestringchar(patternw,ord(c))
  4661. else
  4662. concatwidestringchar(patternw,asciichar2unicode(c))
  4663. end
  4664. else
  4665. begin
  4666. if len>=length(cstringpattern) then
  4667. setlength(cstringpattern,length(cstringpattern)+256);
  4668. inc(len);
  4669. cstringpattern[len]:=c;
  4670. end;
  4671. until false;
  4672. end;
  4673. '^' :
  4674. begin
  4675. readchar;
  4676. c:=upcase(c);
  4677. if c<#64 then
  4678. c:=chr(ord(c)+64)
  4679. else
  4680. c:=chr(ord(c)-64);
  4681. if iswidestring then
  4682. concatwidestringchar(patternw,asciichar2unicode(c))
  4683. else
  4684. begin
  4685. if len>=length(cstringpattern) then
  4686. setlength(cstringpattern,length(cstringpattern)+256);
  4687. inc(len);
  4688. cstringpattern[len]:=c;
  4689. end;
  4690. readchar;
  4691. end;
  4692. else
  4693. break;
  4694. end;
  4695. until false;
  4696. { strings with length 1 become const chars }
  4697. if iswidestring then
  4698. begin
  4699. if patternw^.len=1 then
  4700. token:=_CWCHAR
  4701. else
  4702. token:=_CWSTRING;
  4703. end
  4704. else
  4705. begin
  4706. setlength(cstringpattern,len);
  4707. if length(cstringpattern)=1 then
  4708. begin
  4709. token:=_CCHAR;
  4710. pattern:=cstringpattern;
  4711. end
  4712. else
  4713. token:=_CSTRING;
  4714. end;
  4715. goto exit_label;
  4716. end;
  4717. '>' :
  4718. begin
  4719. readchar;
  4720. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4721. token:=_RSHARPBRACKET
  4722. else
  4723. begin
  4724. case c of
  4725. '=' :
  4726. begin
  4727. readchar;
  4728. token:=_GTE;
  4729. goto exit_label;
  4730. end;
  4731. '>' :
  4732. begin
  4733. readchar;
  4734. token:=_OP_SHR;
  4735. goto exit_label;
  4736. end;
  4737. '<' :
  4738. begin { >< is for a symetric diff for sets }
  4739. readchar;
  4740. token:=_SYMDIF;
  4741. goto exit_label;
  4742. end;
  4743. end;
  4744. token:=_GT;
  4745. end;
  4746. goto exit_label;
  4747. end;
  4748. '<' :
  4749. begin
  4750. readchar;
  4751. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4752. token:=_LSHARPBRACKET
  4753. else
  4754. begin
  4755. case c of
  4756. '>' :
  4757. begin
  4758. readchar;
  4759. token:=_NE;
  4760. goto exit_label;
  4761. end;
  4762. '=' :
  4763. begin
  4764. readchar;
  4765. token:=_LTE;
  4766. goto exit_label;
  4767. end;
  4768. '<' :
  4769. begin
  4770. readchar;
  4771. token:=_OP_SHL;
  4772. goto exit_label;
  4773. end;
  4774. end;
  4775. token:=_LT;
  4776. end;
  4777. goto exit_label;
  4778. end;
  4779. #26 :
  4780. begin
  4781. token:=_EOF;
  4782. checkpreprocstack;
  4783. goto exit_label;
  4784. end;
  4785. else
  4786. Illegal_Char(c);
  4787. end;
  4788. end;
  4789. exit_label:
  4790. lasttoken:=token;
  4791. end;
  4792. function tscannerfile.readpreproc:ttoken;
  4793. var
  4794. low,high,mid: longint;
  4795. optoken: ttoken;
  4796. begin
  4797. skipspace;
  4798. case c of
  4799. '_',
  4800. 'A'..'Z',
  4801. 'a'..'z' :
  4802. begin
  4803. readstring;
  4804. optoken:=_ID;
  4805. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4806. begin
  4807. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4808. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4809. while low<high do
  4810. begin
  4811. mid:=(high+low+1) shr 1;
  4812. if pattern<tokeninfo^[ttoken(mid)].str then
  4813. high:=mid-1
  4814. else
  4815. low:=mid;
  4816. end;
  4817. with tokeninfo^[ttoken(high)] do
  4818. if pattern=str then
  4819. begin
  4820. if (keyword*current_settings.modeswitches)<>[] then
  4821. if op=NOTOKEN then
  4822. optoken:=ttoken(high)
  4823. else
  4824. optoken:=op;
  4825. end;
  4826. if not (optoken in preproc_operators) then
  4827. optoken:=_ID;
  4828. end;
  4829. current_scanner.preproc_pattern:=pattern;
  4830. readpreproc:=optoken;
  4831. end;
  4832. '0'..'9' :
  4833. begin
  4834. readnumber;
  4835. if (c in ['.','e','E']) then
  4836. begin
  4837. { first check for a . }
  4838. if c='.' then
  4839. begin
  4840. readchar;
  4841. if c in ['0'..'9'] then
  4842. begin
  4843. { insert the number after the . }
  4844. pattern:=pattern+'.';
  4845. while c in ['0'..'9'] do
  4846. begin
  4847. pattern:=pattern+c;
  4848. readchar;
  4849. end;
  4850. end
  4851. else
  4852. Illegal_Char(c);
  4853. end;
  4854. { E can also follow after a point is scanned }
  4855. if c in ['e','E'] then
  4856. begin
  4857. pattern:=pattern+'E';
  4858. readchar;
  4859. if c in ['-','+'] then
  4860. begin
  4861. pattern:=pattern+c;
  4862. readchar;
  4863. end;
  4864. if not(c in ['0'..'9']) then
  4865. Illegal_Char(c);
  4866. while c in ['0'..'9'] do
  4867. begin
  4868. pattern:=pattern+c;
  4869. readchar;
  4870. end;
  4871. end;
  4872. readpreproc:=_REALNUMBER;
  4873. end
  4874. else
  4875. readpreproc:=_INTCONST;
  4876. current_scanner.preproc_pattern:=pattern;
  4877. end;
  4878. '$','%':
  4879. begin
  4880. readnumber;
  4881. current_scanner.preproc_pattern:=pattern;
  4882. readpreproc:=_INTCONST;
  4883. end;
  4884. '&' :
  4885. begin
  4886. readnumber;
  4887. if length(pattern)=1 then
  4888. begin
  4889. readstring;
  4890. readpreproc:=_ID;
  4891. end
  4892. else
  4893. readpreproc:=_INTCONST;
  4894. current_scanner.preproc_pattern:=pattern;
  4895. end;
  4896. '.' :
  4897. begin
  4898. readchar;
  4899. readpreproc:=_POINT;
  4900. end;
  4901. ',' :
  4902. begin
  4903. readchar;
  4904. readpreproc:=_COMMA;
  4905. end;
  4906. '}' :
  4907. begin
  4908. readpreproc:=_END;
  4909. end;
  4910. '(' :
  4911. begin
  4912. readchar;
  4913. readpreproc:=_LKLAMMER;
  4914. end;
  4915. ')' :
  4916. begin
  4917. readchar;
  4918. readpreproc:=_RKLAMMER;
  4919. end;
  4920. '[' :
  4921. begin
  4922. readchar;
  4923. readpreproc:=_LECKKLAMMER;
  4924. end;
  4925. ']' :
  4926. begin
  4927. readchar;
  4928. readpreproc:=_RECKKLAMMER;
  4929. end;
  4930. '+' :
  4931. begin
  4932. readchar;
  4933. readpreproc:=_PLUS;
  4934. end;
  4935. '-' :
  4936. begin
  4937. readchar;
  4938. readpreproc:=_MINUS;
  4939. end;
  4940. '*' :
  4941. begin
  4942. readchar;
  4943. readpreproc:=_STAR;
  4944. end;
  4945. '/' :
  4946. begin
  4947. readchar;
  4948. readpreproc:=_SLASH;
  4949. end;
  4950. '=' :
  4951. begin
  4952. readchar;
  4953. readpreproc:=_EQ;
  4954. end;
  4955. '>' :
  4956. begin
  4957. readchar;
  4958. if c='=' then
  4959. begin
  4960. readchar;
  4961. readpreproc:=_GTE;
  4962. end
  4963. else
  4964. readpreproc:=_GT;
  4965. end;
  4966. '<' :
  4967. begin
  4968. readchar;
  4969. case c of
  4970. '>' :
  4971. begin
  4972. readchar;
  4973. readpreproc:=_NE;
  4974. end;
  4975. '=' :
  4976. begin
  4977. readchar;
  4978. readpreproc:=_LTE;
  4979. end;
  4980. else
  4981. readpreproc:=_LT;
  4982. end;
  4983. end;
  4984. #26 :
  4985. begin
  4986. readpreproc:=_EOF;
  4987. checkpreprocstack;
  4988. end;
  4989. else
  4990. begin
  4991. Illegal_Char(c);
  4992. readpreproc:=NOTOKEN;
  4993. end;
  4994. end;
  4995. end;
  4996. function tscannerfile.asmgetcharstart : char;
  4997. begin
  4998. { return first the character already
  4999. available in c }
  5000. lastasmgetchar:=c;
  5001. result:=asmgetchar;
  5002. end;
  5003. function tscannerfile.asmgetchar : char;
  5004. begin
  5005. if lastasmgetchar<>#0 then
  5006. begin
  5007. c:=lastasmgetchar;
  5008. lastasmgetchar:=#0;
  5009. end
  5010. else
  5011. readchar;
  5012. if in_asm_string then
  5013. begin
  5014. asmgetchar:=c;
  5015. exit;
  5016. end;
  5017. repeat
  5018. case c of
  5019. // the { ... } is used in ARM assembler to define register sets, so we can't used
  5020. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  5021. // But compiler directives {$...} are allowed in ARM assembler.
  5022. '{' :
  5023. begin
  5024. {$ifdef arm}
  5025. readchar;
  5026. dec(inputpointer);
  5027. if c<>'$' then
  5028. begin
  5029. asmgetchar:='{';
  5030. exit;
  5031. end
  5032. else
  5033. {$endif arm}
  5034. skipcomment;
  5035. end;
  5036. #10,#13 :
  5037. begin
  5038. linebreak;
  5039. asmgetchar:=c;
  5040. exit;
  5041. end;
  5042. #26 :
  5043. begin
  5044. reload;
  5045. if (c=#26) and not assigned(inputfile.next) then
  5046. end_of_file;
  5047. continue;
  5048. end;
  5049. '/' :
  5050. begin
  5051. readchar;
  5052. if c='/' then
  5053. skipdelphicomment
  5054. else
  5055. begin
  5056. asmgetchar:='/';
  5057. lastasmgetchar:=c;
  5058. exit;
  5059. end;
  5060. end;
  5061. '(' :
  5062. begin
  5063. readchar;
  5064. if c='*' then
  5065. begin
  5066. c:=#0;{Signal skipoldtpcomment to reload a char }
  5067. skipoldtpcomment;
  5068. end
  5069. else
  5070. begin
  5071. asmgetchar:='(';
  5072. lastasmgetchar:=c;
  5073. exit;
  5074. end;
  5075. end;
  5076. else
  5077. begin
  5078. asmgetchar:=c;
  5079. exit;
  5080. end;
  5081. end;
  5082. until false;
  5083. end;
  5084. {*****************************************************************************
  5085. Helpers
  5086. *****************************************************************************}
  5087. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5088. begin
  5089. if dm in [directive_all, directive_turbo] then
  5090. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5091. if dm in [directive_all, directive_mac] then
  5092. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5093. end;
  5094. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5095. begin
  5096. if dm in [directive_all, directive_turbo] then
  5097. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5098. if dm in [directive_all, directive_mac] then
  5099. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5100. end;
  5101. {*****************************************************************************
  5102. Initialization
  5103. *****************************************************************************}
  5104. procedure InitScanner;
  5105. begin
  5106. InitWideString(patternw);
  5107. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5108. mac_scannerdirectives:=TFPHashObjectList.Create;
  5109. { Common directives and conditionals }
  5110. AddDirective('I',directive_all, @dir_include);
  5111. AddDirective('DEFINE',directive_all, @dir_define);
  5112. AddDirective('UNDEF',directive_all, @dir_undef);
  5113. AddConditional('IF',directive_all, @dir_if);
  5114. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5115. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5116. AddConditional('ELSE',directive_all, @dir_else);
  5117. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5118. AddConditional('ENDIF',directive_all, @dir_endif);
  5119. { Directives and conditionals for all modes except mode macpas}
  5120. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5121. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5122. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5123. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5124. AddConditional('IFEND',directive_turbo, @dir_endif);
  5125. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5126. { Directives and conditionals for mode macpas: }
  5127. AddDirective('SETC',directive_mac, @dir_setc);
  5128. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5129. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5130. AddConditional('IFC',directive_mac, @dir_if);
  5131. AddConditional('ELSEC',directive_mac, @dir_else);
  5132. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5133. AddConditional('ENDC',directive_mac, @dir_endif);
  5134. end;
  5135. procedure DoneScanner;
  5136. begin
  5137. turbo_scannerdirectives.Free;
  5138. mac_scannerdirectives.Free;
  5139. DoneWideString(patternw);
  5140. end;
  5141. end.