FoxSemanticChecker.Mod 400 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  127. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  128. BEGIN
  129. IF diagnostics # NIL THEN
  130. Basic.GetErrorMessage(code,message,errorMessage);
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  134. END;
  135. error := TRUE;
  136. END Error;
  137. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  138. VAR errModule: SyntaxTree.Module;
  139. BEGIN
  140. IF diagnostics # NIL THEN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  143. END;
  144. END Warning;
  145. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  146. VAR errorMessage: ARRAY 256 OF CHAR;
  147. BEGIN
  148. IF diagnostics # NIL THEN
  149. Basic.Concat(errorMessage,msg," ", msg2);
  150. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  151. END;
  152. error := TRUE;
  153. END ErrorSS;
  154. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  155. VAR msg, msg2: ARRAY 256 OF CHAR;
  156. BEGIN
  157. IF diagnostics # NIL THEN
  158. COPY(msg1, msg);
  159. Strings.Append(msg, " = ");
  160. Basic.GetString(s, msg2);
  161. Strings.Append(msg, msg2);
  162. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  163. END;
  164. END InfoSS;
  165. (*** symbol lookup ***)
  166. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  167. **)
  168. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  169. VAR
  170. scope,baseScope: SyntaxTree.Scope;
  171. symbol, s: SyntaxTree.Symbol;
  172. ownerRecord,base: SyntaxTree.RecordType;
  173. BEGIN
  174. scope := inScope;
  175. symbol := NIL;
  176. WHILE (scope # NIL) & (symbol = NIL) DO
  177. symbol := scope.FindSymbol(name);
  178. s := NIL;
  179. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  180. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  181. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  182. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  183. END;
  184. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  185. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  186. ELSE
  187. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  188. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  189. symbol.MarkUsed;
  190. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  191. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  192. base := RecordBase(ownerRecord);
  193. IF (base # NIL) THEN
  194. baseScope := base.recordScope;
  195. symbol := Find(baseScope,name,FALSE);
  196. ELSE
  197. symbol := NIL;
  198. END;
  199. ELSE
  200. symbol := NIL;
  201. END;
  202. END;
  203. END;
  204. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  205. END;
  206. IF (symbol # NIL) THEN
  207. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  208. ASSERT(phase = DeclarationPhase);
  209. ResolveSymbol(symbol)
  210. END;
  211. symbol.MarkUsed;
  212. END;
  213. RETURN symbol
  214. END Find;
  215. (*** types ***)
  216. (** find type declaration with name qualifiedIdentifier and return resolved type
  217. - check qualified identifier prefix, set scope to module scope if appropriate
  218. - check suffix in scope
  219. **)
  220. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  221. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  222. BEGIN
  223. result := NIL;
  224. prevScope := currentScope;
  225. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  226. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  227. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  228. IF symbol(SyntaxTree.Import).module = NIL THEN
  229. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  230. result := SyntaxTree.invalidType;
  231. symbol := NIL;
  232. ELSE
  233. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  234. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  235. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  236. IF VerboseErrorMessage THEN
  237. Printout.Info("scope", currentScope);
  238. Printout.Info("symbol", symbol);
  239. END;
  240. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  241. END;
  242. END;
  243. ELSE
  244. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  245. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  246. symbol := NIL;
  247. END;
  248. ELSE
  249. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  250. IF symbol = NIL THEN
  251. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  252. IF VerboseErrorMessage THEN
  253. Printout.Info("Qualident",qualifiedIdentifier);
  254. Printout.Info("in scope",currentScope) ;
  255. END;
  256. END;
  257. END;
  258. IF symbol = NIL THEN (* error already handled *)
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  262. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  263. typeDeclaration := NIL;
  264. result := SyntaxTree.invalidType;
  265. ELSE
  266. currentScope := symbol.scope;
  267. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  268. result := ResolveType(typeDeclaration.declaredType);
  269. symbol.MarkUsed;
  270. ASSERT(result # NIL);
  271. END;
  272. currentScope := prevScope;
  273. RETURN result
  274. END ResolveNamedType;
  275. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  276. If node is currently being resolved then emit a cyclic definition error.
  277. Return TRUE only if node is fully resolved.
  278. **)
  279. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  280. VAR result: BOOLEAN;
  281. BEGIN
  282. IF SyntaxTree.Resolved IN x.state THEN
  283. result := FALSE
  284. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  285. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  286. result := FALSE;
  287. ELSE
  288. result := TRUE;
  289. x.SetState(SyntaxTree.BeingResolved)
  290. END;
  291. RETURN result
  292. END TypeNeedsResolution;
  293. (** Return invalid type if x is currently being resolved, return x otherwise**)
  294. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  295. BEGIN
  296. IF SyntaxTree.Resolved IN x.state THEN
  297. RETURN x
  298. ELSE
  299. RETURN SyntaxTree.invalidType
  300. END;
  301. END ResolvedType;
  302. PROCEDURE VisitType(x: SyntaxTree.Type);
  303. BEGIN
  304. ASSERT(x = SyntaxTree.invalidType);
  305. END VisitType;
  306. (** resolve basic type **)
  307. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  308. BEGIN
  309. IF TypeNeedsResolution(x) THEN
  310. x.SetState(SyntaxTree.Resolved);
  311. END;
  312. resolvedType := ResolvedType(x)
  313. END VisitBasicType;
  314. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitByteType;
  318. (** resolve character type **)
  319. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitCharacterType;
  323. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitBooleanType;
  327. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitSetType;
  331. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitAddressType;
  335. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitSizeType;
  339. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitAnyType;
  343. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitObjectType;
  347. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitNilType;
  351. (** resolve integer type **)
  352. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitIntegerType;
  356. (** resolve real type **)
  357. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitFloatType;
  361. (** resolve complex type **)
  362. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  363. BEGIN
  364. VisitBasicType(x);
  365. END VisitComplexType;
  366. (**
  367. resolve string type: nothing to be done
  368. **)
  369. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  370. BEGIN
  371. IF TypeNeedsResolution(x) THEN
  372. x.SetState(SyntaxTree.Resolved);
  373. END;
  374. resolvedType := ResolvedType(x)
  375. END VisitStringType;
  376. (**
  377. check enumeration scope: enter symbols and check for duplicate names
  378. **)
  379. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  380. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  381. BEGIN
  382. prevScope := currentScope;
  383. currentScope := x;
  384. e := x.firstConstant;
  385. WHILE (e # NIL) DO
  386. Register(e,x,FALSE);
  387. IF SymbolNeedsResolution(e) THEN
  388. IF e.value # NIL THEN
  389. value := ConstantExpression(e.value);
  390. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  391. ELSE
  392. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  393. value.SetType(x.ownerEnumeration);
  394. END;
  395. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  396. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  397. IF nextHighest > highest THEN highest := nextHighest END;
  398. END;
  399. e.SetValue(value);
  400. CheckSymbolVisibility(e);
  401. e.SetType(x.ownerEnumeration);
  402. e.SetState(SyntaxTree.Resolved);
  403. END;
  404. e := e.nextConstant;
  405. END;
  406. currentScope := prevScope;
  407. END CheckEnumerationScope;
  408. (**
  409. resolve enumeration type: check enumeration scope
  410. **)
  411. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  412. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  413. lowest, highest: LONGINT;
  414. BEGIN
  415. IF TypeNeedsResolution(x) THEN
  416. IF x.enumerationBase # NIL THEN
  417. position := x.enumerationBase.position;
  418. baseType := ResolveType(x.enumerationBase);
  419. resolved := baseType.resolved;
  420. baseScope := NIL;
  421. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  422. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  423. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  424. ELSE
  425. enumerationBase := resolved(SyntaxTree.EnumerationType);
  426. lowest := enumerationBase.rangeHighest+1;
  427. END;
  428. x.SetEnumerationBase(baseType);
  429. ELSE lowest := 0;
  430. END;
  431. highest := lowest-1;
  432. CheckEnumerationScope(x.enumerationScope, highest);
  433. x.SetRange(lowest, highest);
  434. x.SetState(SyntaxTree.Resolved);
  435. END;
  436. resolvedType := ResolvedType(x);
  437. END VisitEnumerationType;
  438. (**
  439. resolve range type: nothing to be done
  440. **)
  441. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  442. BEGIN
  443. IF TypeNeedsResolution(x) THEN
  444. x.SetState(SyntaxTree.Resolved);
  445. END;
  446. resolvedType := ResolvedType(x)
  447. END VisitRangeType;
  448. (**
  449. resolve qualified type
  450. - find and resolve named type and set resolved type
  451. **)
  452. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  453. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  454. BEGIN
  455. IF TypeNeedsResolution(x) THEN
  456. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  457. x.SetResolved(type.resolved);
  458. x.SetState(SyntaxTree.Resolved);
  459. x.SetTypeDeclaration (typeDeclaration);
  460. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  461. x.SetResolved(SyntaxTree.invalidType);
  462. END;
  463. resolvedType := x;
  464. END VisitQualifiedType;
  465. (**
  466. resolve array type
  467. - check base type
  468. - array of math array forbidden
  469. - static array of open array forbidden
  470. **)
  471. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  472. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  473. BEGIN
  474. IF TypeNeedsResolution(x) THEN
  475. x.SetArrayBase(ResolveType(x.arrayBase));
  476. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  477. arrayBase := x.arrayBase.resolved;
  478. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  479. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  480. pointerType.SetPointerBase(arrayBase);
  481. pointerType.SetHidden(TRUE);
  482. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  483. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  484. ELSE
  485. x.SetArrayBase(pointerType);
  486. END;
  487. END;
  488. IF x.length # NIL THEN
  489. variableAccessed := FALSE;
  490. e := ResolveExpression(x.length);
  491. IF (e.resolved = NIL) THEN
  492. IF variableAccessed THEN
  493. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  494. END;
  495. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  496. ELSE
  497. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  498. END;
  499. END;
  500. IF arrayBase IS SyntaxTree.ArrayType THEN
  501. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  502. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  503. END;
  504. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  505. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  506. END;
  507. x.SetHasPointers(arrayBase.hasPointers);
  508. x.SetState(SyntaxTree.Resolved);
  509. END;
  510. resolvedType := ResolvedType(x);
  511. END VisitArrayType;
  512. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  513. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  514. BEGIN
  515. module := currentScope.ownerModule;
  516. IF module.name=name THEN
  517. (* do nothing *)
  518. ELSE
  519. moduleScope := module.moduleScope;
  520. import := moduleScope.FindImport(name);
  521. IF import = NIL THEN
  522. import := SyntaxTree.NewImport(position,name,name,TRUE);
  523. moduleScope.AddImport(import);
  524. Register(import,moduleScope,FALSE);
  525. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  526. VisitImport(import);
  527. ELSIF import.direct=FALSE THEN
  528. import.SetScope(module.moduleScope);
  529. import.SetDirect(TRUE);
  530. IF moduleScope.FindSymbol(import.name) = NIL THEN
  531. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  532. duplicate.SetContext(import.context);
  533. duplicate.SetModule(import.module);
  534. Register(duplicate,moduleScope,TRUE);
  535. VisitImport(duplicate);
  536. END;
  537. END;
  538. import.MarkUsed
  539. END;
  540. END ImportModule;
  541. (**
  542. resolve math array type
  543. - check base type
  544. - open math array of array forbidden
  545. - math array of tensor forbidden
  546. - static array of open array forbidden
  547. **)
  548. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  549. VAR arrayBase: SyntaxTree.Type;
  550. BEGIN
  551. IF TypeNeedsResolution(x) THEN
  552. x.SetArrayBase(ResolveType(x.arrayBase));
  553. IF x.length # NIL THEN
  554. x.SetLength(ConstantIntegerGeq0(x.length));
  555. END;
  556. arrayBase := x.arrayBase;
  557. IF arrayBase # NIL THEN
  558. arrayBase := arrayBase.resolved;
  559. IF arrayBase = SyntaxTree.invalidType THEN
  560. (* error already handled *)
  561. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  562. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  563. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  564. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  565. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  566. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  567. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  568. END;
  569. END;
  570. IF x.form = SyntaxTree.Static THEN
  571. x.SetIncrement(system.SizeOf(arrayBase));
  572. END;
  573. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  574. END;
  575. x.SetState(SyntaxTree.Resolved);
  576. END;
  577. resolvedType := ResolvedType(x);
  578. END VisitMathArrayType;
  579. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  580. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  581. (1) Rec = RECORD ... END; Ptr <---> Rec
  582. Ptr = POINTER TO Rec; ^ |
  583. | |
  584. TypeDesc TypeDesc
  585. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  586. ^ /
  587. | /
  588. TypeDesc <-- /
  589. *)
  590. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  591. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  592. BEGIN
  593. Strings.IntToStr(x.position,number);
  594. COPY(prefix,name);
  595. Strings.Append(name,"@");
  596. Strings.Append(name,number);
  597. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  598. typeDeclaration.SetDeclaredType(x);
  599. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  600. x.SetTypeDeclaration(typeDeclaration);
  601. currentScope.AddTypeDeclaration(typeDeclaration);
  602. typeDeclaration.SetScope(currentScope);
  603. END AnonymousTypeDeclaration;
  604. (**
  605. deferred pointer type resolving
  606. - resolve base type
  607. - check that base type is a record or array type
  608. - if error then set base type to invalid type
  609. **)
  610. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  611. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  612. BEGIN
  613. ASSERT(type.pointerBase # NIL);
  614. position := type.pointerBase.position;
  615. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  616. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  617. (* not for pointers, a type is needed for the records only
  618. IF type.typeDeclaration = NIL THEN
  619. AnonymousTypeDeclaration(type);
  620. END;
  621. *)
  622. END;
  623. resolved := ResolveType(type.pointerBase);
  624. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  625. type.SetPointerBase(resolved);
  626. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  627. recordType := resolved.resolved(SyntaxTree.RecordType);
  628. IF recordType.isObject & (recordType.baseType # NIL) THEN
  629. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  630. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  631. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  632. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  633. END;
  634. END;
  635. END;
  636. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  637. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  638. END;
  639. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  640. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  641. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  642. ELS
  643. *)
  644. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  645. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  646. END;
  647. END;
  648. ELSE
  649. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  650. type.SetPointerBase(SyntaxTree.invalidType)
  651. END
  652. END FixPointerType;
  653. (**
  654. resolve pointer type
  655. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  656. **)
  657. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  658. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  659. modifiers: SyntaxTree.Modifier; position: LONGINT;
  660. BEGIN
  661. IF TypeNeedsResolution(x) THEN
  662. modifiers := x.modifiers;
  663. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  664. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  665. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  666. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  667. (* inheritance cycle check
  668. example:
  669. A=POINTER TO RECORD(B) END;
  670. B=POINTER TO RECORD(A) END;
  671. *)
  672. IF x.pointerBase IS SyntaxTree.RecordType THEN
  673. recordType := x.pointerBase(SyntaxTree.RecordType);
  674. recordBaseType := ResolveType(recordType.baseType);
  675. recordType.SetBaseType(recordBaseType);
  676. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  677. END;
  678. CheckModifiers(modifiers, TRUE);
  679. typeFixes.Add(x,currentScope);
  680. x.SetState(SyntaxTree.Resolved);
  681. END;
  682. resolvedType := ResolvedType(x)
  683. END VisitPointerType;
  684. (**
  685. resolve port type
  686. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  687. **)
  688. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  689. VAR value: LONGINT;
  690. BEGIN
  691. IF TypeNeedsResolution(x) THEN
  692. x.SetCellsAreObjects(cellsAreObjects);
  693. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  694. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  695. x.SetSize(value)
  696. ELSE
  697. x.SetSize(system.SizeOf(system.longintType));
  698. END;
  699. x.SetState(SyntaxTree.Resolved);
  700. END;
  701. resolvedType := ResolvedType(x)
  702. END VisitPortType;
  703. (**
  704. deferred procedure type resolving
  705. - resolve return type
  706. - traverse and resolve parameters
  707. **)
  708. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  709. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  710. BEGIN
  711. resolved := ResolveType(procedureType.returnType);
  712. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  713. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  714. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  715. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  716. END;
  717. procedureType.SetReturnType(resolved);
  718. IF (resolved # NIL) THEN
  719. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  720. parameter.SetType(procedureType.returnType);
  721. parameter.SetAccess(SyntaxTree.Hidden);
  722. parameter.SetUntraced(procedureType.hasUntracedReturn);
  723. VisitParameter(parameter);
  724. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  725. END;
  726. (* process parameters *)
  727. parameter :=procedureType.firstParameter;
  728. WHILE (parameter # NIL) DO
  729. VisitParameter(parameter);
  730. parameter := parameter.nextParameter;
  731. END;
  732. END FixProcedureType;
  733. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  734. VAR prev,this: SyntaxTree.Modifier;
  735. BEGIN
  736. this := modifiers;prev := NIL;
  737. WHILE (this # NIL) & (this.identifier # name) DO
  738. prev := this; this := this.nextModifier;
  739. END;
  740. IF this # NIL THEN
  741. IF this.expression # NIL THEN
  742. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  743. END;
  744. this.Resolved;
  745. position := this.position;
  746. RETURN TRUE
  747. ELSE
  748. RETURN FALSE
  749. END;
  750. END HasFlag;
  751. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  752. VAR prev,this: SyntaxTree.Modifier;
  753. BEGIN
  754. this := modifiers;prev := NIL;
  755. WHILE (this # NIL) & (this.identifier # name) DO
  756. prev := this; this := this.nextModifier;
  757. END;
  758. IF this # NIL THEN
  759. IF this.expression = NIL THEN
  760. Error(this.position,Diagnostics.Invalid,"expected expression value");
  761. ELSE
  762. this.SetExpression(ConstantExpression(this.expression));
  763. IF CheckIntegerValue(this.expression,value) THEN END;
  764. END;
  765. this.Resolved;
  766. position := this.position;
  767. RETURN TRUE
  768. ELSE RETURN FALSE
  769. END;
  770. END HasValue;
  771. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: ARRAY OF CHAR): BOOLEAN;
  772. VAR prev,this: SyntaxTree.Modifier;
  773. BEGIN
  774. this := modifiers;prev := NIL;
  775. WHILE (this # NIL) & (this.identifier # name) DO
  776. prev := this; this := this.nextModifier;
  777. END;
  778. IF this # NIL THEN
  779. IF this.expression = NIL THEN
  780. Error(this.position,Diagnostics.Invalid,"expected expression value");
  781. ELSE
  782. this.SetExpression(ConstantExpression(this.expression));
  783. IF CheckStringValue(this.expression,value) THEN END;
  784. END;
  785. this.Resolved;
  786. position := this.position;
  787. RETURN TRUE
  788. ELSE RETURN FALSE
  789. END;
  790. END HasStringValue;
  791. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  792. VAR svalue: ARRAY 32 OF CHAR; position: LONGINT;
  793. BEGIN
  794. IF cellsAreObjects THEN RETURN FALSE END;
  795. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  796. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  797. IF svalue[0] = "~" THEN
  798. Strings.TrimLeft(svalue, "~");
  799. IF svalue = backendName THEN
  800. RETURN TRUE;
  801. END;
  802. ELSIF svalue # backendName THEN
  803. RETURN TRUE;
  804. END;
  805. END;
  806. IF x.baseType # NIL THEN
  807. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  808. END;
  809. RETURN FALSE;
  810. END SkipImplementation;
  811. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  812. VAR this: SyntaxTree.Modifier;
  813. BEGIN
  814. this := modifiers;
  815. WHILE this # NIL DO
  816. IF ~this.resolved THEN
  817. IF checkUse THEN
  818. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  819. ELSE
  820. this.SetExpression(ResolveExpression(this.expression));
  821. this.Resolved;
  822. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  823. END;
  824. END;
  825. this := this.nextModifier
  826. END;
  827. END CheckModifiers;
  828. (**
  829. resolve procedure type
  830. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  831. **)
  832. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  833. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  834. BEGIN
  835. IF TypeNeedsResolution(procedureType) THEN
  836. modifiers := procedureType.modifiers;
  837. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  838. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  839. procedureType.SetInterrupt(TRUE);
  840. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  841. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  842. IF useDarwinCCalls THEN (*fld*)
  843. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  844. ELSE
  845. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  846. END
  847. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  848. procedureType.SetNoReturn(TRUE);
  849. END;
  850. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  851. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  852. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  853. CheckModifiers(modifiers, TRUE);
  854. modifiers := procedureType.returnTypeModifiers;
  855. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  856. CheckModifiers(modifiers, TRUE);
  857. typeFixes.Add(procedureType,currentScope);
  858. procedureType.SetHasPointers(procedureType.isDelegate);
  859. procedureType.SetState(SyntaxTree.Resolved);
  860. END;
  861. resolvedType := ResolvedType(procedureType)
  862. END VisitProcedureType;
  863. (** check and resolve record type
  864. - check base type: must be record, math array or array-structured object type
  865. - check declarations
  866. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  867. **)
  868. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  869. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  870. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  871. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  872. hasPointers: BOOLEAN;
  873. modifiers: SyntaxTree.Modifier;
  874. value: LONGINT;
  875. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  876. BEGIN
  877. type := type.resolved;
  878. IF (type IS SyntaxTree.PointerType) &
  879. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  880. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  881. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  882. RETURN TRUE
  883. ELSE
  884. RETURN FALSE
  885. END;
  886. END IsPointerToRecord;
  887. BEGIN
  888. IF TypeNeedsResolution(x) THEN
  889. hasPointers := FALSE;
  890. modifiers := x.modifiers;
  891. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  892. CheckModifiers(modifiers, TRUE);
  893. IF x.baseType # NIL THEN
  894. position := x.baseType.position;
  895. baseType := ResolveType(x.baseType);
  896. resolved := baseType.resolved;
  897. hasPointers := hasPointers OR resolved.hasPointers;
  898. IF x.isObject THEN (* object *)
  899. ASSERT(x.pointerType # NIL);
  900. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  901. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  902. baseType := NIL
  903. ELSIF IsPointerToRecord(resolved,recordType) THEN
  904. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  905. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  906. ELSE
  907. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  908. END;
  909. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  910. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  911. ELSIF IsPointerToRecord(resolved,recordType) THEN
  912. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  913. ELSIF resolved IS SyntaxTree.RecordType THEN
  914. ELSE
  915. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  916. END;
  917. ELSE
  918. IF resolved IS SyntaxTree.RecordType THEN
  919. ELSE
  920. Error(position, Diagnostics.Invalid,"record does not extend record")
  921. END;
  922. END;
  923. x.SetBaseType(baseType);
  924. IF x.Level() > 15 THEN
  925. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  926. (* note:
  927. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  928. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  929. inheritance history of a type.
  930. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  931. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  932. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  933. *)
  934. END;
  935. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  936. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  937. END;
  938. END;
  939. Declarations(x.recordScope, FALSE);
  940. ResolveArrayStructure(x);
  941. (* computation of sizes and offsets skipped -> done in backend / system *)
  942. recordBase := x.GetBaseRecord();
  943. IF recordBase = NIL THEN numberMethods := 0
  944. ELSE numberMethods := recordBase.recordScope.numberMethods
  945. END;
  946. isRealtime := TRUE;
  947. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  948. WHILE symbol # NIL DO
  949. IF symbol IS SyntaxTree.Variable THEN
  950. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  951. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  952. END;
  953. IF symbol IS SyntaxTree.Procedure THEN
  954. procedure := symbol(SyntaxTree.Procedure);
  955. IF procedure.super # NIL THEN
  956. procedure.SetMethodNumber(procedure.super.methodNumber)
  957. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  958. procedure.SetMethodNumber(numberMethods);
  959. INC(numberMethods);
  960. END;
  961. END;
  962. symbol := symbol.nextSymbol;
  963. END;
  964. IF isRealtime THEN x.SetRealtime(TRUE) END;
  965. x.recordScope.SetNumberMethods(numberMethods);
  966. (* TODO: is this needed anymore? *)
  967. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  968. Error(x.position,Diagnostics.Invalid,"object extends a record")
  969. END;
  970. IF (x.typeDeclaration = NIL) THEN
  971. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  972. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  973. (*
  974. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  975. AnonymousTypeDeclaration(x,name);
  976. *)
  977. ELSE
  978. AnonymousTypeDeclaration(x,"Anonymous");
  979. END;
  980. END;
  981. x.SetHasPointers(hasPointers);
  982. x.SetState(SyntaxTree.Resolved);
  983. END;
  984. resolvedType := ResolvedType(x);
  985. END VisitRecordType;
  986. (** check and resolve cell type
  987. - check base type: must be cell
  988. - check declarations
  989. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  990. **)
  991. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  992. VAR
  993. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  994. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  995. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  996. recordBase: SyntaxTree.RecordType;
  997. numberMethods, int: LONGINT;
  998. real: LONGREAL;
  999. bool: BOOLEAN;
  1000. set: SET;
  1001. v: SyntaxTree.Expression;
  1002. str: Scanner.StringType;
  1003. atype: SyntaxTree.ArrayType;
  1004. prev: SyntaxTree.Scope;
  1005. skip: BOOLEAN;
  1006. svalue: ARRAY 32 OF CHAR;
  1007. BEGIN
  1008. IF TypeNeedsResolution(x) THEN
  1009. recordBase := NIL;
  1010. IF cellsAreObjects THEN
  1011. IF x.baseType = NIL THEN
  1012. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1013. ImportModule(qualifiedIdentifier.prefix, x.position);
  1014. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1015. x.SetBaseType(ResolveType(x.baseType));
  1016. recordBase := x.GetBaseRecord();
  1017. IF recordBase = NIL THEN
  1018. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1019. END;
  1020. ELSE
  1021. x.SetBaseType(ResolveType(x.baseType));
  1022. END;
  1023. ELSE
  1024. x.SetBaseType(ResolveType(x.baseType));
  1025. END;
  1026. IF recordBase = NIL THEN numberMethods := 0
  1027. ELSE numberMethods := recordBase.recordScope.numberMethods
  1028. END;
  1029. modifier := x.modifiers;
  1030. (*IF ~x.isCellNet THEN*)
  1031. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1032. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1033. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1034. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1035. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1036. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1037. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1038. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1039. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1040. symbol := system.activeCellsCapabilities;
  1041. WHILE symbol # NIL DO
  1042. IF HasFlag(modifier, symbol.name, position) THEN END;
  1043. symbol := symbol.nextSymbol;
  1044. END;
  1045. modifier := x.modifiers;
  1046. WHILE (modifier # NIL) DO
  1047. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1048. IF modifier.expression # NIL THEN
  1049. v := ConstantExpression(modifier.expression);
  1050. property.SetValue(v);
  1051. IF IsIntegerValue(modifier.expression, int) THEN
  1052. (*property.SetValue(modifier.expression);*)
  1053. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1054. property.SetType(system.longintType);
  1055. ELSIF IsRealValue(modifier.expression, real) THEN
  1056. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1057. property.SetType(system.longrealType);
  1058. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1059. property.SetType(system.booleanType);
  1060. ELSIF IsSetValue(modifier.expression, set) THEN
  1061. property.SetType(system.setType);
  1062. ELSIF IsStringValue(modifier.expression, str) THEN
  1063. (*property.SetValue(modifier.expression);*)
  1064. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1065. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1066. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1067. property.SetType(atype);
  1068. ELSE
  1069. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1070. END;
  1071. ELSE (* flag property *)
  1072. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1073. property.SetType(system.booleanType);
  1074. END;
  1075. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1076. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1077. x.AddProperty(property);
  1078. modifier := modifier.nextModifier;
  1079. END;
  1080. CheckModifiers(modifier, FALSE);
  1081. Declarations(x.cellScope, SkipImplementation(x));
  1082. (* process parameters *)
  1083. prev := currentScope;
  1084. currentScope := x.cellScope;
  1085. parameter :=x.firstParameter;
  1086. WHILE (parameter # NIL) DO
  1087. VisitParameter(parameter);
  1088. type := parameter.type.resolved;
  1089. IF ~(type IS SyntaxTree.PortType) THEN
  1090. WHILE IsStaticArray(type, type, len) DO
  1091. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1092. END;
  1093. WHILE IsDynamicArray(type, type) DO
  1094. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1095. END;
  1096. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1097. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1098. END;
  1099. END;
  1100. parameter := parameter.nextParameter;
  1101. END;
  1102. currentScope := prev;
  1103. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1104. WHILE symbol # NIL DO
  1105. IF symbol IS SyntaxTree.Variable THEN
  1106. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1107. END;
  1108. symbol := symbol.nextSymbol;
  1109. END;
  1110. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1111. IF (x.typeDeclaration = NIL) THEN
  1112. AnonymousTypeDeclaration(x,"Anonymous");
  1113. END;
  1114. x.SetState(SyntaxTree.Resolved);
  1115. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1116. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1117. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1118. Warning(x.position, "Non-empty body for an engine?");
  1119. END;
  1120. END;
  1121. resolvedType := ResolvedType(x);
  1122. END VisitCellType;
  1123. (* check if an object is an array-structured object type
  1124. - determine the array structure
  1125. - collect operators from top to bottom in the inheritance hierarchy
  1126. - check if LEN operator is declared
  1127. - determine number of possible index operators
  1128. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1129. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1130. *)
  1131. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1132. VAR
  1133. indexOperatorCount, i: LONGINT;
  1134. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1135. isTensor: BOOLEAN;
  1136. BEGIN
  1137. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1138. (* determine array structure *)
  1139. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1140. END;
  1141. IF recordType.HasArrayStructure() THEN
  1142. (* the object is an ASOT *)
  1143. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1144. (* reset array access operators *)
  1145. arrayAccessOperators.len := NIL;
  1146. arrayAccessOperators.generalRead := NIL;
  1147. arrayAccessOperators.generalWrite := NIL;
  1148. IF isTensor THEN
  1149. (* all operators of dimensionalities 1 to max *)
  1150. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1151. ELSE
  1152. (* all operators of certain dimensionality *)
  1153. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1154. END;
  1155. NEW(arrayAccessOperators.read, indexOperatorCount);
  1156. NEW(arrayAccessOperators.write, indexOperatorCount);
  1157. FOR i := 0 TO indexOperatorCount - 1 DO
  1158. arrayAccessOperators.read[i] := NIL;
  1159. arrayAccessOperators.write[i] := NIL
  1160. END;
  1161. (* collect access operators in the record scope *)
  1162. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1163. IF arrayAccessOperators.len = NIL THEN
  1164. (* TODO: think about making this operator optional for static array structures *)
  1165. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1166. END;
  1167. (* show error messages *)
  1168. IF isTensor THEN
  1169. (* require ARRAY [*] OF RANGE *)
  1170. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1171. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1172. ELSE
  1173. (* forbid ARRAY [*] OF RANGE *)
  1174. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1175. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1176. (* require RANGE, RANGE, ... RANGE *)
  1177. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1178. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1179. END;
  1180. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1181. ELSE
  1182. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1183. IF recordType.recordScope.firstOperator # NIL THEN
  1184. RETURN;
  1185. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1186. END
  1187. END
  1188. END ResolveArrayStructure;
  1189. (** collect array access operators in a record scope **)
  1190. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1191. VAR
  1192. baseType: SyntaxTree.Type;
  1193. operator: SyntaxTree.Operator;
  1194. isReadOperator, isGeneralOperator: BOOLEAN;
  1195. indexListSize, indexListKind, hashValue: LONGINT;
  1196. BEGIN
  1197. (* if a parent record scope exists, collect the operators there first *)
  1198. baseType := recordScope.ownerRecord.baseType;
  1199. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1200. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1201. END;
  1202. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1203. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1204. END;
  1205. (* go through all operators in the current record scope *)
  1206. operator := recordScope.firstOperator;
  1207. WHILE operator # NIL DO
  1208. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1209. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1210. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1211. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1212. IF isGeneralOperator THEN
  1213. IF isReadOperator THEN
  1214. arrayAccessOperators.generalRead := operator
  1215. ELSE
  1216. arrayAccessOperators.generalWrite := operator
  1217. END
  1218. ELSE
  1219. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1220. IF isReadOperator THEN
  1221. arrayAccessOperators.read[hashValue] := operator
  1222. ELSE
  1223. arrayAccessOperators.write[hashValue] := operator
  1224. END
  1225. END
  1226. END
  1227. ELSE
  1228. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1229. END;
  1230. operator := operator.nextOperator
  1231. END
  1232. END CollectArrayAccessOperators;
  1233. (** the hash value of an index operator **)
  1234. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1235. VAR result: LONGINT;
  1236. BEGIN
  1237. IF isTensor THEN
  1238. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1239. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1240. ELSE
  1241. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1242. END
  1243. ELSE
  1244. result := indexListKind
  1245. END;
  1246. RETURN result
  1247. END IndexOperatorHash;
  1248. (** 2 to the power of exponent **)
  1249. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1250. VAR result, i: LONGINT;
  1251. BEGIN
  1252. result := 1;
  1253. FOR i := 1 TO exponent DO
  1254. result := result * 2;
  1255. END;
  1256. RETURN result
  1257. END TwoToThePowerOf;
  1258. (** check if a LEN operator has a correct signature. i.e.
  1259. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1260. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1261. **)
  1262. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1263. VAR
  1264. procedureType: SyntaxTree.ProcedureType;
  1265. returnedArrayType: SyntaxTree.MathArrayType;
  1266. result: BOOLEAN;
  1267. BEGIN
  1268. result := FALSE;
  1269. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1270. IF (procedureType.numberParameters = 0) THEN
  1271. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1272. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1273. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1274. IF returnedArrayType.form = SyntaxTree.Open THEN
  1275. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1276. result := TRUE
  1277. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1278. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1279. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1280. result := TRUE
  1281. END
  1282. END
  1283. END
  1284. END
  1285. END;
  1286. IF result THEN
  1287. (* export symbol automatically *)
  1288. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1289. ELSE
  1290. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1291. END;
  1292. RETURN result
  1293. END CheckLenOperator;
  1294. (** check if an index operator has a correct signature. i.e.
  1295. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1296. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1297. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1298. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1299. - determine if it is a read or write operator (existance of return type)
  1300. - check index parameters
  1301. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1302. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1303. [LONGINT] -> binary 0 -> 0
  1304. [RANGE] -> binary 1 -> 1
  1305. [LONGINT, LONGINT] -> binary 00 -> 0
  1306. [LONGINT, RANGE] -> binary 01 -> 1
  1307. [RANGE, LONGINT] -> binary 10 -> 2
  1308. [RANGE, RANGE] -> binary 11 -> 3
  1309. etc.
  1310. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1311. - for read operators, check if return type matches the type of data that is read
  1312. - for write operators, check if last parameter type matches the type of data that is written
  1313. **)
  1314. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1315. VAR
  1316. elementType, otherElementType, dataType: SyntaxTree.Type;
  1317. procedureType: SyntaxTree.ProcedureType;
  1318. mathArrayType: SyntaxTree.MathArrayType;
  1319. parameter: SyntaxTree.Parameter;
  1320. parameterCount, rangeCount, i: LONGINT;
  1321. hasTypeError: BOOLEAN;
  1322. BEGIN
  1323. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1324. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1325. (* determine if it is a read or write operator *)
  1326. isReadOperator := (procedureType.returnType # NIL);
  1327. IF isReadOperator THEN
  1328. indexListSize := parameterCount;
  1329. ELSE
  1330. indexListSize := parameterCount - 1;
  1331. END;
  1332. IF indexListSize < 1 THEN
  1333. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1334. RETURN FALSE
  1335. END;
  1336. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1337. (* general operator *)
  1338. isGeneralOperator := TRUE;
  1339. IF indexListSize > 1 THEN
  1340. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1341. RETURN FALSE
  1342. END;
  1343. (* ARRAY [*] OF RANGE*)
  1344. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1345. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1346. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1347. RETURN FALSE
  1348. END;
  1349. parameter := procedureType.firstParameter.nextParameter
  1350. ELSE
  1351. (* fixed-dim. operator *)
  1352. isGeneralOperator := FALSE;
  1353. (* check number of index parameters *)
  1354. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1355. (* for tensors, limited to a certain size *)
  1356. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1357. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1358. RETURN FALSE
  1359. END
  1360. ELSE
  1361. (* for non-tensors, depends on dimensionality *)
  1362. IF indexListSize # arrayStructure.Dimensionality() THEN
  1363. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1364. RETURN FALSE
  1365. END
  1366. END;
  1367. (* go through all index parameters
  1368. - count the number of ranges
  1369. - determine the index list kind number
  1370. *)
  1371. indexListKind := 0;
  1372. rangeCount := 0;
  1373. parameter := procedureType.firstParameter;
  1374. FOR i := 1 TO indexListSize DO
  1375. indexListKind := indexListKind * 2;
  1376. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1377. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1378. INC(indexListKind);
  1379. INC(rangeCount)
  1380. ELSE
  1381. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1382. RETURN FALSE
  1383. END;
  1384. parameter := parameter.nextParameter
  1385. END;
  1386. END;
  1387. (*
  1388. - for read operators: check type of last parameter
  1389. - for write operators: check return type
  1390. *)
  1391. IF isReadOperator THEN
  1392. dataType := procedureType.returnType (* the return type *)
  1393. ELSE
  1394. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1395. END;
  1396. elementType := arrayStructure.ElementType();
  1397. hasTypeError := FALSE;
  1398. IF isGeneralOperator THEN
  1399. (* ARRAY [?] OF <Element> *)
  1400. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1401. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1402. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1403. hasTypeError := TRUE
  1404. END
  1405. ELSE
  1406. hasTypeError := TRUE
  1407. END
  1408. ELSE
  1409. IF rangeCount = 0 THEN
  1410. (* <Element> *)
  1411. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1412. ELSE
  1413. (* ARRAY [*, *, ..., *] OF <Element> *)
  1414. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1415. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1416. IF mathArrayType.IsFullyDynamic() THEN
  1417. IF mathArrayType.Dimensionality() = rangeCount THEN
  1418. otherElementType := mathArrayType.ElementType();
  1419. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1420. ELSE
  1421. hasTypeError := TRUE
  1422. END
  1423. ELSE
  1424. hasTypeError := TRUE
  1425. END
  1426. ELSE
  1427. hasTypeError := TRUE
  1428. END
  1429. END
  1430. END;
  1431. IF hasTypeError THEN
  1432. IF isReadOperator THEN
  1433. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1434. ELSE
  1435. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1436. END;
  1437. RETURN FALSE
  1438. END;
  1439. (* export symbol automatically *)
  1440. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1441. RETURN TRUE
  1442. END CheckIndexOperator;
  1443. (** resolve all pending types (late resolving).
  1444. - type fixes are resolved at the end of the declaration phase
  1445. - type fixes may imply new type fixes that are also entered at the end of the list
  1446. **)
  1447. PROCEDURE FixTypes;
  1448. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1449. BEGIN
  1450. prevScope := currentScope;
  1451. p := typeFixes.Get(currentScope);
  1452. WHILE p # NIL DO
  1453. ASSERT(currentScope # NIL);
  1454. ASSERT(p IS SyntaxTree.Type);
  1455. IF p IS SyntaxTree.PointerType THEN
  1456. FixPointerType(p(SyntaxTree.PointerType))
  1457. ELSIF p IS SyntaxTree.ProcedureType THEN
  1458. FixProcedureType(p(SyntaxTree.ProcedureType))
  1459. ELSE
  1460. HALT(100);
  1461. END;
  1462. p := typeFixes.Get(currentScope);
  1463. END;
  1464. currentScope :=prevScope;
  1465. END FixTypes;
  1466. (**
  1467. resolve type x
  1468. - if x is nil then return nil
  1469. - if x cannot be resolved then the result is invalidType else the result is x
  1470. - the resolved type is entered into x.resolved
  1471. **)
  1472. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1473. VAR prev,resolved: SyntaxTree.Type;
  1474. BEGIN
  1475. prev := resolvedType;
  1476. resolvedType := SyntaxTree.invalidType;
  1477. IF x = NIL THEN resolvedType := NIL
  1478. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1479. END;
  1480. resolved := resolvedType;
  1481. resolvedType := prev;
  1482. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1483. RETURN resolved
  1484. END ResolveType;
  1485. (*** compatibility rules ***)
  1486. (**
  1487. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1488. **)
  1489. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1490. VAR result: SyntaxTree.Type;
  1491. BEGIN
  1492. result := SyntaxTree.invalidType;
  1493. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1494. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1495. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1496. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1497. ELSE result := type.resolved
  1498. END;
  1499. RETURN result
  1500. END RegularType;
  1501. (** returns signature compatibility of procedure types this and to
  1502. - if not compatible then error is reported
  1503. - compatibility means type equality
  1504. **)
  1505. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1506. VAR result: BOOLEAN;
  1507. BEGIN
  1508. result := SameType(to,this);
  1509. IF ~result THEN
  1510. Error(position,Diagnostics.Invalid,"signature incompatible");
  1511. IF VerboseErrorMessage THEN
  1512. Printout.Info("this",this);
  1513. Printout.Info("to",to);
  1514. END;
  1515. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1516. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1517. END;
  1518. RETURN result
  1519. END SignatureCompatible;
  1520. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1521. - for var parameters compatibility means same type except for
  1522. - formal is of open array of system byte
  1523. - formal is of record type
  1524. - formal is of open array type
  1525. - formal is of open math array type
  1526. - for value parameters compatibllity means assignment compatibility except for
  1527. - formal is of open array type
  1528. if compatible the return true else report error and return false
  1529. **)
  1530. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1531. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1532. BEGIN
  1533. formalType := RegularType(formal.position,formal.type);
  1534. actualType := RegularType(actual.position,actual.type);
  1535. error := FALSE;
  1536. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1537. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1538. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1539. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1540. END;
  1541. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1542. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1543. ELSIF ~IsVariable(actual) THEN
  1544. result := FALSE; error := TRUE;
  1545. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1546. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1547. ELSE
  1548. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1549. END;
  1550. IF VerboseErrorMessage THEN
  1551. Printout.Info("actual",actual);
  1552. Printout.Info("formal",formal);
  1553. END;
  1554. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1555. result := CompatibleTo(system,actualType,formalType);
  1556. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1557. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1558. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1559. IF IsArrayStructuredObjectType(actualType) THEN
  1560. actualType := MathArrayStructureOfType(actualType)
  1561. END;
  1562. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1563. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1564. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1565. END;
  1566. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1567. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1568. END;
  1569. ELSE
  1570. result := SameType(actualType,formalType)
  1571. END
  1572. ELSE
  1573. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1574. actualType := system.characterType;
  1575. END;
  1576. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1577. result := TRUE; (* special rule for WINAPI parameters *)
  1578. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1579. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1580. ELSE
  1581. result := CompatibleTo(system,actualType,formalType);
  1582. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1583. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1584. END;
  1585. END;
  1586. END;
  1587. IF ~result & ~error THEN
  1588. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1589. IF VerboseErrorMessage THEN
  1590. Printout.Info("actual",actual);
  1591. Printout.Info("formal",formal);
  1592. END;
  1593. END;
  1594. RETURN result
  1595. END ParameterCompatible;
  1596. (** check compatibility for expressions of the form left := right
  1597. - if compatible then return true else error report and return false
  1598. - check if left is variable
  1599. - check compatiblity
  1600. **)
  1601. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1602. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1603. BEGIN
  1604. result := FALSE;
  1605. leftType := RegularType(left.position,left.type);
  1606. rightType := RegularType(right.position,right.type);
  1607. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1608. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1609. END;
  1610. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1611. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1612. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1613. (* error already handled *)
  1614. result := TRUE;
  1615. ELSIF ~IsVariable(left) THEN
  1616. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1617. IF VerboseErrorMessage THEN
  1618. Printout.Info("left",left);
  1619. Printout.Info("right",right);
  1620. END;
  1621. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1622. result := TRUE;
  1623. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1624. result := TRUE
  1625. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1626. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1627. IF VerboseErrorMessage THEN
  1628. Printout.Info("left",left);
  1629. Printout.Info("right",right);
  1630. END;
  1631. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1632. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1633. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1634. ELSE
  1635. result := TRUE
  1636. END;
  1637. RETURN result
  1638. END AssignmentCompatible;
  1639. (*** values ***)
  1640. (** check and resolve integer value **)
  1641. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1642. VAR hugeint: HUGEINT;
  1643. BEGIN
  1644. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1645. value.SetType(Global.GetIntegerType(system,hugeint));
  1646. resolvedExpression := value
  1647. END VisitIntegerValue;
  1648. (** check and resolve real value **)
  1649. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1650. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1651. BEGIN
  1652. subtype := value(SyntaxTree.RealValue).subtype;
  1653. IF subtype = Scanner.Real THEN
  1654. type := system.realType
  1655. ELSIF subtype = Scanner.Longreal THEN
  1656. type := system.longrealType
  1657. ELSE
  1658. HALT(100)
  1659. END;
  1660. value.SetType(type);
  1661. resolvedExpression := value
  1662. END VisitRealValue;
  1663. (** check and resolve complex value **)
  1664. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1665. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1666. BEGIN
  1667. subtype := value(SyntaxTree.ComplexValue).subtype;
  1668. IF subtype = Scanner.Real THEN
  1669. type := system.complexType
  1670. ELSIF subtype = Scanner.Longreal THEN
  1671. type := system.longcomplexType
  1672. ELSE
  1673. HALT(100)
  1674. END;
  1675. value.SetType(type);
  1676. resolvedExpression := value
  1677. END VisitComplexValue;
  1678. (** check and resolve set value **)
  1679. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1680. BEGIN
  1681. value.SetType(system.setType);
  1682. resolvedExpression := value
  1683. END VisitSetValue;
  1684. (** check and resolve set value **)
  1685. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1686. BEGIN
  1687. value.SetType(SyntaxTree.invalidType);
  1688. resolvedExpression := value
  1689. END VisitMathArrayValue;
  1690. (** check and resolve boolean value **)
  1691. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1692. BEGIN
  1693. value.SetType(system.booleanType);
  1694. resolvedExpression := value
  1695. END VisitBooleanValue;
  1696. (** check and resolve string value **)
  1697. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1698. BEGIN
  1699. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1700. resolvedExpression := value
  1701. END VisitStringValue;
  1702. (** check and resolve character value **)
  1703. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1704. BEGIN
  1705. value.SetType(system.characterType);
  1706. resolvedExpression := value
  1707. END VisitCharacterValue;
  1708. (** check and resolve nil value **)
  1709. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1710. BEGIN
  1711. value.SetType(system.nilType);
  1712. resolvedExpression := value
  1713. END VisitNilValue;
  1714. (** check and resolve enumerator value **)
  1715. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1716. BEGIN
  1717. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1718. ASSERT(value.type # NIL);
  1719. resolvedExpression := value
  1720. END VisitEnumerationValue;
  1721. (*** expressions ***)
  1722. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1723. - check all elements on integer type
  1724. - if element range is constant, then check lower and upper bound
  1725. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1726. if an error occurs then report error and return invalidExpression
  1727. **)
  1728. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1729. VAR
  1730. i: LONGINT;
  1731. element: SyntaxTree.Expression;
  1732. constant: BOOLEAN;
  1733. elements: SyntaxTree.ExpressionList;
  1734. s: SET;
  1735. result: SyntaxTree.Expression;
  1736. value: SyntaxTree.Value;
  1737. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1738. VAR
  1739. left, right: SyntaxTree.Expression;
  1740. elementResult: SyntaxTree.Expression;
  1741. leftInteger, rightInteger, temp: LONGINT;
  1742. BEGIN
  1743. (* set context of range *)
  1744. IF element IS SyntaxTree.RangeExpression THEN
  1745. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1746. END;
  1747. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1748. IF elementResult = SyntaxTree.invalidExpression THEN
  1749. (* error already reported *)
  1750. constant := FALSE
  1751. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1752. (* the element is a range expression *)
  1753. (* extract left and right hand side of range *)
  1754. left := elementResult(SyntaxTree.RangeExpression).first;
  1755. right := elementResult(SyntaxTree.RangeExpression).last;
  1756. (* guaranteed by VisitRangeExpression: *)
  1757. ASSERT((left # NIL) & (right # NIL));
  1758. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1759. ELSE
  1760. (* the element is not a range expression *)
  1761. (* check type and add conversion if needed *)
  1762. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1763. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1764. ELSE
  1765. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1766. elementResult := SyntaxTree.invalidExpression;
  1767. constant := FALSE
  1768. END;
  1769. left := elementResult;
  1770. right := elementResult
  1771. END;
  1772. IF elementResult # SyntaxTree.invalidExpression THEN
  1773. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1774. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1775. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1776. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1777. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1778. END
  1779. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1780. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1781. ELSE
  1782. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1783. s := {};
  1784. ELSE
  1785. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1786. IF leftInteger < 0 THEN leftInteger := 0 END;
  1787. (*!!!!!!!!! this is a hack !!!!!!! *)
  1788. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1789. extends the range x..31 to x..63 !!!!!! *)
  1790. s := s + {leftInteger..rightInteger};
  1791. END;
  1792. END;
  1793. ELSE
  1794. constant := FALSE;
  1795. END
  1796. END;
  1797. RETURN elementResult
  1798. END CheckElement;
  1799. BEGIN
  1800. result := set; constant := TRUE; s := {}; elements := set.elements;
  1801. IF elements # NIL THEN
  1802. FOR i := 0 TO elements.Length()-1 DO
  1803. element := elements.GetExpression(i);
  1804. element := CheckElement(element);
  1805. IF element = SyntaxTree.invalidExpression THEN
  1806. result := SyntaxTree.invalidExpression
  1807. END;
  1808. elements.SetExpression(i,element);
  1809. END;
  1810. END;
  1811. IF constant THEN
  1812. value := SyntaxTree.NewSetValue(set.position,s);
  1813. value.SetType(system.setType);
  1814. result.SetResolved(value);
  1815. END;
  1816. (* optimization possible
  1817. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1818. left this to the programmer...
  1819. *)
  1820. result.SetType(system.setType);
  1821. resolvedExpression := result;
  1822. END VisitSet;
  1823. (*
  1824. old variant: quite generic but needs better conversion handling, do this?
  1825. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1826. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1827. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1828. BEGIN
  1829. type := NIL;
  1830. numberElements := x.elements.Length();
  1831. FOR i := 0 TO numberElements-1 DO
  1832. expression := x.elements.GetExpression(i);
  1833. position := expression.position;
  1834. expression := ResolveExpression(x.elements.GetExpression(i));
  1835. x.elements.SetExpression(i,de);
  1836. IF type = NIL THEN
  1837. type := expression.type;
  1838. ELSIF CompatibleTo(system,expression.type,type) THEN
  1839. (* ok *)
  1840. ELSIF CompatibleTo(system,type,expression.type) THEN
  1841. type := expression.type
  1842. ELSE
  1843. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1844. type := SyntaxTree.invalidType;
  1845. END;
  1846. END;
  1847. isValue := TRUE;
  1848. FOR i := 0 TO numberElements-1 DO
  1849. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1850. x.elements.SetExpression(i,expression);
  1851. isValue := isValue & (expression.resolved # NIL);
  1852. END;
  1853. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1854. arrayType.SetArrayBase(type);
  1855. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1856. IF isValue THEN
  1857. value := SyntaxTree.NewMathArrayValue(position);
  1858. value.SetElements(x.elements);
  1859. x.SetResolved(value);
  1860. END;
  1861. x.SetType(arrayType);
  1862. resolvedExpression := x;
  1863. END VisitMathArrayExpression;
  1864. *)
  1865. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1866. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1867. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1868. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1869. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1870. BEGIN
  1871. numberElements := x.elements.Length();
  1872. FOR i := 0 TO numberElements-1 DO
  1873. expression := x.elements.GetExpression(i);
  1874. IF expression IS SyntaxTree.MathArrayExpression THEN
  1875. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1876. ELSE
  1877. position := expression.position;
  1878. expression := ResolveExpression(x.elements.GetExpression(i));
  1879. x.elements.SetExpression(i,expression);
  1880. IF type = NIL THEN
  1881. type := expression.type;
  1882. ELSIF CompatibleTo(system,expression.type,type) THEN
  1883. (* ok *)
  1884. ELSIF CompatibleTo(system,type,expression.type) THEN
  1885. type := expression.type
  1886. ELSE
  1887. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1888. type := SyntaxTree.invalidType;
  1889. END;
  1890. END;
  1891. END;
  1892. END RecursivelyFindType;
  1893. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1894. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1895. BEGIN
  1896. numberElements := x.elements.Length();
  1897. FOR i := 0 TO numberElements-1 DO
  1898. expression := x.elements.GetExpression(i);
  1899. IF expression IS SyntaxTree.MathArrayExpression THEN
  1900. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1901. ELSE
  1902. position := expression.position;
  1903. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1904. x.elements.SetExpression(i,expression);
  1905. isValue := isValue & (expression.resolved # NIL);
  1906. END;
  1907. END;
  1908. END RecursivelySetExpression;
  1909. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1910. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1911. arrayType: SyntaxTree.MathArrayType;
  1912. BEGIN
  1913. numberElements := x.elements.Length();
  1914. baseType := NIL;
  1915. gsize := 0;
  1916. FOR i := 0 TO numberElements-1 DO
  1917. expression := x.elements.GetExpression(i);
  1918. IF expression IS SyntaxTree.MathArrayExpression THEN
  1919. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1920. IF i=0 THEN
  1921. gsize := size;
  1922. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1923. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1924. ELSE expression.SetType(baseType)
  1925. END;
  1926. ELSIF baseType = NIL THEN baseType := type;
  1927. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1928. END;
  1929. END;
  1930. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1931. arrayType.SetArrayBase(baseType);
  1932. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1933. RETURN ResolveType(arrayType);
  1934. END RecursivelySetType;
  1935. BEGIN
  1936. type := NIL;
  1937. RecursivelyFindType(x);
  1938. isValue := TRUE;
  1939. RecursivelySetExpression(x);
  1940. arrayType := RecursivelySetType(x);
  1941. x.SetType(arrayType);
  1942. IF isValue THEN
  1943. value := SyntaxTree.NewMathArrayValue(x.position);
  1944. value.SetArray(x);
  1945. x.SetResolved(value);
  1946. value.SetType(arrayType);
  1947. END;
  1948. x.SetType(arrayType);
  1949. resolvedExpression := x;
  1950. END VisitMathArrayExpression;
  1951. (** check and resolve unary expression **)
  1952. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1953. VAR
  1954. left: SyntaxTree.Expression;
  1955. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1956. bool: BOOLEAN;
  1957. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1958. value: SyntaxTree.Value;
  1959. BEGIN
  1960. type := SyntaxTree.invalidType;
  1961. left := ResolveExpression(unaryExpression.left);
  1962. unaryExpression.SetLeft(left);
  1963. operator := unaryExpression.operator;
  1964. result := unaryExpression;
  1965. IF ~system.operatorDefined[operator] THEN
  1966. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1967. RETURN
  1968. ELSIF left.type = NIL THEN
  1969. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1970. resolvedExpression := SyntaxTree.invalidExpression;
  1971. RETURN
  1972. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1973. RETURN
  1974. END;
  1975. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1976. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1977. END;
  1978. IF operatorCall # NIL THEN
  1979. result := operatorCall;
  1980. type := operatorCall.type;
  1981. (* admissible operators
  1982. Minus number, set
  1983. Not boolean
  1984. *)
  1985. ELSE
  1986. CASE unaryExpression.operator OF
  1987. |Scanner.Minus:
  1988. IF IsIntegerType(left.type.resolved) THEN
  1989. IF left.resolved # NIL THEN
  1990. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1991. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1992. result.SetResolved(value);
  1993. type := Global.GetIntegerType(system,int);
  1994. value.SetType(type);
  1995. ELSE
  1996. type := left.type
  1997. END
  1998. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1999. IF IsRealValue(left,real) THEN
  2000. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2001. result.SetResolved(value);
  2002. type := left.type;
  2003. value.SetType(type);
  2004. ELSE
  2005. type := left.type;
  2006. END;
  2007. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2008. IF IsSetValue(left,set) THEN
  2009. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2010. result.SetResolved(value);
  2011. type := left.type;
  2012. value.SetType(type);
  2013. ELSE
  2014. type := left.type;
  2015. END;
  2016. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2017. IF IsComplexValue(left, real, imaginary) THEN
  2018. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2019. result.SetResolved(value);
  2020. type := left.type;
  2021. value.SetType(type);
  2022. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2023. ELSE
  2024. type := left.type;
  2025. END
  2026. ELSE
  2027. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2028. END;
  2029. |Scanner.Not:
  2030. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2031. IF IsBooleanValue(left,bool) THEN
  2032. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2033. result.SetResolved(value);
  2034. type := system.booleanType;
  2035. value.SetType(type);
  2036. ELSE
  2037. type := system.booleanType;
  2038. END;
  2039. ELSE
  2040. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2041. END;
  2042. |Scanner.Plus:
  2043. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2044. result := left; type := left.type;
  2045. ELSE
  2046. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2047. END;
  2048. (* ADDRESS OF *)
  2049. |Scanner.Address:
  2050. IF HasAddress(left) THEN
  2051. type := system.addressType;
  2052. ELSE
  2053. type := SyntaxTree.invalidType;
  2054. Error(left.position,Diagnostics.Invalid,"has no address");
  2055. Printout.Info("par", left);
  2056. END;
  2057. (* SIZE OF *)
  2058. |Scanner.Size:
  2059. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2060. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2061. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2062. value := SyntaxTree.NewIntegerValue(left.position, int);
  2063. result.SetResolved(value);
  2064. type := Global.GetIntegerType(system,int);
  2065. value.SetType(type)
  2066. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2067. ELSE
  2068. (* for variables, system sizeof could represent the physically occupied size
  2069. determined via the type descriptor, implement that ? *)
  2070. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2071. END
  2072. (* ALIAS OF *)
  2073. |Scanner.Alias:
  2074. type := left.type.resolved;
  2075. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2076. type := SyntaxTree.invalidType;
  2077. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2078. END;
  2079. ELSE
  2080. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2081. END;
  2082. END;
  2083. result.SetType(type);
  2084. resolvedExpression := result
  2085. END VisitUnaryExpression;
  2086. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2087. VAR
  2088. result: SyntaxTree.Expression;
  2089. array: SyntaxTree.MathArrayExpression;
  2090. value: SyntaxTree.MathArrayValue;
  2091. isValue: BOOLEAN;
  2092. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2093. BEGIN
  2094. type := type.resolved;
  2095. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2096. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2097. END;
  2098. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2099. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2100. END;
  2101. RETURN type
  2102. END BaseType;
  2103. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2104. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2105. BEGIN
  2106. numberElements := x.elements.Length();
  2107. FOR i := 0 TO numberElements-1 DO
  2108. expression := x.elements.GetExpression(i);
  2109. IF expression IS SyntaxTree.MathArrayExpression THEN
  2110. array := SyntaxTree.NewMathArrayExpression(position);
  2111. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2112. expression := array;
  2113. ELSE
  2114. position := expression.position;
  2115. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2116. isValue := isValue & (expression.resolved # NIL);
  2117. END;
  2118. to.elements.AddExpression(expression);
  2119. END;
  2120. END RecursivelyConvert;
  2121. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2122. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2123. arrayType: SyntaxTree.MathArrayType;
  2124. BEGIN
  2125. numberElements := x.elements.Length();
  2126. baseType := NIL;
  2127. gsize := 0;
  2128. FOR i := 0 TO numberElements-1 DO
  2129. expression := x.elements.GetExpression(i);
  2130. IF expression IS SyntaxTree.MathArrayExpression THEN
  2131. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2132. IF i=0 THEN
  2133. gsize := size;
  2134. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2135. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2136. ELSE expression.SetType(baseType)
  2137. END;
  2138. ELSIF baseType = NIL THEN baseType := type;
  2139. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2140. END;
  2141. END;
  2142. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2143. arrayType.SetArrayBase(baseType);
  2144. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2145. RETURN ResolveType(arrayType);
  2146. END RecursivelySetType;
  2147. BEGIN
  2148. result := SyntaxTree.invalidExpression;
  2149. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2150. result := expression (* do not convert *)
  2151. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2152. isValue := TRUE;
  2153. type := BaseType(type);
  2154. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2155. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2156. value := SyntaxTree.NewMathArrayValue(array.position);
  2157. value.SetArray(array);
  2158. value.SetType(RecursivelySetType(array));
  2159. result := value;
  2160. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2161. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2162. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2163. IF result = NIL THEN
  2164. result := SyntaxTree.invalidExpression;
  2165. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2166. IF VerboseErrorMessage THEN
  2167. Printout.Info("expression",expression);
  2168. Printout.Info("type",type);
  2169. END;
  2170. END;
  2171. END;
  2172. RETURN result
  2173. END MathArrayConversion;
  2174. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2175. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2176. BEGIN
  2177. result := expression; type := type.resolved;
  2178. IF (expression IS SyntaxTree.IntegerValue) THEN
  2179. int := expression(SyntaxTree.IntegerValue).hvalue;
  2180. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2181. int := Global.ConvertSigned(int,system.SizeOf(type));
  2182. result := SyntaxTree.NewIntegerValue(position,int);
  2183. result.SetType(type);
  2184. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2185. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2186. result := SyntaxTree.NewIntegerValue(position,int);
  2187. result.SetType(type);
  2188. ELSIF (type IS SyntaxTree.FloatType) THEN
  2189. result := SyntaxTree.NewRealValue(expression.position,int);
  2190. result.SetType(type);
  2191. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2192. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2193. result.SetType(type);
  2194. ELSIF (type IS SyntaxTree.SetType) THEN
  2195. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2196. result.SetType(type);
  2197. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2198. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2199. result.SetType(type);
  2200. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2201. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2202. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2203. END;
  2204. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2205. result.SetType(type);
  2206. ELSIF (type IS SyntaxTree.PortType) THEN
  2207. result := ConvertValue(position, expression, system.integerType);
  2208. ELSE
  2209. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2210. result := SyntaxTree.invalidExpression;
  2211. IF VerboseErrorMessage THEN
  2212. Printout.Info("expression",expression);
  2213. Printout.Info("type",type);
  2214. END;
  2215. END;
  2216. ELSIF IsRealValue(expression,real) THEN
  2217. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2218. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2219. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2222. int := ENTIERH(real);
  2223. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2224. result.SetType(type);
  2225. ELSIF (type IS SyntaxTree.FloatType) THEN
  2226. result := SyntaxTree.NewRealValue(position,real);
  2227. result.SetType(type);
  2228. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2229. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2230. result.SetType(type);
  2231. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2232. ELSIF (type IS SyntaxTree.PortType) THEN
  2233. result := ConvertValue(position, expression, system.integerType);
  2234. ELSE
  2235. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2236. result := SyntaxTree.invalidExpression;
  2237. END
  2238. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2239. IF (type IS SyntaxTree.ComplexType) THEN
  2240. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2241. result.SetType(type);
  2242. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2243. ELSE
  2244. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2245. result := SyntaxTree.invalidExpression;
  2246. END
  2247. ELSIF IsSetValue(expression,set) THEN
  2248. IF (type IS SyntaxTree.IntegerType) THEN
  2249. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2250. result.SetType(type);
  2251. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2252. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2253. result.SetType(type);
  2254. ELSIF (type IS SyntaxTree.PortType) THEN
  2255. result := ConvertValue(position, expression, system.integerType);
  2256. ELSE
  2257. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2258. result := SyntaxTree.invalidExpression;
  2259. END;
  2260. ELSIF IsStringValue(expression,string) THEN
  2261. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2262. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2263. result.SetType(type);
  2264. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2265. ELSE
  2266. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2267. result := SyntaxTree.invalidExpression;
  2268. END;
  2269. ELSIF IsCharacterValue(expression,char) THEN
  2270. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2271. string[0] := char; string[1] := 0X;
  2272. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2273. result := SyntaxTree.NewStringValue(expression.position,string);
  2274. result.SetType(type);
  2275. ELSIF (type IS SyntaxTree.ByteType) THEN
  2276. (* do not simply set the new type as this could invalidate types of constants *)
  2277. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2278. result.SetType(type)
  2279. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2280. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2281. result.SetType(type);
  2282. ELSIF (type IS SyntaxTree.SetType) THEN
  2283. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2284. result.SetType(type);
  2285. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2286. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2287. result.SetType(type);
  2288. ELSIF (type IS SyntaxTree.PortType) THEN
  2289. result := ConvertValue(position, expression, system.integerType);
  2290. ELSE
  2291. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2292. result := SyntaxTree.invalidExpression;
  2293. END;
  2294. ELSIF expression IS SyntaxTree.NilValue THEN
  2295. IF type IS SyntaxTree.AddressType THEN
  2296. result := SyntaxTree.NewIntegerValue(position,0);
  2297. result.SetType(type);
  2298. ELSE
  2299. result := expression;
  2300. END;
  2301. (* nothing to be done *)
  2302. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2303. result := MathArrayConversion(position, expression,type);
  2304. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2305. int := expression(SyntaxTree.EnumerationValue).value;
  2306. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2307. int := Global.ConvertSigned(int,system.SizeOf(type));
  2308. result := SyntaxTree.NewIntegerValue(position,int);
  2309. result.SetType(type);
  2310. ELSE
  2311. result := expression;
  2312. END;
  2313. (* nothing to be done *)
  2314. ELSE
  2315. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2316. IF VerboseErrorMessage THEN
  2317. Printout.Info("expression",expression);
  2318. Printout.Info("type",type);
  2319. END;
  2320. result := SyntaxTree.invalidExpression;
  2321. END;
  2322. RETURN result
  2323. END ConvertValue;
  2324. (**
  2325. return a conversion of an expression to a given type
  2326. - if expression is already of same type then return expression
  2327. - if incompatible conversion then report error and return invalidExpression
  2328. **)
  2329. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2330. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2331. BEGIN
  2332. type := type.resolved;
  2333. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2334. result := expression;
  2335. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2336. ELSIF expression = NIL THEN (* NIL expression *)
  2337. ELSIF expression.type = NIL THEN
  2338. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2339. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2340. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2341. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2342. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2343. ELSIF expression.resolved # NIL THEN (* value *)
  2344. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2345. IF value IS SyntaxTree.Value THEN
  2346. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2347. result.SetResolved(value(SyntaxTree.Value));
  2348. result.SetType(value.type);
  2349. ELSE
  2350. result := value
  2351. END;
  2352. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2353. expressionList := SyntaxTree.NewExpressionList();
  2354. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2355. typeDeclaration.SetDeclaredType(type);
  2356. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2357. typeSymbol.SetType(typeDeclaration.type);
  2358. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2359. expressionList.AddExpression(expression);
  2360. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2361. result.SetType(type);
  2362. ELSIF IsArrayStructuredObjectType(type) THEN
  2363. (* no type can be converted to an array-structured object type *)
  2364. HALT(100)
  2365. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2366. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2367. result := MathArrayConversion(position, expression,type);
  2368. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2369. expression := ConvertToMathArray(expression);
  2370. type := MathArrayStructureOfType(type);
  2371. result := MathArrayConversion(position, expression, type)
  2372. ELSE
  2373. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2374. END;
  2375. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2376. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2377. OR ~(type IS SyntaxTree.ArrayType) THEN
  2378. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2379. END;
  2380. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2381. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2382. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2383. (*skip, no conversion*)
  2384. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2385. (* skip, no conversion *)
  2386. ELSE
  2387. ASSERT(~(type IS SyntaxTree.RangeType));
  2388. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2389. ASSERT(type # NIL);
  2390. END;
  2391. RETURN result
  2392. END NewConversion;
  2393. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2394. BEGIN
  2395. IF CompatibleTo(system,expression.type, type) THEN
  2396. RETURN NewConversion(position, expression, type, NIL);
  2397. ELSE
  2398. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2399. RETURN SyntaxTree.invalidExpression
  2400. END;
  2401. END CompatibleConversion;
  2402. (**
  2403. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2404. **)
  2405. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2406. VAR leftType,rightType: SyntaxTree.Type;
  2407. BEGIN
  2408. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2409. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2410. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2411. (* error already handled *)
  2412. ELSE
  2413. leftType := left.type.resolved; rightType := right.type.resolved;
  2414. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2415. right := NewConversion(right.position, right, leftType, NIL);
  2416. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2417. left := NewConversion(left.position,left,rightType,NIL);
  2418. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2419. right := NewConversion(right.position, right, leftType, NIL);
  2420. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2421. left := NewConversion(left.position,left,rightType,NIL);
  2422. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2423. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2424. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2425. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2426. ELSIF
  2427. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2428. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2429. (* must be the case LONGREAL / COMPLEX ) *)
  2430. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2431. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2432. ELSE
  2433. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2434. END;
  2435. END;
  2436. END ConvertOperands;
  2437. (** find and return best operator matching to parameter list (nil, if none)
  2438. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2439. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2440. **)
  2441. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2442. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2443. identifier: SyntaxTree.Identifier;
  2444. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2445. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2446. BEGIN
  2447. operator := scope.firstOperator;
  2448. WHILE(operator # NIL) DO
  2449. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2450. procedureType := operator.type(SyntaxTree.ProcedureType);
  2451. distance := Distance(system, procedureType,actualParameters);
  2452. IF (distance < Infinity) THEN
  2453. IF returnType # NIL THEN
  2454. IF procedureType.returnType = NIL THEN
  2455. distance := Infinity
  2456. ELSE
  2457. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2458. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2459. END;
  2460. END;
  2461. END;
  2462. (*
  2463. IF distance < Infinity THEN
  2464. TRACE(distance, operator);
  2465. Printout.Info("potential operator",operator);
  2466. ELSE
  2467. Printout.Info("operator not possible",operator);
  2468. END;
  2469. *)
  2470. IF distance < bestDistance THEN
  2471. bestDistance := distance;
  2472. bestOperator := operator;
  2473. END;
  2474. END;
  2475. operator := operator.nextOperator;
  2476. END;
  2477. (*
  2478. Printout.Info("taken operator",bestOperator);
  2479. *)
  2480. END FindInScope;
  2481. BEGIN
  2482. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2483. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2484. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2485. import := currentScope.ownerModule.moduleScope.firstImport;
  2486. WHILE (bestDistance > 0) & (import # NIL) DO
  2487. IF import.module # NIL THEN
  2488. identifier := Global.GetIdentifier(operator,import.module.case);
  2489. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2490. END;
  2491. import := import.nextImport;
  2492. END;
  2493. RETURN bestOperator
  2494. END FindOperator;
  2495. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2496. BEGIN
  2497. currentScope := scope;
  2498. END SetCurrentScope;
  2499. (**
  2500. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2501. - handle LEN and DIM operator for array-structured object types
  2502. - find operator, if found then
  2503. - if in other module then add import designator
  2504. - create symbol designator for operator
  2505. - if error then return invalidExpression, if no operator then return NIL
  2506. **)
  2507. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2508. VAR
  2509. operator: SyntaxTree.Operator;
  2510. import: SyntaxTree.Import;
  2511. expression, result: SyntaxTree.Expression;
  2512. designator: SyntaxTree.Designator;
  2513. actualParameters, tempList: SyntaxTree.ExpressionList;
  2514. recordType: SyntaxTree.RecordType;
  2515. castReturnType : SyntaxTree.MathArrayType;
  2516. BEGIN
  2517. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2518. result := SyntaxTree.invalidExpression
  2519. ELSIF leftExpression = NIL THEN
  2520. result := NIL
  2521. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2522. (* LEN or DIM operator on array-structured object type *)
  2523. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2524. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2525. IF recordType.arrayAccessOperators.len = NIL THEN
  2526. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2527. result := SyntaxTree.invalidExpression
  2528. ELSE
  2529. ASSERT(leftExpression IS SyntaxTree.Designator);
  2530. designator := leftExpression(SyntaxTree.Designator);
  2531. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2532. ASSERT(expression IS SyntaxTree.Designator);
  2533. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2534. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2535. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2536. result := designator
  2537. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2538. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2539. tempList := SyntaxTree.NewExpressionList();
  2540. tempList.AddExpression(rightExpression);
  2541. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2542. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2543. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2544. tempList := SyntaxTree.NewExpressionList();
  2545. tempList.AddExpression(designator);
  2546. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2547. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2548. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2549. END
  2550. END;
  2551. ELSE
  2552. (* import OCArrayBase if needed *)
  2553. IF ~arrayBaseImported THEN
  2554. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2555. (* operators on complex numbers *)
  2556. ImportModule(Global.ArrayBaseName,position);
  2557. arrayBaseImported := TRUE;
  2558. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2559. IF op = Global.Dim THEN
  2560. (* not existing in OCArrayBase *)
  2561. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2562. (* not existing in OCArrayBase *)
  2563. ELSE
  2564. ImportModule(Global.ArrayBaseName,position);
  2565. arrayBaseImported := TRUE;
  2566. END
  2567. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2568. ImportModule(Global.ArrayBaseName,position);
  2569. arrayBaseImported := TRUE
  2570. END;
  2571. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2572. (* LEN(RANGE) *)
  2573. ImportModule(Global.ArrayBaseName,position);
  2574. arrayBaseImported := TRUE;
  2575. END;
  2576. END;
  2577. actualParameters := SyntaxTree.NewExpressionList();
  2578. actualParameters.AddExpression(leftExpression);
  2579. IF rightExpression # NIL THEN
  2580. actualParameters.AddExpression(rightExpression)
  2581. END;
  2582. operator := FindOperator(system,op,actualParameters,resultType);
  2583. IF operator # NIL THEN
  2584. designator := NIL;
  2585. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2586. import := currentScope.ownerModule.moduleScope.firstImport;
  2587. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2588. import := import.nextImport;
  2589. END;
  2590. expression := NewSymbolDesignator(position,NIL,import);
  2591. designator := expression(SyntaxTree.Designator);
  2592. END;
  2593. expression := NewSymbolDesignator(position,designator,operator);
  2594. designator := expression(SyntaxTree.Designator);
  2595. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2596. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2597. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2598. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2599. result.SetType(castReturnType);
  2600. END;
  2601. ELSE
  2602. result := NIL;
  2603. END;
  2604. END;
  2605. RETURN result
  2606. END NewOperatorCall;
  2607. (** check and resolve binary expression **)
  2608. (*! clean up *)
  2609. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2610. VAR left,right,result: SyntaxTree.Expression;
  2611. leftType, rightType: SyntaxTree.Type;
  2612. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2613. cl,cr: CHAR;
  2614. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2615. type: SyntaxTree.Type;
  2616. value: SyntaxTree.Value;
  2617. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2618. integerConstantFolding: BOOLEAN;
  2619. list: SyntaxTree.ExpressionList;
  2620. PROCEDURE NewBool(v: BOOLEAN);
  2621. BEGIN
  2622. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2623. value.SetType(system.booleanType);
  2624. result.SetResolved(value);
  2625. type := system.booleanType
  2626. END NewBool;
  2627. PROCEDURE NewSet(v: SET);
  2628. BEGIN
  2629. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2630. value.SetType(system.setType);
  2631. result.SetResolved(value);
  2632. type := system.setType;
  2633. END NewSet;
  2634. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2635. BEGIN
  2636. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2637. (* type cast to "larger" type only if the value is still in the range *)
  2638. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2639. value.SetType(t);
  2640. END;
  2641. result.SetResolved(value);
  2642. type := value.type;
  2643. END NewInteger;
  2644. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2645. BEGIN
  2646. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2647. value.SetType(t);
  2648. result.SetResolved(value);
  2649. type := t;
  2650. END NewReal;
  2651. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2652. BEGIN
  2653. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2654. value.SetType(t);
  2655. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2656. result.SetResolved(value);
  2657. type := t;
  2658. END NewComplex;
  2659. BEGIN
  2660. type := SyntaxTree.invalidType;
  2661. left := ResolveExpression(binaryExpression.left);
  2662. right := ResolveExpression(binaryExpression.right);
  2663. binaryExpression.SetLeft(left);
  2664. binaryExpression.SetRight(right);
  2665. result := binaryExpression;
  2666. operator := binaryExpression.operator;
  2667. IF ~system.operatorDefined[operator] THEN
  2668. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2669. result := SyntaxTree.invalidExpression;
  2670. RETURN
  2671. END;
  2672. IF left.type = NIL THEN
  2673. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2674. result := SyntaxTree.invalidExpression;
  2675. RETURN;
  2676. END;
  2677. IF right.type = NIL THEN
  2678. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2679. result := SyntaxTree.invalidExpression;
  2680. RETURN;
  2681. END;
  2682. leftType := left.type.resolved; rightType := right.type.resolved;
  2683. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2684. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2685. END;
  2686. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2687. list := SyntaxTree.NewExpressionList();
  2688. list.AddExpression(right);
  2689. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2690. END;
  2691. IF operatorCall # NIL THEN
  2692. result := operatorCall;
  2693. type := operatorCall.type;
  2694. (* admissible operators:
  2695. Times, Plus, Minus numeric numeric numeric
  2696. set set set
  2697. Slash numeric numeric real /complex
  2698. set set set
  2699. Div , Mod integer integer integer
  2700. And, Or bool bool bool
  2701. Equal, Unequal basic basic bool
  2702. pointer pointer bool
  2703. object object bool
  2704. record record bool
  2705. string string bool
  2706. enumerator enumerator bool
  2707. Less, LessEqual,
  2708. Greater, GreaterEqual integer/real integer/real bool
  2709. enumerator enumerator bool
  2710. In integer set bool
  2711. Is pointer type bool
  2712. object type bool
  2713. record type bool
  2714. Upto: special abbreviation for a..b
  2715. *)
  2716. ELSIF (left.type = NIL) THEN
  2717. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2718. D.Str("nil type in "); D.Type(left); D.Ln;
  2719. result := SyntaxTree.invalidExpression;
  2720. ELSIF (right.type = NIL) THEN
  2721. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2722. result := SyntaxTree.invalidExpression;
  2723. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2724. result := SyntaxTree.invalidExpression;
  2725. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2726. HALT(100);
  2727. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2728. type := system.booleanType;
  2729. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2730. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2731. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2732. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2733. IF VerboseErrorMessage THEN
  2734. Printout.Info("left",left);
  2735. Printout.Info("right",right);
  2736. END;
  2737. ELSIF IsUnsafePointer(left.type) THEN
  2738. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2739. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2740. NewBool(TRUE)
  2741. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2742. NewBool(TRUE);
  2743. ELSIF IsUnextensibleRecord(left) THEN
  2744. NewBool(FALSE)
  2745. END
  2746. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2747. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2748. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2749. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2750. ELSIF operator = Scanner.In THEN (* left IN right *)
  2751. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2752. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2753. NewBool(il IN sr);
  2754. ELSE
  2755. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2756. left := NewConversion(left.position, left, system.longintType,NIL);
  2757. binaryExpression.SetLeft(left)
  2758. END;
  2759. type := system.booleanType;
  2760. END
  2761. ELSE
  2762. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2763. END
  2764. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2765. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2766. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2767. END;
  2768. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2769. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2770. END
  2771. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2772. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2773. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2774. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2775. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2776. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2777. ConvertOperands(left, right);
  2778. binaryExpression.SetLeft(left);
  2779. binaryExpression.SetRight(right);
  2780. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2781. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2782. END;
  2783. type := system.booleanType;
  2784. ELSE
  2785. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2786. END
  2787. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2788. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2789. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2790. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2791. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2792. CASE operator OF
  2793. |Scanner.Equal: NewBool(strl^=strr^);
  2794. |Scanner.Unequal:NewBool(strl^#strr^);
  2795. |Scanner.Less: NewBool(strl^<strr^);
  2796. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2797. |Scanner.Greater: NewBool(strl^>strr^);
  2798. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2799. ELSE
  2800. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2801. END;
  2802. END;
  2803. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2804. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2805. type := system.booleanType
  2806. ELSE
  2807. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2808. END;
  2809. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2810. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2811. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2812. type := system.booleanType;
  2813. ELSE
  2814. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2815. END
  2816. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2817. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2818. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2819. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2820. type := system.booleanType
  2821. ELSE
  2822. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2823. END;
  2824. ELSE
  2825. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2826. END;
  2827. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2828. type := system.booleanType;
  2829. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2830. type := system.booleanType;
  2831. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2832. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2833. THEN
  2834. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2835. IF (leftType # rightType) THEN
  2836. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2837. ConvertOperands(left,right); (* operands must be of the same type here *)
  2838. END;
  2839. binaryExpression.SetLeft(left);
  2840. binaryExpression.SetRight(right);
  2841. leftType := left.type.resolved;
  2842. rightType := right.type.resolved;
  2843. END;
  2844. type := leftType;
  2845. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2846. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2847. IF VerboseErrorMessage THEN
  2848. Printout.Info("left",left);
  2849. Printout.Info("right",right);
  2850. END;
  2851. ELSIF IsIntegerType(leftType) THEN
  2852. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2853. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2854. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2855. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2856. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2857. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2858. END;
  2859. END;
  2860. (* constant folding *)
  2861. (* bootstrap64
  2862. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2863. CASE operator OF
  2864. |Scanner.Plus: NewInteger(il+ir,left.type);
  2865. |Scanner.Minus: NewInteger(il-ir,left.type);
  2866. |Scanner.Times: NewInteger(il*ir,left.type);
  2867. |Scanner.Slash:
  2868. IF ir # 0 THEN
  2869. NewReal(il/ir, system.realType);
  2870. END;
  2871. |Scanner.Mod:
  2872. IF ir > 0 THEN
  2873. NewInteger(il MOD ir,left.type);
  2874. END;
  2875. |Scanner.Div:
  2876. IF ir > 0 THEN
  2877. NewInteger(il DIV ir,left.type);
  2878. END;
  2879. |Scanner.Equal: NewBool(il=ir);
  2880. |Scanner.Unequal:NewBool(il#ir);
  2881. |Scanner.Less: NewBool(il<ir);
  2882. |Scanner.LessEqual: NewBool(il<=ir);
  2883. |Scanner.Greater: NewBool(il>ir);
  2884. |Scanner.GreaterEqual: NewBool(il>=ir);
  2885. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2886. END;
  2887. ELS*)
  2888. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2889. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2890. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2891. CASE operator OF
  2892. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2893. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2894. |Scanner.Times: NewInteger(hl*hr,left.type);
  2895. |Scanner.Slash:
  2896. IF hr = 0 THEN
  2897. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2898. ELSE
  2899. IF type.sizeInBits = 64 THEN
  2900. NewReal(hl/hr,system.longrealType);
  2901. ELSE
  2902. NewReal(hl/hr,system.realType)
  2903. END
  2904. END;
  2905. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2906. |Scanner.Mod:
  2907. IF hr = 0 THEN
  2908. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2909. ELSE
  2910. NewInteger(hl MOD hr, left.type);
  2911. (* bootstrap64
  2912. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2913. *)
  2914. END;
  2915. |Scanner.Div:
  2916. IF hr = 0 THEN
  2917. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2918. ELSE
  2919. NewInteger(hl DIV hr, left.type);
  2920. (* bootstrap64
  2921. NewInteger(Machine.DivH(hl,hr),left.type);
  2922. *)
  2923. END;
  2924. (* *)
  2925. |Scanner.Equal: NewBool(hl=hr);
  2926. |Scanner.Unequal: NewBool(hl#hr);
  2927. |Scanner.Less: NewBool(hl<hr);
  2928. |Scanner.LessEqual: NewBool(hl<=hr);
  2929. |Scanner.Greater: NewBool(hl>hr);
  2930. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2931. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2932. END;
  2933. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2934. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2935. type := left.type
  2936. ELSIF (operator = Scanner.Slash) THEN
  2937. left := NewConversion(left.position,left,system.realType,NIL);
  2938. right := NewConversion(right.position,right,system.realType,NIL);
  2939. binaryExpression.SetLeft(left);
  2940. binaryExpression.SetRight(right);
  2941. type := system.realType
  2942. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2943. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2944. type := system.booleanType
  2945. ELSE
  2946. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2947. END;
  2948. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2949. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2950. CASE operator OF
  2951. |Scanner.Plus: NewReal(rl+rr,leftType);
  2952. |Scanner.Minus: NewReal(rl-rr,leftType);
  2953. |Scanner.Times:NewReal(rl*rr,leftType);
  2954. |Scanner.Slash:
  2955. IF rr = 0 THEN
  2956. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2957. ELSE
  2958. NewReal(rl/rr,leftType);
  2959. END
  2960. |Scanner.Equal: NewBool(rl=rr);
  2961. |Scanner.Unequal: NewBool(rl#rr);
  2962. |Scanner.Less: NewBool(rl<rr);
  2963. |Scanner.LessEqual: NewBool(rl<=rr);
  2964. |Scanner.Greater: NewBool(rl>rr);
  2965. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2966. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2967. END;
  2968. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2969. type := left.type
  2970. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2971. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2972. type := system.booleanType
  2973. ELSE
  2974. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2975. IF VerboseErrorMessage THEN
  2976. Printout.Info("left",left);
  2977. Printout.Info("right",right);
  2978. END;
  2979. END;
  2980. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2981. CASE operator OF
  2982. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2983. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2984. ELSE
  2985. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2986. IF VerboseErrorMessage THEN
  2987. Printout.Info("left", left);
  2988. Printout.Info("right", right)
  2989. END;
  2990. END;
  2991. IF ~error THEN
  2992. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2993. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2994. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2995. (* do constant folding *)
  2996. CASE operator OF
  2997. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2998. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2999. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3000. |Scanner.Slash:
  3001. divisor := c * c + d * d;
  3002. ASSERT(divisor # 0);
  3003. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3004. |Scanner.Equal: NewBool((a = c) & (b = d))
  3005. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3006. END
  3007. END
  3008. END
  3009. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3010. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3011. CASE operator OF
  3012. |Scanner.And: NewBool(bl & br);
  3013. |Scanner.Or: NewBool(bl OR br);
  3014. |Scanner.Equal: NewBool(bl = br);
  3015. |Scanner.Unequal: NewBool(bl # br);
  3016. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3017. END;
  3018. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3019. type := system.booleanType
  3020. ELSE
  3021. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3022. END;
  3023. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3024. (* constant folding *)
  3025. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3026. IF operator = Scanner.Equal THEN
  3027. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3028. ELSIF operator = Scanner.Unequal THEN
  3029. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3030. END;
  3031. END;
  3032. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3033. type := system.booleanType;
  3034. ELSE
  3035. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3036. END;
  3037. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3038. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3039. CASE operator OF
  3040. |Scanner.Plus: NewSet(sl + sr);
  3041. |Scanner.Minus: NewSet(sl - sr);
  3042. |Scanner.Times: NewSet(sl * sr);
  3043. |Scanner.Slash: NewSet(sl / sr);
  3044. |Scanner.Equal: NewBool(sl=sr);
  3045. |Scanner.Unequal: NewBool(sl#sr);
  3046. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3047. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3048. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3049. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3050. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3051. END;
  3052. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3053. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3054. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3055. THEN
  3056. type := system.booleanType
  3057. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3058. type := left.type
  3059. ELSE
  3060. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3061. END;
  3062. ELSIF IsCharacterType(left.type) THEN
  3063. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3064. CASE operator OF
  3065. |Scanner.Equal: NewBool(cl=cr);
  3066. |Scanner.Unequal: NewBool(cl#cr);
  3067. |Scanner.Less: NewBool(cl<cr);
  3068. |Scanner.LessEqual: NewBool(cl<=cr);
  3069. |Scanner.Greater: NewBool(cl>cr);
  3070. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3071. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3072. END;
  3073. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3074. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3075. type := system.booleanType
  3076. ELSE
  3077. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3078. END;
  3079. ELSE
  3080. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3081. END;
  3082. ELSE
  3083. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3084. END;
  3085. IF type = SyntaxTree.invalidType THEN
  3086. result := SyntaxTree.invalidExpression
  3087. ELSE
  3088. result.SetType(type)
  3089. END;
  3090. resolvedExpression := result
  3091. END VisitBinaryExpression;
  3092. (** resolve a range expression of the from <<first .. last BY step>>
  3093. - depending on the context different things are checked:
  3094. ArrayIndex:
  3095. - components must be integers
  3096. - replace missing lower bound with 0
  3097. - replace missing upper bound with MAX(LONGINT)
  3098. - replace missing step size with 1
  3099. SetElement:
  3100. - components must be integers
  3101. - replace missing lower bound with 0
  3102. - replace missing upper bound with MAX(SET)
  3103. - must not have step size
  3104. CaseGuard:
  3105. - components must be constant
  3106. - components must be integers or characters
  3107. - must have lower and upper bound present
  3108. - components are made compatible
  3109. - must not have step size
  3110. - if error: return invalidExpression
  3111. **)
  3112. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3113. VAR
  3114. hasError: BOOLEAN;
  3115. first, last, step: SyntaxTree.Expression;
  3116. BEGIN
  3117. hasError := FALSE;
  3118. first := x.first;
  3119. last := x.last;
  3120. step := x.step;
  3121. (* check lower bound *)
  3122. IF x.context = SyntaxTree.CaseGuard THEN
  3123. IF first = NIL THEN
  3124. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3125. hasError := TRUE
  3126. ELSE
  3127. first := ResolveExpression(first);
  3128. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3129. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3130. hasError := TRUE
  3131. ELSE
  3132. IF first IS SyntaxTree.StringValue THEN
  3133. (* add conversion from string to character *)
  3134. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3135. END
  3136. END;
  3137. (* check if expression is constant *)
  3138. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3139. (* error already reported *)
  3140. hasError := TRUE
  3141. END
  3142. END
  3143. ELSE (* ArrayIndex, SetElement *)
  3144. IF first = NIL THEN
  3145. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3146. END;
  3147. first := ResolveExpression(first);
  3148. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3149. first := NewConversion(first.position, first, system.longintType, NIL)
  3150. ELSE
  3151. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3152. hasError := TRUE
  3153. END
  3154. END;
  3155. (* check upper bound *)
  3156. IF x.context = SyntaxTree.CaseGuard THEN
  3157. IF last = NIL THEN
  3158. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3159. hasError := TRUE
  3160. ELSE
  3161. last := ResolveExpression(last);
  3162. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3163. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3164. hasError := TRUE
  3165. ELSE
  3166. IF last IS SyntaxTree.StringValue THEN
  3167. (* add conversion from string to character *)
  3168. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3169. END
  3170. END;
  3171. (* check if expression is constant *)
  3172. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3173. (* error already reported *)
  3174. hasError := TRUE
  3175. ELSE
  3176. (* try to make lower and upper bound compatible *)
  3177. ConvertOperands(first, last);
  3178. IF first.type.resolved # last.type.resolved THEN
  3179. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3180. hasError := TRUE
  3181. END
  3182. END
  3183. END
  3184. ELSE (* ArrayIndex, SetElement *)
  3185. IF last = NIL THEN
  3186. IF x.context = SyntaxTree.ArrayIndex THEN
  3187. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3188. ELSE
  3189. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3190. END
  3191. END;
  3192. last := ResolveExpression(last);
  3193. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3194. last := NewConversion(last.position, last, system.longintType, NIL)
  3195. ELSE
  3196. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3197. hasError := TRUE
  3198. END
  3199. END;
  3200. (* check step size *)
  3201. IF x.context = SyntaxTree.ArrayIndex THEN
  3202. IF step = NIL THEN
  3203. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3204. END;
  3205. step := ResolveExpression(step);
  3206. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3207. step := NewConversion(step.position, step, system.longintType, NIL)
  3208. ELSE
  3209. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3210. hasError := TRUE
  3211. END
  3212. ELSE (* SetElement, CaseGuard *)
  3213. IF step # NIL THEN
  3214. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3215. hasError := TRUE
  3216. END
  3217. END;
  3218. IF hasError THEN
  3219. resolvedExpression := SyntaxTree.invalidExpression
  3220. ELSE
  3221. x.SetFirst(first);
  3222. x.SetLast(last);
  3223. x.SetStep(step);
  3224. x.SetType(system.rangeType);
  3225. resolvedExpression := x;
  3226. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3227. END
  3228. END VisitRangeExpression;
  3229. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3230. BEGIN
  3231. x.SetType(NIL);
  3232. resolvedExpression := x;
  3233. END VisitTensorRangeExpression;
  3234. (** resolve the expression d and return result as designator
  3235. - resolve expression
  3236. - if expression is a designator then return designator else error message and return invalidDesignator
  3237. **)
  3238. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3239. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3240. BEGIN
  3241. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3242. resolved := ResolveExpression(d);
  3243. IF resolved = SyntaxTree.invalidExpression THEN
  3244. (* error should already have been reported *)
  3245. result := SyntaxTree.invalidDesignator;
  3246. ELSIF resolved IS SyntaxTree.Designator THEN
  3247. result := resolved(SyntaxTree.Designator);
  3248. ELSE
  3249. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3250. result := SyntaxTree.invalidDesignator;
  3251. END;
  3252. (* result.type might be nil. *)
  3253. RETURN result
  3254. END ResolveDesignator;
  3255. (**
  3256. symbol designator generated in this module
  3257. nothing to be resolved
  3258. **)
  3259. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3260. BEGIN
  3261. resolvedExpression := x;
  3262. END VisitSymbolDesignator;
  3263. (**
  3264. self designator generated in this module
  3265. nothing to be resolved
  3266. **)
  3267. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3268. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3269. BEGIN
  3270. (* check if in record scope *)
  3271. scope := currentScope;
  3272. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3273. scope := scope.outerScope;
  3274. END;
  3275. IF scope = NIL THEN (* in module scope *)
  3276. x.SetType(system.anyType);
  3277. ELSIF scope IS SyntaxTree.CellScope THEN
  3278. cell := scope(SyntaxTree.CellScope).ownerCell;
  3279. x.SetType(cell);
  3280. ELSE (* in record scope *)
  3281. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3282. IF (record # NIL) & (record.pointerType # NIL) THEN
  3283. type := ResolveType(record.pointerType);
  3284. x.SetType(type);
  3285. ELSE
  3286. x.SetType(record);
  3287. END;
  3288. END;
  3289. resolvedExpression := x;
  3290. END VisitSelfDesignator;
  3291. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3292. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3293. BEGIN
  3294. scope := currentScope;
  3295. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3296. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3297. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3298. returnType := procedureType.returnType;
  3299. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3300. THEN
  3301. x.SetType(returnType);
  3302. ELSE
  3303. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3304. x.SetType(SyntaxTree.invalidType);
  3305. END;
  3306. ELSE
  3307. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3308. x.SetType(SyntaxTree.invalidType);
  3309. END;
  3310. x.SetAssignable(TRUE);
  3311. resolvedExpression := x;
  3312. END VisitResultDesignator;
  3313. (**
  3314. return symbol designator as an expression
  3315. - if symbol is a constant then return the constant value expression
  3316. - else
  3317. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3318. - if symbol is a guarded variable then return a TypeGuardDesignator
  3319. - else return a symbol designator
  3320. **)
  3321. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3322. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3323. guardType: SyntaxTree.Type;
  3324. BEGIN
  3325. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3326. result := SyntaxTree.invalidExpression;
  3327. ASSERT(symbol # NIL);
  3328. (*
  3329. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3330. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3331. Error(position,Diagnostics.Invalid,"type not allowed here");
  3332. ELS *)
  3333. (* not needed any more as values are stored in the expression
  3334. IF symbol IS SyntaxTree.Constant THEN
  3335. result := symbol(SyntaxTree.Constant).value
  3336. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3337. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3338. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3339. ELSE
  3340. result := symbol(SyntaxTree.Constant).value
  3341. END;
  3342. ELSE
  3343. *)
  3344. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3345. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3346. THEN
  3347. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3348. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3349. left := NewDereferenceDesignator(position,left);
  3350. left.SetHidden(TRUE);
  3351. END;
  3352. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3353. scope := currentScope;
  3354. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3355. scope := scope.outerScope;
  3356. END;
  3357. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3358. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3359. END;
  3360. END;
  3361. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3362. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3363. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3364. ELSE
  3365. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3366. END;
  3367. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3368. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3369. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3370. result.SetType(symbol.type);
  3371. result.SetAssignable(assignable);
  3372. symbol.MarkUsed;
  3373. IF symbol IS SyntaxTree.Constant THEN
  3374. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3375. END;
  3376. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3377. variableAccessed := TRUE
  3378. END;
  3379. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3380. IF GetGuard(symbol,guardType) THEN
  3381. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3382. END;
  3383. END;
  3384. ASSERT(result.type # NIL);
  3385. RETURN result
  3386. END NewSymbolDesignator;
  3387. (** check and resolve an identifier designator "identifier"
  3388. - if identifier = self then return SelfDesignator
  3389. - else find symbol in current scope
  3390. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3391. **)
  3392. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3393. VAR symbol: SyntaxTree.Symbol;
  3394. BEGIN
  3395. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3396. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3397. IF symbol # NIL THEN
  3398. ResolveSymbol(symbol);
  3399. ASSERT(symbol.type # NIL);
  3400. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3401. ELSE
  3402. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3403. IF VerboseErrorMessage THEN
  3404. Printout.Info("undeclared identifier designator",identifierDesignator);
  3405. END;
  3406. resolvedExpression := SyntaxTree.invalidDesignator;
  3407. END;
  3408. END VisitIdentifierDesignator;
  3409. (** check and resolve a selector designator of the form left.designator
  3410. - if left is a pointer type then do auto dereferenciation
  3411. - left denotes a search scope:
  3412. - if left type is import type then set search scope to respective module
  3413. - if left type is enumeration type then set search scope to respective enumeration scope
  3414. - elsif left type is record type then set search scope to record scope
  3415. - search symbol in computed scope
  3416. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3417. **)
  3418. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3419. VAR
  3420. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3421. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3422. BEGIN
  3423. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3424. left := ResolveDesignator(selectorDesignator.left);
  3425. result := SyntaxTree.invalidDesignator;
  3426. IF left # NIL THEN
  3427. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3428. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3429. END;
  3430. scope := NIL;
  3431. IF left.type = NIL THEN
  3432. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3433. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3434. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3435. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3436. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3437. module := symbol(SyntaxTree.Import).module;
  3438. IF module # NIL THEN
  3439. scope := module.moduleScope
  3440. ELSE
  3441. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3442. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3443. END;
  3444. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3445. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3446. ASSERT(scope # NIL)
  3447. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3448. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3449. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3450. IF type IS SyntaxTree.EnumerationType THEN
  3451. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3452. ELSE
  3453. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3454. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3455. END;
  3456. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3457. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3458. ELSE
  3459. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3460. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3461. END;
  3462. symbol := NIL;
  3463. IF scope # NIL THEN
  3464. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3465. IF symbol # NIL THEN
  3466. ResolveSymbol(symbol);
  3467. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3468. symbol.MarkUsed
  3469. ELSE
  3470. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3471. IF VerboseErrorMessage THEN
  3472. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3473. Printout.Info("scope", scope);
  3474. Printout.Info("left", left);
  3475. Printout.Info("undeclared identifier",selectorDesignator);
  3476. Printout.Info("left resolved designator",left);
  3477. END
  3478. END;
  3479. END;
  3480. END;
  3481. resolvedExpression := result;
  3482. END VisitSelectorDesignator;
  3483. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3484. VAR len,idx: LONGINT;
  3485. BEGIN
  3486. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3487. IF idx < 0 THEN
  3488. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3489. ELSE
  3490. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3491. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3492. END;
  3493. END;
  3494. END;
  3495. END IndexCheck;
  3496. (*
  3497. - if index designator has not type, use newBaseType as its type
  3498. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3499. - special rule: if static array of dynamic array occurs, make it all dynamic
  3500. index designator type: new base type: new index designator type:
  3501. NIL z z
  3502. ARRAY [x, y] z ARRAY [x, y] OF z
  3503. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3504. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3505. *)
  3506. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3507. VAR
  3508. mathArrayType: SyntaxTree.MathArrayType;
  3509. makeDynamic: BOOLEAN;
  3510. BEGIN
  3511. IF indexDesignator.type = NIL THEN
  3512. indexDesignator.SetType(newBaseType)
  3513. ELSE
  3514. (* index designator must be a of math array type *)
  3515. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3516. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3517. (* determine if all arrays have to be made dynamic *)
  3518. makeDynamic :=
  3519. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3520. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3521. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3522. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3523. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3524. END;
  3525. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3526. mathArrayType.SetArrayBase(newBaseType)
  3527. END
  3528. END SetIndexBaseType;
  3529. (** check and append index list element to index designator of math array
  3530. - check validity of single index or array range
  3531. - compute new type
  3532. - if range then create new array type (calculate length of resulting array)
  3533. - otherwise take sourceArray.arrayBase as new type
  3534. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3535. **)
  3536. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3537. VAR
  3538. targetArray: SyntaxTree.MathArrayType;
  3539. first, last, step: SyntaxTree.Expression;
  3540. firstValue, lastValue, stepValue, length: LONGINT;
  3541. rangeExpression: SyntaxTree.RangeExpression;
  3542. BEGIN
  3543. IF indexListItem.type = SyntaxTree.invalidType THEN
  3544. (* error already handled *)
  3545. indexDesignator.parameters.AddExpression(indexListItem)
  3546. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3547. indexDesignator.HasRange;
  3548. indexDesignator.HasTensorRange;
  3549. indexDesignator.parameters.AddExpression(indexListItem);
  3550. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3551. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3552. IndexCheck(indexListItem, sourceArray.length);
  3553. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3554. indexDesignator.parameters.AddExpression(indexListItem)
  3555. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3556. indexDesignator.HasRange;
  3557. (* if the range is given as an array range expression, check the validity of its components *)
  3558. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3559. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3560. first := rangeExpression.first;
  3561. last := rangeExpression.last;
  3562. step := rangeExpression.step;
  3563. (* perform static checks on range components *)
  3564. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3565. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3566. END;
  3567. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3568. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3569. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3570. END
  3571. END;
  3572. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3573. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3574. END;
  3575. (* add conversions to size type *)
  3576. (* TODO: needed? *)
  3577. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3578. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3579. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3580. END;
  3581. IF indexDesignator.hasTensorRange THEN
  3582. (* the index designator's base type is a tensor: leave it as is *)
  3583. ELSE
  3584. (* append a new math array to the index designator's base type *)
  3585. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3586. IF ~error THEN
  3587. (*
  3588. (* optimization: calculate length of target array for static ranges *)
  3589. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3590. IF IsStaticallyOpenRange(rangeExpression) THEN
  3591. (* range is open ('*'): reuse source array length as target array length *)
  3592. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3593. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3594. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3595. IF lastValue = MAX(LONGINT) THEN
  3596. IF IsIntegerValue(sourceArray.length, length) THEN
  3597. lastValue := length - 1;
  3598. isStaticTargetArrayLength := TRUE
  3599. ELSE
  3600. isStaticTargetArrayLength := FALSE
  3601. END
  3602. ELSE
  3603. isStaticTargetArrayLength := TRUE
  3604. END;
  3605. IF isStaticTargetArrayLength THEN
  3606. (* calculate static target array length *)
  3607. IF firstValue > lastValue THEN
  3608. length := 0
  3609. ELSE
  3610. length := 1 + lastValue - firstValue;
  3611. IF length MOD stepValue = 0 THEN
  3612. length := length DIV stepValue
  3613. ELSE
  3614. length := length DIV stepValue + 1
  3615. END
  3616. END;
  3617. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3618. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3619. ASSERT(targetArray.form = SyntaxTree.Static)
  3620. END
  3621. END
  3622. END
  3623. *)
  3624. END;
  3625. SetIndexBaseType(indexDesignator, targetArray)
  3626. END;
  3627. indexDesignator.parameters.AddExpression(indexListItem)
  3628. ELSE
  3629. Error(position, Diagnostics.Invalid,"invalid index list item");
  3630. END;
  3631. END AppendMathIndex;
  3632. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3633. VAR parameters: SyntaxTree.ExpressionList;
  3634. BEGIN
  3635. parameters := index.parameters;
  3636. IF (expression.type = NIL) THEN
  3637. Error(position,Diagnostics.Invalid,"invalid index");
  3638. ELSIF IsIntegerType(expression.type.resolved) THEN
  3639. IF over IS SyntaxTree.ArrayType THEN
  3640. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3641. ELSIF over IS SyntaxTree.StringType THEN
  3642. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3643. END;
  3644. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3645. parameters.AddExpression(expression);
  3646. ELSE
  3647. Error(position,Diagnostics.Invalid,"invalid index");
  3648. END;
  3649. END AppendIndex;
  3650. (** convert an expression to math array type
  3651. if expression is of math array type: return expression itself
  3652. if expression is of array-structured object type: return an index operator call on it
  3653. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3654. otherwise: return invalid expression
  3655. **)
  3656. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3657. VAR
  3658. result: SyntaxTree.Expression;
  3659. mathArrayType: SyntaxTree.MathArrayType;
  3660. BEGIN
  3661. IF expression.type = NIL THEN
  3662. result := SyntaxTree.invalidExpression
  3663. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3664. (* expression of math array type *)
  3665. result := expression
  3666. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3667. (* expression of array-structured object type *)
  3668. mathArrayType := MathArrayStructureOfType(expression.type);
  3669. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3670. ELSE
  3671. result := SyntaxTree.invalidExpression
  3672. END;
  3673. RETURN result
  3674. END ConvertToMathArray;
  3675. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3676. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3677. VAR
  3678. result: SyntaxTree.ExpressionList;
  3679. i: LONGINT;
  3680. BEGIN
  3681. result := SyntaxTree.NewExpressionList();
  3682. FOR i := 1 TO itemCount DO
  3683. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3684. END;
  3685. RETURN result
  3686. END ListOfOpenRanges;
  3687. (** create a procedure call designator for an index operator call on an array-structured object type
  3688. - use given index list as actual parameters
  3689. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3690. **)
  3691. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3692. VAR
  3693. operator: SyntaxTree.Operator;
  3694. expression: SyntaxTree.Expression;
  3695. actualParameters, tempList: SyntaxTree.ExpressionList;
  3696. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3697. result, tempDesignator: SyntaxTree.Designator;
  3698. recordType: SyntaxTree.RecordType;
  3699. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3700. i, hashValue, indexListSize, indexListKind: LONGINT;
  3701. castReturnType: SyntaxTree.MathArrayType;
  3702. BEGIN
  3703. ASSERT(IsArrayStructuredObjectType(left.type));
  3704. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3705. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3706. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3707. indexListSize := indexList.Length();
  3708. indexListKind := 0;
  3709. containsNonRange := FALSE;
  3710. FOR i := 0 TO indexList.Length() - 1 DO
  3711. indexListKind := indexListKind * 2;
  3712. expression := indexList.GetExpression(i);
  3713. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3714. INC(indexListKind)
  3715. ELSE
  3716. containsNonRange := TRUE
  3717. END
  3718. END;
  3719. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3720. (* select applicable index operator
  3721. - try to look up optimal index operator
  3722. - if not present, use operator on ranges
  3723. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3724. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3725. *)
  3726. usesGeneralOperator := FALSE;
  3727. IF rhs # NIL THEN
  3728. (* write operator *)
  3729. IF hashValue = -1 THEN
  3730. operator := NIL
  3731. ELSE
  3732. operator := recordType.arrayAccessOperators.write[hashValue];
  3733. END;
  3734. IF operator = NIL THEN
  3735. usesPureRangeOperator := TRUE;
  3736. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3737. operator := recordType.arrayAccessOperators.generalWrite;
  3738. usesGeneralOperator := TRUE
  3739. ELSE
  3740. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3741. operator := recordType.arrayAccessOperators.write[hashValue];
  3742. END
  3743. END
  3744. ELSE
  3745. (* read operator *)
  3746. IF hashValue = -1 THEN
  3747. operator := NIL
  3748. ELSE
  3749. operator := recordType.arrayAccessOperators.read[hashValue];
  3750. END;
  3751. IF operator = NIL THEN
  3752. usesPureRangeOperator := TRUE;
  3753. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3754. operator := recordType.arrayAccessOperators.generalRead;
  3755. usesGeneralOperator := TRUE
  3756. ELSE
  3757. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3758. operator := recordType.arrayAccessOperators.read[hashValue];
  3759. END
  3760. END
  3761. END;
  3762. IF operator = NIL THEN
  3763. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3764. result := SyntaxTree.invalidDesignator;
  3765. ELSE
  3766. (* determine if reshaping is needed *)
  3767. needsReshaping := containsNonRange & usesPureRangeOperator;
  3768. (* import OCArrayBase if reshaping is needed *)
  3769. IF needsReshaping & ~arrayBaseImported THEN
  3770. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3771. arrayBaseImported := TRUE
  3772. END;
  3773. (* add the index list item to the list of actual parameters
  3774. - for general operators: add a single inline array containing the index list items as parameter
  3775. - otherwise: add all index list items as individual parameters
  3776. *)
  3777. actualParameters := SyntaxTree.NewExpressionList();
  3778. IF usesGeneralOperator THEN
  3779. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3780. END;
  3781. FOR i := 0 TO indexListSize - 1 DO
  3782. expression := indexList.GetExpression(i);
  3783. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3784. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3785. tempList := SyntaxTree.NewExpressionList();
  3786. tempList.AddExpression(expression);
  3787. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3788. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3789. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3790. END;
  3791. IF usesGeneralOperator THEN
  3792. tempMathArrayExpression.elements.AddExpression(expression);
  3793. ELSE
  3794. actualParameters.AddExpression(expression)
  3795. END
  3796. END;
  3797. IF usesGeneralOperator THEN
  3798. actualParameters.AddExpression(tempMathArrayExpression)
  3799. END;
  3800. IF rhs # NIL THEN
  3801. (* add actual parameter for RHS *)
  3802. IF needsReshaping THEN
  3803. (* reshape using OCArrayBase.ExpandDimensions *)
  3804. tempList := SyntaxTree.NewExpressionList();
  3805. (* source array *)
  3806. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3807. tempList.AddExpression(rhs);
  3808. ELSE
  3809. (* convert scalar to one-dimensional array *)
  3810. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3811. tempMathArrayExpression.elements.AddExpression(rhs);
  3812. tempList.AddExpression(tempMathArrayExpression)
  3813. END;
  3814. (* list of kept dimensions *)
  3815. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3816. FOR i := 0 TO indexListSize - 1 DO
  3817. expression := indexList.GetExpression(i);
  3818. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3819. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3820. ELSE
  3821. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3822. END
  3823. END;
  3824. tempList.AddExpression(tempMathArrayExpression);
  3825. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3826. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3827. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3828. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3829. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3830. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3831. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3832. expression.SetType(castReturnType);
  3833. ELSE
  3834. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3835. END;
  3836. actualParameters.AddExpression(expression)
  3837. ELSE
  3838. actualParameters.AddExpression(rhs)
  3839. END
  3840. END;
  3841. (* add dereference operator and create procedure call designator *)
  3842. ASSERT(left IS SyntaxTree.Designator);
  3843. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3844. ASSERT(expression IS SyntaxTree.Designator);
  3845. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3846. IF (rhs = NIL) & needsReshaping THEN
  3847. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3848. tempList := SyntaxTree.NewExpressionList();
  3849. FOR i := 0 TO indexList.Length() - 1 DO
  3850. expression := indexList.GetExpression(i);
  3851. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3852. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3853. ELSE
  3854. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3855. END
  3856. END;
  3857. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3858. END;
  3859. IF rhs = NIL THEN
  3860. (* special rule: index read operator calls are considered to be assignable *)
  3861. result.SetAssignable(TRUE)
  3862. END;
  3863. (* put information about this index operator call into the resulting designator *)
  3864. result.SetRelatedAsot(left);
  3865. result.SetRelatedIndexList(indexList)
  3866. END;
  3867. RETURN result
  3868. END NewIndexOperatorCall;
  3869. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3870. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3871. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3872. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3873. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3874. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3875. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3876. CONST trace = FALSE;
  3877. BEGIN
  3878. IF trace THEN
  3879. FOR i := 0 TO actualParameters.Length()-1 DO
  3880. Printout.Info("par", actualParameters.GetExpression(i));
  3881. END;
  3882. END;
  3883. operator := scope.firstOperator;
  3884. WHILE(operator # NIL) DO
  3885. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3886. procedureType := operator.type(SyntaxTree.ProcedureType);
  3887. distance := Distance(system, procedureType,actualParameters);
  3888. IF trace THEN Printout.Info("check op ",operator) END;
  3889. IF distance < bestDistance THEN
  3890. IF trace THEN Printout.Info("taken op",operator) END;
  3891. bestDistance := distance;
  3892. bestOperator := operator;
  3893. END;
  3894. END;
  3895. operator := operator.nextOperator;
  3896. END;
  3897. END FindInScope;
  3898. BEGIN
  3899. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3900. IF oper = 0 THEN (* index *)
  3901. identifier := SyntaxTree.NewIdentifier("[]");
  3902. ELSE
  3903. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3904. END;
  3905. WHILE (recordType # NIL) DO
  3906. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3907. recordType := recordType.GetBaseRecord();
  3908. END;
  3909. RETURN bestOperator
  3910. END FindOperator;
  3911. BEGIN
  3912. type := left.type.resolved;
  3913. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3914. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3915. actualParameters := SyntaxTree.NewExpressionList();
  3916. IF parameters # NIL THEN
  3917. FOR i := 0 TO parameters.Length()-1 DO
  3918. expression := ResolveExpression(parameters.GetExpression(i));
  3919. actualParameters.AddExpression(expression);
  3920. END;
  3921. END;
  3922. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3923. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3924. IF op # NIL THEN
  3925. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3926. ASSERT(expression IS SyntaxTree.Designator);
  3927. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3928. result.SetRelatedAsot(left);
  3929. result.SetRelatedIndexList(parameters);
  3930. (* check if write operator exists, for var parameters *)
  3931. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3932. actualParameters := SyntaxTree.NewExpressionList();
  3933. FOR i := 0 TO parameters.Length()-1 DO
  3934. expression := ResolveExpression(parameters.GetExpression(i));
  3935. actualParameters.AddExpression(expression);
  3936. END;
  3937. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3938. actualParameters.AddExpression(rhs);
  3939. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3940. IF op = NIL THEN rhs := NIL END;
  3941. END;
  3942. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3943. ELSE
  3944. result := NIL;
  3945. END;
  3946. RETURN result;
  3947. END NewObjectOperatorCall;
  3948. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3949. 1. convert bracket designator chains into a single one that contains separators
  3950. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3951. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3952. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3953. - if an array or math array is indexed over, create index designator
  3954. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3955. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3956. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3957. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3958. - if an array-structured object type is indexed over, create procedure call designator
  3959. e.g.: a[x, y] -> a^."[]"(x, y)
  3960. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3961. - a[i, *] = a[i][*]
  3962. - a[*, i] # a[*][i]
  3963. Because:
  3964. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3965. - 'i-th column' = a[*, i]
  3966. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3967. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3968. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3969. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3970. **)
  3971. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3972. VAR
  3973. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3974. indexDesignator: SyntaxTree.IndexDesignator;
  3975. designator: SyntaxTree.Designator;
  3976. type: SyntaxTree.Type;
  3977. recordType: SyntaxTree.RecordType;
  3978. expression, rhs: SyntaxTree.Expression;
  3979. indexList: SyntaxTree.ExpressionList;
  3980. i: LONGINT;
  3981. hasError, done: BOOLEAN;
  3982. PROCEDURE FinalizeIndexDesignator;
  3983. BEGIN
  3984. IF indexDesignator # NIL THEN
  3985. (* the end of a tensor has been reached: *)
  3986. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3987. SetIndexBaseType(indexDesignator, type);
  3988. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3989. designator := indexDesignator;
  3990. type := designator.type.resolved;
  3991. indexDesignator := NIL;
  3992. ASSERT(SyntaxTree.Resolved IN type.state)
  3993. END
  3994. END FinalizeIndexDesignator;
  3995. BEGIN
  3996. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3997. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3998. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3999. (* copy all index list entries including a separator to the left bracket designator *)
  4000. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4001. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4002. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4003. END;
  4004. (* propagate the related RHS *)
  4005. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4006. (* only resolve left bracket designator and use as final result *)
  4007. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4008. ELSE
  4009. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4010. designator := ResolveDesignator(bracketDesignator.left);
  4011. type := designator.type.resolved;
  4012. indexDesignator := NIL;
  4013. (*!!! clean up *)
  4014. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4015. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4016. IF resolvedExpression = NIL THEN
  4017. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4018. resolvedExpression := SyntaxTree.invalidDesignator
  4019. END;
  4020. RETURN;
  4021. END;
  4022. i := 0;
  4023. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4024. expression := bracketDesignator.parameters.GetExpression(i);
  4025. expression := ResolveExpression(expression);
  4026. bracketDesignator.parameters.SetExpression(i, expression);
  4027. IF expression = SyntaxTree.indexListSeparator THEN
  4028. (* finalize an existing index designator if needed *)
  4029. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4030. INC(i)
  4031. ELSE
  4032. (* do auto-dereferencing if needed *)
  4033. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4034. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4035. & (i=0)*)
  4036. THEN
  4037. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4038. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4039. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4040. designator := SyntaxTree.invalidDesignator;
  4041. type := SyntaxTree.invalidType
  4042. ELSE
  4043. FinalizeIndexDesignator;
  4044. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4045. type := designator.type.resolved
  4046. END
  4047. END;
  4048. (* create a new index designator, if needed *)
  4049. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4050. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4051. indexDesignator.SetAssignable(designator.assignable);
  4052. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4053. (* designator := indexDesignator *)
  4054. END;
  4055. IF type = SyntaxTree.invalidType THEN
  4056. (* error already handled *)
  4057. INC(i)
  4058. ELSIF type IS SyntaxTree.ArrayType THEN
  4059. (* indexing over an array *)
  4060. ASSERT(indexDesignator # NIL);
  4061. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4062. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4063. INC(i)
  4064. ELSIF type IS SyntaxTree.StringType THEN
  4065. (* indexing over an array *)
  4066. ASSERT(indexDesignator # NIL);
  4067. AppendIndex(expression.position, indexDesignator, expression, type);
  4068. type := type(SyntaxTree.StringType).baseType.resolved;
  4069. INC(i)
  4070. ELSIF type IS SyntaxTree.MathArrayType THEN
  4071. (* indexing over a math array *)
  4072. ASSERT(indexDesignator # NIL);
  4073. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4074. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4075. INC(i)
  4076. ELSIF IsArrayStructuredObjectType(type) THEN
  4077. (* indexing over ASOTs *)
  4078. FinalizeIndexDesignator;
  4079. ASSERT(type IS SyntaxTree.PointerType);
  4080. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4081. (*
  4082. - collect index list items from bracket designator that belong to ASOT
  4083. - check for errors
  4084. *)
  4085. indexList := SyntaxTree.NewExpressionList();
  4086. hasError := FALSE;
  4087. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4088. (* indexing over tensor ASOT:
  4089. - stop at index list end or separator
  4090. - dimensionality is given by number of index list items
  4091. *)
  4092. done := FALSE;
  4093. WHILE ~done DO
  4094. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4095. done := TRUE;
  4096. ELSE
  4097. expression := bracketDesignator.parameters.GetExpression(i);
  4098. IF expression = SyntaxTree.indexListSeparator THEN
  4099. done := TRUE;
  4100. ELSE
  4101. expression := ResolveExpression(expression);
  4102. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4103. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4104. hasError := TRUE
  4105. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4106. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4107. expression := SyntaxTree.invalidExpression;
  4108. hasError := TRUE
  4109. END;
  4110. indexList.AddExpression(expression)
  4111. END;
  4112. INC(i)
  4113. END
  4114. END
  4115. ELSE
  4116. (* indexing over non-tensor ASOT:
  4117. - ignore separators
  4118. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4119. *)
  4120. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4121. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4122. expression := bracketDesignator.parameters.GetExpression(i);
  4123. ELSE
  4124. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4125. END;
  4126. IF expression # SyntaxTree.indexListSeparator THEN
  4127. expression := ResolveExpression(expression);
  4128. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4129. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4130. expression := SyntaxTree.invalidExpression;
  4131. hasError := TRUE
  4132. END;
  4133. indexList.AddExpression(expression)
  4134. END;
  4135. INC(i)
  4136. END;
  4137. END;
  4138. IF hasError THEN
  4139. designator := SyntaxTree.invalidDesignator;
  4140. type := SyntaxTree.invalidType;
  4141. ELSE
  4142. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4143. and the last entry in the index list belongs to the array-structured object type in question.
  4144. E.g.: for a 2-dimensional array-structured object type:
  4145. - 'lhs := asot[1, 2]' -> read mode
  4146. - 'asot[1, 2] := rhs' -> write mode
  4147. - 'asot[1, 2, 3] := rhs' -> read mode
  4148. *)
  4149. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4150. rhs := bracketDesignator.relatedRhs
  4151. ELSE
  4152. rhs := NIL
  4153. END;
  4154. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4155. type := designator.type
  4156. END
  4157. ELSE
  4158. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4159. designator := SyntaxTree.invalidDesignator;
  4160. type := SyntaxTree.invalidType;
  4161. INC(i)
  4162. END
  4163. END
  4164. END;
  4165. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4166. resolvedExpression := designator
  4167. END
  4168. END VisitBracketDesignator;
  4169. (** check and resolve expression list
  4170. - resolve each expression in an expression list
  4171. - returns true if and only if all statements could have successfully been resolved
  4172. **)
  4173. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4174. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4175. BEGIN
  4176. result := TRUE;
  4177. FOR i := 0 TO expressionList.Length()-1 DO
  4178. expression := ResolveExpression(expressionList.GetExpression(i));
  4179. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4180. expressionList.SetExpression(i,expression);
  4181. END;
  4182. RETURN result
  4183. END ExpressionList;
  4184. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4185. BEGIN
  4186. type := type.resolved;
  4187. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4188. RETURN TRUE
  4189. ELSIF system.CanPassInRegister # NIL THEN
  4190. RETURN system.CanPassInRegister(type);
  4191. ELSE
  4192. RETURN FALSE
  4193. END;
  4194. END CanPassInRegister;
  4195. (** return procedure call designator left(actualParameters)
  4196. - check realtime procedure call in realtime procedure
  4197. - check number of parameters
  4198. - check parameter compatibility
  4199. return invalidDesignator if error
  4200. **)
  4201. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4202. VAR result: SyntaxTree.Designator;
  4203. numberFormalParameters, numberActualParameters: LONGINT;
  4204. formalType: SyntaxTree.ProcedureType;
  4205. formalParameter: SyntaxTree.Parameter;
  4206. actualParameter: SyntaxTree.Expression;
  4207. i: LONGINT;
  4208. BEGIN
  4209. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4210. result := SyntaxTree.invalidDesignator;
  4211. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4212. numberFormalParameters := formalType.numberParameters;
  4213. numberActualParameters := actualParameters.Length();
  4214. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4215. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4216. END;
  4217. IF ~ExpressionList(actualParameters) THEN
  4218. result := SyntaxTree.invalidDesignator
  4219. ELSE
  4220. IF numberActualParameters <= numberFormalParameters THEN
  4221. formalParameter := formalType.firstParameter;
  4222. FOR i := 0 TO numberActualParameters-1 DO
  4223. actualParameter := actualParameters.GetExpression(i);
  4224. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4225. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4226. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4227. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4228. ELSE
  4229. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4230. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4231. END;
  4232. actualParameters.SetExpression(i,actualParameter);
  4233. END;
  4234. formalParameter := formalParameter.nextParameter;
  4235. END;
  4236. WHILE (formalParameter # NIL) DO
  4237. IF formalParameter.defaultValue # NIL THEN
  4238. actualParameters.AddExpression(formalParameter.defaultValue);
  4239. formalParameter := formalParameter.nextParameter
  4240. ELSE
  4241. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4242. formalParameter := NIL;
  4243. END;
  4244. END;
  4245. ELSE
  4246. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4247. END;
  4248. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4249. result.SetAssignable(FALSE);
  4250. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4251. END;
  4252. RETURN result
  4253. END NewProcedureCallDesignator;
  4254. (**
  4255. builtin call designator generated in VisitParameterDesignator
  4256. -> nothing to be resolved
  4257. **)
  4258. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4259. BEGIN
  4260. resolvedExpression := x;
  4261. END VisitTypeGuardDesignator;
  4262. (**
  4263. builtin call designator generated in VisitParameterDesignator
  4264. -> nothing to be resolved
  4265. **)
  4266. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4267. BEGIN
  4268. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4269. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4270. ASSERT(resolvedExpression.type # NIL);
  4271. ELSIF ExpressionList(x.parameters) THEN
  4272. resolvedExpression := x;
  4273. END;
  4274. END VisitBuiltinCallDesignator;
  4275. (**
  4276. procedure call designator generated in VisitParameterDesignator
  4277. -> nothing to be resolved
  4278. **)
  4279. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4280. BEGIN
  4281. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4282. resolvedExpression := x;
  4283. END VisitProcedureCallDesignator;
  4284. (** return true if x is a variable else return false and report error **)
  4285. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4286. VAR result: BOOLEAN;
  4287. BEGIN
  4288. result := TRUE;
  4289. IF x = SyntaxTree.invalidExpression THEN
  4290. result := FALSE;
  4291. ELSIF ~IsVariable(x) THEN
  4292. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4293. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4294. result := FALSE;
  4295. END;
  4296. RETURN result
  4297. END CheckVariable;
  4298. (**
  4299. if expression x is of basic type then return true else report error and return false
  4300. **)
  4301. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4302. VAR result: BOOLEAN;
  4303. BEGIN
  4304. result := FALSE;
  4305. IF x = SyntaxTree.invalidExpression THEN
  4306. ELSIF ~IsBasicType(x.type) THEN
  4307. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4308. result := FALSE
  4309. ELSE result := TRUE
  4310. END;
  4311. RETURN result
  4312. END CheckBasicType;
  4313. (**
  4314. if expression x is of number type then return true else report error and return false
  4315. **)
  4316. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4317. VAR result: BOOLEAN;
  4318. BEGIN
  4319. result := FALSE;
  4320. IF x = SyntaxTree.invalidExpression THEN
  4321. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4322. Error(x.position,Diagnostics.Invalid,"is non number type");
  4323. ELSE result := TRUE
  4324. END;
  4325. RETURN result
  4326. END CheckNumberType;
  4327. (**
  4328. if expression x is of number or size type but not complex then return true else report error and return false
  4329. **)
  4330. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4331. VAR result: BOOLEAN;
  4332. BEGIN
  4333. result := FALSE;
  4334. IF x = SyntaxTree.invalidExpression THEN
  4335. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4336. Error(x.position,Diagnostics.Invalid,"is complex type");
  4337. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4338. Error(x.position,Diagnostics.Invalid,"is non number type");
  4339. ELSE result := TRUE
  4340. END;
  4341. RETURN result
  4342. END CheckNonComplexNumberSizeType;
  4343. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4344. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4345. BEGIN
  4346. result := FALSE; type := x.type.resolved;
  4347. IF x = SyntaxTree.invalidExpression THEN
  4348. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4349. TRACE(type.sizeInBits);
  4350. TRACE(system.addressType.sizeInBits);
  4351. Error(x.position,Diagnostics.Invalid,"is no address type");
  4352. ELSE result := TRUE
  4353. END;
  4354. RETURN result
  4355. END CheckAddressType;
  4356. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4357. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4358. BEGIN
  4359. result := FALSE; type := x.type.resolved;
  4360. IF x = SyntaxTree.invalidExpression THEN
  4361. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4362. Error(x.position,Diagnostics.Invalid,"is no size type");
  4363. ELSE result := TRUE
  4364. END;
  4365. RETURN result
  4366. END CheckSizeType;
  4367. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4368. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4369. BEGIN
  4370. result := FALSE; type := x.type.resolved;
  4371. IF x = SyntaxTree.invalidExpression THEN
  4372. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4373. Error(x.position,Diagnostics.Invalid,"is no object type");
  4374. ELSE result := TRUE
  4375. END;
  4376. RETURN result
  4377. END CheckObjectType;
  4378. (**
  4379. if expression x is of integer type then return true else report error and return false
  4380. **)
  4381. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4382. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4383. BEGIN
  4384. result := FALSE; type := x.type.resolved;
  4385. IF x = SyntaxTree.invalidExpression THEN
  4386. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4387. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4388. ELSE result := TRUE
  4389. END;
  4390. RETURN result
  4391. END CheckIntegerType;
  4392. (**
  4393. if expression x is of character type then return true else report error and return false
  4394. **)
  4395. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4396. VAR result: BOOLEAN;
  4397. BEGIN
  4398. result := FALSE;
  4399. IF x = SyntaxTree.invalidExpression THEN
  4400. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4401. Error(x.position,Diagnostics.Invalid,"is no character type");
  4402. ELSE result := TRUE
  4403. END;
  4404. RETURN result
  4405. END CheckCharacterType;
  4406. (**
  4407. if expression x is of real type then return true else report error and return false
  4408. **)
  4409. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4410. VAR result: BOOLEAN;
  4411. BEGIN
  4412. result := FALSE;
  4413. IF x = SyntaxTree.invalidExpression THEN
  4414. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4415. Error(x.position,Diagnostics.Invalid,"is no float type");
  4416. ELSE result := TRUE
  4417. END;
  4418. RETURN result
  4419. END CheckRealType;
  4420. (**
  4421. if expression x is of range type then return true else report error and return false
  4422. **)
  4423. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4424. VAR result: BOOLEAN;
  4425. BEGIN
  4426. result := FALSE;
  4427. IF x = SyntaxTree.invalidExpression THEN
  4428. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4429. Error(x.position,Diagnostics.Invalid,"is no range type");
  4430. ELSE result := TRUE
  4431. END;
  4432. RETURN result
  4433. END CheckRangeType;
  4434. (**
  4435. if expression x is of boolean type then return true else report error and return false
  4436. **)
  4437. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4438. VAR result: BOOLEAN;
  4439. BEGIN
  4440. result := FALSE;
  4441. IF x = SyntaxTree.invalidExpression THEN
  4442. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4443. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4444. ELSE result := TRUE
  4445. END;
  4446. RETURN result
  4447. END CheckBooleanType;
  4448. (**
  4449. if expression x is of set type then return true else report error and return false
  4450. **)
  4451. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4452. VAR result: BOOLEAN;
  4453. BEGIN
  4454. result := FALSE;
  4455. IF x = SyntaxTree.invalidExpression THEN
  4456. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4457. Error(x.position,Diagnostics.Invalid,"is no set type");
  4458. ELSE result := TRUE
  4459. END;
  4460. RETURN result
  4461. END CheckSetType;
  4462. (**
  4463. if expression x is of string or array of character type then return true else report error and return false
  4464. **)
  4465. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4466. VAR result: BOOLEAN;
  4467. BEGIN
  4468. result := FALSE;
  4469. IF x = SyntaxTree.invalidExpression THEN
  4470. ELSIF ~IsStringType(x.type.resolved) THEN
  4471. Error(x.position,Diagnostics.Invalid,"is no string type");
  4472. ELSE result := TRUE
  4473. END;
  4474. RETURN result
  4475. END CheckStringType;
  4476. (**
  4477. if expression x is a type declaration type return true else report error and return false
  4478. **)
  4479. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4480. VAR result: BOOLEAN;
  4481. BEGIN
  4482. result := FALSE;
  4483. IF x = SyntaxTree.invalidExpression THEN
  4484. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4485. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4486. ELSE result := TRUE
  4487. END;
  4488. RETURN result
  4489. END CheckTypeDeclarationType;
  4490. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4491. VAR result: BOOLEAN;
  4492. BEGIN
  4493. result := FALSE;
  4494. IF x = SyntaxTree.invalidExpression THEN
  4495. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4496. result := TRUE;
  4497. value := x.resolved(SyntaxTree.IntegerValue).value;
  4498. ELSE
  4499. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4500. END;
  4501. RETURN result;
  4502. END CheckIntegerValue;
  4503. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4504. VAR result: BOOLEAN;
  4505. BEGIN
  4506. result := FALSE;
  4507. IF x = SyntaxTree.invalidExpression THEN
  4508. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4509. result := TRUE;
  4510. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4511. ELSE
  4512. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4513. END;
  4514. RETURN result;
  4515. END CheckStringValue;
  4516. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4517. BEGIN
  4518. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4519. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4520. ELSE
  4521. RETURN FALSE
  4522. END;
  4523. END IsUnsignedValue;
  4524. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4525. BEGIN
  4526. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4527. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4528. ELSE
  4529. RETURN FALSE
  4530. END
  4531. END IsAddressValue;
  4532. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4533. BEGIN
  4534. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4535. END IsAddressExpression;
  4536. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4537. BEGIN
  4538. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4539. END IsSizeExpression;
  4540. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4541. VAR result: BOOLEAN;
  4542. BEGIN
  4543. result := FALSE;
  4544. IF x = SyntaxTree.invalidExpression THEN
  4545. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4546. result := TRUE;
  4547. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4548. ELSE
  4549. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4550. END;
  4551. RETURN result;
  4552. END CheckEnumerationValue;
  4553. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4554. VAR result: BOOLEAN;
  4555. BEGIN
  4556. result := FALSE;
  4557. IF x = SyntaxTree.invalidExpression THEN
  4558. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4559. result := TRUE;
  4560. value := x.resolved(SyntaxTree.CharacterValue).value;
  4561. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4562. result := TRUE;
  4563. value := x.resolved(SyntaxTree.StringValue).value[0];
  4564. ELSE
  4565. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4566. END;
  4567. RETURN result;
  4568. END CheckCharacterValue;
  4569. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4570. VAR result: BOOLEAN;
  4571. BEGIN
  4572. result := FALSE;
  4573. IF x = SyntaxTree.invalidExpression THEN
  4574. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4575. value := x.resolved(SyntaxTree.IntegerValue).value;
  4576. IF (value > 0) OR includeZero & (value = 0) THEN
  4577. result := TRUE;
  4578. ELSE
  4579. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4580. END
  4581. ELSE
  4582. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4583. END;
  4584. RETURN result;
  4585. END CheckPositiveIntegerValue;
  4586. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4587. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4588. BEGIN
  4589. result := FALSE;
  4590. IF x = SyntaxTree.invalidExpression THEN
  4591. ELSE
  4592. type := x.type.resolved;
  4593. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4594. portType := type(SyntaxTree.PortType);
  4595. result := TRUE
  4596. ELSE
  4597. Error(x.position,Diagnostics.Invalid,"no port type");
  4598. END;
  4599. END;
  4600. RETURN result
  4601. END CheckPortType;
  4602. (* move to builtin procedure call statement ?
  4603. remove builtin procedure call designator ?
  4604. *)
  4605. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4606. VAR
  4607. numberActualParameters,numberFormalParameters: LONGINT;
  4608. formalParameter: SyntaxTree.Parameter;
  4609. actualParameter: SyntaxTree.Expression;
  4610. procedureType: SyntaxTree.ProcedureType;
  4611. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4612. inPort, outPort: SyntaxTree.PortType;
  4613. constructor: SyntaxTree.Procedure;
  4614. type0,type1,type2: SyntaxTree.Type;
  4615. type,base,parameterType: SyntaxTree.Type;
  4616. arrayType: SyntaxTree.ArrayType;
  4617. i,i0,i1: LONGINT;
  4618. r,r0,r1,im: LONGREAL;
  4619. c: CHAR;
  4620. id: LONGINT;
  4621. b: BOOLEAN;
  4622. first: LONGINT;
  4623. mathArrayType: SyntaxTree.MathArrayType;
  4624. customBuiltin: SyntaxTree.CustomBuiltin;
  4625. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4626. VAR resultB: BOOLEAN;
  4627. BEGIN
  4628. IF numberActualParameters < from THEN
  4629. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4630. result := SyntaxTree.invalidExpression;
  4631. resultB := FALSE;
  4632. ELSIF numberActualParameters > to THEN
  4633. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4634. result := SyntaxTree.invalidExpression;
  4635. resultB := FALSE;
  4636. ELSE
  4637. resultB := TRUE;
  4638. END;
  4639. RETURN resultB
  4640. END CheckArity;
  4641. BEGIN
  4642. type := NIL; result := NIL;
  4643. type0 := NIL; type1 := NIL; type2 := NIL;
  4644. numberActualParameters := actualParameters.Length();
  4645. IF numberActualParameters>0 THEN
  4646. parameter0 := actualParameters.GetExpression(0);
  4647. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4648. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4649. result := SyntaxTree.invalidExpression
  4650. END
  4651. END;
  4652. IF numberActualParameters >1 THEN
  4653. parameter1 := actualParameters.GetExpression(1);
  4654. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4655. ELSE
  4656. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4657. result := SyntaxTree.invalidExpression
  4658. END
  4659. END;
  4660. IF numberActualParameters >2 THEN
  4661. parameter2 := actualParameters.GetExpression(2);
  4662. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4663. ELSE
  4664. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4665. result := SyntaxTree.invalidExpression
  4666. END
  4667. END;
  4668. IF returnType # NIL THEN
  4669. id := Global.New;
  4670. result := NIL;
  4671. ELSE
  4672. id := builtin.id;
  4673. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4674. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4675. END;
  4676. END;
  4677. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4678. ELSIF result # NIL THEN type := result.type (* operator *)
  4679. ELSE
  4680. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4681. result(SyntaxTree.Designator).SetLeft(left);
  4682. IF returnType # NIL THEN
  4683. type := returnType;
  4684. END;
  4685. (* ---- ASSERT ----- *)
  4686. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4687. IF CheckBooleanType(parameter0) THEN
  4688. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4689. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4690. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4691. *)
  4692. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4693. Error(position,Diagnostics.Invalid,"assert failed");
  4694. END;
  4695. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4696. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4697. rules imposed by the architecture / current runtime
  4698. *)
  4699. END;
  4700. END;
  4701. (* ---- COPY ----- *)
  4702. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4703. IF~IsStringType(type0) THEN
  4704. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4705. END;
  4706. IF ~IsStringType(type1) THEN
  4707. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4708. ELSIF CheckVariable(parameter1) THEN
  4709. IF (type0 IS SyntaxTree.StringType) THEN
  4710. arrayType := type1(SyntaxTree.ArrayType);
  4711. IF arrayType.form = SyntaxTree.Static THEN
  4712. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4713. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4714. END;
  4715. END;
  4716. END;
  4717. END;
  4718. (* ---- INC, DEC----- *)
  4719. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4720. IF numberActualParameters = 1 THEN
  4721. parameter1 :=Global.NewIntegerValue(system,position,1);
  4722. actualParameters.AddExpression(parameter1);
  4723. END;
  4724. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4725. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4726. Error(position,Diagnostics.Invalid,"incompatible increment");
  4727. ELSE
  4728. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4729. actualParameters.SetExpression(1,parameter1);
  4730. END;
  4731. END;
  4732. (* ---- EXCL, INCL----- *)
  4733. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4734. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4735. IF IsIntegerValue(parameter1,i0) THEN
  4736. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4737. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4738. END;
  4739. END;
  4740. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4741. actualParameters.SetExpression(1,parameter1);
  4742. END;
  4743. (* ---- HALT, SYSTEM.HALT ----- *)
  4744. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4745. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4746. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4747. rules imposed by the architecture / current runtime
  4748. *)
  4749. END;
  4750. (* ---- WAIT ----- *)
  4751. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4752. IF CheckObjectType(parameter0) THEN
  4753. END;
  4754. (* ---- NEW ----- *)
  4755. ELSIF (id = Global.New) THEN
  4756. IF returnType # NIL THEN
  4757. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4758. ELSE
  4759. first := 1;
  4760. END;
  4761. IF CheckArity(first,Infinity) THEN
  4762. IF currentIsRealtime THEN
  4763. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4764. END;
  4765. (* check constructor *)
  4766. IF (first =0) OR CheckVariable(parameter0) THEN
  4767. IF type0 IS SyntaxTree.PointerType THEN
  4768. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4769. ELSIF type0 IS SyntaxTree.CellType THEN
  4770. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4771. ELSE
  4772. Error(position, Diagnostics.Invalid, "forbidden new on value type");
  4773. END;
  4774. IF type0 IS SyntaxTree.ArrayType THEN
  4775. arrayType := type0(SyntaxTree.ArrayType);
  4776. IF arrayType.form = SyntaxTree.Static THEN
  4777. i := first
  4778. ELSIF arrayType.form = SyntaxTree.Open THEN
  4779. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4780. ELSE HALT(100)
  4781. END;
  4782. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4783. i := first;
  4784. REPEAT
  4785. actualParameter := actualParameters.GetExpression(i);
  4786. IF CheckSizeType(actualParameter) THEN
  4787. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4788. actualParameters.SetExpression(i,actualParameter);
  4789. END;
  4790. INC(i);
  4791. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4792. END;
  4793. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4794. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4795. IF constructor = NIL THEN
  4796. IF CheckArity(first,first) THEN END;
  4797. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4798. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4799. ELSE
  4800. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4801. numberFormalParameters := procedureType.numberParameters;
  4802. IF numberActualParameters-first <= numberFormalParameters THEN
  4803. formalParameter := procedureType.firstParameter;
  4804. FOR i := first TO numberActualParameters-1 DO
  4805. actualParameter := actualParameters.GetExpression(i);
  4806. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4807. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4808. ELSE
  4809. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4810. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4811. END;
  4812. actualParameters.SetExpression(i,actualParameter);
  4813. END;
  4814. formalParameter := formalParameter.nextParameter;
  4815. END;
  4816. WHILE (formalParameter # NIL) DO
  4817. IF formalParameter.defaultValue # NIL THEN
  4818. actualParameters.AddExpression(formalParameter.defaultValue);
  4819. formalParameter := formalParameter.nextParameter
  4820. ELSE
  4821. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4822. formalParameter := NIL;
  4823. END;
  4824. END;
  4825. ELSE
  4826. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4827. END;
  4828. END;
  4829. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4830. mathArrayType := type0(SyntaxTree.MathArrayType);
  4831. IF mathArrayType.form = SyntaxTree.Static THEN
  4832. Error(position,Diagnostics.Invalid,"new on static array");
  4833. ELSE
  4834. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4835. i0 := first+1; i1 := Infinity;
  4836. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4837. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4838. i1 := i0;
  4839. ELSE HALT(100);
  4840. END;
  4841. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4842. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4843. base := ArrayBase(type0,MAX(LONGINT));
  4844. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4845. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4846. IF ~CompatibleTo(system,type0,parameterType) THEN
  4847. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4848. result := SyntaxTree.invalidExpression;
  4849. ELSE
  4850. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4851. END;
  4852. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4853. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4854. IF ~CompatibleTo(system,type1,parameterType) THEN
  4855. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4856. result := SyntaxTree.invalidExpression;
  4857. ELSE
  4858. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4859. END;
  4860. ELSE
  4861. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4862. i := first;
  4863. REPEAT
  4864. actualParameter := actualParameters.GetExpression(i);
  4865. IF CheckSizeType(actualParameter) THEN
  4866. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4867. actualParameters.SetExpression(i,actualParameter);
  4868. END;
  4869. INC(i);
  4870. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4871. END;
  4872. END;
  4873. END;
  4874. ELSIF type0 IS SyntaxTree.CellType THEN
  4875. IF ~(currentIsCellNet) THEN
  4876. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4877. ELSE
  4878. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4879. IF (constructor = NIL) & CheckArity(1,1) THEN
  4880. (* ok *)
  4881. ELSE
  4882. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4883. numberFormalParameters := procedureType.numberParameters;
  4884. DEC(numberActualParameters);
  4885. IF numberActualParameters <= numberFormalParameters THEN
  4886. formalParameter := procedureType.firstParameter;
  4887. FOR i := first TO numberActualParameters DO
  4888. actualParameter := actualParameters.GetExpression(i);
  4889. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4890. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4891. ELSE
  4892. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4893. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4894. END;
  4895. actualParameters.SetExpression(i,actualParameter);
  4896. END;
  4897. formalParameter := formalParameter.nextParameter;
  4898. END;
  4899. WHILE (formalParameter # NIL) DO
  4900. IF formalParameter.defaultValue # NIL THEN
  4901. actualParameters.AddExpression(formalParameter.defaultValue);
  4902. formalParameter := formalParameter.nextParameter
  4903. ELSE
  4904. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4905. formalParameter := NIL;
  4906. END;
  4907. END;
  4908. ELSE
  4909. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4910. END;
  4911. END;
  4912. END;
  4913. activeCellsStatement := TRUE;
  4914. ELSE
  4915. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4916. END;
  4917. END;
  4918. END;
  4919. (* ---- DISPOSE ----- *)
  4920. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4921. IF ~IsPointerType(parameter0.type) THEN
  4922. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4923. ELSIF ~IsDisposable(parameter0.type) THEN
  4924. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4925. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4926. END
  4927. (* ---- GETPROCEDURE ----- *)
  4928. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4929. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4930. IF CheckVariable(parameter2) THEN
  4931. IF ~GetProcedureAllowed(parameter2.type) THEN
  4932. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4933. END;
  4934. END;
  4935. END;
  4936. (* ---- ABS ----- *)
  4937. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4938. (* note: ABS on complex numbers is done using overloading *)
  4939. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4940. type := type0;
  4941. IF IsIntegerValue(parameter0,i0) THEN
  4942. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4943. type := Global.GetIntegerType(system,ABS(i0));
  4944. ELSIF IsRealValue(parameter0,r) THEN
  4945. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4946. END;
  4947. ELSE
  4948. type := SyntaxTree.invalidType;
  4949. END;
  4950. (* ---- ASH, ASR ----- *)
  4951. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4952. type := type0;
  4953. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4954. (*
  4955. ConvertOperands(parameter0,parameter1); (* same type *)
  4956. *)
  4957. type := parameter0.type;
  4958. IF IsIntegerValue(parameter0,i0) THEN
  4959. IF IsIntegerValue(parameter1,i1) THEN
  4960. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4961. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4962. result := ResolveExpression(result);
  4963. type := Global.GetIntegerType(system,i0);
  4964. END;
  4965. END;
  4966. IF type.resolved.sizeInBits < 32 THEN
  4967. type := system.longintType;
  4968. END;
  4969. (*!compatibility with release, remove when resolved
  4970. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4971. *)
  4972. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4973. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4974. actualParameters.SetExpression(0,parameter0);
  4975. actualParameters.SetExpression(1,parameter1);
  4976. END;
  4977. (* ---- CAP ----- *)
  4978. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4979. type := system.characterType;
  4980. IF CheckCharacterType (parameter0) THEN
  4981. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4982. actualParameters.SetExpression(0,parameter0);
  4983. IF IsCharacterValue(parameter0,c) THEN
  4984. IF (c <= "z") & (c >= "a") THEN
  4985. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4986. ELSE
  4987. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4988. END;
  4989. END;
  4990. END;
  4991. (* ---- CHR ----- *)
  4992. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4993. IF id = Global.Chr THEN
  4994. type := system.characterType
  4995. ELSE
  4996. type := system.characterType32
  4997. END;
  4998. IF CheckIntegerType(parameter0) THEN
  4999. IF IsIntegerValue(parameter0,i0) THEN
  5000. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5001. result := ResolveExpression(result);
  5002. ELSE
  5003. (*
  5004. result := NewConversion(parameter0.position,parameter0,type);
  5005. *)
  5006. END;
  5007. END
  5008. (* ---- ENTIER ----- *)
  5009. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5010. type := system.longintType;
  5011. IF CheckRealType(parameter0) THEN
  5012. IF IsRealValue(parameter0,r) THEN
  5013. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5014. type := Global.GetIntegerType(system,ENTIER(r));
  5015. END
  5016. END;
  5017. (* ---- ENTIERH ----- *)
  5018. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5019. type := system.hugeintType;
  5020. IF CheckRealType(parameter0) THEN
  5021. IF IsRealValue(parameter0,r) THEN
  5022. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5023. END
  5024. END;
  5025. (* ---- LEN ----- *)
  5026. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5027. type := system.longintType;
  5028. base := type0;
  5029. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5030. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5031. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5032. END;
  5033. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5034. actualParameters.SetExpression(0,parameter0);
  5035. type0 := parameter0.type.resolved;
  5036. base := type0;
  5037. END;
  5038. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5039. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5040. IF i1 < 0 THEN
  5041. Error(position,Diagnostics.Invalid,"invalid dimension");
  5042. base := SyntaxTree.invalidType;
  5043. ELSE
  5044. base := ArrayBase(base,i1);
  5045. IF (base # NIL) & Indexable(base) THEN
  5046. ELSE
  5047. Error(position,Diagnostics.Invalid,"len on no array");
  5048. IF VerboseErrorMessage THEN
  5049. Printout.Info("base",base);
  5050. END;
  5051. base := SyntaxTree.invalidType;
  5052. END;
  5053. END;
  5054. IF numberActualParameters=2 THEN
  5055. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5056. actualParameters.SetExpression(1,parameter1);
  5057. ELSIF base IS SyntaxTree.MathArrayType THEN
  5058. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5059. END;
  5060. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5061. IF base IS SyntaxTree.ArrayType THEN
  5062. arrayType := base(SyntaxTree.ArrayType);
  5063. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5064. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5065. result := Global.NewIntegerValue(system,position,i);
  5066. type := result.type;(* arrayType.length.type;*)
  5067. ASSERT(type # NIL);
  5068. END;
  5069. ELSIF base IS SyntaxTree.MathArrayType THEN
  5070. mathArrayType := base(SyntaxTree.MathArrayType);
  5071. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5072. result := Global.NewIntegerValue(system,position,i);
  5073. type := result.type;
  5074. (*
  5075. type := mathArrayType.length.type;
  5076. *)
  5077. ASSERT(type # NIL);
  5078. END;
  5079. END;
  5080. END;
  5081. ELSE
  5082. type := system.longintType;
  5083. END;
  5084. (* ---- FIRST ---- *)
  5085. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5086. type := system.longintType;
  5087. IF CheckRangeType(parameter0) THEN END;
  5088. result.SetAssignable(parameter0.assignable)
  5089. (* ---- LAST ---- *)
  5090. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5091. type := system.longintType;
  5092. IF CheckRangeType(parameter0) THEN END;
  5093. result.SetAssignable(parameter0.assignable)
  5094. (* ---- STEP ---- *)
  5095. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5096. type := system.longintType;
  5097. IF CheckRangeType(parameter0) THEN END;
  5098. result.SetAssignable(parameter0.assignable)
  5099. (* ---- RE ---- *)
  5100. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5101. IF CheckNumberType(parameter0) THEN
  5102. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5103. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5104. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5105. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5106. type := parameter0.type
  5107. ELSE
  5108. type := system.realType
  5109. END
  5110. END;
  5111. result.SetAssignable(parameter0.assignable)
  5112. (* ---- IM ---- *)
  5113. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5114. IF CheckNumberType(parameter0) THEN
  5115. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5116. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5117. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5118. ELSE
  5119. type := system.realType;
  5120. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5121. END
  5122. END;
  5123. result.SetAssignable(parameter0.assignable)
  5124. (* ---- MAX ----- *)
  5125. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5126. IF numberActualParameters = 1 THEN
  5127. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5128. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5129. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5130. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5131. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5132. *)
  5133. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5134. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5135. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5136. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5137. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5138. END;
  5139. ELSE
  5140. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5141. END
  5142. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5143. ConvertOperands(parameter0,parameter1);
  5144. actualParameters.SetExpression(0,parameter0);
  5145. actualParameters.SetExpression(1,parameter1);
  5146. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5147. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5148. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5149. END;
  5150. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5151. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5152. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5153. END;
  5154. END;
  5155. type := parameter0.type;
  5156. ELSE type := SyntaxTree.invalidType;
  5157. END;
  5158. (* ---- MIN ----- *)
  5159. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5160. IF numberActualParameters = 1 THEN
  5161. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5162. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5163. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5164. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5165. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5166. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5167. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5168. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5169. END;
  5170. ELSE
  5171. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5172. END
  5173. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5174. ConvertOperands(parameter0,parameter1);
  5175. actualParameters.SetExpression(0,parameter0);
  5176. actualParameters.SetExpression(1,parameter1);
  5177. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5178. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5179. ELSE result.SetResolved(parameter1.resolved)
  5180. END;
  5181. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5182. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5183. ELSE result.SetResolved(parameter1.resolved)
  5184. END;
  5185. END;
  5186. type := parameter0.type;
  5187. ELSE type := SyntaxTree.invalidType;
  5188. END;
  5189. (* ---- ODD ----- *)
  5190. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5191. type := system.booleanType;
  5192. IF CheckIntegerType(parameter0) THEN
  5193. IF IsIntegerValue(parameter0,i0) THEN
  5194. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5195. type := system.booleanType;
  5196. END;
  5197. END;
  5198. (* ---- ORD ----- *)
  5199. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5200. IF id = Global.Ord THEN
  5201. type := system.integerType;
  5202. ELSE
  5203. type := system.longintType;
  5204. END;
  5205. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5206. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5207. actualParameters.SetExpression(0,parameter0);
  5208. (* IF CheckCharacterType(parameter0) THEN*)
  5209. IF IsCharacterValue(parameter0,c)THEN
  5210. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5211. type := Global.GetSignedIntegerType(system,ORD(c));
  5212. END;
  5213. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5214. END;
  5215. (* ---- SHORT ----- *)
  5216. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5217. type := type0;
  5218. IF IsSignedIntegerType(type) THEN
  5219. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5220. ELSIF type = system.integerType THEN type := system.shortintType
  5221. ELSIF type = system.longintType THEN type := system.integerType
  5222. ELSIF type = system.hugeintType THEN type:= system.longintType
  5223. ELSE
  5224. CASE type.sizeInBits OF
  5225. 16: type := Global.Integer8
  5226. |32: type := Global.Integer16
  5227. |64: type := Global.Integer32
  5228. END;
  5229. END;
  5230. ELSIF type IS SyntaxTree.FloatType THEN
  5231. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5232. ELSIF type = system.longrealType THEN type := system.realType
  5233. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5234. END;
  5235. ELSIF type IS SyntaxTree.ComplexType THEN
  5236. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5237. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5238. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5239. END;
  5240. ELSE
  5241. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5242. END;
  5243. IF (parameter0.resolved # NIL) THEN
  5244. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5245. IF parameter0 IS SyntaxTree.Value THEN
  5246. result.SetResolved(parameter0(SyntaxTree.Value));
  5247. END;
  5248. END;
  5249. (* ---- LONG ----- *)
  5250. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5251. type := type0;
  5252. IF IsSignedIntegerType(type) THEN
  5253. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5254. ELSIF type = system.longintType THEN type := system.hugeintType
  5255. ELSIF type = system.integerType THEN type := system.longintType
  5256. ELSIF type = system.shortintType THEN type := system.integerType
  5257. ELSE
  5258. CASE type.sizeInBits OF
  5259. 8: type := Global.Integer16
  5260. |16: type := Global.Integer32
  5261. |32: type := Global.Integer64
  5262. END;
  5263. END;
  5264. ELSIF type IS SyntaxTree.FloatType THEN
  5265. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5266. ELSIF type= system.realType THEN type := system.longrealType
  5267. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5268. END;
  5269. ELSIF type IS SyntaxTree.ComplexType THEN
  5270. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5271. ELSIF type = system.complexType THEN type := system.longcomplexType
  5272. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5273. END;
  5274. ELSE
  5275. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5276. END;
  5277. IF (parameter0.resolved # NIL) THEN
  5278. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5279. IF parameter0 IS SyntaxTree.Value THEN
  5280. result.SetResolved(parameter0(SyntaxTree.Value));
  5281. END;
  5282. END;
  5283. (* ---- SIZE OF ----- *)
  5284. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5285. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5286. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5287. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5288. type := system.integerType;
  5289. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5290. ELSE
  5291. (* for variables, system sizeof could represent the physically occupied size
  5292. determined via the type descriptor, implement that ? *)
  5293. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5294. END
  5295. (* ---- SYSTEM.TRACE -----*)
  5296. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5297. FOR i := 0 TO numberActualParameters-1 DO
  5298. parameter0 := actualParameters.GetExpression(i);
  5299. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5300. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5301. END;
  5302. END;
  5303. (* remaining issues can only be tested in backend *)
  5304. (* ---- ADDRESSOF----- *)
  5305. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5306. IF HasAddress(parameter0) THEN
  5307. type := system.addressType;
  5308. ELSE
  5309. type := SyntaxTree.invalidType;
  5310. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5311. END;
  5312. (* ---- BIT ----- *)
  5313. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5314. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5315. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5316. actualParameters.SetExpression(0,parameter0);
  5317. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5318. actualParameters.SetExpression(1,parameter1);
  5319. END;
  5320. type := system.booleanType;
  5321. (* ----- MSK ---- *)
  5322. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5323. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5324. ConvertOperands(parameter0,parameter1);
  5325. actualParameters.SetExpression(0,parameter0);
  5326. actualParameters.SetExpression(1,parameter1);
  5327. END;
  5328. type := parameter0.type;
  5329. (* ---- SYSTEM.GET64 ----- *)
  5330. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5331. IF CheckAddressType(parameter0) THEN
  5332. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5333. actualParameters.SetExpression(0,parameter0);
  5334. END;
  5335. type := system.hugeintType;
  5336. (* ---- SYSTEM.GET32 ----- *)
  5337. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5338. IF CheckAddressType(parameter0) THEN
  5339. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5340. actualParameters.SetExpression(0,parameter0);
  5341. END;
  5342. type := system.longintType;
  5343. (* ---- SYSTEM.GET16 ----- *)
  5344. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5345. IF CheckAddressType(parameter0) THEN
  5346. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5347. actualParameters.SetExpression(0,parameter0);
  5348. END;
  5349. type := system.integerType;
  5350. (* ---- SYSTEM.GET8 ----- *)
  5351. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5352. IF CheckAddressType(parameter0) THEN
  5353. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5354. actualParameters.SetExpression(0,parameter0);
  5355. END;
  5356. type := system.shortintType;
  5357. (* ---- SYSTEM.GetStackPointer ----- *)
  5358. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5359. type := system.addressType;
  5360. (* ---- SYSTEM.GetFramePointer ----- *)
  5361. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5362. type := system.addressType;
  5363. (* ---- SYSTEM.GetActivity ----- *)
  5364. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5365. type := system.objectType;
  5366. (* ---- SYSTEM.SetStackPointer ----- *)
  5367. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5368. IF CheckAddressType(parameter0) THEN
  5369. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5370. actualParameters.SetExpression(0,parameter0);
  5371. END;
  5372. (* ---- SYSTEM.SetFramePointer ----- *)
  5373. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5374. IF CheckAddressType(parameter0) THEN
  5375. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5376. actualParameters.SetExpression(0,parameter0);
  5377. END;
  5378. (* ---- SYSTEM.SetActivity ----- *)
  5379. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5380. IF CheckObjectType(parameter0) THEN
  5381. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5382. actualParameters.SetExpression(0,parameter0);
  5383. END;
  5384. (* ---- LSH, LSL, ROT, ROR ----- *)
  5385. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5386. type := type0;
  5387. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5388. actualParameters.SetExpression(1, parameter1);
  5389. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5390. IF id = Global.Lsh THEN
  5391. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5392. ELSIF id = Global.Rot THEN
  5393. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5394. ELSIF id = Global.Ror THEN
  5395. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5396. END;
  5397. END;
  5398. (* ---- SYSTEM.VAL ----- *)
  5399. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5400. IF CheckTypeDeclarationType(parameter0) THEN
  5401. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5402. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5403. result := SyntaxTree.invalidExpression;
  5404. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5405. ELSE
  5406. IF (parameter1.resolved # NIL) THEN
  5407. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5408. IF parameter0 IS SyntaxTree.Value THEN
  5409. result.SetResolved(parameter0(SyntaxTree.Value));
  5410. END;
  5411. END;
  5412. result.SetAssignable(parameter1.assignable);
  5413. END;
  5414. END;
  5415. (* ---- SYSTEM.GET ----- *)
  5416. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5417. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5418. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5419. actualParameters.SetExpression(0,parameter0);
  5420. END;
  5421. (* ---- SYSTEM.PUT ----- *)
  5422. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5423. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5424. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5425. actualParameters.SetExpression(0,parameter0);
  5426. END;
  5427. (* ---- SYSTEM.PUT64 ----- *)
  5428. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5429. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5430. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5431. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5432. actualParameters.SetExpression(0,parameter0);
  5433. actualParameters.SetExpression(1,parameter1);
  5434. END;
  5435. (* ---- SYSTEM.PUT32 ----- *)
  5436. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5437. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5438. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5439. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5440. actualParameters.SetExpression(0,parameter0);
  5441. actualParameters.SetExpression(1,parameter1);
  5442. END;
  5443. (* ---- SYSTEM.PUT16 ----- *)
  5444. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5445. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5446. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5447. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5448. actualParameters.SetExpression(0,parameter0);
  5449. actualParameters.SetExpression(1,parameter1);
  5450. END;
  5451. (* ---- SYSTEM.PUT8 ----- *)
  5452. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5453. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5454. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5455. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5456. actualParameters.SetExpression(0,parameter0);
  5457. actualParameters.SetExpression(1,parameter1);
  5458. END;
  5459. (* ---- SYSTEM.MOVE ----- *)
  5460. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5461. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5462. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5463. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5464. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5465. actualParameters.SetExpression(0,parameter0);
  5466. actualParameters.SetExpression(1,parameter1);
  5467. actualParameters.SetExpression(2,parameter2);
  5468. END;
  5469. (* ---- SYSTEM.NEW ----- *)
  5470. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5471. IF ~IsPointerType(parameter0.type) THEN
  5472. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5473. ELSIF CheckSizeType(parameter1) THEN
  5474. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5475. actualParameters.SetExpression(1,parameter1);
  5476. END;
  5477. (* ----SYSTEM.REF ---- *)
  5478. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5479. type := system.addressType
  5480. (* ---- INCR ----- *)
  5481. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5482. type := system.sizeType;
  5483. base := type0;
  5484. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5485. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5486. IF i1 < 0 THEN
  5487. Error(position,Diagnostics.Invalid,"invalid dimension");
  5488. base := SyntaxTree.invalidType;
  5489. ELSE
  5490. base := ArrayBase(base,i1);
  5491. IF (base # NIL) & Indexable(base) THEN
  5492. ELSE
  5493. Error(position,Diagnostics.Invalid,"len on no array");
  5494. IF VerboseErrorMessage THEN
  5495. Printout.Info("base",base);
  5496. END;
  5497. base := SyntaxTree.invalidType;
  5498. END;
  5499. END;
  5500. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5501. actualParameters.SetExpression(1,parameter1);
  5502. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5503. mathArrayType := base(SyntaxTree.MathArrayType);
  5504. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5505. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5506. type := system.longintType;
  5507. END;
  5508. END;
  5509. ELSE
  5510. type := system.longintType;
  5511. END;
  5512. (* ---- SUM ----- *)
  5513. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5514. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5515. (* ---- ALL ----- *)
  5516. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5517. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5518. (* ---- DIM ----- *)
  5519. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5520. type := system.sizeType;
  5521. IF type0 IS SyntaxTree.MathArrayType THEN
  5522. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5523. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5524. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5525. END;
  5526. ELSE
  5527. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5528. END;
  5529. (* ---- CAS ----- *)
  5530. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5531. IF type0.IsComposite () THEN
  5532. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5533. result := SyntaxTree.invalidExpression;
  5534. ELSIF ~IsVariable (parameter0) THEN
  5535. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5536. result := SyntaxTree.invalidExpression;
  5537. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5538. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5539. result := SyntaxTree.invalidExpression;
  5540. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5541. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5542. result := SyntaxTree.invalidExpression;
  5543. ELSE
  5544. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5545. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5546. type := type0;
  5547. END;
  5548. (* ---- RESHAPE ----- *)
  5549. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5550. IF type0 IS SyntaxTree.MathArrayType THEN
  5551. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5552. base := ArrayBase(type0,MAX(LONGINT));
  5553. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5554. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5555. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5556. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5557. IF ~CompatibleTo(system,type0,parameterType) THEN
  5558. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5559. result := SyntaxTree.invalidExpression;
  5560. ELSE
  5561. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5562. END;
  5563. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5564. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5565. IF ~CompatibleTo(system,type1,parameterType) THEN
  5566. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5567. result := SyntaxTree.invalidExpression;
  5568. ELSE
  5569. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5570. END;
  5571. ELSE
  5572. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5573. result := SyntaxTree.invalidExpression;
  5574. END;
  5575. (* ---- SYSTEM.TYPECODE ----- *)
  5576. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5577. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5578. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5579. type := type.resolved;
  5580. IF type IS SyntaxTree.PointerType THEN
  5581. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5582. END;
  5583. IF ~(type IS SyntaxTree.RecordType) THEN
  5584. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5585. END;
  5586. ELSE
  5587. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5588. END;
  5589. type := system.addressType;
  5590. (* -------- FLT --------- *)
  5591. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5592. type := system.realType;
  5593. IF IsRealValue(parameter0, r) THEN
  5594. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5595. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5596. i0 := i; i := ABS(i);
  5597. IF i # 0 THEN
  5598. i1 := 23;
  5599. IF i >= 2*800000H THEN
  5600. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5601. ELSIF i < 800000H THEN
  5602. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5603. END;
  5604. i := (i1 + 127)*800000H - 800000H + i;
  5605. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5606. END;
  5607. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5608. END;
  5609. (* ------- CONNECT -------*)
  5610. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5611. IF ~(currentIsCellNet) THEN
  5612. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5613. END;
  5614. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5615. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5616. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5617. END;
  5618. IF numberActualParameters = 3 THEN
  5619. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5620. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5621. END;
  5622. *)
  5623. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5624. actualParameters.SetExpression(2,parameter2);
  5625. END;
  5626. activeCellsStatement := TRUE;
  5627. (* ---------- DELEGATE --------*)
  5628. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5629. IF ~(currentIsCellNet) THEN
  5630. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5631. END;
  5632. IF ~CheckPortType(parameter1, inPort) THEN
  5633. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5634. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5635. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5636. ELSIF (outPort.direction # inPort.direction) THEN
  5637. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5638. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5639. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5640. END;
  5641. activeCellsStatement := TRUE;
  5642. (* --------- RECEIVE ---------*)
  5643. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5644. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5645. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5646. IF inPort.direction # SyntaxTree.InPort THEN
  5647. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5648. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5649. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5650. END;
  5651. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5652. IF ~SameType(parameter2.type, system.integerType) THEN
  5653. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5654. END;
  5655. END;
  5656. END;
  5657. (* --------- SEND ---------*)
  5658. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5659. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5660. IF CheckPortType(parameter0,outPort) THEN
  5661. IF outPort.direction # SyntaxTree.OutPort THEN
  5662. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5663. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5664. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5665. ELSE
  5666. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5667. actualParameters.SetExpression(1,parameter1);
  5668. END;
  5669. END;
  5670. (* ------- custom builtins ----- *)
  5671. ELSIF id = Global.systemSpecial THEN
  5672. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5673. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5674. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5675. type := procedureType.returnType;
  5676. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5677. (* go through all formal parameters *)
  5678. formalParameter := procedureType.firstParameter;
  5679. FOR i := 0 TO actualParameters.Length() - 1 DO
  5680. actualParameter := actualParameters.GetExpression(i);
  5681. IF actualParameter = SyntaxTree.invalidExpression THEN
  5682. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5683. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5684. ELSE
  5685. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5686. END;
  5687. actualParameters.SetExpression(i, actualParameter);
  5688. formalParameter := formalParameter.nextParameter
  5689. END
  5690. END
  5691. ELSE
  5692. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5693. result := SyntaxTree.invalidExpression;
  5694. END;
  5695. END;
  5696. IF result # SyntaxTree.invalidExpression THEN
  5697. type := ResolveType(type);
  5698. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5699. result.SetType(type);
  5700. END;
  5701. RETURN result
  5702. END NewBuiltinCallDesignator;
  5703. (** return type guard designator left(type)
  5704. - check if type can be extended (i.e. is no static record)
  5705. - check if type is a type extension of left.type
  5706. - returns new type guard designator
  5707. returns invalidDesignator = invalidExpression if error
  5708. **)
  5709. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5710. VAR result: SyntaxTree.Designator;
  5711. BEGIN
  5712. result := SyntaxTree.invalidDesignator;
  5713. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5714. Error(position,Diagnostics.Invalid,"no type extension of type");
  5715. IF VerboseErrorMessage THEN
  5716. Printout.Info("left",left);
  5717. Printout.Info("type",type);
  5718. END;
  5719. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5720. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5721. ELSIF IsUnsafePointer(left.type) THEN
  5722. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5723. ELSE
  5724. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5725. result.SetType(type);
  5726. result.SetAssignable(left.assignable);
  5727. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5728. END;
  5729. RETURN result
  5730. END NewTypeGuardDesignator;
  5731. (** check and resolve parameter designator left(expression list)
  5732. - check expression list
  5733. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5734. - elsif left is a procedure type then
  5735. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5736. - else return is a procedure call then return ProcedureCallDesignator
  5737. returns invalidDesignator = invalidExpression if error
  5738. **)
  5739. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5740. VAR
  5741. parameters: SyntaxTree.ExpressionList;
  5742. left: SyntaxTree.Designator;
  5743. result,expression: SyntaxTree.Expression;
  5744. typeDeclaration: SyntaxTree.TypeDeclaration;
  5745. type, expressionType: SyntaxTree.Type;
  5746. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5747. BEGIN
  5748. type := type.resolved;
  5749. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5750. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5751. END;
  5752. RETURN type
  5753. END BaseType;
  5754. BEGIN
  5755. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5756. result := SyntaxTree.invalidDesignator;
  5757. left := ResolveDesignator(designator.left);
  5758. IF left # SyntaxTree.invalidDesignator THEN
  5759. parameters := designator.parameters;
  5760. IF ExpressionList(parameters) THEN
  5761. IF (left.type = NIL) THEN
  5762. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5763. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5764. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5765. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5766. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5767. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5768. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5769. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5770. ELSE
  5771. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5772. END
  5773. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5774. expression := parameters.GetExpression(0);
  5775. type := typeDeclaration.declaredType.resolved;
  5776. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5777. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5778. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5779. OR (expressionType IS SyntaxTree.EnumerationType)
  5780. ) THEN
  5781. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5782. ELSE
  5783. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5784. END;
  5785. ELSE
  5786. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5787. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5788. result := SyntaxTree.invalidDesignator;
  5789. END;
  5790. ELSE
  5791. result := SyntaxTree.invalidDesignator
  5792. END;
  5793. END;
  5794. resolvedExpression := result;
  5795. END VisitParameterDesignator;
  5796. (** check dereference designator left^
  5797. - check if left is pointer type or left is object type
  5798. - return new dereference designator with type = left.baseType.type (if appropriate)
  5799. with error handling
  5800. returns invalidDesignator = invalidExpression if error
  5801. **)
  5802. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5803. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5804. BEGIN
  5805. result := SyntaxTree.invalidDesignator;
  5806. type := left.type;
  5807. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5808. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5809. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5810. result.SetAssignable(TRUE);
  5811. result.SetType(type);
  5812. result.SetHidden(left.isHidden);
  5813. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5814. type := type.resolved;
  5815. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5816. result.SetAssignable(TRUE);
  5817. result.SetType(type);
  5818. result.SetHidden(left.isHidden);
  5819. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5820. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5821. result.SetAssignable(TRUE);
  5822. result.SetType(type);
  5823. result.SetHidden(left.isHidden);
  5824. ELSE
  5825. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5826. IF VerboseErrorMessage THEN
  5827. Printout.Info("pointer", type);
  5828. Printout.Info("scope", currentScope);
  5829. END;
  5830. END;
  5831. RETURN result
  5832. END NewDereferenceDesignator;
  5833. (** check supercall designator left^
  5834. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5835. - return new supercall designator with type = left.type
  5836. with error handling
  5837. **)
  5838. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5839. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5840. objectScope: SyntaxTree.Scope;
  5841. BEGIN
  5842. result := SyntaxTree.invalidDesignator;
  5843. IF left = SyntaxTree.invalidDesignator THEN
  5844. (* error already handled *)
  5845. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5846. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5847. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5848. IF symbol IS SyntaxTree.Procedure THEN
  5849. procedure := symbol(SyntaxTree.Procedure);
  5850. objectScope := currentScope;
  5851. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5852. objectScope := objectScope.outerScope;
  5853. END;
  5854. IF (left.left = NIL) OR ~
  5855. (
  5856. (left.left IS SyntaxTree.SelfDesignator) OR
  5857. (left.left IS SyntaxTree.DereferenceDesignator)
  5858. & (left.left(SyntaxTree.Designator).left # NIL)
  5859. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5860. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5861. IF VerboseErrorMessage THEN
  5862. Printout.Info("left.left",left.left);
  5863. END;
  5864. ELSIF procedure.super # NIL THEN
  5865. result := SyntaxTree.NewSupercallDesignator(position,left);
  5866. result.SetType(left.type.resolved)
  5867. ELSE
  5868. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5869. END;
  5870. ELSE
  5871. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5872. END;
  5873. ELSE
  5874. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5875. END;
  5876. RETURN result
  5877. END NewSupercallDesignator;
  5878. (** check and semantically resolve arrow designator left^
  5879. - if left is procedure type -> result := SupercallDesignator
  5880. - else result := DereferenceDesignator
  5881. returns result via global variable resolvedExpression
  5882. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5883. **)
  5884. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5885. VAR left: SyntaxTree.Designator;
  5886. BEGIN
  5887. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5888. left := ResolveDesignator(arrowDesignator.left);
  5889. IF left # NIL THEN
  5890. IF (left.type = NIL) THEN
  5891. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5892. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5893. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5894. ELSE
  5895. IF IsPointerToObject(left.type) THEN
  5896. Warning(arrowDesignator.position, "forbidden dereference on object");
  5897. END;
  5898. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5899. END
  5900. END
  5901. END VisitArrowDesignator;
  5902. (** check and return expression
  5903. - if expression has no type then resolve expression
  5904. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5905. - return result
  5906. **)
  5907. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5908. VAR result,prev: SyntaxTree.Expression;
  5909. BEGIN
  5910. IF expression = NIL THEN result := NIL
  5911. ELSIF (expression.type = NIL) THEN
  5912. prev := resolvedExpression;
  5913. resolvedExpression := SyntaxTree.invalidExpression;
  5914. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5915. expression.SetType(SyntaxTree.invalidType);
  5916. END;
  5917. expression.Accept(SELF);
  5918. result := resolvedExpression;
  5919. IF currentIsRealtime THEN
  5920. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5921. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5922. END;
  5923. END;
  5924. (* designator modifiers for backends if they support it ...*)
  5925. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5926. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5927. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5928. END;
  5929. resolvedExpression := prev
  5930. ELSE
  5931. result := expression
  5932. END;
  5933. RETURN result
  5934. END ResolveExpression;
  5935. (**
  5936. check expression to be constant expression
  5937. - resolve expression
  5938. - if valid then check that of value type
  5939. report error and return invalidExpression if anything fails
  5940. **)
  5941. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5942. VAR position: LONGINT;
  5943. BEGIN
  5944. position := expression.position;
  5945. expression := ResolveExpression(expression);
  5946. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5947. ELSIF (expression.resolved = NIL) THEN
  5948. Error(position,Diagnostics.Invalid,"expression is not constant");
  5949. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5950. expression := SyntaxTree.invalidExpression;
  5951. END;
  5952. RETURN expression
  5953. END ConstantExpression;
  5954. (** check expression to be constant integer
  5955. - resolve expresssion
  5956. - if valid then check that of integer value type
  5957. report error and return invalidExpression if anything fails
  5958. **)
  5959. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5960. VAR position: LONGINT;
  5961. BEGIN
  5962. position := expression.position;
  5963. expression := ResolveExpression(expression);
  5964. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5965. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5966. expression := SyntaxTree.invalidExpression;
  5967. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5968. END;
  5969. RETURN expression
  5970. END ConstantInteger;
  5971. (** check expression as positive (>=0) constant integer
  5972. - resolve expression
  5973. - if valid then check that integer value
  5974. - if integer value then check that value >= 0
  5975. report error and return invalidExpression if anything fails
  5976. **)
  5977. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5978. VAR position: LONGINT;
  5979. BEGIN
  5980. position := expression.position;
  5981. expression := ConstantExpression(expression);
  5982. IF expression = SyntaxTree.invalidExpression THEN
  5983. (* error already reported *)
  5984. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5985. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5986. expression := SyntaxTree.invalidExpression
  5987. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5988. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5989. END;
  5990. RETURN expression
  5991. END ConstantIntegerGeq0;
  5992. (** check expression as condition
  5993. - resolve expression
  5994. - if valid expression then check that result type is boolean
  5995. report error and return invalidExpression if anything fails
  5996. **)
  5997. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5998. VAR position: LONGINT;
  5999. BEGIN
  6000. position := expression.position;
  6001. expression := ResolveExpression(expression);
  6002. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6003. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6004. expression := SyntaxTree.invalidExpression;
  6005. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6006. END;
  6007. RETURN expression
  6008. END ResolveCondition;
  6009. (*** symbols ***)
  6010. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6011. BEGIN
  6012. x.Accept(SELF);
  6013. END ResolveSymbol;
  6014. (** check a symbol
  6015. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6016. **)
  6017. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6018. VAR scope: SyntaxTree.Scope;
  6019. BEGIN
  6020. (* visibility *)
  6021. scope := symbol.scope;
  6022. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6023. scope := scope.outerScope;
  6024. END;
  6025. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6026. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6027. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6028. IF VerboseErrorMessage THEN
  6029. Printout.Info("symbol",symbol);
  6030. END;
  6031. END;
  6032. END;
  6033. END CheckSymbolVisibility;
  6034. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6035. If node is currently being resolved then emit a cyclic definition error.
  6036. Return TRUE only if node is fully resolved.
  6037. **)
  6038. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6039. VAR result: BOOLEAN;
  6040. BEGIN
  6041. IF SyntaxTree.Resolved IN x.state THEN
  6042. result := FALSE
  6043. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6044. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6045. result := FALSE;
  6046. ELSE
  6047. result := TRUE;
  6048. x.SetState(SyntaxTree.BeingResolved)
  6049. END;
  6050. RETURN result
  6051. END SymbolNeedsResolution;
  6052. (** check and resolve a type declaration symbol = Type
  6053. - set type to declaration type
  6054. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6055. This is so because the type declaration itself does not have a type but it only stands for a type.
  6056. In the implementation of the compiler this made a lot much easier.
  6057. - resolve and set declared type
  6058. - check symbol
  6059. **)
  6060. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6061. VAR prevScope: SyntaxTree.Scope;
  6062. BEGIN
  6063. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6064. IF SymbolNeedsResolution(typeDeclaration) THEN
  6065. prevScope := currentScope;
  6066. currentScope := typeDeclaration.scope;
  6067. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6068. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6069. CheckSymbolVisibility(typeDeclaration);
  6070. typeDeclaration.SetState(SyntaxTree.Resolved);
  6071. currentScope := prevScope;
  6072. END;
  6073. END VisitTypeDeclaration;
  6074. (** check and resolve a constant declaration symbol = (constant) expression
  6075. - check expression
  6076. - set type and value
  6077. - check symbol
  6078. **)
  6079. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6080. VAR
  6081. expression: SyntaxTree.Expression;
  6082. type: SyntaxTree.Type;
  6083. name: Basic.SegmentedName;
  6084. replacement: Replacement;
  6085. BEGIN
  6086. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6087. IF SymbolNeedsResolution(constant) THEN
  6088. expression := constant.value;
  6089. IF replacements # NIL THEN
  6090. Global.GetSymbolSegmentedName(constant, name);
  6091. replacement := replacements;
  6092. WHILE (replacement # NIL) & (replacement.name # name) DO
  6093. replacement := replacement.next;
  6094. END;
  6095. IF replacement # NIL THEN
  6096. InfoSS(constant.position, "replacing constant", constant.name);
  6097. (*
  6098. NEW(stringReader, Strings.Length(replacement.string^));
  6099. stringReader.Set(replacement.string^);
  6100. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6101. NEW(parser, scanner, diagnostics);
  6102. expression := parser.Expression();
  6103. *)
  6104. expression := replacement.expression;
  6105. replacement.used := TRUE;
  6106. END;
  6107. END;
  6108. constant.SetType(SyntaxTree.invalidType);
  6109. expression := ConstantExpression(expression);
  6110. ASSERT(expression.type # NIL);
  6111. type := expression.type.resolved;
  6112. constant.SetType(type);
  6113. constant.SetValue(expression);
  6114. CheckSymbolVisibility(constant);
  6115. constant.SetState(SyntaxTree.Resolved);
  6116. END;
  6117. END VisitConstant;
  6118. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6119. VAR procedureAlignment: LONGINT;
  6120. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6121. (* least common multiple *)
  6122. VAR a,b: LONGINT;
  6123. BEGIN
  6124. a := a0; b := b0;
  6125. WHILE (a # b) DO
  6126. IF a < b THEN a := a+a0
  6127. ELSE b := b + b0
  6128. END;
  6129. END;
  6130. RETURN a
  6131. END LCM;
  6132. BEGIN
  6133. IF alignment > 1 THEN
  6134. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6135. IF (procedureAlignment > 1) THEN
  6136. alignment := LCM(alignment, procedureAlignment);
  6137. END;
  6138. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6139. END;
  6140. END AdaptStackAlignment;
  6141. (** check and resolve a variable / field
  6142. - check and set type
  6143. - negative check on open array type
  6144. - check symbol
  6145. **)
  6146. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6147. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6148. BEGIN
  6149. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6150. IF SymbolNeedsResolution(variable) THEN
  6151. modifiers := variable.modifiers;
  6152. (*
  6153. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6154. variable.AddFlags(flags);
  6155. *)
  6156. variable.SetType(ResolveType(variable.type));
  6157. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6158. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6159. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6160. END;
  6161. END;
  6162. CheckSymbolVisibility(variable);
  6163. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6164. variable.SetUntraced(TRUE);
  6165. IF ~ContainsPointer(variable.type) THEN
  6166. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6167. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6168. END;
  6169. END;
  6170. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6171. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6172. IF ~PowerOf2(value) THEN
  6173. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6174. ELSE
  6175. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6176. END;
  6177. END;
  6178. variable.SetAlignment(FALSE,value);
  6179. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6180. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6181. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6182. END;
  6183. variable.SetAlignment(TRUE, value);
  6184. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6185. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6186. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6187. END;
  6188. variable.SetFictive(value);
  6189. variable.SetOffset(value*system.dataUnit);
  6190. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6191. END;
  6192. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6193. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6194. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6195. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6196. END;
  6197. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6198. IF variable.initializer # NIL THEN
  6199. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6200. END;
  6201. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6202. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6203. pointerType.SetPointerBase(variable.type);
  6204. pointerType.SetHidden(TRUE);
  6205. variable.SetType(ResolveType(pointerType));
  6206. END;
  6207. variable.SetState(SyntaxTree.Resolved);
  6208. END;
  6209. END VisitVariable;
  6210. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6211. BEGIN
  6212. VisitVariable(property)
  6213. END VisitProperty;
  6214. (** check and resolve a (procedure) parameter
  6215. - check and set type
  6216. - check symbol
  6217. - check parameter kind and set read-only flags if appropriate
  6218. **)
  6219. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6220. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6221. BEGIN
  6222. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6223. IF SymbolNeedsResolution(parameter) THEN
  6224. modifiers := parameter.modifiers;
  6225. parameter.SetType(ResolveType(parameter.type));
  6226. ASSERT(parameter.type.resolved # NIL);
  6227. CheckSymbolVisibility(parameter);
  6228. IF parameter.defaultValue # NIL THEN
  6229. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6230. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6231. ELSE
  6232. expression := ConstantExpression(parameter.defaultValue);
  6233. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6234. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6235. parameter.SetDefaultValue(expression);
  6236. END;
  6237. END;
  6238. END;
  6239. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6240. parameter.SetUntraced(TRUE);
  6241. IF ~ContainsPointer(parameter.type) THEN
  6242. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6243. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6244. END;
  6245. END;
  6246. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6247. parameter.SetMoveable(TRUE);
  6248. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6249. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6250. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6251. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6252. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6253. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6254. END;
  6255. END;
  6256. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6257. parameter.SetState(SyntaxTree.Resolved);
  6258. END;
  6259. END VisitParameter;
  6260. (** check and resolve a procedure (with declaration and implementation scope)
  6261. - check the procedure type
  6262. - check if method (i.e. in record scope), if so then
  6263. - check if (unique) constructor
  6264. - check if (unique) finalizer
  6265. - check if super method available, if so then check signature
  6266. - of not in record scope then negative check on constructor flag
  6267. - of not in record scope then negative check on finalizer flag
  6268. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6269. - check procedure symbol
  6270. **)
  6271. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6272. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6273. procedureType: SyntaxTree.ProcedureType;
  6274. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6275. qualifiedType: SyntaxTree.QualifiedType;
  6276. value: LONGINT;
  6277. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6278. position: LONGINT;
  6279. BEGIN
  6280. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6281. IF IsOberonInline(procedure) THEN
  6282. IF SyntaxTree.Public * procedure.access # {} THEN
  6283. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6284. END;
  6285. procedure.SetInline(FALSE);
  6286. procedure.SetOberonInline(TRUE);
  6287. END;
  6288. IF SymbolNeedsResolution(procedure) THEN
  6289. recentIsRealtime := currentIsRealtime;
  6290. recentIsBodyProcedure := currentIsBodyProcedure;
  6291. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6292. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6293. modifiers := procedureType.modifiers;
  6294. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6295. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6296. IF useDarwinCCalls THEN (*fld*)
  6297. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6298. ELSE
  6299. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6300. END
  6301. END;
  6302. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6303. procedureType.SetInterrupt(TRUE);
  6304. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6305. END;
  6306. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6307. procedureType.SetNoReturn(TRUE);
  6308. END;
  6309. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6310. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6311. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6312. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6313. END;
  6314. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6315. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6316. END;
  6317. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6318. IF ~PowerOf2(value) THEN
  6319. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6320. ELSE
  6321. procedureType.SetStackAlignment(value)
  6322. END;
  6323. END;
  6324. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6325. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6326. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6327. END;
  6328. CheckModifiers(modifiers, TRUE);
  6329. modifiers := procedureType.returnTypeModifiers;
  6330. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6331. CheckModifiers(modifiers, TRUE);
  6332. FixProcedureType(procedureType);
  6333. currentIsRealtime := procedureType.isRealtime;
  6334. currentIsBodyProcedure := procedure.isBodyProcedure;
  6335. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6336. THEN
  6337. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6338. END;
  6339. CheckSymbolVisibility(procedure);
  6340. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6341. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6342. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6343. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6344. END;
  6345. END;
  6346. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6347. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6348. procedureType.SetDelegate(TRUE);
  6349. IF procedureType.isRealtime & ~record.isRealtime THEN
  6350. Error(procedure.position, Diagnostics.Invalid, "invalid realtime procedure in non-realtime object");
  6351. END;
  6352. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6353. IF record.pointerType.typeDeclaration = NIL THEN
  6354. selfParameter.SetType(record.pointerType);
  6355. ELSE
  6356. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6357. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6358. qualifiedType.SetResolved(record.pointerType);
  6359. selfParameter.SetType(qualifiedType);
  6360. END;
  6361. selfParameter.SetAccess(SyntaxTree.Hidden);
  6362. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6363. IF procedure.isConstructor THEN
  6364. (*! constructor is always visible, compatibility to paco
  6365. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6366. *)
  6367. procedure.MarkUsed;
  6368. IF procedureType.returnType # NIL THEN
  6369. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6370. END;
  6371. proc := procedure.scope.firstProcedure;
  6372. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6373. proc := proc.nextProcedure;
  6374. END;
  6375. IF proc # NIL THEN
  6376. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6377. ELSE
  6378. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6379. END;
  6380. END;
  6381. IF procedure.isFinalizer THEN
  6382. procedure.MarkUsed;
  6383. IF procedureType.returnType # NIL THEN
  6384. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6385. END;
  6386. IF procedureType.numberParameters # 0 THEN
  6387. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6388. END;
  6389. proc := procedure.scope.firstProcedure;
  6390. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6391. proc := proc.nextProcedure;
  6392. END;
  6393. IF proc # NIL THEN
  6394. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6395. ELSE
  6396. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6397. END;
  6398. END;
  6399. super := FindSuperProcedure(record.recordScope, procedure);
  6400. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6401. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6402. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6403. END;
  6404. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6405. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6406. END;
  6407. IF super.isFinal THEN
  6408. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6409. END;
  6410. procedure.SetSuper(super);
  6411. super.SetOverwritten(TRUE);
  6412. procedure.SetAccess(procedure.access+super.access);
  6413. procedure.MarkUsed;
  6414. END;
  6415. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6416. THEN
  6417. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6418. END;
  6419. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6420. IF cellsAreObjects THEN
  6421. procedureType.SetDelegate(TRUE);
  6422. END;
  6423. IF procedure.isConstructor THEN
  6424. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6425. END;
  6426. ELSIF procedure.isConstructor THEN
  6427. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6428. END;
  6429. Declarations(procedure.procedureScope, FALSE);
  6430. (* body resolution part done as late fix of the procedure type *)
  6431. procedure.SetState(SyntaxTree.Resolved);
  6432. currentIsRealtime := recentIsRealtime;
  6433. currentIsBodyProcedure := recentIsBodyProcedure;
  6434. END;
  6435. END VisitProcedure;
  6436. (**
  6437. a builtin procedure is a global item that may not be modified locally
  6438. instead the resolving of builtin procedure calls are done in the esignator
  6439. **)
  6440. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6441. VAR type: SyntaxTree.Type;
  6442. BEGIN
  6443. type := ResolveType(builtinProcedure.type);
  6444. END VisitBuiltin;
  6445. (* nopov *)
  6446. (** check and resolve operator
  6447. - operators are first checked as procedures
  6448. - then additional operator-specific checks are done
  6449. - note that only module-scope operators are checked here
  6450. (operators in a record scope are only allowed in the context of
  6451. array-structured object types and checked in 'ResolveArrayStructure')
  6452. - also note that inter-operator conformity is not checked here
  6453. **)
  6454. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6455. VAR
  6456. procedureType: SyntaxTree.ProcedureType;
  6457. leftType, rightType: SyntaxTree.Type;
  6458. identifierNumber, position: LONGINT;
  6459. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6460. modifiers: SyntaxTree.Modifier;
  6461. (** whether a type is locally defined in the current module scope
  6462. for arrays, the base type must be locally defined **)
  6463. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6464. BEGIN
  6465. IF type = NIL THEN
  6466. RETURN FALSE
  6467. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6468. RETURN TRUE
  6469. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6470. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6471. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6472. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6473. ELSE
  6474. RETURN FALSE
  6475. END
  6476. END IsLocallyDefined;
  6477. BEGIN
  6478. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6479. procedureType := operator.type(SyntaxTree.ProcedureType);
  6480. modifiers := procedureType.modifiers;
  6481. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6482. CheckModifiers(modifiers, TRUE);
  6483. VisitProcedure(operator);
  6484. IF operator.scope IS SyntaxTree.RecordScope THEN
  6485. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6486. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6487. IF identifierNumber = -1 THEN
  6488. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6489. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6490. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6491. ELSE
  6492. IF procedureType.numberParameters < 1 THEN
  6493. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6494. ELSIF procedureType.numberParameters > 2 THEN
  6495. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6496. ELSE
  6497. (* determine operand types *)
  6498. leftType := procedureType.firstParameter.type;
  6499. IF procedureType.numberParameters > 1 THEN
  6500. rightType := procedureType.firstParameter.nextParameter.type
  6501. ELSE
  6502. rightType := NIL
  6503. END;
  6504. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6505. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6506. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6507. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6508. END
  6509. END;
  6510. (* TODO: refine the checks, think about how restrictive the checks should be
  6511. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6512. They might be used for intersection, union, complement of custom object types *)
  6513. (* defaults *)
  6514. hasReturnType := TRUE;
  6515. mustBeUnary := FALSE;
  6516. mustBeBinary := FALSE;
  6517. mustReturnBoolean := FALSE;
  6518. mustReturnInteger := FALSE;
  6519. mustHaveEquitypedOperands := FALSE;
  6520. (* operator-specific exceptions *)
  6521. CASE identifierNumber OF
  6522. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6523. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6524. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6525. mustBeBinary := TRUE
  6526. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6527. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6528. | Scanner.Times: mustBeBinary := TRUE
  6529. | Scanner.TimesTimes: mustBeBinary := TRUE
  6530. | Scanner.DotTimes: mustBeBinary := TRUE
  6531. | Scanner.PlusTimes: mustBeBinary := TRUE
  6532. | Scanner.Slash: mustBeBinary := TRUE
  6533. | Scanner.Backslash: mustBeBinary := TRUE
  6534. | Scanner.DotSlash: mustBeBinary := TRUE
  6535. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6536. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6537. | Scanner.Not: mustBeUnary := TRUE
  6538. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6539. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6540. | Scanner.Transpose: mustBeUnary := TRUE;
  6541. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6542. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6543. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6544. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6545. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6546. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6547. | Global.Abs: mustBeUnary := TRUE;
  6548. | Global.Ash: (* TODO: arity? *)
  6549. | Global.Cap: (* TODO: arity? *)
  6550. | Global.Chr: mustBeUnary := TRUE;
  6551. | Global.Entier: (* TODO: arity? *)
  6552. | Global.EntierH: (* TODO: arity? *)
  6553. | Global.Len: (* unary and binary *)
  6554. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6555. | Global.Max, Global.Min: (* unary and binary *)
  6556. | Global.Odd: (* TODO: arity? *)
  6557. | Global.Sum: (* TODO: arity? *)
  6558. | Global.All: (* TODO: arity? *)
  6559. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6560. | Scanner.Alias:
  6561. | Scanner.GreaterGreater, Scanner.LessLess:
  6562. mustBeBinary := TRUE; hasReturnType := FALSE;
  6563. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6564. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6565. END;
  6566. (* check parameter count *)
  6567. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6568. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6569. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6570. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6571. END;
  6572. (* check parameter types *)
  6573. (* TODO: is this used at all? *)
  6574. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6575. leftType := procedureType.firstParameter.type;
  6576. rightType := procedureType.firstParameter.nextParameter.type;
  6577. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6578. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6579. END
  6580. END;
  6581. (* check return type *)
  6582. IF hasReturnType THEN
  6583. IF procedureType.returnType = NIL THEN
  6584. Error(operator.position, Diagnostics.Invalid, "return type required")
  6585. ELSIF mustReturnBoolean THEN
  6586. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6587. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6588. END
  6589. ELSIF mustReturnInteger THEN
  6590. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6591. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6592. END
  6593. END
  6594. ELSIF procedureType.returnType # NIL THEN
  6595. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6596. END
  6597. END
  6598. END
  6599. END
  6600. END VisitOperator;
  6601. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6602. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6603. BEGIN
  6604. IF error THEN RETURN FALSE END;
  6605. prevScope := currentScope;
  6606. prevDiagnostics := diagnostics;
  6607. diagnostics := NIL; (* suppress error output *)
  6608. currentScope := module.moduleScope;
  6609. VisitImport(x);
  6610. IF ~error THEN
  6611. module.moduleScope.AddImport(x);
  6612. x.SetScope(module.moduleScope);
  6613. END;
  6614. currentScope := prevScope;
  6615. diagnostics := prevDiagnostics;
  6616. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6617. END AddImport;
  6618. (** check and resolve import
  6619. - check for name = SYSTEM
  6620. - check for forbidden self import
  6621. - search through global import cache: already imported?
  6622. - check if already imported indirectly
  6623. - import if necessary -> set module and enter into import cache
  6624. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6625. - after this import this direct import and all indirect imports are stored in the current module's import list
  6626. **)
  6627. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6628. VAR
  6629. module: SyntaxTree.Module;
  6630. moduleScope: SyntaxTree.ModuleScope;
  6631. import,reimport: SyntaxTree.Import;
  6632. filename: FileName;
  6633. prevScope: SyntaxTree.Scope;
  6634. BEGIN
  6635. IF SymbolNeedsResolution(x) THEN
  6636. prevScope := currentScope;
  6637. x.SetType(SyntaxTree.importType);
  6638. moduleScope := currentScope.ownerModule.moduleScope;
  6639. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6640. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6641. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6642. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6643. ELSE
  6644. (* search through global import list: already imported ? *)
  6645. IF (x.module = NIL) & (importCache # NIL) THEN
  6646. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6647. ELSE import := NIL
  6648. END;
  6649. IF x.module # NIL THEN (* already imported indirectly *)
  6650. module := x.module;
  6651. ELSIF import # NIL THEN (* already in module list *)
  6652. module := import.module;
  6653. ASSERT(module # NIL);
  6654. x.SetModule(module);
  6655. ELSE (* must be imported *)
  6656. Global.ModuleFileName(x.moduleName,x.context,filename);
  6657. IF symbolFileFormat # NIL THEN
  6658. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6659. IF module = NIL THEN
  6660. ErrorSS(x.position,"could not import",filename);
  6661. IF VerboseErrorMessage THEN
  6662. Printout.Info("import",x)
  6663. END
  6664. ELSE
  6665. (*
  6666. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6667. (*! should rather be done by importer *)
  6668. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6669. checker.importCache := importCache;
  6670. checker.arrayBaseImported := arrayBaseImported;
  6671. checker.global := global;
  6672. checker.Module(module); (* semantic check *)
  6673. error := error OR checker.error;
  6674. END;
  6675. *)
  6676. (*
  6677. ASSERT(SyntaxTree.Resolved IN module.state);
  6678. *)
  6679. x.SetModule(module);
  6680. IF importCache # NIL THEN
  6681. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6682. import.SetContext(x.context);
  6683. import.SetModule(module);
  6684. importCache.AddImport(import);
  6685. END;
  6686. END;
  6687. ELSE
  6688. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6689. END;
  6690. END;
  6691. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6692. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6693. END;
  6694. import := module.moduleScope.firstImport;
  6695. WHILE(import # NIL) DO
  6696. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6697. ASSERT(currentScope # NIL);
  6698. ASSERT(currentScope.ownerModule # NIL);
  6699. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6700. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6701. Error(x.position,Diagnostics.Invalid,"recursive import");
  6702. ELSE
  6703. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6704. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6705. IF reimport = NIL THEN (* indirect import *)
  6706. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6707. reimport.SetContext(import.context);
  6708. reimport.SetModule(import.module);
  6709. moduleScope.AddImport(reimport);
  6710. reimport.SetScope(moduleScope);
  6711. ELSE
  6712. ASSERT(import.module # NIL);
  6713. reimport.SetModule(import.module); (* direct or indirect import *)
  6714. END;
  6715. END;
  6716. import := import.nextImport;
  6717. END;
  6718. END;
  6719. END;
  6720. currentScope := prevScope;
  6721. (* ELSE nothing to be done *)
  6722. x.SetState(SyntaxTree.Resolved);
  6723. END;
  6724. END VisitImport;
  6725. (*** statements ***)
  6726. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6727. VAR prev,resolved: SyntaxTree.Statement;
  6728. BEGIN
  6729. prev := resolvedStatement;
  6730. resolvedStatement := x;
  6731. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6732. activeCellsStatement := FALSE;
  6733. x.Accept(SELF);
  6734. (* removed this, implementation restriction should be resolved by backend
  6735. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6736. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6737. END;
  6738. *)
  6739. resolved := resolvedStatement;
  6740. resolvedStatement := prev;
  6741. RETURN resolved
  6742. END ResolveStatement;
  6743. (** check and resolve statement sequence
  6744. - check all statements, replace if necessary
  6745. **)
  6746. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6747. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6748. BEGIN
  6749. IF statementSequence # NIL THEN (* else empty *)
  6750. FOR i := 0 TO statementSequence.Length()-1 DO
  6751. statement := statementSequence.GetStatement(i);
  6752. resolved := ResolveStatement(statement);
  6753. IF (resolved # statement) THEN
  6754. statementSequence.SetStatement(i,resolved);
  6755. END;
  6756. END;
  6757. END;
  6758. END StatementSequence;
  6759. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6760. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6761. - check if procedure is callable
  6762. - check return type = NIL (otherwise must be assignment statement)
  6763. **)
  6764. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6765. VAR call: SyntaxTree.Designator;
  6766. BEGIN
  6767. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6768. call := procedureCall.call;
  6769. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6770. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6771. END;
  6772. call := ResolveDesignator(call);
  6773. IF call = SyntaxTree.invalidDesignator THEN
  6774. (* error already handled *)
  6775. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6776. (* inline call in a statement *)
  6777. ELSIF ~IsCallable(call) THEN
  6778. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6779. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6780. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6781. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6782. END;
  6783. procedureCall.SetCall(call);
  6784. (*
  6785. IF call = SyntaxTree.invalidDesignator THEN
  6786. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6787. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6788. IF IsOberonInline(procedure) THEN
  6789. Warning(procedure.position,"call to inline proc");
  6790. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6791. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6792. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6793. resolvedStatement := block;
  6794. RETURN;
  6795. END;
  6796. END;
  6797. *)
  6798. END VisitProcedureCallStatement;
  6799. (** check and resolve assignment LHS := RHS
  6800. - resolve LHS and RHS
  6801. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6802. - check if assignment is compatible
  6803. - check if LHS is variable (i.e. assignable)
  6804. - convert RHS if necessary
  6805. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6806. - assignment between different ASOTs
  6807. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6808. - assignment to ASOT elements:
  6809. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6810. **)
  6811. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6812. VAR
  6813. left: SyntaxTree.Designator;
  6814. right, expression: SyntaxTree.Expression;
  6815. designator: SyntaxTree.Designator;
  6816. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6817. mathArrayType: SyntaxTree.MathArrayType;
  6818. BEGIN
  6819. right := ResolveExpression(assignment.right);
  6820. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6821. left := ResolveDesignator(assignment.left);
  6822. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6823. (* error already handled *)
  6824. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6825. (* LHS is index write operator call on ASOT *)
  6826. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6827. (* necessary ?
  6828. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6829. type := procedureType.firstParameter.type;
  6830. expression := procedureCallDesignator.parameters.GetExpression(0);
  6831. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6832. *)
  6833. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6834. ELSIF CheckVariable(left) THEN
  6835. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6836. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6837. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6838. (* conversion done by procedure call
  6839. (* try to convert to left argument *)
  6840. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6841. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6842. procedureCallDesignator.parameters.SetExpression(1, right);
  6843. END;
  6844. *)
  6845. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6846. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6847. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6848. ELSIF AssignmentCompatible(left, right) THEN
  6849. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6850. mathArrayType := MathArrayStructureOfType(left.type);
  6851. right := NewConversion(right.position, right, mathArrayType, NIL);
  6852. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6853. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6854. ELSE
  6855. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6856. assignment.SetLeft(left);
  6857. assignment.SetRight(right);
  6858. resolvedStatement := assignment
  6859. END
  6860. END
  6861. END
  6862. END VisitAssignment;
  6863. (** check and resolve assignment LHS := RHS
  6864. - resolve LHS and RHS
  6865. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6866. - check if assignment is compatible
  6867. - check if LHS is variable (i.e. assignable)
  6868. - convert RHS if necessary
  6869. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6870. - assignment between different ASOTs
  6871. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6872. - assignment to ASOT elements:
  6873. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6874. **)
  6875. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6876. VAR
  6877. left: SyntaxTree.Designator;
  6878. right: SyntaxTree.Expression;
  6879. inPort, outPort: SyntaxTree.PortType;
  6880. expression: SyntaxTree.Expression;
  6881. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6882. BEGIN
  6883. right := ResolveExpression(communication.right);
  6884. left := ResolveDesignator(communication.left);
  6885. communication.SetLeft(left);
  6886. communication.SetRight(right);
  6887. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6888. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6889. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6890. (* conversion done by procedure call
  6891. (* try to convert to left argument *)
  6892. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6893. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6894. procedureCallDesignator.parameters.SetExpression(1, right);
  6895. END;
  6896. *)
  6897. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6898. ELSE
  6899. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6900. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6901. (* error already handled *)
  6902. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6903. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6904. IF outPort.direction # SyntaxTree.OutPort THEN
  6905. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6906. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6907. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6908. ELSE
  6909. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6910. communication.SetRight(right)
  6911. END;
  6912. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6913. IF CheckVariable(left) THEN
  6914. IF inPort.direction # SyntaxTree.InPort THEN
  6915. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6916. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6917. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6918. END;
  6919. END;
  6920. ELSE
  6921. Error(communication.position, -1, "unsupported stream operation");
  6922. END;
  6923. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6924. IF outPort.direction # SyntaxTree.OutPort THEN
  6925. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6926. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6927. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6928. ELSE
  6929. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6930. communication.SetRight(right)
  6931. END;
  6932. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6933. IF CheckVariable(right) THEN
  6934. IF inPort.direction # SyntaxTree.InPort THEN
  6935. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6936. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6937. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6938. END;
  6939. END;
  6940. ELSE
  6941. Error(communication.position, -1, "unsupported operation");
  6942. END;
  6943. END;
  6944. END VisitCommunicationStatement;
  6945. (** check and resolve if/eslif part
  6946. - check condition
  6947. - check statement sequence
  6948. **)
  6949. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6950. VAR prevUnreachable, b: BOOLEAN;
  6951. BEGIN
  6952. prevUnreachable := currentIsUnreachable;
  6953. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6954. IF IsBooleanValue(ifPart.condition,b) THEN
  6955. IF b=FALSE THEN
  6956. currentIsUnreachable := TRUE
  6957. ELSIF b=TRUE THEN
  6958. true := TRUE
  6959. END;
  6960. END;
  6961. StatementSequence(ifPart.statements);
  6962. currentIsUnreachable := prevUnreachable;
  6963. END IfPart;
  6964. (** check and resolve if statement
  6965. - check if parts and else part statement sequence
  6966. **)
  6967. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6968. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6969. BEGIN
  6970. prevUnreachable := currentIsUnreachable;
  6971. ifPartTrue := FALSE;
  6972. IfPart(ifStatement.ifPart,ifPartTrue);
  6973. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6974. elsif := ifStatement.GetElsifPart(i);
  6975. IfPart(elsif,ifPartTrue);
  6976. END;
  6977. IF ifStatement.elsePart # NIL THEN
  6978. IF ifPartTrue THEN
  6979. currentIsUnreachable := TRUE
  6980. END;
  6981. StatementSequence(ifStatement.elsePart)
  6982. END;
  6983. currentIsUnreachable := prevUnreachable;
  6984. END VisitIfStatement;
  6985. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6986. VAR variable: SyntaxTree.Designator;
  6987. type,variableType: SyntaxTree.Type;
  6988. withEntry: WithEntry;
  6989. BEGIN
  6990. variable := ResolveDesignator(withPart.variable);
  6991. variableType := variable.type.resolved;
  6992. withPart.SetVariable(variable);
  6993. type := ResolveType(withPart.type);
  6994. withPart.SetType(type);
  6995. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6996. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6997. END;
  6998. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6999. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7000. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  7001. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7002. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7003. IF VerboseErrorMessage THEN
  7004. Printout.Info("variable",variable)
  7005. END;
  7006. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7007. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7008. IF VerboseErrorMessage THEN
  7009. Printout.Info("variable",variable);
  7010. Printout.Info("type",type);
  7011. END;
  7012. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7013. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7014. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7015. IF VerboseErrorMessage THEN
  7016. Printout.Info("variable",variable);
  7017. Printout.Info("type",type);
  7018. END;
  7019. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7020. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7021. ELSE
  7022. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7023. NEW(withEntry);
  7024. withEntry.previous := withEntries;
  7025. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7026. withEntry.type := type;
  7027. withEntries := withEntry;
  7028. StatementSequence(withPart.statements);
  7029. withEntries := withEntries.previous;
  7030. END;
  7031. END WithPart;
  7032. (** check and resolve with statement WITH variable: type DO ... END;
  7033. - check type and variable
  7034. - check that variable type is type extension of type
  7035. - check that variable is a variable
  7036. - enter new with scope and enter guardedVariable with same name and reference to variable
  7037. - create if statement:
  7038. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7039. **)
  7040. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7041. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7042. BEGIN
  7043. prevScope := currentScope; symbol := NIL;
  7044. FOR i := 0 TO withStatement.WithParts()-1 DO
  7045. WithPart(withStatement.GetWithPart(i),symbol);
  7046. END;
  7047. IF withStatement.elsePart # NIL THEN
  7048. StatementSequence(withStatement.elsePart)
  7049. END;
  7050. currentScope := prevScope;
  7051. END VisitWithStatement;
  7052. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7053. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7054. - check 'first' < 'last' and no overlaps between different case labels
  7055. - check statement sequence
  7056. **)
  7057. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7058. VAR
  7059. i: LONGINT;
  7060. position: LONGINT;
  7061. expression, left, right: SyntaxTree.Expression;
  7062. expressionType: SyntaxTree.Type;
  7063. l, r: LONGINT;
  7064. cl, cr: CHAR;
  7065. thiscases: SyntaxTree.CaseConstant;
  7066. BEGIN
  7067. thiscases := NIL;
  7068. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7069. expression := casePart.elements.GetExpression(i);
  7070. position := expression.position;
  7071. (* set context of range *)
  7072. IF expression IS SyntaxTree.RangeExpression THEN
  7073. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7074. END;
  7075. expression := ResolveExpression(expression);
  7076. IF expression = SyntaxTree.invalidExpression THEN
  7077. (* error already reported *)
  7078. expressionType := SyntaxTree.invalidType;
  7079. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7080. (* read out 'first' and 'last' *)
  7081. left := expression(SyntaxTree.RangeExpression).first;
  7082. right := expression(SyntaxTree.RangeExpression).last;
  7083. (* guaranteed by VisitRangeExpression: *)
  7084. ASSERT((left # NIL) & (right # NIL));
  7085. ASSERT(left.type.resolved = right.type.resolved);
  7086. left := CompatibleConversion(left.position, left, type);
  7087. right := CompatibleConversion(right.position, right, type);
  7088. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7089. expression(SyntaxTree.RangeExpression).SetLast(right);
  7090. expressionType := RegularType(position,left.type);
  7091. ELSE
  7092. expression := ConstantExpression(expression);
  7093. expression := CompatibleConversion(expression.position, expression, type);
  7094. (*
  7095. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7096. left := Global.NewCharacterValue(system,expression.position,cl);
  7097. expression := casePart.elements.GetExpression(i);
  7098. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7099. expression := left
  7100. END;
  7101. *)
  7102. casePart.elements.SetExpression(i,expression);
  7103. left := expression; right := expression;
  7104. expressionType := RegularType(position,expression.type)
  7105. END;
  7106. IF (expressionType = SyntaxTree.invalidType) THEN
  7107. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7108. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7109. expression := SyntaxTree.invalidExpression;
  7110. ELSE
  7111. l := 0; r := 0;
  7112. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7113. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7114. l := ORD(cl); r := ORD(cr);
  7115. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7116. ELSE
  7117. expression := SyntaxTree.invalidExpression
  7118. END;
  7119. IF expression # SyntaxTree.invalidExpression THEN
  7120. IF l>r THEN
  7121. Error(position,Diagnostics.Invalid,"empty case label")
  7122. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7123. Error(position,Diagnostics.Invalid,"duplicate case label");
  7124. ELSE
  7125. IF l < min THEN min := l END;
  7126. IF r > max THEN max := r END;
  7127. END;
  7128. END;
  7129. END;
  7130. casePart.elements.SetExpression(i,expression);
  7131. END;
  7132. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7133. casePart.SetConstants(thiscases);
  7134. StatementSequence(casePart.statements);
  7135. END CasePart;
  7136. (** check and resolve case statement CASE variable OF ... END;
  7137. - check variable
  7138. - check case parts
  7139. **)
  7140. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7141. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7142. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7143. BEGIN
  7144. expression := ResolveExpression(caseStatement.variable);
  7145. type := RegularType(expression.position,expression.type);
  7146. IF type = SyntaxTree.invalidType THEN
  7147. expression := SyntaxTree.invalidExpression;
  7148. ELSIF IsIntegerType(type) THEN
  7149. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7150. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7151. (*
  7152. expression := Global.NewCharacterValue(system,expression.position,ch);
  7153. *)
  7154. type := expression.type;
  7155. ELSIF IsCharacterType(type) THEN
  7156. ELSIF IsEnumerationType(type) THEN
  7157. ELSE
  7158. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7159. expression := SyntaxTree.invalidExpression;
  7160. END;
  7161. caseStatement.SetVariable(expression);
  7162. caseList := NIL;
  7163. min := MAX(LONGINT); max := MIN(LONGINT);
  7164. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7165. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7166. END;
  7167. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7168. msg := "huge sparse case table ";
  7169. Strings.AppendInt(msg, max-min);
  7170. Strings.Append(msg,"/");
  7171. Strings.AppendInt(msg, caseStatement.CaseParts());
  7172. Warning(caseStatement.position,msg);
  7173. END;
  7174. caseStatement.SetMinMax(min,max);
  7175. StatementSequence(caseStatement.elsePart);
  7176. IF expression.resolved # NIL THEN
  7177. IF IsCharacterValue(expression,ch) THEN
  7178. l := ORD(ch)
  7179. ELSIF IsIntegerValue(expression,l) THEN
  7180. END;
  7181. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7182. END;
  7183. END VisitCaseStatement;
  7184. (** check and resolve while statement
  7185. - check condition
  7186. - check statement sequence
  7187. **)
  7188. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7189. VAR prevIsUnreachable,b: BOOLEAN;
  7190. BEGIN
  7191. prevIsUnreachable := currentIsUnreachable;
  7192. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7193. IF IsBooleanValue(whileStatement.condition,b) THEN
  7194. IF b=FALSE THEN
  7195. currentIsUnreachable := TRUE
  7196. END;
  7197. END;
  7198. StatementSequence(whileStatement.statements);
  7199. currentIsUnreachable := prevIsUnreachable
  7200. END VisitWhileStatement;
  7201. (** check and resolve repeat statement
  7202. - check condition
  7203. - check statement sequence
  7204. **)
  7205. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7206. BEGIN
  7207. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7208. StatementSequence(repeatStatement.statements);
  7209. END VisitRepeatStatement;
  7210. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7211. VAR withEntry: WithEntry;
  7212. BEGIN
  7213. withEntry := withEntries;
  7214. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7215. withEntry := withEntry.previous
  7216. END;
  7217. IF withEntry = NIL THEN RETURN FALSE
  7218. ELSE
  7219. type := withEntry.type;
  7220. RETURN TRUE
  7221. END;
  7222. END GetGuard;
  7223. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7224. - check that variable is an integer variable
  7225. - check that from is integer typed with compatible type
  7226. - check that to has compatible type
  7227. - check that by is constant integer with compatible type
  7228. **)
  7229. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7230. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7231. BEGIN
  7232. designator := ResolveDesignator(forStatement.variable);
  7233. type := SyntaxTree.invalidType;
  7234. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7235. designator := SyntaxTree.invalidDesignator;
  7236. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7237. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7238. designator := SyntaxTree.invalidDesignator;
  7239. ELSIF CheckVariable(designator) THEN
  7240. type := designator.type;
  7241. END;
  7242. forStatement.SetVariable(designator);
  7243. expression := ResolveExpression(forStatement.from);
  7244. IF expression = SyntaxTree.invalidExpression THEN
  7245. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7246. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7247. expression := SyntaxTree.invalidExpression;
  7248. ELSIF type # SyntaxTree.invalidType THEN
  7249. expression := NewConversion(expression.position,expression,type,NIL)
  7250. END;
  7251. forStatement.SetFrom(expression);
  7252. expression := ResolveExpression(forStatement.to);
  7253. IF expression = SyntaxTree.invalidExpression THEN
  7254. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7255. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7256. expression := SyntaxTree.invalidExpression;
  7257. ELSIF type # SyntaxTree.invalidType THEN
  7258. expression := NewConversion(expression.position,expression,type,NIL)
  7259. END;
  7260. forStatement.SetTo(expression);
  7261. IF forStatement.by # NIL THEN
  7262. expression := ConstantInteger(forStatement.by);
  7263. ELSE
  7264. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7265. END;
  7266. IF expression = SyntaxTree.invalidExpression THEN
  7267. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7268. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7269. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7270. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7271. ELSIF type # SyntaxTree.invalidType THEN
  7272. expression := NewConversion(expression.position,expression,type,NIL)
  7273. END;
  7274. forStatement.SetBy(expression);
  7275. StatementSequence(forStatement.statements);
  7276. END VisitForStatement;
  7277. (** check and resolve loop statement LOOP StatementSequence END
  7278. - check statement sequence
  7279. **)
  7280. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7281. BEGIN
  7282. StatementSequence(loopStatement.statements)
  7283. END VisitLoopStatement;
  7284. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7285. BEGIN
  7286. StatementSequence(exitableBlock.statements);
  7287. END VisitExitableBlock;
  7288. (** check and resolve exit statement EXIT
  7289. - check that exit is within LOOP statement block
  7290. **)
  7291. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7292. VAR outer: SyntaxTree.Statement;
  7293. BEGIN
  7294. outer := exitStatement.outer;
  7295. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7296. outer := outer.outer;
  7297. END;
  7298. IF outer = NIL THEN
  7299. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7300. END;
  7301. END VisitExitStatement;
  7302. (** check and resolve return statement RETURN [expression]
  7303. - check expression (if any)
  7304. - check if in procedure scope
  7305. - if in procedure scope then check expression compatibility
  7306. - if not in procecdure scope then check on return without expression
  7307. **)
  7308. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7309. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7310. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7311. BEGIN
  7312. position := returnStatement.position;
  7313. expression := returnStatement.returnValue;
  7314. IF expression # NIL THEN
  7315. expression := ResolveExpression(expression);
  7316. returnStatement.SetReturnValue(expression);
  7317. END;
  7318. outer := returnStatement.outer;
  7319. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7320. outer := outer.outer
  7321. END;
  7322. IF (outer # NIL) THEN
  7323. scope := outer(SyntaxTree.Body).inScope;
  7324. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7325. IF (expression # NIL) THEN
  7326. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7327. END;
  7328. ELSE
  7329. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7330. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7331. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7332. END;
  7333. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7334. IF returnType # NIL THEN
  7335. returnType := returnType.resolved;
  7336. IF expression = NIL THEN
  7337. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7338. ELSIF expression.type = NIL THEN
  7339. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7340. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7341. Error(position,Diagnostics.Invalid,"return type not compatible");
  7342. IF VerboseErrorMessage THEN
  7343. Printout.Info("returnType",returnType);
  7344. Printout.Info("expression",expression);
  7345. END;
  7346. ELSE
  7347. expression := NewConversion(expression.position,expression,returnType,NIL);
  7348. returnStatement.SetReturnValue(expression);
  7349. END;
  7350. ELSIF expression # NIL THEN
  7351. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7352. END;
  7353. END;
  7354. END;
  7355. END VisitReturnStatement;
  7356. (** check and resolve await statement AWAIT(condition: Expression)
  7357. - check await condition
  7358. **)
  7359. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7360. VAR condition: SyntaxTree.Expression;
  7361. BEGIN
  7362. condition := ResolveCondition(awaitStatement.condition);
  7363. IF currentIsRealtime THEN
  7364. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7365. END;
  7366. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7367. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7368. END;
  7369. awaitStatement.SetCondition(condition);
  7370. END VisitAwaitStatement;
  7371. PROCEDURE CheckSystemImport(position: LONGINT);
  7372. VAR import: SyntaxTree.Import;
  7373. BEGIN
  7374. import := currentScope.ownerModule.moduleScope.firstImport;
  7375. WHILE(import # NIL) DO
  7376. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7377. RETURN;
  7378. END;
  7379. import := import.nextImport;
  7380. END;
  7381. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7382. END CheckSystemImport;
  7383. (** check and resolve code statement: do nothing, must be done by assembler
  7384. **)
  7385. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7386. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7387. BEGIN
  7388. CheckSystemImport(code.position);
  7389. FOR i := 0 TO code.inRules.Length()-1 DO
  7390. statement := code.inRules.GetStatement(i);
  7391. IF statement IS SyntaxTree.Assignment THEN
  7392. WITH statement: SyntaxTree.Assignment DO
  7393. statement.SetRight(ResolveExpression(statement.right));
  7394. END;
  7395. ELSE
  7396. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7397. END;
  7398. END;
  7399. FOR i := 0 TO code.outRules.Length()-1 DO
  7400. statement := code.outRules.GetStatement(i);
  7401. IF statement IS SyntaxTree.Assignment THEN
  7402. WITH statement: SyntaxTree.Assignment DO
  7403. statement.SetLeft(ResolveDesignator(statement.left));
  7404. END;
  7405. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7406. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7407. ELSE
  7408. Printout.Info("out statement ", statement);
  7409. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7410. END;
  7411. END;
  7412. END VisitCode;
  7413. (** check and set flags of a statement block
  7414. - check for multiply occurence of a flag
  7415. - check and set priority only in bodies
  7416. - check for valid names
  7417. **)
  7418. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7419. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7420. flag: LONGINT; recordBody: SyntaxTree.Body;
  7421. PROCEDURE SetProtectedRecord;
  7422. VAR scope: SyntaxTree.Scope;
  7423. BEGIN
  7424. scope := currentScope;
  7425. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7426. scope := scope.outerScope
  7427. END;
  7428. IF scope # NIL THEN
  7429. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7430. END;
  7431. END SetProtectedRecord;
  7432. BEGIN
  7433. flags := {};
  7434. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7435. recordBody := block(SyntaxTree.Body)
  7436. ELSE
  7437. recordBody := NIL
  7438. END;
  7439. blockModifier := block.blockModifiers;
  7440. WHILE(blockModifier # NIL) DO
  7441. name := blockModifier.identifier;
  7442. expression := blockModifier.expression;
  7443. position := blockModifier.position;
  7444. flag := -1;
  7445. IF name=Global.NamePriority THEN
  7446. IF expression = NIL THEN
  7447. Error(position,Diagnostics.Invalid,"missing priority expression");
  7448. ELSIF recordBody = NIL THEN
  7449. Error(position,Diagnostics.Invalid,"priority not on record body");
  7450. ELSIF recordBody.priority # NIL THEN
  7451. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7452. ELSE
  7453. recordBody.SetPriority(expression);
  7454. END;
  7455. ELSIF expression # NIL THEN
  7456. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7457. ELSIF name=Global.NameExclusive THEN
  7458. IF block.isExclusive THEN
  7459. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7460. END;
  7461. block.SetExclusive(TRUE); SetProtectedRecord;
  7462. ELSIF name=Global.NameActive THEN
  7463. IF recordBody = NIL THEN
  7464. Error(position,Diagnostics.Invalid,"active not in record body");
  7465. ELSIF recordBody.isActive THEN
  7466. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7467. ELSE
  7468. recordBody.SetActive(TRUE); SetProtectedRecord;
  7469. END;
  7470. ELSIF name=Global.NameSafe THEN
  7471. IF recordBody = NIL THEN
  7472. Error(position,Diagnostics.Invalid,"safe not in record body");
  7473. ELSIF recordBody.isSafe THEN
  7474. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7475. ELSE
  7476. recordBody.SetSafe(TRUE);
  7477. SetProtectedRecord;
  7478. END;
  7479. ELSIF name=Global.NameRealtime THEN
  7480. IF recordBody = NIL THEN
  7481. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7482. ELSIF recordBody.isRealtime THEN
  7483. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7484. ELSE
  7485. recordBody.SetRealtime(TRUE);
  7486. block.SetRealtime(TRUE);
  7487. END;
  7488. ELSIF name=Global.NameUnchecked THEN
  7489. IF block.isUnchecked THEN
  7490. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7491. ELSE
  7492. block.SetUnchecked(TRUE);
  7493. END;
  7494. ELSIF (name=Global.NameUncooperative) THEN
  7495. IF block.isUncooperative THEN
  7496. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7497. ELSE
  7498. block.SetUncooperative(TRUE);
  7499. END;
  7500. ELSE
  7501. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7502. END;
  7503. blockModifier := blockModifier.nextModifier;
  7504. END;
  7505. END BlockFlags;
  7506. (** check and resolve statement block
  7507. - check flags (exclusive)
  7508. - check statement sequence
  7509. **)
  7510. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7511. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7512. BEGIN
  7513. BlockFlags(statementBlock);
  7514. IF statementBlock.isExclusive THEN
  7515. (* check that not in exclusive block *)
  7516. IF currentIsExclusive THEN
  7517. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7518. ELSIF currentIsRealtime THEN
  7519. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7520. END;
  7521. END;
  7522. recentExclusive := currentIsExclusive;
  7523. recentUnreachable := currentIsUnreachable;
  7524. recentRealtime := currentIsRealtime;
  7525. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7526. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7527. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7528. StatementSequence(statementBlock.statements);
  7529. currentIsRealtime := recentRealtime;
  7530. currentIsExclusive := recentExclusive;
  7531. currentIsUnreachable := recentUnreachable;
  7532. END VisitStatementBlock;
  7533. (** check and resolve body
  7534. - check flags (active, priority, safe)
  7535. - check body and finally part
  7536. **)
  7537. PROCEDURE Body(body: SyntaxTree.Body);
  7538. BEGIN
  7539. VisitStatementBlock(body);
  7540. IF body.isActive THEN
  7541. IF ~currentIsBodyProcedure THEN
  7542. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7543. ELSIF body.priority # NIL THEN
  7544. body.SetPriority(ConstantInteger(body.priority));
  7545. END;
  7546. ELSIF body.isSafe THEN
  7547. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7548. ELSIF body.priority # NIL THEN
  7549. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7550. END;
  7551. IF body.code # NIL THEN
  7552. CheckSystemImport(body.position);
  7553. END;
  7554. StatementSequence(body.finally)
  7555. END Body;
  7556. (*** scopes ***)
  7557. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7558. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7559. VAR duplicateSymbol: BOOLEAN;
  7560. BEGIN
  7561. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7562. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7563. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7564. END;
  7565. scope.EnterSymbol(symbol,duplicateSymbol);
  7566. IF ~allowDuplicate & duplicateSymbol THEN
  7567. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7568. IF VerboseErrorMessage THEN
  7569. Printout.Info("multiply defined identifier",symbol);
  7570. Printout.Info("in scope",scope);
  7571. END;
  7572. END;
  7573. END Register;
  7574. (**
  7575. implementation: check and resolve an implementation part
  7576. **)
  7577. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7578. move implementation checker to a separate object ? *)
  7579. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7580. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7581. BEGIN
  7582. prevIsRealtime := currentIsRealtime;
  7583. prevIsBodyProcedure := currentIsBodyProcedure;
  7584. prevIsCellNet := currentIsCellNet;
  7585. prevScope := currentScope;
  7586. currentScope := scope;
  7587. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7588. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7589. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7590. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7591. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7592. (*
  7593. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7594. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7595. END;
  7596. *)
  7597. END;
  7598. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7599. (* module body, record bodies are wrapped into an artifical procedure *)
  7600. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7601. Body(scope(SyntaxTree.ProcedureScope).body)
  7602. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7603. Body(scope(SyntaxTree.ProcedureScope).body)
  7604. END;
  7605. END;
  7606. currentScope := prevScope;
  7607. currentIsRealtime := prevIsRealtime;
  7608. currentIsBodyProcedure := prevIsBodyProcedure;
  7609. currentIsCellNet := prevIsCellNet;
  7610. END Implementation;
  7611. (** implementation phase:
  7612. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7613. **)
  7614. PROCEDURE Implementations(x: SyntaxTree.Module);
  7615. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7616. BEGIN
  7617. prevPhase := phase;
  7618. phase := InlinePhase;
  7619. scope := x.firstScope;
  7620. WHILE(scope # NIL) DO
  7621. Implementation(scope);
  7622. scope := scope.nextScope;
  7623. END;
  7624. phase := ImplementationPhase;
  7625. scope := x.firstScope;
  7626. WHILE(scope # NIL) DO
  7627. Implementation(scope);
  7628. scope := scope.nextScope;
  7629. END;
  7630. phase := prevPhase;
  7631. END Implementations;
  7632. (** declaration phase:
  7633. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7634. - import lists (for module scopes)
  7635. - parameter list (for procedure scopes)
  7636. - constant declarations
  7637. - type declarations
  7638. - variable declarations
  7639. - procedure declarations
  7640. preformed in two stages:
  7641. - first all symbols are entered into the symbol table (with uniqueness check),
  7642. - then all symbols are resolved
  7643. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7644. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7645. **)
  7646. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7647. VAR
  7648. constant: SyntaxTree.Constant;
  7649. typeDeclaration: SyntaxTree.TypeDeclaration;
  7650. variable: SyntaxTree.Variable;
  7651. procedure: SyntaxTree.Procedure;
  7652. prevScope: SyntaxTree.Scope;
  7653. parameter: SyntaxTree.Parameter;
  7654. import: SyntaxTree.Import;
  7655. symbol: SyntaxTree.Symbol;
  7656. prevPhase: LONGINT;
  7657. prevError : BOOLEAN;
  7658. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7659. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7660. BEGIN
  7661. IF type.baseType # NIL THEN
  7662. baseType := type.baseType.resolved;
  7663. IF baseType IS SyntaxTree.PointerType THEN
  7664. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7665. END;
  7666. IF baseType IS SyntaxTree.CellType THEN
  7667. DeclareCell(baseType(SyntaxTree.CellType));
  7668. END;
  7669. END;
  7670. parameter := type.firstParameter;
  7671. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7672. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7673. variable.SetType(parameter.type);
  7674. variable.SetAccess(SyntaxTree.Hidden);
  7675. variable.SetModifiers(parameter.modifiers);
  7676. currentScope.PushVariable(variable);
  7677. (*
  7678. Register(parameter,scope, FALSE);
  7679. *)
  7680. parameter := parameter.nextParameter;
  7681. END;
  7682. property := type.firstProperty;
  7683. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7684. variable := currentScope.FindVariable(property.name);
  7685. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7686. prop := variable(SyntaxTree.Property);
  7687. ELSE (* add, duplicate symbols detection later *)
  7688. prop := SyntaxTree.NewProperty(property.position, property.name);
  7689. currentScope.PushVariable(prop);
  7690. END;
  7691. prop.SetType(property.type);
  7692. prop.SetValue(property.value);
  7693. prop.SetAccess(SyntaxTree.Hidden);
  7694. property := property.nextProperty;
  7695. END;
  7696. END DeclareCell;
  7697. BEGIN
  7698. prevError := error;
  7699. prevPhase := phase;
  7700. phase := DeclarationPhase;
  7701. prevScope := currentScope;
  7702. currentScope := scope;
  7703. error := FALSE;
  7704. (* first enter all symbols in scope *)
  7705. IF scope IS SyntaxTree.ModuleScope THEN
  7706. (* treat imports first for a module scope, , set default context if necessary *)
  7707. import := scope(SyntaxTree.ModuleScope).firstImport;
  7708. WHILE(import # NIL) DO
  7709. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7710. Register(import, currentScope, FALSE);
  7711. import := import.nextImport;
  7712. END;
  7713. import := scope(SyntaxTree.ModuleScope).firstImport;
  7714. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7715. ResolveSymbol(import);
  7716. import := import.nextImport;
  7717. END;
  7718. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7719. (* enter parameters for a procedure scope *)
  7720. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7721. WHILE(parameter # NIL) DO
  7722. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7723. END;
  7724. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7725. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7726. ELSIF scope IS SyntaxTree.CellScope THEN
  7727. IF~skipImplementation THEN
  7728. import := scope(SyntaxTree.CellScope).firstImport;
  7729. WHILE(import # NIL) DO
  7730. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7731. Register(import, currentScope, FALSE);
  7732. import := import.nextImport;
  7733. END;
  7734. import := scope(SyntaxTree.CellScope).firstImport;
  7735. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7736. ResolveSymbol(import);
  7737. import := import.nextImport;
  7738. END;
  7739. END;
  7740. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7741. END;
  7742. IF error THEN RETURN END;
  7743. (* constants *)
  7744. constant := scope.firstConstant;
  7745. WHILE (constant # NIL) DO
  7746. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7747. END;
  7748. (* type declarations *)
  7749. typeDeclaration := scope.firstTypeDeclaration;
  7750. WHILE (typeDeclaration # NIL) DO
  7751. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7752. END;
  7753. (* variables *)
  7754. variable := scope.firstVariable;
  7755. WHILE (variable # NIL) DO
  7756. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7757. END;
  7758. (* procedures *)
  7759. procedure := scope.firstProcedure;
  7760. WHILE (procedure # NIL) DO
  7761. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7762. END;
  7763. IF ~skipImplementation THEN
  7764. (* now process all symbols without any presumption on the order *)
  7765. symbol := scope.firstSymbol;
  7766. WHILE(symbol # NIL) DO
  7767. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7768. ResolveSymbol(symbol);
  7769. END;
  7770. symbol := symbol.nextSymbol;
  7771. END;
  7772. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7773. symbol := scope.firstSymbol;
  7774. WHILE symbol # NIL DO
  7775. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7776. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7777. pointerFixes.Add(symbol, currentScope);
  7778. END;
  7779. IF ~symbol.type.resolved.isRealtime THEN
  7780. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7781. END;
  7782. END;
  7783. symbol := symbol.nextSymbol
  7784. END;
  7785. END;
  7786. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7787. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7788. END;
  7789. IF (scope.ownerModule # NIL) THEN
  7790. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7791. scope.ownerModule.AddScope(scope);
  7792. END;
  7793. END;
  7794. phase := prevPhase;
  7795. currentScope := prevScope;
  7796. error := error OR prevError;
  7797. END Declarations;
  7798. (* nopov *)
  7799. (** check if all operators from one module are compatible to the ones in the other module
  7800. - check if there are not multiple operators with the same signature
  7801. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7802. - check for all operators whose signatures are compatible, whether the return types are compatible
  7803. note that:
  7804. - the return type is not considered to be part of the signature
  7805. - two signatures are considered compatible, if all of the operands are compatible
  7806. **)
  7807. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7808. VAR
  7809. thisOperator, thatOperator: SyntaxTree.Operator;
  7810. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7811. thisParameter, thatParameter: SyntaxTree.Parameter;
  7812. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7813. i: LONGINT;
  7814. BEGIN
  7815. currentScope := thisModuleScope;
  7816. hasError := FALSE;
  7817. (* go through all operators in the other module *)
  7818. thatOperator := thatModuleScope.firstOperator;
  7819. WHILE (thatOperator # NIL) & ~hasError DO
  7820. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7821. (* the other operator is accessible *)
  7822. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7823. (* the other operator is not the conversion operator *)
  7824. (* go through all operators in this module *)
  7825. thisOperator := thisModuleScope.firstOperator;
  7826. WHILE (thisOperator # NIL) & ~hasError DO
  7827. IF thisOperator # thatOperator THEN
  7828. (* the operators are not the same *)
  7829. IF thisOperator.name = thatOperator.name THEN
  7830. (* the operators share the same identifier *)
  7831. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7832. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7833. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7834. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7835. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7836. (* both operators have the same paramter count *)
  7837. thisParameter := thisProcedureType.firstParameter;
  7838. thatParameter := thatProcedureType.firstParameter;
  7839. operandsAreEqual := TRUE;
  7840. operandsAreCompatible := TRUE;
  7841. (* go through all parameters *)
  7842. FOR i := 1 TO thisProcedureType.numberParameters DO
  7843. ASSERT(thatParameter # NIL);
  7844. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7845. operandsAreEqual := FALSE;
  7846. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7847. operandsAreCompatible := FALSE
  7848. END
  7849. END;
  7850. thisParameter := thisParameter.nextParameter;
  7851. thatParameter := thatParameter.nextParameter
  7852. END;
  7853. IF operandsAreEqual THEN
  7854. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7855. hasError := TRUE
  7856. ELSIF operandsAreCompatible THEN
  7857. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7858. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7859. hasError := TRUE
  7860. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7861. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7862. hasError := TRUE
  7863. END
  7864. END
  7865. END
  7866. END
  7867. END;
  7868. thisOperator := thisOperator.nextOperator
  7869. END
  7870. END
  7871. END;
  7872. thatOperator := thatOperator.nextOperator
  7873. END
  7874. END CheckInterOperatorConformity;
  7875. (** check module:
  7876. - check module declaration
  7877. - add context, if necessary
  7878. - remove module from import cache, if necessary
  7879. - check declarations
  7880. - resolve all type fixes
  7881. - check implementation (bodies)
  7882. **)
  7883. PROCEDURE Module*(x: SyntaxTree.Module);
  7884. VAR (* nopov *)
  7885. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7886. BEGIN
  7887. prevScope := currentScope;
  7888. prevIsCellNet := currentIsCellNet;
  7889. module := x;
  7890. ASSERT(x # NIL);
  7891. global := system.globalScope[x.case];
  7892. x.moduleScope.SetGlobalScope(global);
  7893. currentScope := global;
  7894. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7895. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7896. RemoveModuleFromCache(importCache,x);
  7897. Declarations(x.moduleScope, FALSE);
  7898. FixTypes();
  7899. IF module.isCellNet THEN
  7900. currentIsCellNet := TRUE;
  7901. modifier := x.modifiers;
  7902. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7903. CheckModifiers(modifier, FALSE);
  7904. END;
  7905. (* nopov *)
  7906. IF ~error THEN
  7907. (* check if operators conform to each other within this module *)
  7908. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7909. (* go through all imports *)
  7910. import := x.moduleScope.firstImport;
  7911. WHILE import # NIL DO
  7912. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7913. (* check if all operators in this module conform to the ones of the imported module *)
  7914. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7915. END;
  7916. import := import.nextImport
  7917. END;
  7918. END;
  7919. Implementations(x);
  7920. module := NIL;
  7921. currentIsCellNet := prevIsCellNet;
  7922. currentScope := prevScope;
  7923. END Module;
  7924. END Checker;
  7925. Warnings*=OBJECT (SyntaxTree.Visitor)
  7926. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7927. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7928. BEGIN
  7929. SELF.diagnostics := diagnostics
  7930. END InitWarnings;
  7931. (** types *)
  7932. PROCEDURE Type(x: SyntaxTree.Type);
  7933. BEGIN x.Accept(SELF)
  7934. END Type;
  7935. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7936. BEGIN END VisitType;
  7937. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7938. BEGIN END VisitBasicType;
  7939. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7940. BEGIN END VisitCharacterType;
  7941. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7942. BEGIN END VisitIntegerType;
  7943. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7944. BEGIN END VisitFloatType;
  7945. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7946. BEGIN END VisitQualifiedType;
  7947. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7948. BEGIN END VisitStringType;
  7949. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7950. BEGIN END VisitEnumerationType;
  7951. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7952. BEGIN END VisitRangeType;
  7953. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7954. BEGIN
  7955. IF ~(SyntaxTree.Warned IN x.state) THEN
  7956. x.SetState(SyntaxTree.Warned);
  7957. Type(x.arrayBase);
  7958. END;
  7959. END VisitArrayType;
  7960. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7961. BEGIN
  7962. IF ~(SyntaxTree.Warned IN x.state) THEN
  7963. x.SetState(SyntaxTree.Warned);
  7964. Type(x.arrayBase);
  7965. END;
  7966. END VisitMathArrayType;
  7967. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7968. BEGIN
  7969. IF ~(SyntaxTree.Warned IN x.state) THEN
  7970. x.SetState(SyntaxTree.Warned);
  7971. Type(x.pointerBase);
  7972. END;
  7973. END VisitPointerType;
  7974. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7975. BEGIN Scope(x.recordScope) END VisitRecordType;
  7976. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7977. BEGIN Scope(x.cellScope) END VisitCellType;
  7978. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7979. BEGIN END VisitProcedureType;
  7980. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7981. VAR msg: ARRAY 256 OF CHAR;
  7982. BEGIN
  7983. Global.GetSymbolName(x,msg);
  7984. Strings.Append(msg," ");
  7985. Strings.Append(msg,text);
  7986. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7987. END Warning;
  7988. (** symbols *)
  7989. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7990. BEGIN
  7991. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7992. IF ~(x IS SyntaxTree.Parameter) THEN
  7993. Warning(x,"never used");
  7994. END;
  7995. END;
  7996. x.Accept(SELF);
  7997. END Symbol;
  7998. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7999. BEGIN END VisitSymbol;
  8000. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8001. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8002. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8003. BEGIN END VisitConstant;
  8004. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8005. BEGIN END VisitVariable;
  8006. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8007. BEGIN END VisitProperty;
  8008. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8009. BEGIN END VisitParameter;
  8010. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8011. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8012. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8013. BEGIN END VisitOperator;
  8014. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8015. BEGIN END VisitImport;
  8016. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8017. VAR
  8018. symbol: SyntaxTree.Symbol;
  8019. BEGIN
  8020. symbol := scope.firstSymbol;
  8021. WHILE(symbol # NIL) DO
  8022. Symbol(symbol);
  8023. symbol := symbol.nextSymbol;
  8024. END;
  8025. END Scope;
  8026. PROCEDURE Module*(x: SyntaxTree.Module);
  8027. BEGIN
  8028. SELF.module := x;
  8029. Scope(x.moduleScope);
  8030. END Module;
  8031. END Warnings;
  8032. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8033. BEGIN
  8034. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8035. END IsOberonInline;
  8036. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8037. BEGIN
  8038. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8039. END Resolved;
  8040. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8041. VAR i: LONGINT;
  8042. BEGIN
  8043. i := 1;
  8044. WHILE i < x DO
  8045. i := i *2
  8046. END;
  8047. RETURN i=x
  8048. END PowerOf2;
  8049. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8050. BEGIN
  8051. RETURN
  8052. (scope # NIL) &
  8053. (scope IS SyntaxTree.ModuleScope)
  8054. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8055. OR
  8056. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8057. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8058. END IsCellNetScope;
  8059. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8060. BEGIN
  8061. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8062. END IsCellScope;
  8063. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8064. BEGIN
  8065. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8066. RETURN (scope # NIL) & IsCellNetScope(scope)
  8067. END InCellNetScope;
  8068. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8069. BEGIN
  8070. ASSERT(size MOD system.dataUnit = 0);
  8071. RETURN size DIV system.dataUnit
  8072. END ToMemoryUnits;
  8073. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8074. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8075. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8076. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8077. BEGIN
  8078. IF t = NIL THEN
  8079. RETURN TRUE
  8080. ELSE
  8081. t := t.resolved;
  8082. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8083. END;
  8084. END TypeAllowed;
  8085. BEGIN
  8086. type := type.resolved;
  8087. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8088. RETURN FALSE
  8089. ELSE
  8090. procedureType := type(SyntaxTree.ProcedureType);
  8091. numberParameters := procedureType.numberParameters;
  8092. RETURN
  8093. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8094. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8095. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8096. END;
  8097. END GetProcedureAllowed;
  8098. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8099. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8100. VAR import: SyntaxTree.Import;
  8101. BEGIN
  8102. import := importCache.ImportByModuleName(x.name,x.context);
  8103. IF import # NIL THEN
  8104. importCache.RemoveImporters(x.name,x.context);
  8105. END;
  8106. END RemoveModuleFromCache;
  8107. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8108. (* to <- this assignment compatibility *)
  8109. VAR result: BOOLEAN;
  8110. BEGIN
  8111. IF this= NIL THEN result := (to=NIL)
  8112. ELSIF to=NIL THEN result := FALSE
  8113. ELSE
  8114. (*! will be replaced by this:
  8115. ELSE result := this.CompatibleTo(to.resolved);
  8116. *)
  8117. this := this.resolved; to := to.resolved;
  8118. IF to=SyntaxTree.invalidType THEN result := FALSE
  8119. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8120. ELSIF to = this THEN
  8121. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8122. ELSIF to IS SyntaxTree.BasicType THEN
  8123. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8124. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8125. result := this.CompatibleTo(to.resolved)
  8126. ELSE
  8127. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8128. END
  8129. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8130. result := to.sizeInBits = this.sizeInBits;
  8131. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8132. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8133. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8134. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8135. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8136. result := TRUE;
  8137. ELSIF to IS SyntaxTree.AnyType THEN
  8138. result := (this IS SyntaxTree.RecordType) & this(SyntaxTree.RecordType).isObject OR (this IS SyntaxTree.PointerType) OR (this IS SyntaxTree.ProcedureType) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.AnyType) OR (this IS SyntaxTree.ObjectType);
  8139. ELSIF to IS SyntaxTree.ObjectType THEN
  8140. result := IsPointerToRecord(this) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ObjectType) OR (this IS SyntaxTree.AnyType) (*! remove when symbol file can distinguish OBJECT from ANY *) ;
  8141. ELSIF to IS SyntaxTree.ByteType THEN
  8142. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8143. ELSIF to IS SyntaxTree.CharacterType THEN
  8144. result := IsCharacterType(this)
  8145. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8146. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8147. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8148. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8149. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8150. result := TRUE;
  8151. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8152. result := TRUE;
  8153. ELSE
  8154. result := FALSE
  8155. END;
  8156. ELSIF to IS SyntaxTree.PointerType THEN
  8157. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8158. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8159. & (~to.isRealtime OR this.isRealtime);
  8160. ELSIF to IS SyntaxTree.ProcedureType THEN
  8161. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8162. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8163. & (~to.isRealtime OR this.isRealtime)
  8164. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8165. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8166. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8167. ELSIF to IS SyntaxTree.RecordType THEN
  8168. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8169. ELSIF to IS SyntaxTree.ArrayType THEN
  8170. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8171. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8172. ELSIF StaticArrayCompatible(to, this) THEN
  8173. result := TRUE
  8174. ELSE
  8175. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8176. END;
  8177. ELSIF to IS SyntaxTree.MathArrayType THEN
  8178. IF this IS SyntaxTree.MathArrayType THEN
  8179. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8180. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8181. result := TRUE;
  8182. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8183. result := TRUE;
  8184. ELSE
  8185. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8186. END;
  8187. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8188. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8189. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8190. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8191. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8192. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8193. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8194. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8195. ELSE
  8196. result := FALSE
  8197. END;
  8198. (* an array-structured object type is compatible to the type of its array structure *)
  8199. ELSIF IsArrayStructuredObjectType(this) THEN
  8200. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8201. ELSE
  8202. result := FALSE;
  8203. END;
  8204. ELSIF to IS SyntaxTree.StringType THEN
  8205. result := FALSE;
  8206. ELSIF to IS SyntaxTree.EnumerationType THEN
  8207. result := IsEnumerationExtension(this,to);
  8208. ELSIF to IS SyntaxTree.PortType THEN
  8209. result := SameType(to, this)
  8210. ELSE
  8211. Printout.Info("CompatibleTo",to);
  8212. HALT(100); (* implement missing type check *)
  8213. END;
  8214. END;
  8215. RETURN result
  8216. END CompatibleTo;
  8217. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8218. VAR actualBase, formalBase: SyntaxTree.Type;
  8219. BEGIN
  8220. IF SameType(formal,actual) THEN
  8221. RETURN TRUE
  8222. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8223. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8224. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8225. RETURN
  8226. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8227. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8228. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8229. & StaticArrayCompatible(formalBase,actualBase)
  8230. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8231. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8232. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8233. RETURN
  8234. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8235. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8236. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8237. & StaticArrayCompatible(formalBase,actualBase)
  8238. ELSE RETURN FALSE
  8239. END;
  8240. END StaticArrayCompatible;
  8241. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8242. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8243. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8244. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8245. BEGIN
  8246. result := SameType(formal,actual);
  8247. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8248. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8249. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8250. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8251. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8252. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8253. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8254. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8255. & TC(formalBase, actualBase);
  8256. END;
  8257. RETURN result
  8258. END TC;
  8259. BEGIN
  8260. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8261. ELSE
  8262. arrayBase := formalType.arrayBase.resolved;
  8263. IF (actualType IS SyntaxTree.StringType) THEN
  8264. result := arrayBase IS SyntaxTree.CharacterType
  8265. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8266. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8267. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8268. result := TC(formalType, actualType);
  8269. ELSE
  8270. result := (arrayBase IS SyntaxTree.ByteType)
  8271. END;
  8272. END;
  8273. RETURN result
  8274. END OpenArrayCompatible;
  8275. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8276. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8277. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8278. BEGIN
  8279. IF actualType IS SyntaxTree.MathArrayType THEN
  8280. actualArray := actualType(SyntaxTree.MathArrayType);
  8281. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8282. (*
  8283. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8284. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8285. *)
  8286. actualBase := ArrayBase(actualType,Infinity);
  8287. formalBase := ArrayBase(formalType,Infinity);
  8288. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8289. ELSE
  8290. (*
  8291. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8292. *)
  8293. formalBase := Resolved(formalType.arrayBase);
  8294. actualBase := Resolved(actualArray.arrayBase);
  8295. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8296. (*
  8297. ARRAY [k] -> ARRAY [n]
  8298. *)
  8299. result := (formalType.staticLength = actualArray.staticLength)
  8300. ELSE
  8301. result := TRUE
  8302. END;
  8303. IF ~result THEN
  8304. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8305. ELSIF actualBase = NIL THEN result := FALSE
  8306. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8307. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8308. ELSE
  8309. result := SameType(formalBase,actualBase)
  8310. END;
  8311. END;
  8312. ELSE
  8313. result := FALSE
  8314. END;
  8315. RETURN result
  8316. END MathArrayCompatible;
  8317. (**
  8318. Math Array Type distance for assignments / parameter passings of the form
  8319. from -> to
  8320. variants:
  8321. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8322. allowed:
  8323. static -> static (& size match)
  8324. static -> open
  8325. static -> tensor
  8326. open -> open
  8327. open -> tensor
  8328. open -> static
  8329. tensor -> tensor
  8330. tensor -> open
  8331. tensor -> static
  8332. **)
  8333. (*! think about the metric here: is form matching more important than element type matching? *)
  8334. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8335. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8336. BEGIN
  8337. fromBase := Resolved(from.arrayBase);
  8338. toBase := Resolved(to.arrayBase);
  8339. i := Infinity;
  8340. IF from = to THEN
  8341. i := 0;
  8342. ELSIF (from.form = to.form) THEN
  8343. (* static -> static, open -> open, tensor -> tensor *)
  8344. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8345. IF fromBase = toBase THEN i := 0
  8346. ELSIF toBase = NIL THEN i := 1
  8347. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8348. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8349. ELSE
  8350. i := TypeDistance(system,fromBase, toBase, varpar);
  8351. END;
  8352. END;
  8353. ELSIF (to.form = SyntaxTree.Static) THEN
  8354. (* forbidden *)
  8355. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8356. (* static -> tensor, open -> tensor, tensor -> open *)
  8357. IF toBase=fromBase THEN i := 0;
  8358. ELSIF toBase = NIL THEN i := 1;
  8359. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8360. toBase := ArrayBase(toBase,Infinity);
  8361. IF (fromBase=toBase) THEN i := 0
  8362. ELSIF (toBase = NIL) THEN i:= 1
  8363. ELSIF (fromBase = NIL) THEN i := Infinity;
  8364. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8365. END;
  8366. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8367. fromBase := ArrayBase(fromBase,Infinity);
  8368. IF (fromBase=toBase) THEN i := 0
  8369. ELSIF (toBase = NIL) THEN i := 1
  8370. ELSIF (fromBase = NIL) THEN i := Infinity;
  8371. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8372. END;
  8373. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8374. END;
  8375. IF i # Infinity THEN INC(i,2) END;
  8376. ELSIF (from.form = SyntaxTree.Static) THEN
  8377. (* static -> open *)
  8378. IF toBase=fromBase THEN i := 0
  8379. ELSIF toBase = NIL THEN i := 1
  8380. ELSIF fromBase = NIL THEN i := Infinity
  8381. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8382. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8383. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8384. END;
  8385. IF i # Infinity THEN INC(i,1) END;
  8386. ELSE HALT(100); (* unknown case *)
  8387. END;
  8388. RETURN i;
  8389. END MathArrayTypeDistance;
  8390. (** compute and return the distance of two array types
  8391. - return the distance of the base types
  8392. **)
  8393. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8394. VAR i: LONGINT;
  8395. BEGIN
  8396. i := Infinity;
  8397. IF from = to THEN
  8398. i := 0
  8399. ELSE
  8400. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8401. (*
  8402. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8403. i := TypeDistance(from.base, to.base);
  8404. IF i >= 0 THEN INC(i) END
  8405. ELSIF (from.mode = open) & (to.mode = open) THEN
  8406. i := TypeDistance(from.base, to.base);
  8407. *)
  8408. END;
  8409. RETURN i
  8410. END ArrayTypeDistance;
  8411. (** compute the signature distance of a procedure and an actual parameter list
  8412. - if any of the parameters are not compatible, the result is infinite
  8413. - add up and return the distance over all parameters
  8414. **)
  8415. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8416. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8417. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8418. BEGIN
  8419. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8420. result := Infinity
  8421. ELSE
  8422. formalParameter := procedureType.firstParameter;
  8423. i := 0;
  8424. result := 0;
  8425. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8426. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8427. actualParameter := actualParameters.GetExpression(i);
  8428. ASSERT(formalParameter.type # NIL);
  8429. IF (actualParameter.type = NIL) THEN distance := Infinity
  8430. ELSE
  8431. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8432. END;
  8433. IF distance = Infinity THEN
  8434. result := Infinity;
  8435. ELSE
  8436. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8437. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8438. (* already handled varpar *)
  8439. (*
  8440. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8441. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8442. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8443. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8444. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8445. END;
  8446. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8447. result := Infinity
  8448. END;
  8449. *)
  8450. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8451. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8452. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8453. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8454. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8455. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8456. END;
  8457. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8458. result := Infinity
  8459. END;
  8460. ELSE
  8461. result := Infinity
  8462. END;
  8463. ELSE
  8464. INC(result,distance);
  8465. END;
  8466. END;
  8467. formalParameter := formalParameter.nextParameter; INC(i);
  8468. END;
  8469. END;
  8470. ASSERT(result >= 0);
  8471. RETURN result
  8472. END Distance;
  8473. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8474. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8475. BEGIN
  8476. IF right.numberParameters # (procedureType.numberParameters) THEN
  8477. result := Infinity
  8478. ELSE
  8479. formalParameter := procedureType.firstParameter;
  8480. rightParameter := right.firstParameter;
  8481. i := 0;
  8482. result := 0;
  8483. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8484. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8485. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8486. IF distance = Infinity THEN
  8487. result := Infinity;
  8488. ELSE
  8489. INC(result,distance);
  8490. END;
  8491. formalParameter := formalParameter.nextParameter;
  8492. rightParameter := rightParameter.nextParameter;
  8493. END;
  8494. END;
  8495. ASSERT(result >= 0);
  8496. RETURN result
  8497. END ProcedureTypeDistance;
  8498. (** compute and return the distance between two types, used for computation of signature distance
  8499. from -> to
  8500. **)
  8501. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8502. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8503. BEGIN
  8504. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8505. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8506. END;
  8507. i := Infinity;
  8508. IF from = to THEN
  8509. i := 0
  8510. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8511. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8512. i := Infinity;
  8513. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8514. i := 1
  8515. ELSIF (from IS SyntaxTree.StringType) THEN
  8516. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8517. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8518. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8519. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8520. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8521. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8522. i := 1
  8523. ELSIF (from IS SyntaxTree.NilType) THEN
  8524. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8525. (*
  8526. ELSIF (from = NoType) THEN
  8527. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8528. *)
  8529. ELSIF (from IS SyntaxTree.BasicType) THEN
  8530. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8531. IF varpar & (i # 0) THEN i := Infinity END;
  8532. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8533. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8534. ELSIF (from IS SyntaxTree.RecordType) THEN
  8535. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8536. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8537. IF to IS SyntaxTree.MathArrayType THEN
  8538. (*
  8539. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8540. i := Infinity;
  8541. ELSE
  8542. *)
  8543. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8544. (*
  8545. END;
  8546. *)
  8547. END
  8548. ELSIF (from IS SyntaxTree.PointerType) THEN
  8549. ptr := from(SyntaxTree.PointerType);
  8550. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8551. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8552. (* ELSE i := TypeDistance(ptr.base, to); *)
  8553. END
  8554. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8555. IF (to IS SyntaxTree.ProcedureType) THEN
  8556. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8557. END;
  8558. ELSIF (from IS SyntaxTree.PortType) THEN
  8559. IF (to IS SyntaxTree.PortType) THEN
  8560. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8561. i := 0;
  8562. END;
  8563. END;
  8564. (*no procedure test, procedure must be the same*)
  8565. END;
  8566. RETURN i
  8567. END TypeDistance;
  8568. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8569. BEGIN
  8570. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8571. END IsIntegerType;
  8572. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8573. BEGIN
  8574. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8575. END IsAddressType;
  8576. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8577. BEGIN
  8578. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8579. END IsSizeType;
  8580. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8581. BEGIN
  8582. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8583. END IsSignedIntegerType;
  8584. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8585. BEGIN
  8586. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8587. END IsUnsignedIntegerType;
  8588. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8589. VAR result: BOOLEAN;
  8590. BEGIN
  8591. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8592. value := x.resolved(SyntaxTree.IntegerValue).value;
  8593. result := TRUE
  8594. ELSE
  8595. result := FALSE
  8596. END;
  8597. RETURN result
  8598. END IsIntegerValue;
  8599. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8600. VAR result: BOOLEAN;
  8601. BEGIN
  8602. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8603. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8604. result := TRUE
  8605. ELSE
  8606. result := FALSE
  8607. END;
  8608. RETURN result
  8609. END IsEnumerationValue;
  8610. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8611. VAR result: BOOLEAN;
  8612. BEGIN
  8613. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8614. value := x.resolved(SyntaxTree.RealValue).value;
  8615. result := TRUE
  8616. ELSE
  8617. result := FALSE
  8618. END;
  8619. RETURN result
  8620. END IsRealValue;
  8621. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8622. VAR result: BOOLEAN;
  8623. BEGIN
  8624. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8625. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8626. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8627. result := TRUE
  8628. ELSE
  8629. result := FALSE
  8630. END;
  8631. RETURN result
  8632. END IsComplexValue;
  8633. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8634. VAR result: BOOLEAN;
  8635. BEGIN
  8636. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8637. value := x.resolved(SyntaxTree.CharacterValue).value;
  8638. result := TRUE
  8639. ELSE
  8640. result := FALSE
  8641. END;
  8642. RETURN result
  8643. END IsCharacterValue;
  8644. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8645. VAR result: BOOLEAN;
  8646. BEGIN
  8647. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8648. value := x.resolved(SyntaxTree.BooleanValue).value;
  8649. result := TRUE
  8650. ELSE
  8651. result := FALSE
  8652. END;
  8653. RETURN result
  8654. END IsBooleanValue;
  8655. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8656. VAR result: BOOLEAN;
  8657. BEGIN
  8658. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8659. value := x.resolved(SyntaxTree.SetValue).value;
  8660. result := TRUE
  8661. ELSE
  8662. result := FALSE
  8663. END;
  8664. RETURN result
  8665. END IsSetValue;
  8666. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8667. VAR result: BOOLEAN;
  8668. BEGIN
  8669. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8670. value := x.resolved(SyntaxTree.StringValue).value;
  8671. result := TRUE
  8672. ELSE
  8673. result := FALSE
  8674. END;
  8675. RETURN result
  8676. END IsStringValue;
  8677. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8678. BEGIN
  8679. x := x.resolved;
  8680. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8681. END Indexable;
  8682. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8683. BEGIN
  8684. RETURN t1.SameType(t2.resolved);
  8685. END SameType;
  8686. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8687. BEGIN
  8688. IF t IS SyntaxTree.MathArrayType THEN
  8689. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8690. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8691. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8692. DEC(max);
  8693. END;
  8694. ELSIF t IS SyntaxTree.ArrayType THEN
  8695. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8696. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8697. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8698. END;
  8699. END;
  8700. RETURN t;
  8701. END ArrayBase;
  8702. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8703. BEGIN
  8704. type := type.resolved;
  8705. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8706. base := type(SyntaxTree.ArrayType).arrayBase;
  8707. RETURN TRUE;
  8708. END;
  8709. RETURN FALSE;
  8710. END IsOpenArray;
  8711. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8712. BEGIN
  8713. type := type.resolved;
  8714. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8715. base := type(SyntaxTree.ArrayType).arrayBase;
  8716. dim := type(SyntaxTree.ArrayType).staticLength;
  8717. RETURN TRUE
  8718. ELSE
  8719. RETURN FALSE
  8720. END;
  8721. END IsStaticArray;
  8722. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8723. BEGIN
  8724. type := type.resolved;
  8725. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8726. base := type(SyntaxTree.ArrayType).arrayBase;
  8727. RETURN TRUE
  8728. ELSE
  8729. RETURN FALSE
  8730. END;
  8731. END IsDynamicArray;
  8732. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8733. VAR i: LONGINT;
  8734. BEGIN
  8735. i := 0;
  8736. t := t.resolved;
  8737. IF t IS SyntaxTree.MathArrayType THEN
  8738. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8739. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8740. END;
  8741. ELSIF t IS SyntaxTree.ArrayType THEN
  8742. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8743. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8744. END;
  8745. END;
  8746. RETURN i
  8747. END Dimension;
  8748. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8749. BEGIN
  8750. RETURN expression.assignable;
  8751. END IsVariable;
  8752. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8753. BEGIN
  8754. IF (symbol IS SyntaxTree.Parameter) THEN
  8755. WITH symbol: SyntaxTree.Parameter DO
  8756. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8757. END;
  8758. ELSE
  8759. RETURN FALSE
  8760. END;
  8761. END IsVariableParameter;
  8762. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8763. VAR result: BOOLEAN;
  8764. BEGIN
  8765. IF type = NIL THEN result := FALSE
  8766. ELSE
  8767. type := type.resolved;
  8768. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8769. END;
  8770. RETURN result
  8771. END IsPointerType;
  8772. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8773. VAR result: BOOLEAN;
  8774. BEGIN
  8775. IF type = NIL THEN result := FALSE
  8776. ELSE
  8777. type := type.resolved;
  8778. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8779. END;
  8780. RETURN result
  8781. END IsUnsafePointer;
  8782. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8783. BEGIN
  8784. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8785. END IsDisposable;
  8786. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8787. VAR result: BOOLEAN;
  8788. BEGIN
  8789. IF type = NIL THEN result := FALSE
  8790. ELSE
  8791. type := type.resolved;
  8792. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8793. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8794. result := result OR (type IS SyntaxTree.ObjectType);
  8795. END;
  8796. RETURN result
  8797. END IsPointerToRecord;
  8798. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8799. VAR result: BOOLEAN;
  8800. BEGIN
  8801. IF type = NIL THEN result := FALSE
  8802. ELSE
  8803. type := type.resolved;
  8804. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8805. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8806. ;
  8807. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8808. result := result OR (type IS SyntaxTree.ObjectType);
  8809. END;
  8810. RETURN result
  8811. END IsPointerToObject;
  8812. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8813. BEGIN
  8814. IF type # NIL THEN
  8815. RETURN type.resolved.hasPointers
  8816. ELSE
  8817. RETURN FALSE
  8818. END;
  8819. END ContainsPointer;
  8820. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8821. BEGIN
  8822. IF type = NIL THEN RETURN FALSE END;
  8823. type := type.resolved;
  8824. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8825. END IsStringType;
  8826. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8827. BEGIN
  8828. IF type = NIL THEN RETURN FALSE END;
  8829. type := type.resolved;
  8830. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8831. END IsCharacterType;
  8832. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8833. BEGIN
  8834. IF type = NIL THEN RETURN FALSE END;
  8835. type := type.resolved;
  8836. RETURN (type IS SyntaxTree.EnumerationType)
  8837. END IsEnumerationType;
  8838. (** cf. section "Type extension (base type)" in the language report **)
  8839. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8840. VAR result: BOOLEAN;
  8841. BEGIN
  8842. ASSERT(base # NIL); ASSERT(extension # NIL);
  8843. base := base.resolved; extension := extension.resolved;
  8844. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8845. result := TRUE;
  8846. ELSE
  8847. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8848. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8849. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8850. END;
  8851. WHILE (extension # NIL) & (extension # base) DO
  8852. IF extension IS SyntaxTree.RecordType THEN
  8853. extension := extension(SyntaxTree.RecordType).baseType;
  8854. IF (extension # NIL) THEN extension := extension.resolved END;
  8855. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8856. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8857. END;
  8858. ELSE extension := NIL;
  8859. END;
  8860. END;
  8861. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8862. END;
  8863. RETURN result
  8864. END IsTypeExtension;
  8865. (** check if base is the base enumeration type of extension **)
  8866. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8867. BEGIN
  8868. base := base.resolved; extension := extension.resolved;
  8869. WHILE (extension # NIL) & (extension # base) DO
  8870. IF extension IS SyntaxTree.EnumerationType THEN
  8871. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8872. IF extension # NIL THEN extension := extension.resolved END;
  8873. ELSE
  8874. extension := NIL
  8875. END;
  8876. END;
  8877. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8878. END IsEnumerationExtension;
  8879. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8880. BEGIN
  8881. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8882. RETURN TRUE
  8883. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8884. RETURN TRUE
  8885. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8886. RETURN TRUE
  8887. ELSE
  8888. RETURN FALSE
  8889. END
  8890. END IsCallable;
  8891. (** compute and return the distance of two record types
  8892. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8893. **)
  8894. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8895. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8896. BEGIN
  8897. i := 0;
  8898. WHILE (from # NIL) & (from # to) DO
  8899. baseType := from.baseType;
  8900. IF (baseType # NIL) THEN
  8901. baseType := baseType.resolved;
  8902. IF baseType IS SyntaxTree.PointerType THEN
  8903. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8904. END;
  8905. IF baseType IS SyntaxTree.RecordType THEN
  8906. from := baseType(SyntaxTree.RecordType);
  8907. ELSE
  8908. from := NIL;
  8909. END;
  8910. ELSE
  8911. from := NIL
  8912. END;
  8913. INC(i)
  8914. END;
  8915. IF from = NIL THEN i := Infinity END;
  8916. RETURN i
  8917. END RecordTypeDistance;
  8918. (** compute and return the distance of two pointer types **)
  8919. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8920. BEGIN
  8921. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8922. RETURN Infinity;
  8923. ELSE
  8924. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8925. END;
  8926. END PointerTypeDistance;
  8927. (** check if expression contains a symbol designator pointing to a type declaration.
  8928. - if so then enter type declaration into typeDeclaration and return true else return false
  8929. **)
  8930. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8931. VAR result: BOOLEAN;
  8932. BEGIN
  8933. result := FALSE;
  8934. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8935. result := TRUE;
  8936. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8937. END;
  8938. RETURN result
  8939. END IsTypeDesignator;
  8940. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8941. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8942. VAR result: BOOLEAN;
  8943. BEGIN
  8944. type := type.resolved;
  8945. IF type IS SyntaxTree.PointerType THEN
  8946. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8947. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8948. result := TRUE
  8949. ELSE
  8950. result := type IS SyntaxTree.RecordType
  8951. END;
  8952. RETURN result
  8953. END IsExtensibleType;
  8954. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8955. BEGIN
  8956. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8957. (d IS SyntaxTree.SymbolDesignator) &
  8958. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8959. OR
  8960. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8961. END IsUnextensibleRecord;
  8962. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8963. BEGIN
  8964. IF IsUnextensibleRecord(d) THEN
  8965. RETURN FALSE
  8966. ELSE RETURN IsExtensibleType(d.type.resolved)
  8967. END;
  8968. END IsExtensibleDesignator;
  8969. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8970. BEGIN
  8971. type := type.resolved;
  8972. IF (type IS SyntaxTree.PointerType) THEN
  8973. RETURN TRUE
  8974. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8975. RETURN TRUE
  8976. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8977. RETURN TRUE
  8978. ELSIF (type IS SyntaxTree.BasicType) THEN
  8979. RETURN TRUE
  8980. END;
  8981. RETURN FALSE
  8982. END IsBasicType;
  8983. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8984. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8985. BEGIN
  8986. baseType := record.baseType;
  8987. IF (baseType # NIL) THEN
  8988. baseType := baseType.resolved;
  8989. IF (baseType IS SyntaxTree.PointerType) THEN
  8990. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8991. END;
  8992. END;
  8993. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8994. recordType := baseType(SyntaxTree.RecordType);
  8995. ELSE
  8996. recordType := NIL;
  8997. END;
  8998. RETURN recordType
  8999. END RecordBase;
  9000. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9001. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9002. BEGIN
  9003. baseRecord := RecordBase(scope.ownerRecord);
  9004. IF baseRecord = NIL THEN RETURN NIL END;
  9005. scope := baseRecord.recordScope;
  9006. procedureType := procedure.type.resolved;
  9007. IF procedure IS SyntaxTree.Operator THEN
  9008. operator := scope.firstOperator;
  9009. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9010. (*
  9011. Printout.Info("not same ",procedureType);
  9012. Printout.Info("with ",operator.type);
  9013. *)
  9014. operator := operator.nextOperator;
  9015. END;
  9016. super := operator;
  9017. ELSE
  9018. super := scope.firstProcedure;
  9019. WHILE (super # NIL) & (super.name # procedure.name) DO
  9020. super := super.nextProcedure;
  9021. END;
  9022. END;
  9023. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9024. RETURN super
  9025. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9026. RETURN super
  9027. ELSE
  9028. RETURN FindSuperProcedure(scope,procedure);
  9029. END;
  9030. END FindSuperProcedure;
  9031. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9032. VAR procedure: SyntaxTree.Procedure;
  9033. BEGIN
  9034. procedure := record.recordScope.constructor;
  9035. IF procedure = NIL THEN
  9036. record := RecordBase(record);
  9037. IF record # NIL THEN
  9038. procedure := GetConstructor(record)
  9039. END;
  9040. END;
  9041. RETURN procedure;
  9042. END GetConstructor;
  9043. (* enter a case into a list of cases in a sorted way and check for collision *)
  9044. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9045. VAR prev,this,new: SyntaxTree.CaseConstant;
  9046. BEGIN
  9047. this := root;
  9048. prev := NIL;
  9049. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9050. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9051. RETURN FALSE
  9052. ELSE
  9053. IF (this # NIL) & (this.min = max+1) THEN
  9054. this.min := min
  9055. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9056. prev.max := min
  9057. ELSE
  9058. NEW(new); new.min := min; new.max := max;
  9059. new.next := this;
  9060. IF prev = NIL THEN
  9061. root := new;
  9062. ELSE
  9063. prev.next := new
  9064. END
  9065. END;
  9066. RETURN TRUE
  9067. END;
  9068. END EnterCase;
  9069. (** generate and return a new checker object, errors are entered into diagnostics **)
  9070. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9071. VAR checker: Checker;
  9072. BEGIN
  9073. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9074. RETURN checker
  9075. END NewChecker;
  9076. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9077. VAR warnings: Warnings;
  9078. BEGIN
  9079. NEW(warnings, diagnostics); RETURN warnings;
  9080. END NewWarnings;
  9081. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9082. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9083. END IsRangeType;
  9084. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9085. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9086. END IsMathArrayType;
  9087. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9088. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9089. END IsArrayType;
  9090. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9091. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9092. END IsComplexType;
  9093. (** if a type is an array-structured object type *)
  9094. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9095. VAR recordType: SyntaxTree.RecordType;
  9096. BEGIN
  9097. IF type = NIL THEN
  9098. RETURN FALSE
  9099. ELSE
  9100. type := type.resolved;
  9101. IF type IS SyntaxTree.PointerType THEN
  9102. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9103. IF type IS SyntaxTree.RecordType THEN
  9104. recordType := type(SyntaxTree.RecordType);
  9105. RETURN recordType.isObject & recordType.HasArrayStructure()
  9106. ELSE
  9107. RETURN FALSE
  9108. END
  9109. ELSE
  9110. RETURN FALSE
  9111. END
  9112. END
  9113. END IsArrayStructuredObjectType;
  9114. (** the math array structure of a type
  9115. - for math arrays: the array itself
  9116. - for pointers: the math array structure of the pointer base
  9117. - for array-structured object types: the underlying structure
  9118. - for non-math arrays and all other types: NIL
  9119. **)
  9120. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9121. VAR
  9122. result: SyntaxTree.MathArrayType;
  9123. BEGIN
  9124. IF type = NIL THEN
  9125. result := NIL
  9126. ELSE
  9127. type := type.resolved;
  9128. IF type IS SyntaxTree.PointerType THEN
  9129. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9130. END;
  9131. IF type IS SyntaxTree.MathArrayType THEN
  9132. result := type(SyntaxTree.MathArrayType)
  9133. ELSIF type IS SyntaxTree.RecordType THEN
  9134. result := type(SyntaxTree.RecordType).arrayStructure
  9135. ELSE
  9136. result := NIL
  9137. END
  9138. END;
  9139. RETURN result
  9140. END MathArrayStructureOfType;
  9141. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9142. VAR
  9143. result: BOOLEAN;
  9144. rangeExpression: SyntaxTree.RangeExpression;
  9145. BEGIN
  9146. IF x IS SyntaxTree.RangeExpression THEN
  9147. rangeExpression := x(SyntaxTree.RangeExpression);
  9148. result := TRUE;
  9149. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9150. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9151. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9152. ELSE
  9153. result := FALSE
  9154. END;
  9155. RETURN result
  9156. END IsStaticRange;
  9157. (** whether a type is a math array of tensor form **)
  9158. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9159. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9160. END IsTensor;
  9161. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9162. BEGIN
  9163. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9164. length := type(SyntaxTree.MathArrayType).staticLength;
  9165. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9166. RETURN TRUE
  9167. ELSE
  9168. RETURN FALSE
  9169. END;
  9170. END IsStaticMathArray;
  9171. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9172. BEGIN
  9173. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9174. END SymbolHasAddress;
  9175. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9176. BEGIN
  9177. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9178. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9179. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9180. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9181. END HasAddress;
  9182. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9183. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9184. BEGIN
  9185. IF (e IS SyntaxTree.Designator) THEN
  9186. d := e(SyntaxTree.Designator);
  9187. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9188. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9189. e := d.left;
  9190. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9191. END;
  9192. IF d # NIL THEN
  9193. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9194. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9195. END;
  9196. END;
  9197. RETURN FALSE;
  9198. END IsLocalVariable;
  9199. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9200. BEGIN
  9201. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9202. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9203. ELSE
  9204. RETURN TRUE
  9205. END;
  9206. END IsStaticProcedure;
  9207. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9208. CONST OptimizeMethodTable = FALSE;
  9209. BEGIN
  9210. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9211. END InMethodTable;
  9212. END FoxSemanticChecker.
  9213. SystemTools.FreeDownTo FoxSemanticChecker ~