2
0

scanner.pas 186 KB

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