scanner.pas 220 KB

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