scanner.pas 240 KB

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