scanner.pas 199 KB

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