scanner.pas 182 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529
  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. { satisfy DFA because it assumes that size may be 0 and doesn't know that
  2634. recordtokenbuf.write wouldn't use tmpset in that case }
  2635. tmpset[0]:=0;
  2636. for i:=0 to size-1 do
  2637. tmpset[i]:=reverse_byte(Pbyte(@b)[i]);
  2638. recordtokenbuf.write(tmpset,size);
  2639. {$else}
  2640. recordtokenbuf.write(b,size);
  2641. {$endif}
  2642. end;
  2643. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2644. { This procedure
  2645. needs to be changed whenever
  2646. globals.tsettings type is changed,
  2647. the problem is that no error will appear
  2648. before tests with generics are tested. PM }
  2649. var
  2650. startpos, endpos : longword;
  2651. begin
  2652. { WARNING all those fields need to be in the correct
  2653. order otherwise cross_endian PPU reading will fail }
  2654. startpos:=replaytokenbuf.pos;
  2655. with asettings do
  2656. begin
  2657. alignment.procalign:=tokenreadlongint;
  2658. alignment.loopalign:=tokenreadlongint;
  2659. alignment.jumpalign:=tokenreadlongint;
  2660. alignment.constalignmin:=tokenreadlongint;
  2661. alignment.constalignmax:=tokenreadlongint;
  2662. alignment.varalignmin:=tokenreadlongint;
  2663. alignment.varalignmax:=tokenreadlongint;
  2664. alignment.localalignmin:=tokenreadlongint;
  2665. alignment.localalignmax:=tokenreadlongint;
  2666. alignment.recordalignmin:=tokenreadlongint;
  2667. alignment.recordalignmax:=tokenreadlongint;
  2668. alignment.maxCrecordalign:=tokenreadlongint;
  2669. tokenreadset(globalswitches,sizeof(globalswitches));
  2670. tokenreadset(targetswitches,sizeof(targetswitches));
  2671. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2672. tokenreadset(localswitches,sizeof(localswitches));
  2673. tokenreadset(modeswitches,sizeof(modeswitches));
  2674. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2675. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2676. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2677. tokenreadset(debugswitches,sizeof(debugswitches));
  2678. { 0: old behaviour for sets <=256 elements
  2679. >0: round to this size }
  2680. setalloc:=tokenreadshortint;
  2681. packenum:=tokenreadshortint;
  2682. packrecords:=tokenreadshortint;
  2683. maxfpuregisters:=tokenreadshortint;
  2684. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2685. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2686. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2687. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2688. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2689. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2690. { tstringencoding is word type,
  2691. thus this should be OK here }
  2692. sourcecodepage:=tstringEncoding(tokenreadword);
  2693. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2694. disabledircache:=boolean(tokenreadbyte);
  2695. {$if defined(ARM) or defined(AVR)}
  2696. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)));
  2697. {$endif defined(ARM) or defined(AVR)}
  2698. endpos:=replaytokenbuf.pos;
  2699. if endpos-startpos<>expected_size then
  2700. Comment(V_Error,'Wrong size of Settings read-in');
  2701. end;
  2702. end;
  2703. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2704. { This procedure
  2705. needs to be changed whenever
  2706. globals.tsettings type is changed,
  2707. the problem is that no error will appear
  2708. before tests with generics are tested. PM }
  2709. var
  2710. sizepos, startpos, endpos : longword;
  2711. begin
  2712. { WARNING all those fields need to be in the correct
  2713. order otherwise cross_endian PPU reading will fail }
  2714. sizepos:=recordtokenbuf.pos;
  2715. size:=0;
  2716. tokenwritesizeint(size);
  2717. startpos:=recordtokenbuf.pos;
  2718. with asettings do
  2719. begin
  2720. tokenwritelongint(alignment.procalign);
  2721. tokenwritelongint(alignment.loopalign);
  2722. tokenwritelongint(alignment.jumpalign);
  2723. tokenwritelongint(alignment.constalignmin);
  2724. tokenwritelongint(alignment.constalignmax);
  2725. tokenwritelongint(alignment.varalignmin);
  2726. tokenwritelongint(alignment.varalignmax);
  2727. tokenwritelongint(alignment.localalignmin);
  2728. tokenwritelongint(alignment.localalignmax);
  2729. tokenwritelongint(alignment.recordalignmin);
  2730. tokenwritelongint(alignment.recordalignmax);
  2731. tokenwritelongint(alignment.maxCrecordalign);
  2732. tokenwriteset(globalswitches,sizeof(globalswitches));
  2733. tokenwriteset(targetswitches,sizeof(targetswitches));
  2734. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  2735. tokenwriteset(localswitches,sizeof(localswitches));
  2736. tokenwriteset(modeswitches,sizeof(modeswitches));
  2737. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  2738. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2739. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2740. tokenwriteset(debugswitches,sizeof(debugswitches));
  2741. { 0: old behaviour for sets <=256 elements
  2742. >0: round to this size }
  2743. tokenwriteshortint(setalloc);
  2744. tokenwriteshortint(packenum);
  2745. tokenwriteshortint(packrecords);
  2746. tokenwriteshortint(maxfpuregisters);
  2747. tokenwriteenum(cputype,sizeof(tcputype));
  2748. tokenwriteenum(optimizecputype,sizeof(tcputype));
  2749. tokenwriteenum(fputype,sizeof(tfputype));
  2750. tokenwriteenum(asmmode,sizeof(tasmmode));
  2751. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  2752. tokenwriteenum(defproccall,sizeof(tproccalloption));
  2753. { tstringencoding is word type,
  2754. thus this should be OK here }
  2755. tokenwriteword(sourcecodepage);
  2756. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  2757. recordtokenbuf.write(byte(disabledircache),1);
  2758. {$if defined(ARM) or defined(AVR)}
  2759. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  2760. {$endif defined(ARM) or defined(AVR)}
  2761. endpos:=recordtokenbuf.pos;
  2762. size:=endpos-startpos;
  2763. recordtokenbuf.seek(sizepos);
  2764. tokenwritesizeint(size);
  2765. recordtokenbuf.seek(endpos);
  2766. end;
  2767. end;
  2768. procedure tscannerfile.recordtoken;
  2769. var
  2770. t : ttoken;
  2771. s : tspecialgenerictoken;
  2772. len,msgnb,copy_size : asizeint;
  2773. val : longint;
  2774. b : byte;
  2775. pmsg : pmessagestaterecord;
  2776. begin
  2777. if not assigned(recordtokenbuf) then
  2778. internalerror(200511176);
  2779. t:=_GENERICSPECIALTOKEN;
  2780. { settings changed? }
  2781. { last field pmessage is handled separately below in
  2782. ST_LOADMESSAGES }
  2783. if CompareByte(current_settings,last_settings,
  2784. sizeof(current_settings)-sizeof(pointer))<>0 then
  2785. begin
  2786. { use a special token to record it }
  2787. s:=ST_LOADSETTINGS;
  2788. writetoken(t);
  2789. recordtokenbuf.write(s,1);
  2790. copy_size:=sizeof(current_settings)-sizeof(pointer);
  2791. tokenwritesettings(current_settings,copy_size);
  2792. last_settings:=current_settings;
  2793. end;
  2794. if current_settings.pmessage<>last_message then
  2795. begin
  2796. { use a special token to record it }
  2797. s:=ST_LOADMESSAGES;
  2798. writetoken(t);
  2799. recordtokenbuf.write(s,1);
  2800. msgnb:=0;
  2801. pmsg:=current_settings.pmessage;
  2802. while assigned(pmsg) do
  2803. begin
  2804. if msgnb=high(asizeint) then
  2805. { Too many messages }
  2806. internalerror(2011090401);
  2807. inc(msgnb);
  2808. pmsg:=pmsg^.next;
  2809. end;
  2810. tokenwritesizeint(msgnb);
  2811. pmsg:=current_settings.pmessage;
  2812. while assigned(pmsg) do
  2813. begin
  2814. { What about endianess here?}
  2815. { SB: this is handled by tokenreadlongint }
  2816. val:=pmsg^.value;
  2817. tokenwritelongint(val);
  2818. val:=ord(pmsg^.state);
  2819. tokenwritelongint(val);
  2820. pmsg:=pmsg^.next;
  2821. end;
  2822. last_message:=current_settings.pmessage;
  2823. end;
  2824. { file pos changes? }
  2825. if current_tokenpos.line<>last_filepos.line then
  2826. begin
  2827. s:=ST_LINE;
  2828. writetoken(t);
  2829. recordtokenbuf.write(s,1);
  2830. tokenwritelongint(current_tokenpos.line);
  2831. last_filepos.line:=current_tokenpos.line;
  2832. end;
  2833. if current_tokenpos.column<>last_filepos.column then
  2834. begin
  2835. s:=ST_COLUMN;
  2836. writetoken(t);
  2837. { can the column be written packed? }
  2838. if current_tokenpos.column<$80 then
  2839. begin
  2840. b:=$80 or current_tokenpos.column;
  2841. recordtokenbuf.write(b,1);
  2842. end
  2843. else
  2844. begin
  2845. recordtokenbuf.write(s,1);
  2846. tokenwriteword(current_tokenpos.column);
  2847. end;
  2848. last_filepos.column:=current_tokenpos.column;
  2849. end;
  2850. if current_tokenpos.fileindex<>last_filepos.fileindex then
  2851. begin
  2852. s:=ST_FILEINDEX;
  2853. writetoken(t);
  2854. recordtokenbuf.write(s,1);
  2855. tokenwriteword(current_tokenpos.fileindex);
  2856. last_filepos.fileindex:=current_tokenpos.fileindex;
  2857. end;
  2858. writetoken(token);
  2859. if token<>_GENERICSPECIALTOKEN then
  2860. writetoken(idtoken);
  2861. case token of
  2862. _CWCHAR,
  2863. _CWSTRING :
  2864. begin
  2865. tokenwritesizeint(patternw^.len);
  2866. if patternw^.len>0 then
  2867. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2868. end;
  2869. _CSTRING:
  2870. begin
  2871. len:=length(cstringpattern);
  2872. tokenwritesizeint(len);
  2873. if len>0 then
  2874. recordtokenbuf.write(cstringpattern[1],len);
  2875. end;
  2876. _CCHAR,
  2877. _INTCONST,
  2878. _REALNUMBER :
  2879. begin
  2880. { pexpr.pas messes with pattern in case of negative integer consts,
  2881. see around line 2562 the comment of JM; remove the - before recording it
  2882. (FK)
  2883. }
  2884. if (token=_INTCONST) and (pattern[1]='-') then
  2885. delete(pattern,1,1);
  2886. recordtokenbuf.write(pattern[0],1);
  2887. recordtokenbuf.write(pattern[1],length(pattern));
  2888. end;
  2889. _ID :
  2890. begin
  2891. recordtokenbuf.write(orgpattern[0],1);
  2892. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  2893. end;
  2894. end;
  2895. end;
  2896. procedure tscannerfile.startreplaytokens(buf:tdynamicarray);
  2897. begin
  2898. if not assigned(buf) then
  2899. internalerror(200511175);
  2900. { save current token }
  2901. if token in [_CWCHAR,_CWSTRING,_CCHAR,_CSTRING,_INTCONST,_REALNUMBER,_ID] then
  2902. internalerror(200511178);
  2903. replaystack:=treplaystack.create(token,current_settings,
  2904. replaytokenbuf,replaystack);
  2905. if assigned(inputpointer) then
  2906. dec(inputpointer);
  2907. { install buffer }
  2908. replaytokenbuf:=buf;
  2909. { reload next token }
  2910. replaytokenbuf.seek(0);
  2911. replaytoken;
  2912. end;
  2913. function tscannerfile.readtoken: ttoken;
  2914. var
  2915. b,b2 : byte;
  2916. begin
  2917. replaytokenbuf.read(b,1);
  2918. if (b and $80)<>0 then
  2919. begin
  2920. replaytokenbuf.read(b2,1);
  2921. result:=ttoken(((b and $7f) shl 8) or b2);
  2922. end
  2923. else
  2924. result:=ttoken(b);
  2925. end;
  2926. procedure tscannerfile.replaytoken;
  2927. var
  2928. wlen,mesgnb,copy_size : asizeint;
  2929. specialtoken : tspecialgenerictoken;
  2930. i : byte;
  2931. pmsg,prevmsg : pmessagestaterecord;
  2932. begin
  2933. if not assigned(replaytokenbuf) then
  2934. internalerror(200511177);
  2935. { End of replay buffer? Then load the next char from the file again }
  2936. if replaytokenbuf.pos>=replaytokenbuf.size then
  2937. begin
  2938. token:=replaystack.token;
  2939. replaytokenbuf:=replaystack.tokenbuf;
  2940. { restore compiler settings }
  2941. current_settings:=replaystack.settings;
  2942. popreplaystack;
  2943. if assigned(inputpointer) then
  2944. begin
  2945. c:=inputpointer^;
  2946. inc(inputpointer);
  2947. end;
  2948. exit;
  2949. end;
  2950. repeat
  2951. { load token from the buffer }
  2952. token:=readtoken;
  2953. if token<>_GENERICSPECIALTOKEN then
  2954. idtoken:=readtoken
  2955. else
  2956. idtoken:=_NOID;
  2957. case token of
  2958. _CWCHAR,
  2959. _CWSTRING :
  2960. begin
  2961. wlen:=tokenreadsizeint;
  2962. setlengthwidestring(patternw,wlen);
  2963. if wlen>0 then
  2964. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  2965. orgpattern:='';
  2966. pattern:='';
  2967. cstringpattern:='';
  2968. end;
  2969. _CSTRING:
  2970. begin
  2971. wlen:=tokenreadsizeint;
  2972. if wlen>0 then
  2973. begin
  2974. setlength(cstringpattern,wlen);
  2975. replaytokenbuf.read(cstringpattern[1],wlen);
  2976. end
  2977. else
  2978. cstringpattern:='';
  2979. orgpattern:='';
  2980. pattern:='';
  2981. end;
  2982. _CCHAR,
  2983. _INTCONST,
  2984. _REALNUMBER :
  2985. begin
  2986. replaytokenbuf.read(pattern[0],1);
  2987. replaytokenbuf.read(pattern[1],length(pattern));
  2988. orgpattern:='';
  2989. end;
  2990. _ID :
  2991. begin
  2992. replaytokenbuf.read(orgpattern[0],1);
  2993. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  2994. pattern:=upper(orgpattern);
  2995. end;
  2996. _GENERICSPECIALTOKEN:
  2997. begin
  2998. replaytokenbuf.read(specialtoken,1);
  2999. { packed column? }
  3000. if (ord(specialtoken) and $80)<>0 then
  3001. begin
  3002. current_tokenpos.column:=ord(specialtoken) and $7f;
  3003. current_filepos:=current_tokenpos;
  3004. end
  3005. else
  3006. case specialtoken of
  3007. ST_LOADSETTINGS:
  3008. begin
  3009. copy_size:=tokenreadsizeint;
  3010. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3011. // internalerror(2011090501);
  3012. {
  3013. replaytokenbuf.read(current_settings,copy_size);
  3014. }
  3015. tokenreadsettings(current_settings,copy_size);
  3016. end;
  3017. ST_LOADMESSAGES:
  3018. begin
  3019. current_settings.pmessage:=nil;
  3020. mesgnb:=tokenreadsizeint;
  3021. if mesgnb>0 then
  3022. Comment(V_Error,'Message recordind not yet supported');
  3023. prevmsg:=nil;
  3024. for i:=1 to mesgnb do
  3025. begin
  3026. new(pmsg);
  3027. if i=1 then
  3028. current_settings.pmessage:=pmsg
  3029. else
  3030. prevmsg^.next:=pmsg;
  3031. pmsg^.value:=tokenreadlongint;
  3032. pmsg^.state:=tmsgstate(tokenreadlongint);
  3033. pmsg^.next:=nil;
  3034. prevmsg:=pmsg;
  3035. end;
  3036. end;
  3037. ST_LINE:
  3038. begin
  3039. current_tokenpos.line:=tokenreadlongint;
  3040. current_filepos:=current_tokenpos;
  3041. end;
  3042. ST_COLUMN:
  3043. begin
  3044. current_tokenpos.column:=tokenreadword;
  3045. current_filepos:=current_tokenpos;
  3046. end;
  3047. ST_FILEINDEX:
  3048. begin
  3049. current_tokenpos.fileindex:=tokenreadword;
  3050. current_filepos:=current_tokenpos;
  3051. end;
  3052. else
  3053. internalerror(2006103010);
  3054. end;
  3055. continue;
  3056. end;
  3057. end;
  3058. break;
  3059. until false;
  3060. end;
  3061. procedure tscannerfile.addfile(hp:tinputfile);
  3062. begin
  3063. saveinputfile;
  3064. { add to list }
  3065. hp.next:=inputfile;
  3066. inputfile:=hp;
  3067. { load new inputfile }
  3068. restoreinputfile;
  3069. end;
  3070. procedure tscannerfile.reload;
  3071. begin
  3072. with inputfile do
  3073. begin
  3074. { when nothing more to read then leave immediatly, so we
  3075. don't change the current_filepos and leave it point to the last
  3076. char }
  3077. if (c=#26) and (not assigned(next)) then
  3078. exit;
  3079. repeat
  3080. { still more to read?, then change the #0 to a space so its seen
  3081. as a seperator, this can't be used for macro's which can change
  3082. the place of the #0 in the buffer with tempopen }
  3083. if (c=#0) and (bufsize>0) and
  3084. not(inputfile.is_macro) and
  3085. (inputpointer-inputbuffer<bufsize) then
  3086. begin
  3087. c:=' ';
  3088. inc(inputpointer);
  3089. exit;
  3090. end;
  3091. { can we read more from this file ? }
  3092. if (c<>#26) and (not endoffile) then
  3093. begin
  3094. readbuf;
  3095. inputpointer:=buf;
  3096. inputbuffer:=buf;
  3097. inputstart:=bufstart;
  3098. { first line? }
  3099. if line_no=0 then
  3100. begin
  3101. c:=inputpointer^;
  3102. { eat utf-8 signature? }
  3103. if (ord(inputpointer^)=$ef) and
  3104. (ord((inputpointer+1)^)=$bb) and
  3105. (ord((inputpointer+2)^)=$bf) then
  3106. begin
  3107. (* we don't support including files with an UTF-8 bom
  3108. inside another file that wasn't encoded as UTF-8
  3109. already (we don't support {$codepage xxx} switches in
  3110. the middle of a file either) *)
  3111. if (current_settings.sourcecodepage<>CP_UTF8) and
  3112. not current_module.in_global then
  3113. Message(scanner_f_illegal_utf8_bom);
  3114. inc(inputpointer,3);
  3115. message(scan_c_switching_to_utf8);
  3116. current_settings.sourcecodepage:=CP_UTF8;
  3117. include(current_settings.moduleswitches,cs_explicit_codepage);
  3118. end;
  3119. line_no:=1;
  3120. if cs_asm_source in current_settings.globalswitches then
  3121. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3122. end;
  3123. end
  3124. else
  3125. begin
  3126. { load eof position in tokenpos/current_filepos }
  3127. gettokenpos;
  3128. { close file }
  3129. closeinputfile;
  3130. { no next module, than EOF }
  3131. if not assigned(inputfile.next) then
  3132. begin
  3133. c:=#26;
  3134. exit;
  3135. end;
  3136. { load next file and reopen it }
  3137. nextfile;
  3138. tempopeninputfile;
  3139. { status }
  3140. Message1(scan_t_back_in,inputfile.name);
  3141. end;
  3142. { load next char }
  3143. c:=inputpointer^;
  3144. inc(inputpointer);
  3145. until c<>#0; { if also end, then reload again }
  3146. end;
  3147. end;
  3148. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint);
  3149. var
  3150. hp : tinputfile;
  3151. begin
  3152. { save old postion }
  3153. dec(inputpointer);
  3154. tempcloseinputfile;
  3155. { create macro 'file' }
  3156. { use special name to dispose after !! }
  3157. hp:=do_openinputfile('_Macro_.'+macname);
  3158. addfile(hp);
  3159. with inputfile do
  3160. begin
  3161. setmacro(p,len);
  3162. { local buffer }
  3163. inputbuffer:=buf;
  3164. inputpointer:=buf;
  3165. inputstart:=bufstart;
  3166. ref_index:=fileindex;
  3167. end;
  3168. { reset line }
  3169. line_no:=line;
  3170. lastlinepos:=0;
  3171. lasttokenpos:=0;
  3172. nexttokenpos:=0;
  3173. { load new c }
  3174. c:=inputpointer^;
  3175. inc(inputpointer);
  3176. end;
  3177. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3178. begin
  3179. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3180. filepos.line:=line_no;
  3181. filepos.column:=tokenpos-lastlinepos;
  3182. filepos.fileindex:=inputfile.ref_index;
  3183. filepos.moduleindex:=current_module.unit_index;
  3184. end;
  3185. procedure tscannerfile.gettokenpos;
  3186. { load the values of tokenpos and lasttokenpos }
  3187. begin
  3188. do_gettokenpos(lasttokenpos,current_tokenpos);
  3189. current_filepos:=current_tokenpos;
  3190. end;
  3191. procedure tscannerfile.cachenexttokenpos;
  3192. begin
  3193. do_gettokenpos(nexttokenpos,next_filepos);
  3194. end;
  3195. procedure tscannerfile.setnexttoken;
  3196. begin
  3197. token:=nexttoken;
  3198. nexttoken:=NOTOKEN;
  3199. lasttokenpos:=nexttokenpos;
  3200. current_tokenpos:=next_filepos;
  3201. current_filepos:=current_tokenpos;
  3202. nexttokenpos:=0;
  3203. end;
  3204. procedure tscannerfile.savetokenpos;
  3205. begin
  3206. oldlasttokenpos:=lasttokenpos;
  3207. oldcurrent_filepos:=current_filepos;
  3208. oldcurrent_tokenpos:=current_tokenpos;
  3209. end;
  3210. procedure tscannerfile.restoretokenpos;
  3211. begin
  3212. lasttokenpos:=oldlasttokenpos;
  3213. current_filepos:=oldcurrent_filepos;
  3214. current_tokenpos:=oldcurrent_tokenpos;
  3215. end;
  3216. procedure tscannerfile.inc_comment_level;
  3217. begin
  3218. if (m_nested_comment in current_settings.modeswitches) then
  3219. inc(comment_level)
  3220. else
  3221. comment_level:=1;
  3222. if (comment_level>1) then
  3223. begin
  3224. savetokenpos;
  3225. gettokenpos; { update for warning }
  3226. Message1(scan_w_comment_level,tostr(comment_level));
  3227. restoretokenpos;
  3228. end;
  3229. end;
  3230. procedure tscannerfile.dec_comment_level;
  3231. begin
  3232. if (m_nested_comment in current_settings.modeswitches) then
  3233. dec(comment_level)
  3234. else
  3235. comment_level:=0;
  3236. end;
  3237. procedure tscannerfile.linebreak;
  3238. var
  3239. cur : char;
  3240. begin
  3241. with inputfile do
  3242. begin
  3243. if (byte(inputpointer^)=0) and not(endoffile) then
  3244. begin
  3245. cur:=c;
  3246. reload;
  3247. if byte(cur)+byte(c)<>23 then
  3248. dec(inputpointer);
  3249. end
  3250. else
  3251. begin
  3252. { Support all combination of #10 and #13 as line break }
  3253. if (byte(inputpointer^)+byte(c)=23) then
  3254. inc(inputpointer);
  3255. end;
  3256. { Always return #10 as line break }
  3257. c:=#10;
  3258. { increase line counters }
  3259. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3260. inc(line_no);
  3261. { update linebuffer }
  3262. if cs_asm_source in current_settings.globalswitches then
  3263. inputfile.setline(line_no,lastlinepos);
  3264. { update for status and call the show status routine,
  3265. but don't touch current_filepos ! }
  3266. savetokenpos;
  3267. gettokenpos; { update for v_status }
  3268. inc(status.compiledlines);
  3269. ShowStatus;
  3270. restoretokenpos;
  3271. end;
  3272. end;
  3273. procedure tscannerfile.illegal_char(c:char);
  3274. var
  3275. s : string;
  3276. begin
  3277. if c in [#32..#255] then
  3278. s:=''''+c+''''
  3279. else
  3280. s:='#'+tostr(ord(c));
  3281. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3282. end;
  3283. procedure tscannerfile.end_of_file;
  3284. begin
  3285. checkpreprocstack;
  3286. Message(scan_f_end_of_file);
  3287. end;
  3288. {-------------------------------------------
  3289. IF Conditional Handling
  3290. -------------------------------------------}
  3291. procedure tscannerfile.checkpreprocstack;
  3292. begin
  3293. { check for missing ifdefs }
  3294. while assigned(preprocstack) do
  3295. begin
  3296. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3297. preprocstack.owner.inputfile.name,tostr(preprocstack.line_nb));
  3298. poppreprocstack;
  3299. end;
  3300. end;
  3301. procedure tscannerfile.poppreprocstack;
  3302. var
  3303. hp : tpreprocstack;
  3304. begin
  3305. if assigned(preprocstack) then
  3306. begin
  3307. Message1(scan_c_endif_found,preprocstack.name);
  3308. hp:=preprocstack.next;
  3309. preprocstack.free;
  3310. preprocstack:=hp;
  3311. end
  3312. else
  3313. Message(scan_e_endif_without_if);
  3314. end;
  3315. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3316. var
  3317. condition: Boolean;
  3318. valuedescr: String;
  3319. begin
  3320. if (preprocstack=nil) or preprocstack.accept then
  3321. condition:=compile_time_predicate(valuedescr)
  3322. else
  3323. begin
  3324. condition:= false;
  3325. valuedescr:= '';
  3326. end;
  3327. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3328. preprocstack.name:=valuedescr;
  3329. preprocstack.line_nb:=line_no;
  3330. preprocstack.owner:=self;
  3331. if preprocstack.accept then
  3332. Message2(messid,preprocstack.name,'accepted')
  3333. else
  3334. Message2(messid,preprocstack.name,'rejected');
  3335. end;
  3336. procedure tscannerfile.elsepreprocstack;
  3337. begin
  3338. if assigned(preprocstack) and
  3339. (preprocstack.typ<>pp_else) then
  3340. begin
  3341. if (preprocstack.typ=pp_elseif) then
  3342. preprocstack.accept:=false
  3343. else
  3344. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3345. preprocstack.accept:=not preprocstack.accept;
  3346. preprocstack.typ:=pp_else;
  3347. preprocstack.line_nb:=line_no;
  3348. if preprocstack.accept then
  3349. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3350. else
  3351. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3352. end
  3353. else
  3354. Message(scan_e_endif_without_if);
  3355. end;
  3356. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3357. var
  3358. valuedescr: String;
  3359. begin
  3360. if assigned(preprocstack) and
  3361. (preprocstack.typ in [pp_if,pp_elseif]) then
  3362. begin
  3363. { when the branch is accepted we use pp_elseif so we know that
  3364. all the next branches need to be rejected. when this branch is still
  3365. not accepted then leave it at pp_if }
  3366. if (preprocstack.typ=pp_elseif) then
  3367. preprocstack.accept:=false
  3368. else if (preprocstack.typ=pp_if) and preprocstack.accept then
  3369. begin
  3370. preprocstack.accept:=false;
  3371. preprocstack.typ:=pp_elseif;
  3372. end
  3373. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  3374. and compile_time_predicate(valuedescr) then
  3375. begin
  3376. preprocstack.name:=valuedescr;
  3377. preprocstack.accept:=true;
  3378. preprocstack.typ:=pp_elseif;
  3379. end;
  3380. preprocstack.line_nb:=line_no;
  3381. if preprocstack.accept then
  3382. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3383. else
  3384. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3385. end
  3386. else
  3387. Message(scan_e_endif_without_if);
  3388. end;
  3389. procedure tscannerfile.popreplaystack;
  3390. var
  3391. hp : treplaystack;
  3392. begin
  3393. if assigned(replaystack) then
  3394. begin
  3395. hp:=replaystack.next;
  3396. replaystack.free;
  3397. replaystack:=hp;
  3398. end;
  3399. end;
  3400. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  3401. begin
  3402. savetokenpos;
  3403. repeat
  3404. current_scanner.gettokenpos;
  3405. Message1(scan_d_handling_switch,'$'+p.name);
  3406. p.proc();
  3407. { accept the text ? }
  3408. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  3409. break
  3410. else
  3411. begin
  3412. current_scanner.gettokenpos;
  3413. Message(scan_c_skipping_until);
  3414. repeat
  3415. current_scanner.skipuntildirective;
  3416. if not (m_mac in current_settings.modeswitches) then
  3417. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  3418. else
  3419. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  3420. until assigned(p) and (p.is_conditional);
  3421. current_scanner.gettokenpos;
  3422. end;
  3423. until false;
  3424. restoretokenpos;
  3425. end;
  3426. procedure tscannerfile.handledirectives;
  3427. var
  3428. t : tdirectiveitem;
  3429. hs : string;
  3430. begin
  3431. gettokenpos;
  3432. readchar; {Remove the $}
  3433. hs:=readid;
  3434. { handle empty directive }
  3435. if hs='' then
  3436. begin
  3437. Message1(scan_w_illegal_switch,'$');
  3438. exit;
  3439. end;
  3440. {$ifdef PREPROCWRITE}
  3441. if parapreprocess then
  3442. begin
  3443. t:=Get_Directive(hs);
  3444. if not(is_conditional(t) or (t=_DIR_DEFINE) or (t=_DIR_UNDEF)) then
  3445. begin
  3446. preprocfile^.AddSpace;
  3447. preprocfile^.Add('{$'+hs+current_scanner.readcomment+'}');
  3448. exit;
  3449. end;
  3450. end;
  3451. {$endif PREPROCWRITE}
  3452. { skip this directive? }
  3453. if (ignoredirectives.find(hs)<>nil) then
  3454. begin
  3455. if (comment_level>0) then
  3456. readcomment;
  3457. { we've read the whole comment }
  3458. aktcommentstyle:=comment_none;
  3459. exit;
  3460. end;
  3461. { Check for compiler switches }
  3462. while (length(hs)=1) and (c in ['-','+']) do
  3463. begin
  3464. Message1(scan_d_handling_switch,'$'+hs+c);
  3465. HandleSwitch(hs[1],c);
  3466. current_scanner.readchar; {Remove + or -}
  3467. if c=',' then
  3468. begin
  3469. current_scanner.readchar; {Remove , }
  3470. { read next switch, support $v+,$+}
  3471. hs:=current_scanner.readid;
  3472. if (hs='') then
  3473. begin
  3474. if (c='$') and (m_fpc in current_settings.modeswitches) then
  3475. begin
  3476. current_scanner.readchar; { skip $ }
  3477. hs:=current_scanner.readid;
  3478. end;
  3479. if (hs='') then
  3480. Message1(scan_w_illegal_directive,'$'+c);
  3481. end;
  3482. end
  3483. else
  3484. hs:='';
  3485. end;
  3486. { directives may follow switches after a , }
  3487. if hs<>'' then
  3488. begin
  3489. if not (m_mac in current_settings.modeswitches) then
  3490. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3491. else
  3492. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3493. if assigned(t) then
  3494. begin
  3495. if t.is_conditional then
  3496. handleconditional(t)
  3497. else
  3498. begin
  3499. Message1(scan_d_handling_switch,'$'+hs);
  3500. t.proc();
  3501. end;
  3502. end
  3503. else
  3504. begin
  3505. current_scanner.ignoredirectives.Add(hs,nil);
  3506. Message1(scan_w_illegal_directive,'$'+hs);
  3507. end;
  3508. { conditionals already read the comment }
  3509. if (current_scanner.comment_level>0) then
  3510. current_scanner.readcomment;
  3511. { we've read the whole comment }
  3512. aktcommentstyle:=comment_none;
  3513. end;
  3514. end;
  3515. procedure tscannerfile.readchar;
  3516. begin
  3517. c:=inputpointer^;
  3518. if c=#0 then
  3519. reload
  3520. else
  3521. inc(inputpointer);
  3522. end;
  3523. procedure tscannerfile.readstring;
  3524. var
  3525. i : longint;
  3526. err : boolean;
  3527. begin
  3528. err:=false;
  3529. i:=0;
  3530. repeat
  3531. case c of
  3532. '_',
  3533. '0'..'9',
  3534. 'A'..'Z' :
  3535. begin
  3536. if i<255 then
  3537. begin
  3538. inc(i);
  3539. orgpattern[i]:=c;
  3540. pattern[i]:=c;
  3541. end
  3542. else
  3543. begin
  3544. if not err then
  3545. begin
  3546. Message(scan_e_string_exceeds_255_chars);
  3547. err:=true;
  3548. end;
  3549. end;
  3550. c:=inputpointer^;
  3551. inc(inputpointer);
  3552. end;
  3553. 'a'..'z' :
  3554. begin
  3555. if i<255 then
  3556. begin
  3557. inc(i);
  3558. orgpattern[i]:=c;
  3559. pattern[i]:=chr(ord(c)-32)
  3560. end
  3561. else
  3562. begin
  3563. if not err then
  3564. begin
  3565. Message(scan_e_string_exceeds_255_chars);
  3566. err:=true;
  3567. end;
  3568. end;
  3569. c:=inputpointer^;
  3570. inc(inputpointer);
  3571. end;
  3572. #0 :
  3573. reload;
  3574. else
  3575. break;
  3576. end;
  3577. until false;
  3578. orgpattern[0]:=chr(i);
  3579. pattern[0]:=chr(i);
  3580. end;
  3581. procedure tscannerfile.readnumber;
  3582. var
  3583. base,
  3584. i : longint;
  3585. begin
  3586. case c of
  3587. '%' :
  3588. begin
  3589. readchar;
  3590. base:=2;
  3591. pattern[1]:='%';
  3592. i:=1;
  3593. end;
  3594. '&' :
  3595. begin
  3596. readchar;
  3597. base:=8;
  3598. pattern[1]:='&';
  3599. i:=1;
  3600. end;
  3601. '$' :
  3602. begin
  3603. readchar;
  3604. base:=16;
  3605. pattern[1]:='$';
  3606. i:=1;
  3607. end;
  3608. else
  3609. begin
  3610. base:=10;
  3611. i:=0;
  3612. end;
  3613. end;
  3614. while ((base>=10) and (c in ['0'..'9'])) or
  3615. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3616. ((base=8) and (c in ['0'..'7'])) or
  3617. ((base=2) and (c in ['0'..'1'])) do
  3618. begin
  3619. if i<255 then
  3620. begin
  3621. inc(i);
  3622. pattern[i]:=c;
  3623. end;
  3624. readchar;
  3625. end;
  3626. pattern[0]:=chr(i);
  3627. end;
  3628. function tscannerfile.readid:string;
  3629. begin
  3630. readstring;
  3631. readid:=pattern;
  3632. end;
  3633. function tscannerfile.readval:longint;
  3634. var
  3635. l : longint;
  3636. w : integer;
  3637. begin
  3638. readnumber;
  3639. val(pattern,l,w);
  3640. readval:=l;
  3641. end;
  3642. function tscannerfile.readcomment:string;
  3643. var
  3644. i : longint;
  3645. begin
  3646. i:=0;
  3647. repeat
  3648. case c of
  3649. '{' :
  3650. begin
  3651. if aktcommentstyle=comment_tp then
  3652. inc_comment_level;
  3653. end;
  3654. '}' :
  3655. begin
  3656. if aktcommentstyle=comment_tp then
  3657. begin
  3658. readchar;
  3659. dec_comment_level;
  3660. if comment_level=0 then
  3661. break
  3662. else
  3663. continue;
  3664. end;
  3665. end;
  3666. '*' :
  3667. begin
  3668. if aktcommentstyle=comment_oldtp then
  3669. begin
  3670. readchar;
  3671. if c=')' then
  3672. begin
  3673. readchar;
  3674. dec_comment_level;
  3675. break;
  3676. end
  3677. else
  3678. { Add both characters !!}
  3679. if (i<255) then
  3680. begin
  3681. inc(i);
  3682. readcomment[i]:='*';
  3683. if (i<255) then
  3684. begin
  3685. inc(i);
  3686. readcomment[i]:=c;
  3687. end;
  3688. end;
  3689. end
  3690. else
  3691. { Not old TP comment, so add...}
  3692. begin
  3693. if (i<255) then
  3694. begin
  3695. inc(i);
  3696. readcomment[i]:='*';
  3697. end;
  3698. end;
  3699. end;
  3700. #10,#13 :
  3701. linebreak;
  3702. #26 :
  3703. end_of_file;
  3704. else
  3705. begin
  3706. if (i<255) then
  3707. begin
  3708. inc(i);
  3709. readcomment[i]:=c;
  3710. end;
  3711. end;
  3712. end;
  3713. readchar;
  3714. until false;
  3715. readcomment[0]:=chr(i);
  3716. end;
  3717. function tscannerfile.readquotedstring:string;
  3718. var
  3719. i : longint;
  3720. msgwritten : boolean;
  3721. begin
  3722. i:=0;
  3723. msgwritten:=false;
  3724. if (c='''') then
  3725. begin
  3726. repeat
  3727. readchar;
  3728. case c of
  3729. #26 :
  3730. end_of_file;
  3731. #10,#13 :
  3732. Message(scan_f_string_exceeds_line);
  3733. '''' :
  3734. begin
  3735. readchar;
  3736. if c<>'''' then
  3737. break;
  3738. end;
  3739. end;
  3740. if i<255 then
  3741. begin
  3742. inc(i);
  3743. result[i]:=c;
  3744. end
  3745. else
  3746. begin
  3747. if not msgwritten then
  3748. begin
  3749. Message(scan_e_string_exceeds_255_chars);
  3750. msgwritten:=true;
  3751. end;
  3752. end;
  3753. until false;
  3754. end;
  3755. result[0]:=chr(i);
  3756. end;
  3757. function tscannerfile.readstate:char;
  3758. var
  3759. state : char;
  3760. begin
  3761. state:=' ';
  3762. if c=' ' then
  3763. begin
  3764. current_scanner.skipspace;
  3765. current_scanner.readid;
  3766. if pattern='ON' then
  3767. state:='+'
  3768. else
  3769. if pattern='OFF' then
  3770. state:='-';
  3771. end
  3772. else
  3773. state:=c;
  3774. if not (state in ['+','-']) then
  3775. Message(scan_e_wrong_switch_toggle);
  3776. readstate:=state;
  3777. end;
  3778. function tscannerfile.readstatedefault:char;
  3779. var
  3780. state : char;
  3781. begin
  3782. state:=' ';
  3783. if c=' ' then
  3784. begin
  3785. current_scanner.skipspace;
  3786. current_scanner.readid;
  3787. if pattern='ON' then
  3788. state:='+'
  3789. else
  3790. if pattern='OFF' then
  3791. state:='-'
  3792. else
  3793. if pattern='DEFAULT' then
  3794. state:='*';
  3795. end
  3796. else
  3797. state:=c;
  3798. if not (state in ['+','-','*']) then
  3799. Message(scan_e_wrong_switch_toggle_default);
  3800. readstatedefault:=state;
  3801. end;
  3802. procedure tscannerfile.skipspace;
  3803. begin
  3804. repeat
  3805. case c of
  3806. #26 :
  3807. begin
  3808. reload;
  3809. if (c=#26) and not assigned(inputfile.next) then
  3810. break;
  3811. continue;
  3812. end;
  3813. #10,
  3814. #13 :
  3815. linebreak;
  3816. #9,#11,#12,' ' :
  3817. ;
  3818. else
  3819. break;
  3820. end;
  3821. readchar;
  3822. until false;
  3823. end;
  3824. procedure tscannerfile.skipuntildirective;
  3825. var
  3826. found : longint;
  3827. next_char_loaded : boolean;
  3828. begin
  3829. found:=0;
  3830. next_char_loaded:=false;
  3831. repeat
  3832. case c of
  3833. #10,
  3834. #13 :
  3835. linebreak;
  3836. #26 :
  3837. begin
  3838. reload;
  3839. if (c=#26) and not assigned(inputfile.next) then
  3840. end_of_file;
  3841. continue;
  3842. end;
  3843. '{' :
  3844. begin
  3845. if (aktcommentstyle in [comment_tp,comment_none]) then
  3846. begin
  3847. aktcommentstyle:=comment_tp;
  3848. if (comment_level=0) then
  3849. found:=1;
  3850. inc_comment_level;
  3851. end;
  3852. end;
  3853. '*' :
  3854. begin
  3855. if (aktcommentstyle=comment_oldtp) then
  3856. begin
  3857. readchar;
  3858. if c=')' then
  3859. begin
  3860. dec_comment_level;
  3861. found:=0;
  3862. aktcommentstyle:=comment_none;
  3863. end
  3864. else
  3865. next_char_loaded:=true;
  3866. end
  3867. else
  3868. found := 0;
  3869. end;
  3870. '}' :
  3871. begin
  3872. if (aktcommentstyle=comment_tp) then
  3873. begin
  3874. dec_comment_level;
  3875. if (comment_level=0) then
  3876. aktcommentstyle:=comment_none;
  3877. found:=0;
  3878. end;
  3879. end;
  3880. '$' :
  3881. begin
  3882. if found=1 then
  3883. found:=2;
  3884. end;
  3885. '''' :
  3886. if (aktcommentstyle=comment_none) then
  3887. begin
  3888. repeat
  3889. readchar;
  3890. case c of
  3891. #26 :
  3892. end_of_file;
  3893. #10,#13 :
  3894. break;
  3895. '''' :
  3896. begin
  3897. readchar;
  3898. if c<>'''' then
  3899. begin
  3900. next_char_loaded:=true;
  3901. break;
  3902. end;
  3903. end;
  3904. end;
  3905. until false;
  3906. end;
  3907. '(' :
  3908. begin
  3909. if (aktcommentstyle=comment_none) then
  3910. begin
  3911. readchar;
  3912. if c='*' then
  3913. begin
  3914. readchar;
  3915. if c='$' then
  3916. begin
  3917. found:=2;
  3918. inc_comment_level;
  3919. aktcommentstyle:=comment_oldtp;
  3920. end
  3921. else
  3922. begin
  3923. skipoldtpcomment;
  3924. next_char_loaded:=true;
  3925. end;
  3926. end
  3927. else
  3928. next_char_loaded:=true;
  3929. end
  3930. else
  3931. found:=0;
  3932. end;
  3933. '/' :
  3934. begin
  3935. if (aktcommentstyle=comment_none) then
  3936. begin
  3937. readchar;
  3938. if c='/' then
  3939. skipdelphicomment;
  3940. next_char_loaded:=true;
  3941. end
  3942. else
  3943. found:=0;
  3944. end;
  3945. else
  3946. found:=0;
  3947. end;
  3948. if next_char_loaded then
  3949. next_char_loaded:=false
  3950. else
  3951. readchar;
  3952. until (found=2);
  3953. end;
  3954. {****************************************************************************
  3955. Comment Handling
  3956. ****************************************************************************}
  3957. procedure tscannerfile.skipcomment;
  3958. begin
  3959. aktcommentstyle:=comment_tp;
  3960. readchar;
  3961. inc_comment_level;
  3962. { handle compiler switches }
  3963. if (c='$') then
  3964. handledirectives;
  3965. { handle_switches can dec comment_level, }
  3966. while (comment_level>0) do
  3967. begin
  3968. case c of
  3969. '{' :
  3970. inc_comment_level;
  3971. '}' :
  3972. dec_comment_level;
  3973. #10,#13 :
  3974. linebreak;
  3975. #26 :
  3976. begin
  3977. reload;
  3978. if (c=#26) and not assigned(inputfile.next) then
  3979. end_of_file;
  3980. continue;
  3981. end;
  3982. end;
  3983. readchar;
  3984. end;
  3985. aktcommentstyle:=comment_none;
  3986. end;
  3987. procedure tscannerfile.skipdelphicomment;
  3988. begin
  3989. aktcommentstyle:=comment_delphi;
  3990. inc_comment_level;
  3991. readchar;
  3992. { this is not supported }
  3993. if c='$' then
  3994. Message(scan_w_wrong_styled_switch);
  3995. { skip comment }
  3996. while not (c in [#10,#13,#26]) do
  3997. readchar;
  3998. dec_comment_level;
  3999. aktcommentstyle:=comment_none;
  4000. end;
  4001. procedure tscannerfile.skipoldtpcomment;
  4002. var
  4003. found : longint;
  4004. begin
  4005. aktcommentstyle:=comment_oldtp;
  4006. inc_comment_level;
  4007. { only load a char if last already processed,
  4008. was cause of bug1634 PM }
  4009. if c=#0 then
  4010. readchar;
  4011. { this is now supported }
  4012. if (c='$') then
  4013. handledirectives;
  4014. { skip comment }
  4015. while (comment_level>0) do
  4016. begin
  4017. found:=0;
  4018. repeat
  4019. case c of
  4020. #26 :
  4021. begin
  4022. reload;
  4023. if (c=#26) and not assigned(inputfile.next) then
  4024. end_of_file;
  4025. continue;
  4026. end;
  4027. #10,#13 :
  4028. begin
  4029. if found=4 then
  4030. inc_comment_level;
  4031. linebreak;
  4032. found:=0;
  4033. end;
  4034. '*' :
  4035. begin
  4036. if found=3 then
  4037. found:=4
  4038. else
  4039. found:=1;
  4040. end;
  4041. ')' :
  4042. begin
  4043. if found in [1,4] then
  4044. begin
  4045. dec_comment_level;
  4046. if comment_level=0 then
  4047. found:=2
  4048. else
  4049. found:=0;
  4050. end
  4051. else
  4052. found:=0;
  4053. end;
  4054. '(' :
  4055. begin
  4056. if found=4 then
  4057. inc_comment_level;
  4058. found:=3;
  4059. end;
  4060. else
  4061. begin
  4062. if found=4 then
  4063. inc_comment_level;
  4064. found:=0;
  4065. end;
  4066. end;
  4067. readchar;
  4068. until (found=2);
  4069. end;
  4070. aktcommentstyle:=comment_none;
  4071. end;
  4072. {****************************************************************************
  4073. Token Scanner
  4074. ****************************************************************************}
  4075. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4076. var
  4077. code : integer;
  4078. len,
  4079. low,high,mid : longint;
  4080. w : word;
  4081. m : longint;
  4082. mac : tmacro;
  4083. asciinr : string[33];
  4084. iswidestring : boolean;
  4085. label
  4086. exit_label;
  4087. begin
  4088. flushpendingswitchesstate;
  4089. { record tokens? }
  4090. if allowrecordtoken and
  4091. assigned(recordtokenbuf) then
  4092. recordtoken;
  4093. { replay tokens? }
  4094. if assigned(replaytokenbuf) then
  4095. begin
  4096. replaytoken;
  4097. goto exit_label;
  4098. end;
  4099. { was there already a token read, then return that token }
  4100. if nexttoken<>NOTOKEN then
  4101. begin
  4102. setnexttoken;
  4103. goto exit_label;
  4104. end;
  4105. { Skip all spaces and comments }
  4106. repeat
  4107. case c of
  4108. '{' :
  4109. skipcomment;
  4110. #26 :
  4111. begin
  4112. reload;
  4113. if (c=#26) and not assigned(inputfile.next) then
  4114. break;
  4115. end;
  4116. ' ',#9..#13 :
  4117. begin
  4118. {$ifdef PREPROCWRITE}
  4119. if parapreprocess then
  4120. begin
  4121. if c=#10 then
  4122. preprocfile.eolfound:=true
  4123. else
  4124. preprocfile.spacefound:=true;
  4125. end;
  4126. {$endif PREPROCWRITE}
  4127. skipspace;
  4128. end
  4129. else
  4130. break;
  4131. end;
  4132. until false;
  4133. { Save current token position, for EOF its already loaded }
  4134. if c<>#26 then
  4135. gettokenpos;
  4136. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4137. if c in ['A'..'Z','a'..'z','_'] then
  4138. begin
  4139. readstring;
  4140. token:=_ID;
  4141. idtoken:=_ID;
  4142. { keyword or any other known token,
  4143. pattern is always uppercased }
  4144. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4145. begin
  4146. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4147. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4148. while low<high do
  4149. begin
  4150. mid:=(high+low+1) shr 1;
  4151. if pattern<tokeninfo^[ttoken(mid)].str then
  4152. high:=mid-1
  4153. else
  4154. low:=mid;
  4155. end;
  4156. with tokeninfo^[ttoken(high)] do
  4157. if pattern=str then
  4158. begin
  4159. if (keyword*current_settings.modeswitches)<>[] then
  4160. if op=NOTOKEN then
  4161. token:=ttoken(high)
  4162. else
  4163. token:=op;
  4164. idtoken:=ttoken(high);
  4165. end;
  4166. end;
  4167. { Only process identifiers and not keywords }
  4168. if token=_ID then
  4169. begin
  4170. { this takes some time ... }
  4171. if (cs_support_macro in current_settings.moduleswitches) then
  4172. begin
  4173. mac:=tmacro(search_macro(pattern));
  4174. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4175. begin
  4176. if yylexcount<max_macro_nesting then
  4177. begin
  4178. mac.is_used:=true;
  4179. inc(yylexcount);
  4180. substitutemacro(pattern,mac.buftext,mac.buflen,
  4181. mac.fileinfo.line,mac.fileinfo.fileindex);
  4182. { handle empty macros }
  4183. if c=#0 then
  4184. reload;
  4185. readtoken(false);
  4186. { that's all folks }
  4187. dec(yylexcount);
  4188. exit;
  4189. end
  4190. else
  4191. Message(scan_w_macro_too_deep);
  4192. end;
  4193. end;
  4194. end;
  4195. { return token }
  4196. goto exit_label;
  4197. end
  4198. else
  4199. begin
  4200. idtoken:=_NOID;
  4201. case c of
  4202. '$' :
  4203. begin
  4204. readnumber;
  4205. token:=_INTCONST;
  4206. goto exit_label;
  4207. end;
  4208. '%' :
  4209. begin
  4210. if not(m_fpc in current_settings.modeswitches) then
  4211. Illegal_Char(c)
  4212. else
  4213. begin
  4214. readnumber;
  4215. token:=_INTCONST;
  4216. goto exit_label;
  4217. end;
  4218. end;
  4219. '&' :
  4220. begin
  4221. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4222. begin
  4223. readnumber;
  4224. if length(pattern)=1 then
  4225. begin
  4226. { does really an identifier follow? }
  4227. if not (c in ['_','A'..'Z','a'..'z']) then
  4228. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4229. readstring;
  4230. token:=_ID;
  4231. idtoken:=_ID;
  4232. end
  4233. else
  4234. token:=_INTCONST;
  4235. goto exit_label;
  4236. end
  4237. else if m_mac in current_settings.modeswitches then
  4238. begin
  4239. readchar;
  4240. token:=_AMPERSAND;
  4241. goto exit_label;
  4242. end
  4243. else
  4244. Illegal_Char(c);
  4245. end;
  4246. '0'..'9' :
  4247. begin
  4248. readnumber;
  4249. if (c in ['.','e','E']) then
  4250. begin
  4251. { first check for a . }
  4252. if c='.' then
  4253. begin
  4254. cachenexttokenpos;
  4255. readchar;
  4256. { is it a .. from a range? }
  4257. case c of
  4258. '.' :
  4259. begin
  4260. readchar;
  4261. token:=_INTCONST;
  4262. nexttoken:=_POINTPOINT;
  4263. goto exit_label;
  4264. end;
  4265. ')' :
  4266. begin
  4267. readchar;
  4268. token:=_INTCONST;
  4269. nexttoken:=_RECKKLAMMER;
  4270. goto exit_label;
  4271. end;
  4272. '0'..'9' :
  4273. begin
  4274. { insert the number after the . }
  4275. pattern:=pattern+'.';
  4276. while c in ['0'..'9'] do
  4277. begin
  4278. pattern:=pattern+c;
  4279. readchar;
  4280. end;
  4281. end;
  4282. else
  4283. begin
  4284. token:=_INTCONST;
  4285. nexttoken:=_POINT;
  4286. goto exit_label;
  4287. end;
  4288. end;
  4289. end;
  4290. { E can also follow after a point is scanned }
  4291. if c in ['e','E'] then
  4292. begin
  4293. pattern:=pattern+'E';
  4294. readchar;
  4295. if c in ['-','+'] then
  4296. begin
  4297. pattern:=pattern+c;
  4298. readchar;
  4299. end;
  4300. if not(c in ['0'..'9']) then
  4301. Illegal_Char(c);
  4302. while c in ['0'..'9'] do
  4303. begin
  4304. pattern:=pattern+c;
  4305. readchar;
  4306. end;
  4307. end;
  4308. token:=_REALNUMBER;
  4309. goto exit_label;
  4310. end;
  4311. token:=_INTCONST;
  4312. goto exit_label;
  4313. end;
  4314. ';' :
  4315. begin
  4316. readchar;
  4317. token:=_SEMICOLON;
  4318. goto exit_label;
  4319. end;
  4320. '[' :
  4321. begin
  4322. readchar;
  4323. token:=_LECKKLAMMER;
  4324. goto exit_label;
  4325. end;
  4326. ']' :
  4327. begin
  4328. readchar;
  4329. token:=_RECKKLAMMER;
  4330. goto exit_label;
  4331. end;
  4332. '(' :
  4333. begin
  4334. readchar;
  4335. case c of
  4336. '*' :
  4337. begin
  4338. c:=#0;{Signal skipoldtpcomment to reload a char }
  4339. skipoldtpcomment;
  4340. readtoken(false);
  4341. exit;
  4342. end;
  4343. '.' :
  4344. begin
  4345. readchar;
  4346. token:=_LECKKLAMMER;
  4347. goto exit_label;
  4348. end;
  4349. end;
  4350. token:=_LKLAMMER;
  4351. goto exit_label;
  4352. end;
  4353. ')' :
  4354. begin
  4355. readchar;
  4356. token:=_RKLAMMER;
  4357. goto exit_label;
  4358. end;
  4359. '+' :
  4360. begin
  4361. readchar;
  4362. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4363. begin
  4364. readchar;
  4365. token:=_PLUSASN;
  4366. goto exit_label;
  4367. end;
  4368. token:=_PLUS;
  4369. goto exit_label;
  4370. end;
  4371. '-' :
  4372. begin
  4373. readchar;
  4374. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4375. begin
  4376. readchar;
  4377. token:=_MINUSASN;
  4378. goto exit_label;
  4379. end;
  4380. token:=_MINUS;
  4381. goto exit_label;
  4382. end;
  4383. ':' :
  4384. begin
  4385. readchar;
  4386. if c='=' then
  4387. begin
  4388. readchar;
  4389. token:=_ASSIGNMENT;
  4390. goto exit_label;
  4391. end;
  4392. token:=_COLON;
  4393. goto exit_label;
  4394. end;
  4395. '*' :
  4396. begin
  4397. readchar;
  4398. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4399. begin
  4400. readchar;
  4401. token:=_STARASN;
  4402. end
  4403. else
  4404. if c='*' then
  4405. begin
  4406. readchar;
  4407. token:=_STARSTAR;
  4408. end
  4409. else
  4410. token:=_STAR;
  4411. goto exit_label;
  4412. end;
  4413. '/' :
  4414. begin
  4415. readchar;
  4416. case c of
  4417. '=' :
  4418. begin
  4419. if (cs_support_c_operators in current_settings.moduleswitches) then
  4420. begin
  4421. readchar;
  4422. token:=_SLASHASN;
  4423. goto exit_label;
  4424. end;
  4425. end;
  4426. '/' :
  4427. begin
  4428. skipdelphicomment;
  4429. readtoken(false);
  4430. exit;
  4431. end;
  4432. end;
  4433. token:=_SLASH;
  4434. goto exit_label;
  4435. end;
  4436. '|' :
  4437. if m_mac in current_settings.modeswitches then
  4438. begin
  4439. readchar;
  4440. token:=_PIPE;
  4441. goto exit_label;
  4442. end
  4443. else
  4444. Illegal_Char(c);
  4445. '=' :
  4446. begin
  4447. readchar;
  4448. token:=_EQ;
  4449. goto exit_label;
  4450. end;
  4451. '.' :
  4452. begin
  4453. readchar;
  4454. case c of
  4455. '.' :
  4456. begin
  4457. readchar;
  4458. case c of
  4459. '.' :
  4460. begin
  4461. readchar;
  4462. token:=_POINTPOINTPOINT;
  4463. goto exit_label;
  4464. end;
  4465. else
  4466. begin
  4467. token:=_POINTPOINT;
  4468. goto exit_label;
  4469. end;
  4470. end;
  4471. end;
  4472. ')' :
  4473. begin
  4474. readchar;
  4475. token:=_RECKKLAMMER;
  4476. goto exit_label;
  4477. end;
  4478. end;
  4479. token:=_POINT;
  4480. goto exit_label;
  4481. end;
  4482. '@' :
  4483. begin
  4484. readchar;
  4485. token:=_KLAMMERAFFE;
  4486. goto exit_label;
  4487. end;
  4488. ',' :
  4489. begin
  4490. readchar;
  4491. token:=_COMMA;
  4492. goto exit_label;
  4493. end;
  4494. '''','#','^' :
  4495. begin
  4496. len:=0;
  4497. cstringpattern:='';
  4498. iswidestring:=false;
  4499. if c='^' then
  4500. begin
  4501. readchar;
  4502. c:=upcase(c);
  4503. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4504. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4505. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4506. begin
  4507. token:=_CARET;
  4508. goto exit_label;
  4509. end
  4510. else
  4511. begin
  4512. inc(len);
  4513. setlength(cstringpattern,256);
  4514. if c<#64 then
  4515. cstringpattern[len]:=chr(ord(c)+64)
  4516. else
  4517. cstringpattern[len]:=chr(ord(c)-64);
  4518. readchar;
  4519. end;
  4520. end;
  4521. repeat
  4522. case c of
  4523. '#' :
  4524. begin
  4525. readchar; { read # }
  4526. case c of
  4527. '$':
  4528. begin
  4529. readchar; { read leading $ }
  4530. asciinr:='$';
  4531. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=5) do
  4532. begin
  4533. asciinr:=asciinr+c;
  4534. readchar;
  4535. end;
  4536. end;
  4537. '&':
  4538. begin
  4539. readchar; { read leading $ }
  4540. asciinr:='&';
  4541. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=7) do
  4542. begin
  4543. asciinr:=asciinr+c;
  4544. readchar;
  4545. end;
  4546. end;
  4547. '%':
  4548. begin
  4549. readchar; { read leading $ }
  4550. asciinr:='%';
  4551. while (upcase(c) in ['0','1']) and (length(asciinr)<=17) do
  4552. begin
  4553. asciinr:=asciinr+c;
  4554. readchar;
  4555. end;
  4556. end;
  4557. else
  4558. begin
  4559. asciinr:='';
  4560. while (c in ['0'..'9']) and (length(asciinr)<=5) do
  4561. begin
  4562. asciinr:=asciinr+c;
  4563. readchar;
  4564. end;
  4565. end;
  4566. end;
  4567. val(asciinr,m,code);
  4568. if (asciinr='') or (code<>0) then
  4569. Message(scan_e_illegal_char_const)
  4570. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4571. begin
  4572. if (m>=0) and (m<=65535) then
  4573. begin
  4574. if not iswidestring then
  4575. begin
  4576. if len>0 then
  4577. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4578. else
  4579. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4580. iswidestring:=true;
  4581. len:=0;
  4582. end;
  4583. concatwidestringchar(patternw,tcompilerwidechar(m));
  4584. end
  4585. else
  4586. Message(scan_e_illegal_char_const)
  4587. end
  4588. else if iswidestring then
  4589. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  4590. else
  4591. begin
  4592. if len>=length(cstringpattern) then
  4593. setlength(cstringpattern,length(cstringpattern)+256);
  4594. inc(len);
  4595. cstringpattern[len]:=chr(m);
  4596. end;
  4597. end;
  4598. '''' :
  4599. begin
  4600. repeat
  4601. readchar;
  4602. case c of
  4603. #26 :
  4604. end_of_file;
  4605. #10,#13 :
  4606. Message(scan_f_string_exceeds_line);
  4607. '''' :
  4608. begin
  4609. readchar;
  4610. if c<>'''' then
  4611. break;
  4612. end;
  4613. end;
  4614. { interpret as utf-8 string? }
  4615. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  4616. begin
  4617. { convert existing string to an utf-8 string }
  4618. if not iswidestring then
  4619. begin
  4620. if len>0 then
  4621. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4622. else
  4623. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  4624. iswidestring:=true;
  4625. len:=0;
  4626. end;
  4627. { four or more chars aren't handled }
  4628. if (ord(c) and $f0)=$f0 then
  4629. message(scan_e_utf8_bigger_than_65535)
  4630. { three chars }
  4631. else if (ord(c) and $e0)=$e0 then
  4632. begin
  4633. w:=ord(c) and $f;
  4634. readchar;
  4635. if (ord(c) and $c0)<>$80 then
  4636. message(scan_e_utf8_malformed);
  4637. w:=(w shl 6) or (ord(c) and $3f);
  4638. readchar;
  4639. if (ord(c) and $c0)<>$80 then
  4640. message(scan_e_utf8_malformed);
  4641. w:=(w shl 6) or (ord(c) and $3f);
  4642. concatwidestringchar(patternw,w);
  4643. end
  4644. { two chars }
  4645. else if (ord(c) and $c0)<>0 then
  4646. begin
  4647. w:=ord(c) and $1f;
  4648. readchar;
  4649. if (ord(c) and $c0)<>$80 then
  4650. message(scan_e_utf8_malformed);
  4651. w:=(w shl 6) or (ord(c) and $3f);
  4652. concatwidestringchar(patternw,w);
  4653. end
  4654. { illegal }
  4655. else if (ord(c) and $80)<>0 then
  4656. message(scan_e_utf8_malformed)
  4657. else
  4658. concatwidestringchar(patternw,tcompilerwidechar(c))
  4659. end
  4660. else if iswidestring then
  4661. begin
  4662. if current_settings.sourcecodepage=CP_UTF8 then
  4663. concatwidestringchar(patternw,ord(c))
  4664. else
  4665. concatwidestringchar(patternw,asciichar2unicode(c))
  4666. end
  4667. else
  4668. begin
  4669. if len>=length(cstringpattern) then
  4670. setlength(cstringpattern,length(cstringpattern)+256);
  4671. inc(len);
  4672. cstringpattern[len]:=c;
  4673. end;
  4674. until false;
  4675. end;
  4676. '^' :
  4677. begin
  4678. readchar;
  4679. c:=upcase(c);
  4680. if c<#64 then
  4681. c:=chr(ord(c)+64)
  4682. else
  4683. c:=chr(ord(c)-64);
  4684. if iswidestring then
  4685. concatwidestringchar(patternw,asciichar2unicode(c))
  4686. else
  4687. begin
  4688. if len>=length(cstringpattern) then
  4689. setlength(cstringpattern,length(cstringpattern)+256);
  4690. inc(len);
  4691. cstringpattern[len]:=c;
  4692. end;
  4693. readchar;
  4694. end;
  4695. else
  4696. break;
  4697. end;
  4698. until false;
  4699. { strings with length 1 become const chars }
  4700. if iswidestring then
  4701. begin
  4702. if patternw^.len=1 then
  4703. token:=_CWCHAR
  4704. else
  4705. token:=_CWSTRING;
  4706. end
  4707. else
  4708. begin
  4709. setlength(cstringpattern,len);
  4710. if length(cstringpattern)=1 then
  4711. begin
  4712. token:=_CCHAR;
  4713. pattern:=cstringpattern;
  4714. end
  4715. else
  4716. token:=_CSTRING;
  4717. end;
  4718. goto exit_label;
  4719. end;
  4720. '>' :
  4721. begin
  4722. readchar;
  4723. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4724. token:=_RSHARPBRACKET
  4725. else
  4726. begin
  4727. case c of
  4728. '=' :
  4729. begin
  4730. readchar;
  4731. token:=_GTE;
  4732. goto exit_label;
  4733. end;
  4734. '>' :
  4735. begin
  4736. readchar;
  4737. token:=_OP_SHR;
  4738. goto exit_label;
  4739. end;
  4740. '<' :
  4741. begin { >< is for a symetric diff for sets }
  4742. readchar;
  4743. token:=_SYMDIF;
  4744. goto exit_label;
  4745. end;
  4746. end;
  4747. token:=_GT;
  4748. end;
  4749. goto exit_label;
  4750. end;
  4751. '<' :
  4752. begin
  4753. readchar;
  4754. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  4755. token:=_LSHARPBRACKET
  4756. else
  4757. begin
  4758. case c of
  4759. '>' :
  4760. begin
  4761. readchar;
  4762. token:=_NE;
  4763. goto exit_label;
  4764. end;
  4765. '=' :
  4766. begin
  4767. readchar;
  4768. token:=_LTE;
  4769. goto exit_label;
  4770. end;
  4771. '<' :
  4772. begin
  4773. readchar;
  4774. token:=_OP_SHL;
  4775. goto exit_label;
  4776. end;
  4777. end;
  4778. token:=_LT;
  4779. end;
  4780. goto exit_label;
  4781. end;
  4782. #26 :
  4783. begin
  4784. token:=_EOF;
  4785. checkpreprocstack;
  4786. goto exit_label;
  4787. end;
  4788. else
  4789. Illegal_Char(c);
  4790. end;
  4791. end;
  4792. exit_label:
  4793. lasttoken:=token;
  4794. end;
  4795. function tscannerfile.readpreproc:ttoken;
  4796. var
  4797. low,high,mid: longint;
  4798. optoken: ttoken;
  4799. begin
  4800. skipspace;
  4801. case c of
  4802. '_',
  4803. 'A'..'Z',
  4804. 'a'..'z' :
  4805. begin
  4806. readstring;
  4807. optoken:=_ID;
  4808. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4809. begin
  4810. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4811. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4812. while low<high do
  4813. begin
  4814. mid:=(high+low+1) shr 1;
  4815. if pattern<tokeninfo^[ttoken(mid)].str then
  4816. high:=mid-1
  4817. else
  4818. low:=mid;
  4819. end;
  4820. with tokeninfo^[ttoken(high)] do
  4821. if pattern=str then
  4822. begin
  4823. if (keyword*current_settings.modeswitches)<>[] then
  4824. if op=NOTOKEN then
  4825. optoken:=ttoken(high)
  4826. else
  4827. optoken:=op;
  4828. end;
  4829. if not (optoken in preproc_operators) then
  4830. optoken:=_ID;
  4831. end;
  4832. current_scanner.preproc_pattern:=pattern;
  4833. readpreproc:=optoken;
  4834. end;
  4835. '0'..'9' :
  4836. begin
  4837. readnumber;
  4838. if (c in ['.','e','E']) then
  4839. begin
  4840. { first check for a . }
  4841. if c='.' then
  4842. begin
  4843. readchar;
  4844. if c in ['0'..'9'] then
  4845. begin
  4846. { insert the number after the . }
  4847. pattern:=pattern+'.';
  4848. while c in ['0'..'9'] do
  4849. begin
  4850. pattern:=pattern+c;
  4851. readchar;
  4852. end;
  4853. end
  4854. else
  4855. Illegal_Char(c);
  4856. end;
  4857. { E can also follow after a point is scanned }
  4858. if c in ['e','E'] then
  4859. begin
  4860. pattern:=pattern+'E';
  4861. readchar;
  4862. if c in ['-','+'] then
  4863. begin
  4864. pattern:=pattern+c;
  4865. readchar;
  4866. end;
  4867. if not(c in ['0'..'9']) then
  4868. Illegal_Char(c);
  4869. while c in ['0'..'9'] do
  4870. begin
  4871. pattern:=pattern+c;
  4872. readchar;
  4873. end;
  4874. end;
  4875. readpreproc:=_REALNUMBER;
  4876. end
  4877. else
  4878. readpreproc:=_INTCONST;
  4879. current_scanner.preproc_pattern:=pattern;
  4880. end;
  4881. '$','%':
  4882. begin
  4883. readnumber;
  4884. current_scanner.preproc_pattern:=pattern;
  4885. readpreproc:=_INTCONST;
  4886. end;
  4887. '&' :
  4888. begin
  4889. readnumber;
  4890. if length(pattern)=1 then
  4891. begin
  4892. readstring;
  4893. readpreproc:=_ID;
  4894. end
  4895. else
  4896. readpreproc:=_INTCONST;
  4897. current_scanner.preproc_pattern:=pattern;
  4898. end;
  4899. '.' :
  4900. begin
  4901. readchar;
  4902. readpreproc:=_POINT;
  4903. end;
  4904. ',' :
  4905. begin
  4906. readchar;
  4907. readpreproc:=_COMMA;
  4908. end;
  4909. '}' :
  4910. begin
  4911. readpreproc:=_END;
  4912. end;
  4913. '(' :
  4914. begin
  4915. readchar;
  4916. readpreproc:=_LKLAMMER;
  4917. end;
  4918. ')' :
  4919. begin
  4920. readchar;
  4921. readpreproc:=_RKLAMMER;
  4922. end;
  4923. '[' :
  4924. begin
  4925. readchar;
  4926. readpreproc:=_LECKKLAMMER;
  4927. end;
  4928. ']' :
  4929. begin
  4930. readchar;
  4931. readpreproc:=_RECKKLAMMER;
  4932. end;
  4933. '+' :
  4934. begin
  4935. readchar;
  4936. readpreproc:=_PLUS;
  4937. end;
  4938. '-' :
  4939. begin
  4940. readchar;
  4941. readpreproc:=_MINUS;
  4942. end;
  4943. '*' :
  4944. begin
  4945. readchar;
  4946. readpreproc:=_STAR;
  4947. end;
  4948. '/' :
  4949. begin
  4950. readchar;
  4951. readpreproc:=_SLASH;
  4952. end;
  4953. '=' :
  4954. begin
  4955. readchar;
  4956. readpreproc:=_EQ;
  4957. end;
  4958. '>' :
  4959. begin
  4960. readchar;
  4961. if c='=' then
  4962. begin
  4963. readchar;
  4964. readpreproc:=_GTE;
  4965. end
  4966. else
  4967. readpreproc:=_GT;
  4968. end;
  4969. '<' :
  4970. begin
  4971. readchar;
  4972. case c of
  4973. '>' :
  4974. begin
  4975. readchar;
  4976. readpreproc:=_NE;
  4977. end;
  4978. '=' :
  4979. begin
  4980. readchar;
  4981. readpreproc:=_LTE;
  4982. end;
  4983. else
  4984. readpreproc:=_LT;
  4985. end;
  4986. end;
  4987. #26 :
  4988. begin
  4989. readpreproc:=_EOF;
  4990. checkpreprocstack;
  4991. end;
  4992. else
  4993. begin
  4994. Illegal_Char(c);
  4995. readpreproc:=NOTOKEN;
  4996. end;
  4997. end;
  4998. end;
  4999. function tscannerfile.asmgetcharstart : char;
  5000. begin
  5001. { return first the character already
  5002. available in c }
  5003. lastasmgetchar:=c;
  5004. result:=asmgetchar;
  5005. end;
  5006. function tscannerfile.asmgetchar : char;
  5007. begin
  5008. if lastasmgetchar<>#0 then
  5009. begin
  5010. c:=lastasmgetchar;
  5011. lastasmgetchar:=#0;
  5012. end
  5013. else
  5014. readchar;
  5015. if in_asm_string then
  5016. begin
  5017. asmgetchar:=c;
  5018. exit;
  5019. end;
  5020. repeat
  5021. case c of
  5022. // the { ... } is used in ARM assembler to define register sets, so we can't used
  5023. // it as comment, either (* ... *), /* ... */ or // ... should be used instead.
  5024. // But compiler directives {$...} are allowed in ARM assembler.
  5025. '{' :
  5026. begin
  5027. {$ifdef arm}
  5028. readchar;
  5029. dec(inputpointer);
  5030. if c<>'$' then
  5031. begin
  5032. asmgetchar:='{';
  5033. exit;
  5034. end
  5035. else
  5036. {$endif arm}
  5037. skipcomment;
  5038. end;
  5039. #10,#13 :
  5040. begin
  5041. linebreak;
  5042. asmgetchar:=c;
  5043. exit;
  5044. end;
  5045. #26 :
  5046. begin
  5047. reload;
  5048. if (c=#26) and not assigned(inputfile.next) then
  5049. end_of_file;
  5050. continue;
  5051. end;
  5052. '/' :
  5053. begin
  5054. readchar;
  5055. if c='/' then
  5056. skipdelphicomment
  5057. else
  5058. begin
  5059. asmgetchar:='/';
  5060. lastasmgetchar:=c;
  5061. exit;
  5062. end;
  5063. end;
  5064. '(' :
  5065. begin
  5066. readchar;
  5067. if c='*' then
  5068. begin
  5069. c:=#0;{Signal skipoldtpcomment to reload a char }
  5070. skipoldtpcomment;
  5071. end
  5072. else
  5073. begin
  5074. asmgetchar:='(';
  5075. lastasmgetchar:=c;
  5076. exit;
  5077. end;
  5078. end;
  5079. else
  5080. begin
  5081. asmgetchar:=c;
  5082. exit;
  5083. end;
  5084. end;
  5085. until false;
  5086. end;
  5087. {*****************************************************************************
  5088. Helpers
  5089. *****************************************************************************}
  5090. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5091. begin
  5092. if dm in [directive_all, directive_turbo] then
  5093. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5094. if dm in [directive_all, directive_mac] then
  5095. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5096. end;
  5097. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5098. begin
  5099. if dm in [directive_all, directive_turbo] then
  5100. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5101. if dm in [directive_all, directive_mac] then
  5102. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5103. end;
  5104. {*****************************************************************************
  5105. Initialization
  5106. *****************************************************************************}
  5107. procedure InitScanner;
  5108. begin
  5109. InitWideString(patternw);
  5110. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5111. mac_scannerdirectives:=TFPHashObjectList.Create;
  5112. { Common directives and conditionals }
  5113. AddDirective('I',directive_all, @dir_include);
  5114. AddDirective('DEFINE',directive_all, @dir_define);
  5115. AddDirective('UNDEF',directive_all, @dir_undef);
  5116. AddConditional('IF',directive_all, @dir_if);
  5117. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5118. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5119. AddConditional('ELSE',directive_all, @dir_else);
  5120. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5121. AddConditional('ENDIF',directive_all, @dir_endif);
  5122. { Directives and conditionals for all modes except mode macpas}
  5123. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5124. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5125. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5126. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5127. AddConditional('IFEND',directive_turbo, @dir_endif);
  5128. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5129. { Directives and conditionals for mode macpas: }
  5130. AddDirective('SETC',directive_mac, @dir_setc);
  5131. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5132. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5133. AddConditional('IFC',directive_mac, @dir_if);
  5134. AddConditional('ELSEC',directive_mac, @dir_else);
  5135. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5136. AddConditional('ENDC',directive_mac, @dir_endif);
  5137. end;
  5138. procedure DoneScanner;
  5139. begin
  5140. turbo_scannerdirectives.Free;
  5141. mac_scannerdirectives.Free;
  5142. DoneWideString(patternw);
  5143. end;
  5144. end.