scanner.pas 219 KB

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