FoxSemanticChecker.Mod 401 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995
  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. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  675. recordBaseType := ResolveType(recordType.baseType);
  676. recordType.SetBaseType(recordBaseType);
  677. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  678. END;
  679. CheckModifiers(modifiers, TRUE);
  680. typeFixes.Add(x,currentScope);
  681. x.SetState(SyntaxTree.Resolved);
  682. END;
  683. resolvedType := ResolvedType(x)
  684. END VisitPointerType;
  685. (**
  686. resolve port type
  687. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  688. **)
  689. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  690. VAR value: LONGINT;
  691. BEGIN
  692. IF TypeNeedsResolution(x) THEN
  693. x.SetCellsAreObjects(cellsAreObjects);
  694. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  695. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  696. x.SetSize(value)
  697. ELSE
  698. x.SetSize(system.SizeOf(system.longintType));
  699. END;
  700. x.SetState(SyntaxTree.Resolved);
  701. END;
  702. resolvedType := ResolvedType(x)
  703. END VisitPortType;
  704. (**
  705. deferred procedure type resolving
  706. - resolve return type
  707. - traverse and resolve parameters
  708. **)
  709. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  710. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  711. BEGIN
  712. resolved := ResolveType(procedureType.returnType);
  713. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  714. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  715. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  716. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  717. END;
  718. procedureType.SetReturnType(resolved);
  719. IF (resolved # NIL) THEN
  720. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  721. parameter.SetType(procedureType.returnType);
  722. parameter.SetAccess(SyntaxTree.Hidden);
  723. parameter.SetUntraced(procedureType.hasUntracedReturn);
  724. VisitParameter(parameter);
  725. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  726. END;
  727. (* process parameters *)
  728. parameter :=procedureType.firstParameter;
  729. WHILE (parameter # NIL) DO
  730. VisitParameter(parameter);
  731. parameter := parameter.nextParameter;
  732. END;
  733. END FixProcedureType;
  734. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  735. VAR prev,this: SyntaxTree.Modifier;
  736. BEGIN
  737. this := modifiers;prev := NIL;
  738. WHILE (this # NIL) & (this.identifier # name) DO
  739. prev := this; this := this.nextModifier;
  740. END;
  741. IF this # NIL THEN
  742. IF this.expression # NIL THEN
  743. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  744. END;
  745. this.Resolved;
  746. position := this.position;
  747. RETURN TRUE
  748. ELSE
  749. RETURN FALSE
  750. END;
  751. END HasFlag;
  752. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  753. VAR prev,this: SyntaxTree.Modifier;
  754. BEGIN
  755. this := modifiers;prev := NIL;
  756. WHILE (this # NIL) & (this.identifier # name) DO
  757. prev := this; this := this.nextModifier;
  758. END;
  759. IF this # NIL THEN
  760. IF this.expression = NIL THEN
  761. Error(this.position,Diagnostics.Invalid,"expected expression value");
  762. ELSE
  763. this.SetExpression(ConstantExpression(this.expression));
  764. IF CheckIntegerValue(this.expression,value) THEN END;
  765. END;
  766. this.Resolved;
  767. position := this.position;
  768. RETURN TRUE
  769. ELSE RETURN FALSE
  770. END;
  771. END HasValue;
  772. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: ARRAY OF CHAR): BOOLEAN;
  773. VAR prev,this: SyntaxTree.Modifier;
  774. BEGIN
  775. this := modifiers;prev := NIL;
  776. WHILE (this # NIL) & (this.identifier # name) DO
  777. prev := this; this := this.nextModifier;
  778. END;
  779. IF this # NIL THEN
  780. IF this.expression = NIL THEN
  781. Error(this.position,Diagnostics.Invalid,"expected expression value");
  782. ELSE
  783. this.SetExpression(ConstantExpression(this.expression));
  784. IF CheckStringValue(this.expression,value) THEN END;
  785. END;
  786. this.Resolved;
  787. position := this.position;
  788. RETURN TRUE
  789. ELSE RETURN FALSE
  790. END;
  791. END HasStringValue;
  792. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  793. VAR svalue: ARRAY 32 OF CHAR; position: LONGINT;
  794. BEGIN
  795. IF cellsAreObjects THEN RETURN FALSE END;
  796. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  797. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  798. IF svalue[0] = "~" THEN
  799. Strings.TrimLeft(svalue, "~");
  800. IF svalue = backendName THEN
  801. RETURN TRUE;
  802. END;
  803. ELSIF svalue # backendName THEN
  804. RETURN TRUE;
  805. END;
  806. END;
  807. IF x.baseType # NIL THEN
  808. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  809. END;
  810. RETURN FALSE;
  811. END SkipImplementation;
  812. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  813. VAR this: SyntaxTree.Modifier;
  814. BEGIN
  815. this := modifiers;
  816. WHILE this # NIL DO
  817. IF ~this.resolved THEN
  818. IF checkUse THEN
  819. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  820. ELSE
  821. this.SetExpression(ResolveExpression(this.expression));
  822. this.Resolved;
  823. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  824. END;
  825. END;
  826. this := this.nextModifier
  827. END;
  828. END CheckModifiers;
  829. (**
  830. resolve procedure type
  831. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  832. **)
  833. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  834. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  835. BEGIN
  836. IF TypeNeedsResolution(procedureType) THEN
  837. modifiers := procedureType.modifiers;
  838. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  839. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  840. procedureType.SetInterrupt(TRUE);
  841. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  842. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  843. IF useDarwinCCalls THEN (*fld*)
  844. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  845. ELSE
  846. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  847. END
  848. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  849. procedureType.SetNoReturn(TRUE);
  850. END;
  851. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  852. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  853. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  854. CheckModifiers(modifiers, TRUE);
  855. modifiers := procedureType.returnTypeModifiers;
  856. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  857. CheckModifiers(modifiers, TRUE);
  858. typeFixes.Add(procedureType,currentScope);
  859. procedureType.SetHasPointers(procedureType.isDelegate);
  860. procedureType.SetState(SyntaxTree.Resolved);
  861. END;
  862. resolvedType := ResolvedType(procedureType)
  863. END VisitProcedureType;
  864. (** check and resolve record type
  865. - check base type: must be record, math array or array-structured object type
  866. - check declarations
  867. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  868. **)
  869. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  870. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  871. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  872. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  873. hasPointers: BOOLEAN;
  874. modifiers: SyntaxTree.Modifier;
  875. value: LONGINT;
  876. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  877. BEGIN
  878. type := type.resolved;
  879. IF (type IS SyntaxTree.PointerType) &
  880. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  881. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  882. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  883. RETURN TRUE
  884. ELSE
  885. RETURN FALSE
  886. END;
  887. END IsPointerToRecord;
  888. BEGIN
  889. IF TypeNeedsResolution(x) THEN
  890. hasPointers := FALSE;
  891. modifiers := x.modifiers;
  892. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  893. CheckModifiers(modifiers, TRUE);
  894. IF x.baseType # NIL THEN
  895. position := x.baseType.position;
  896. baseType := ResolveType(x.baseType);
  897. resolved := baseType.resolved;
  898. hasPointers := hasPointers OR resolved.hasPointers;
  899. IF x.isObject THEN (* object *)
  900. ASSERT(x.pointerType # NIL);
  901. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  902. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  903. baseType := NIL
  904. ELSIF IsPointerToRecord(resolved,recordType) THEN
  905. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  906. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  907. ELSE
  908. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  909. END;
  910. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  911. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  912. ELSIF IsPointerToRecord(resolved,recordType) THEN
  913. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  914. ELSIF resolved IS SyntaxTree.RecordType THEN
  915. ELSE
  916. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  917. END;
  918. ELSE
  919. IF resolved IS SyntaxTree.RecordType THEN
  920. ELSE
  921. Error(position, Diagnostics.Invalid,"record does not extend record")
  922. END;
  923. END;
  924. x.SetBaseType(baseType);
  925. IF x.Level() > 15 THEN
  926. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  927. (* note:
  928. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  929. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  930. inheritance history of a type.
  931. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  932. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  933. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  934. *)
  935. END;
  936. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  937. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  938. END;
  939. END;
  940. Declarations(x.recordScope, FALSE);
  941. ResolveArrayStructure(x);
  942. (* computation of sizes and offsets skipped -> done in backend / system *)
  943. recordBase := x.GetBaseRecord();
  944. IF recordBase = NIL THEN numberMethods := 0
  945. ELSE numberMethods := recordBase.recordScope.numberMethods
  946. END;
  947. isRealtime := TRUE;
  948. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  949. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  950. END;
  951. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  952. WHILE symbol # NIL DO
  953. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  954. IF symbol IS SyntaxTree.Variable THEN
  955. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  956. END;
  957. IF symbol IS SyntaxTree.Procedure THEN
  958. procedure := symbol(SyntaxTree.Procedure);
  959. IF procedure.super # NIL THEN
  960. procedure.SetMethodNumber(procedure.super.methodNumber)
  961. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  962. procedure.SetMethodNumber(numberMethods);
  963. INC(numberMethods);
  964. END;
  965. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  966. Error(procedure.position, Diagnostics.Invalid,"realtime procedure in non-realtime object")
  967. END;
  968. END;
  969. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  970. Error(symbol.position, Diagnostics.Invalid,"non-realtime symbol in realtime object")
  971. END;
  972. symbol := symbol.nextSymbol;
  973. END;
  974. IF isRealtime THEN x.SetRealtime(TRUE) END;
  975. x.recordScope.SetNumberMethods(numberMethods);
  976. (* TODO: is this needed anymore? *)
  977. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  978. Error(x.position,Diagnostics.Invalid,"object extends a record")
  979. END;
  980. IF (x.typeDeclaration = NIL) THEN
  981. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  982. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  983. (*
  984. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  985. AnonymousTypeDeclaration(x,name);
  986. *)
  987. ELSE
  988. AnonymousTypeDeclaration(x,"Anonymous");
  989. END;
  990. END;
  991. x.SetHasPointers(hasPointers);
  992. x.SetState(SyntaxTree.Resolved);
  993. END;
  994. resolvedType := ResolvedType(x);
  995. END VisitRecordType;
  996. (** check and resolve cell type
  997. - check base type: must be cell
  998. - check declarations
  999. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1000. **)
  1001. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1002. VAR
  1003. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1004. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1005. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1006. recordBase: SyntaxTree.RecordType;
  1007. numberMethods, int: LONGINT;
  1008. real: LONGREAL;
  1009. bool: BOOLEAN;
  1010. set: SET;
  1011. v: SyntaxTree.Expression;
  1012. str: Scanner.StringType;
  1013. atype: SyntaxTree.ArrayType;
  1014. prev: SyntaxTree.Scope;
  1015. skip: BOOLEAN;
  1016. svalue: ARRAY 32 OF CHAR;
  1017. BEGIN
  1018. IF TypeNeedsResolution(x) THEN
  1019. recordBase := NIL;
  1020. IF cellsAreObjects THEN
  1021. IF x.baseType = NIL THEN
  1022. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1023. ImportModule(qualifiedIdentifier.prefix, x.position);
  1024. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1025. x.SetBaseType(ResolveType(x.baseType));
  1026. recordBase := x.GetBaseRecord();
  1027. IF recordBase = NIL THEN
  1028. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1029. END;
  1030. ELSE
  1031. x.SetBaseType(ResolveType(x.baseType));
  1032. END;
  1033. ELSE
  1034. x.SetBaseType(ResolveType(x.baseType));
  1035. END;
  1036. IF recordBase = NIL THEN numberMethods := 0
  1037. ELSE numberMethods := recordBase.recordScope.numberMethods
  1038. END;
  1039. modifier := x.modifiers;
  1040. (*IF ~x.isCellNet THEN*)
  1041. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1042. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1043. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1044. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1045. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1046. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1047. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1048. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1049. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1050. symbol := system.activeCellsCapabilities;
  1051. WHILE symbol # NIL DO
  1052. IF HasFlag(modifier, symbol.name, position) THEN END;
  1053. symbol := symbol.nextSymbol;
  1054. END;
  1055. modifier := x.modifiers;
  1056. WHILE (modifier # NIL) DO
  1057. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1058. IF modifier.expression # NIL THEN
  1059. v := ConstantExpression(modifier.expression);
  1060. property.SetValue(v);
  1061. IF IsIntegerValue(modifier.expression, int) THEN
  1062. (*property.SetValue(modifier.expression);*)
  1063. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1064. property.SetType(system.longintType);
  1065. ELSIF IsRealValue(modifier.expression, real) THEN
  1066. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1067. property.SetType(system.longrealType);
  1068. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1069. property.SetType(system.booleanType);
  1070. ELSIF IsSetValue(modifier.expression, set) THEN
  1071. property.SetType(system.setType);
  1072. ELSIF IsStringValue(modifier.expression, str) THEN
  1073. (*property.SetValue(modifier.expression);*)
  1074. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1075. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1076. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1077. property.SetType(atype);
  1078. ELSE
  1079. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1080. END;
  1081. ELSE (* flag property *)
  1082. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1083. property.SetType(system.booleanType);
  1084. END;
  1085. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1086. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1087. x.AddProperty(property);
  1088. modifier := modifier.nextModifier;
  1089. END;
  1090. CheckModifiers(modifier, FALSE);
  1091. Declarations(x.cellScope, SkipImplementation(x));
  1092. (* process parameters *)
  1093. prev := currentScope;
  1094. currentScope := x.cellScope;
  1095. parameter :=x.firstParameter;
  1096. WHILE (parameter # NIL) DO
  1097. VisitParameter(parameter);
  1098. type := parameter.type.resolved;
  1099. IF ~(type IS SyntaxTree.PortType) THEN
  1100. WHILE IsStaticArray(type, type, len) DO
  1101. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1102. END;
  1103. WHILE IsDynamicArray(type, type) DO
  1104. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1105. END;
  1106. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1107. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1108. END;
  1109. END;
  1110. parameter := parameter.nextParameter;
  1111. END;
  1112. currentScope := prev;
  1113. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1114. WHILE symbol # NIL DO
  1115. IF symbol IS SyntaxTree.Variable THEN
  1116. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1117. END;
  1118. symbol := symbol.nextSymbol;
  1119. END;
  1120. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1121. IF (x.typeDeclaration = NIL) THEN
  1122. AnonymousTypeDeclaration(x,"Anonymous");
  1123. END;
  1124. x.SetState(SyntaxTree.Resolved);
  1125. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1126. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1127. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1128. Warning(x.position, "Non-empty body for an engine?");
  1129. END;
  1130. END;
  1131. resolvedType := ResolvedType(x);
  1132. END VisitCellType;
  1133. (* check if an object is an array-structured object type
  1134. - determine the array structure
  1135. - collect operators from top to bottom in the inheritance hierarchy
  1136. - check if LEN operator is declared
  1137. - determine number of possible index operators
  1138. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1139. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1140. *)
  1141. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1142. VAR
  1143. indexOperatorCount, i: LONGINT;
  1144. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1145. isTensor: BOOLEAN;
  1146. BEGIN
  1147. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1148. (* determine array structure *)
  1149. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1150. END;
  1151. IF recordType.HasArrayStructure() THEN
  1152. (* the object is an ASOT *)
  1153. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1154. (* reset array access operators *)
  1155. arrayAccessOperators.len := NIL;
  1156. arrayAccessOperators.generalRead := NIL;
  1157. arrayAccessOperators.generalWrite := NIL;
  1158. IF isTensor THEN
  1159. (* all operators of dimensionalities 1 to max *)
  1160. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1161. ELSE
  1162. (* all operators of certain dimensionality *)
  1163. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1164. END;
  1165. NEW(arrayAccessOperators.read, indexOperatorCount);
  1166. NEW(arrayAccessOperators.write, indexOperatorCount);
  1167. FOR i := 0 TO indexOperatorCount - 1 DO
  1168. arrayAccessOperators.read[i] := NIL;
  1169. arrayAccessOperators.write[i] := NIL
  1170. END;
  1171. (* collect access operators in the record scope *)
  1172. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1173. IF arrayAccessOperators.len = NIL THEN
  1174. (* TODO: think about making this operator optional for static array structures *)
  1175. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1176. END;
  1177. (* show error messages *)
  1178. IF isTensor THEN
  1179. (* require ARRAY [*] OF RANGE *)
  1180. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1181. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1182. ELSE
  1183. (* forbid ARRAY [*] OF RANGE *)
  1184. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1185. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1186. (* require RANGE, RANGE, ... RANGE *)
  1187. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1188. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1189. END;
  1190. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1191. ELSE
  1192. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1193. IF recordType.recordScope.firstOperator # NIL THEN
  1194. RETURN;
  1195. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1196. END
  1197. END
  1198. END ResolveArrayStructure;
  1199. (** collect array access operators in a record scope **)
  1200. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1201. VAR
  1202. baseType: SyntaxTree.Type;
  1203. operator: SyntaxTree.Operator;
  1204. isReadOperator, isGeneralOperator: BOOLEAN;
  1205. indexListSize, indexListKind, hashValue: LONGINT;
  1206. BEGIN
  1207. (* if a parent record scope exists, collect the operators there first *)
  1208. baseType := recordScope.ownerRecord.baseType;
  1209. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1210. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1211. END;
  1212. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1213. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1214. END;
  1215. (* go through all operators in the current record scope *)
  1216. operator := recordScope.firstOperator;
  1217. WHILE operator # NIL DO
  1218. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1219. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1220. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1221. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1222. IF isGeneralOperator THEN
  1223. IF isReadOperator THEN
  1224. arrayAccessOperators.generalRead := operator
  1225. ELSE
  1226. arrayAccessOperators.generalWrite := operator
  1227. END
  1228. ELSE
  1229. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1230. IF isReadOperator THEN
  1231. arrayAccessOperators.read[hashValue] := operator
  1232. ELSE
  1233. arrayAccessOperators.write[hashValue] := operator
  1234. END
  1235. END
  1236. END
  1237. ELSE
  1238. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1239. END;
  1240. operator := operator.nextOperator
  1241. END
  1242. END CollectArrayAccessOperators;
  1243. (** the hash value of an index operator **)
  1244. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1245. VAR result: LONGINT;
  1246. BEGIN
  1247. IF isTensor THEN
  1248. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1249. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1250. ELSE
  1251. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1252. END
  1253. ELSE
  1254. result := indexListKind
  1255. END;
  1256. RETURN result
  1257. END IndexOperatorHash;
  1258. (** 2 to the power of exponent **)
  1259. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1260. VAR result, i: LONGINT;
  1261. BEGIN
  1262. result := 1;
  1263. FOR i := 1 TO exponent DO
  1264. result := result * 2;
  1265. END;
  1266. RETURN result
  1267. END TwoToThePowerOf;
  1268. (** check if a LEN operator has a correct signature. i.e.
  1269. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1270. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1271. **)
  1272. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1273. VAR
  1274. procedureType: SyntaxTree.ProcedureType;
  1275. returnedArrayType: SyntaxTree.MathArrayType;
  1276. result: BOOLEAN;
  1277. BEGIN
  1278. result := FALSE;
  1279. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1280. IF (procedureType.numberParameters = 0) THEN
  1281. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1282. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1283. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1284. IF returnedArrayType.form = SyntaxTree.Open THEN
  1285. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1286. result := TRUE
  1287. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1288. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1289. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1290. result := TRUE
  1291. END
  1292. END
  1293. END
  1294. END
  1295. END;
  1296. IF result THEN
  1297. (* export symbol automatically *)
  1298. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1299. ELSE
  1300. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1301. END;
  1302. RETURN result
  1303. END CheckLenOperator;
  1304. (** check if an index operator has a correct signature. i.e.
  1305. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1306. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1307. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1308. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1309. - determine if it is a read or write operator (existance of return type)
  1310. - check index parameters
  1311. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1312. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1313. [LONGINT] -> binary 0 -> 0
  1314. [RANGE] -> binary 1 -> 1
  1315. [LONGINT, LONGINT] -> binary 00 -> 0
  1316. [LONGINT, RANGE] -> binary 01 -> 1
  1317. [RANGE, LONGINT] -> binary 10 -> 2
  1318. [RANGE, RANGE] -> binary 11 -> 3
  1319. etc.
  1320. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1321. - for read operators, check if return type matches the type of data that is read
  1322. - for write operators, check if last parameter type matches the type of data that is written
  1323. **)
  1324. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1325. VAR
  1326. elementType, otherElementType, dataType: SyntaxTree.Type;
  1327. procedureType: SyntaxTree.ProcedureType;
  1328. mathArrayType: SyntaxTree.MathArrayType;
  1329. parameter: SyntaxTree.Parameter;
  1330. parameterCount, rangeCount, i: LONGINT;
  1331. hasTypeError: BOOLEAN;
  1332. BEGIN
  1333. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1334. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1335. (* determine if it is a read or write operator *)
  1336. isReadOperator := (procedureType.returnType # NIL);
  1337. IF isReadOperator THEN
  1338. indexListSize := parameterCount;
  1339. ELSE
  1340. indexListSize := parameterCount - 1;
  1341. END;
  1342. IF indexListSize < 1 THEN
  1343. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1344. RETURN FALSE
  1345. END;
  1346. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1347. (* general operator *)
  1348. isGeneralOperator := TRUE;
  1349. IF indexListSize > 1 THEN
  1350. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1351. RETURN FALSE
  1352. END;
  1353. (* ARRAY [*] OF RANGE*)
  1354. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1355. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1356. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1357. RETURN FALSE
  1358. END;
  1359. parameter := procedureType.firstParameter.nextParameter
  1360. ELSE
  1361. (* fixed-dim. operator *)
  1362. isGeneralOperator := FALSE;
  1363. (* check number of index parameters *)
  1364. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1365. (* for tensors, limited to a certain size *)
  1366. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1367. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1368. RETURN FALSE
  1369. END
  1370. ELSE
  1371. (* for non-tensors, depends on dimensionality *)
  1372. IF indexListSize # arrayStructure.Dimensionality() THEN
  1373. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1374. RETURN FALSE
  1375. END
  1376. END;
  1377. (* go through all index parameters
  1378. - count the number of ranges
  1379. - determine the index list kind number
  1380. *)
  1381. indexListKind := 0;
  1382. rangeCount := 0;
  1383. parameter := procedureType.firstParameter;
  1384. FOR i := 1 TO indexListSize DO
  1385. indexListKind := indexListKind * 2;
  1386. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1387. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1388. INC(indexListKind);
  1389. INC(rangeCount)
  1390. ELSE
  1391. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1392. RETURN FALSE
  1393. END;
  1394. parameter := parameter.nextParameter
  1395. END;
  1396. END;
  1397. (*
  1398. - for read operators: check type of last parameter
  1399. - for write operators: check return type
  1400. *)
  1401. IF isReadOperator THEN
  1402. dataType := procedureType.returnType (* the return type *)
  1403. ELSE
  1404. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1405. END;
  1406. elementType := arrayStructure.ElementType();
  1407. hasTypeError := FALSE;
  1408. IF isGeneralOperator THEN
  1409. (* ARRAY [?] OF <Element> *)
  1410. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1411. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1412. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1413. hasTypeError := TRUE
  1414. END
  1415. ELSE
  1416. hasTypeError := TRUE
  1417. END
  1418. ELSE
  1419. IF rangeCount = 0 THEN
  1420. (* <Element> *)
  1421. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1422. ELSE
  1423. (* ARRAY [*, *, ..., *] OF <Element> *)
  1424. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1425. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1426. IF mathArrayType.IsFullyDynamic() THEN
  1427. IF mathArrayType.Dimensionality() = rangeCount THEN
  1428. otherElementType := mathArrayType.ElementType();
  1429. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1430. ELSE
  1431. hasTypeError := TRUE
  1432. END
  1433. ELSE
  1434. hasTypeError := TRUE
  1435. END
  1436. ELSE
  1437. hasTypeError := TRUE
  1438. END
  1439. END
  1440. END;
  1441. IF hasTypeError THEN
  1442. IF isReadOperator THEN
  1443. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1444. ELSE
  1445. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1446. END;
  1447. RETURN FALSE
  1448. END;
  1449. (* export symbol automatically *)
  1450. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1451. RETURN TRUE
  1452. END CheckIndexOperator;
  1453. (** resolve all pending types (late resolving).
  1454. - type fixes are resolved at the end of the declaration phase
  1455. - type fixes may imply new type fixes that are also entered at the end of the list
  1456. **)
  1457. PROCEDURE FixTypes;
  1458. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1459. BEGIN
  1460. prevScope := currentScope;
  1461. p := typeFixes.Get(currentScope);
  1462. WHILE p # NIL DO
  1463. ASSERT(currentScope # NIL);
  1464. ASSERT(p IS SyntaxTree.Type);
  1465. IF p IS SyntaxTree.PointerType THEN
  1466. FixPointerType(p(SyntaxTree.PointerType))
  1467. ELSIF p IS SyntaxTree.ProcedureType THEN
  1468. FixProcedureType(p(SyntaxTree.ProcedureType))
  1469. ELSE
  1470. HALT(100);
  1471. END;
  1472. p := typeFixes.Get(currentScope);
  1473. END;
  1474. currentScope :=prevScope;
  1475. END FixTypes;
  1476. (**
  1477. resolve type x
  1478. - if x is nil then return nil
  1479. - if x cannot be resolved then the result is invalidType else the result is x
  1480. - the resolved type is entered into x.resolved
  1481. **)
  1482. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1483. VAR prev,resolved: SyntaxTree.Type;
  1484. BEGIN
  1485. prev := resolvedType;
  1486. resolvedType := SyntaxTree.invalidType;
  1487. IF x = NIL THEN resolvedType := NIL
  1488. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1489. END;
  1490. resolved := resolvedType;
  1491. resolvedType := prev;
  1492. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1493. RETURN resolved
  1494. END ResolveType;
  1495. (*** compatibility rules ***)
  1496. (**
  1497. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1498. **)
  1499. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1500. VAR result: SyntaxTree.Type;
  1501. BEGIN
  1502. result := SyntaxTree.invalidType;
  1503. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1504. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1505. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1506. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1507. ELSE result := type.resolved
  1508. END;
  1509. RETURN result
  1510. END RegularType;
  1511. (** returns signature compatibility of procedure types this and to
  1512. - if not compatible then error is reported
  1513. - compatibility means type equality
  1514. **)
  1515. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1516. VAR result: BOOLEAN;
  1517. BEGIN
  1518. result := SameType(to,this);
  1519. IF ~result THEN
  1520. Error(position,Diagnostics.Invalid,"signature incompatible");
  1521. IF VerboseErrorMessage THEN
  1522. Printout.Info("this",this);
  1523. Printout.Info("to",to);
  1524. END;
  1525. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1526. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1527. END;
  1528. RETURN result
  1529. END SignatureCompatible;
  1530. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1531. - for var parameters compatibility means same type except for
  1532. - formal is of open array of system byte
  1533. - formal is of record type
  1534. - formal is of open array type
  1535. - formal is of open math array type
  1536. - for value parameters compatibllity means assignment compatibility except for
  1537. - formal is of open array type
  1538. if compatible the return true else report error and return false
  1539. **)
  1540. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1541. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1542. BEGIN
  1543. formalType := RegularType(formal.position,formal.type);
  1544. actualType := RegularType(actual.position,actual.type);
  1545. error := FALSE;
  1546. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1547. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1548. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1549. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1550. END;
  1551. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1552. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1553. ELSIF ~IsVariable(actual) THEN
  1554. result := FALSE; error := TRUE;
  1555. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1556. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1557. ELSE
  1558. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1559. END;
  1560. IF VerboseErrorMessage THEN
  1561. Printout.Info("actual",actual);
  1562. Printout.Info("formal",formal);
  1563. END;
  1564. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1565. result := CompatibleTo(system,actualType,formalType);
  1566. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1567. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1568. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1569. IF IsArrayStructuredObjectType(actualType) THEN
  1570. actualType := MathArrayStructureOfType(actualType)
  1571. END;
  1572. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1573. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1574. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1575. END;
  1576. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1577. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1578. END;
  1579. ELSE
  1580. result := SameType(actualType,formalType)
  1581. END
  1582. ELSE
  1583. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1584. actualType := system.characterType;
  1585. END;
  1586. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1587. result := TRUE; (* special rule for WINAPI parameters *)
  1588. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1589. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1590. ELSE
  1591. result := CompatibleTo(system,actualType,formalType);
  1592. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1593. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1594. END;
  1595. END;
  1596. END;
  1597. IF ~result & ~error THEN
  1598. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1599. IF VerboseErrorMessage THEN
  1600. Printout.Info("actual",actual);
  1601. Printout.Info("formal",formal);
  1602. END;
  1603. END;
  1604. RETURN result
  1605. END ParameterCompatible;
  1606. (** check compatibility for expressions of the form left := right
  1607. - if compatible then return true else error report and return false
  1608. - check if left is variable
  1609. - check compatiblity
  1610. **)
  1611. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1612. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1613. BEGIN
  1614. result := FALSE;
  1615. leftType := RegularType(left.position,left.type);
  1616. rightType := RegularType(right.position,right.type);
  1617. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1618. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1619. END;
  1620. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1621. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1622. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1623. (* error already handled *)
  1624. result := TRUE;
  1625. ELSIF ~IsVariable(left) THEN
  1626. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1627. IF VerboseErrorMessage THEN
  1628. Printout.Info("left",left);
  1629. Printout.Info("right",right);
  1630. END;
  1631. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1632. result := TRUE;
  1633. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1634. result := TRUE
  1635. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1636. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1637. IF VerboseErrorMessage THEN
  1638. Printout.Info("left",left);
  1639. Printout.Info("right",right);
  1640. END;
  1641. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1642. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1643. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1644. ELSE
  1645. result := TRUE
  1646. END;
  1647. RETURN result
  1648. END AssignmentCompatible;
  1649. (*** values ***)
  1650. (** check and resolve integer value **)
  1651. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1652. VAR hugeint: HUGEINT;
  1653. BEGIN
  1654. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1655. value.SetType(Global.GetIntegerType(system,hugeint));
  1656. resolvedExpression := value
  1657. END VisitIntegerValue;
  1658. (** check and resolve real value **)
  1659. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1660. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1661. BEGIN
  1662. subtype := value(SyntaxTree.RealValue).subtype;
  1663. IF subtype = Scanner.Real THEN
  1664. type := system.realType
  1665. ELSIF subtype = Scanner.Longreal THEN
  1666. type := system.longrealType
  1667. ELSE
  1668. HALT(100)
  1669. END;
  1670. value.SetType(type);
  1671. resolvedExpression := value
  1672. END VisitRealValue;
  1673. (** check and resolve complex value **)
  1674. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1675. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1676. BEGIN
  1677. subtype := value(SyntaxTree.ComplexValue).subtype;
  1678. IF subtype = Scanner.Real THEN
  1679. type := system.complexType
  1680. ELSIF subtype = Scanner.Longreal THEN
  1681. type := system.longcomplexType
  1682. ELSE
  1683. HALT(100)
  1684. END;
  1685. value.SetType(type);
  1686. resolvedExpression := value
  1687. END VisitComplexValue;
  1688. (** check and resolve set value **)
  1689. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1690. BEGIN
  1691. value.SetType(system.setType);
  1692. resolvedExpression := value
  1693. END VisitSetValue;
  1694. (** check and resolve set value **)
  1695. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1696. BEGIN
  1697. value.SetType(SyntaxTree.invalidType);
  1698. resolvedExpression := value
  1699. END VisitMathArrayValue;
  1700. (** check and resolve boolean value **)
  1701. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1702. BEGIN
  1703. value.SetType(system.booleanType);
  1704. resolvedExpression := value
  1705. END VisitBooleanValue;
  1706. (** check and resolve string value **)
  1707. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1708. BEGIN
  1709. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1710. resolvedExpression := value
  1711. END VisitStringValue;
  1712. (** check and resolve character value **)
  1713. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1714. BEGIN
  1715. value.SetType(system.characterType);
  1716. resolvedExpression := value
  1717. END VisitCharacterValue;
  1718. (** check and resolve nil value **)
  1719. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1720. BEGIN
  1721. value.SetType(system.nilType);
  1722. resolvedExpression := value
  1723. END VisitNilValue;
  1724. (** check and resolve enumerator value **)
  1725. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1726. BEGIN
  1727. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1728. ASSERT(value.type # NIL);
  1729. resolvedExpression := value
  1730. END VisitEnumerationValue;
  1731. (*** expressions ***)
  1732. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1733. - check all elements on integer type
  1734. - if element range is constant, then check lower and upper bound
  1735. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1736. if an error occurs then report error and return invalidExpression
  1737. **)
  1738. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1739. VAR
  1740. i: LONGINT;
  1741. element: SyntaxTree.Expression;
  1742. constant: BOOLEAN;
  1743. elements: SyntaxTree.ExpressionList;
  1744. s: SET;
  1745. result: SyntaxTree.Expression;
  1746. value: SyntaxTree.Value;
  1747. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1748. VAR
  1749. left, right: SyntaxTree.Expression;
  1750. elementResult: SyntaxTree.Expression;
  1751. leftInteger, rightInteger, temp: LONGINT;
  1752. BEGIN
  1753. (* set context of range *)
  1754. IF element IS SyntaxTree.RangeExpression THEN
  1755. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1756. END;
  1757. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1758. IF elementResult = SyntaxTree.invalidExpression THEN
  1759. (* error already reported *)
  1760. constant := FALSE
  1761. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1762. (* the element is a range expression *)
  1763. (* extract left and right hand side of range *)
  1764. left := elementResult(SyntaxTree.RangeExpression).first;
  1765. right := elementResult(SyntaxTree.RangeExpression).last;
  1766. (* guaranteed by VisitRangeExpression: *)
  1767. ASSERT((left # NIL) & (right # NIL));
  1768. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1769. ELSE
  1770. (* the element is not a range expression *)
  1771. (* check type and add conversion if needed *)
  1772. IF IsIntegerType(elementResult.type.resolved) THEN
  1773. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1774. ELSE
  1775. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1776. elementResult := SyntaxTree.invalidExpression;
  1777. constant := FALSE
  1778. END;
  1779. left := elementResult;
  1780. right := elementResult
  1781. END;
  1782. IF elementResult # SyntaxTree.invalidExpression THEN
  1783. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1784. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1785. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1786. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1787. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1788. END
  1789. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1790. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1791. ELSE
  1792. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1793. s := {};
  1794. ELSE
  1795. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1796. IF leftInteger < 0 THEN leftInteger := 0 END;
  1797. (*!!!!!!!!! this is a hack !!!!!!! *)
  1798. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1799. extends the range x..31 to x..63 !!!!!! *)
  1800. s := s + {leftInteger..rightInteger};
  1801. END;
  1802. END;
  1803. ELSE
  1804. constant := FALSE;
  1805. END
  1806. END;
  1807. RETURN elementResult
  1808. END CheckElement;
  1809. BEGIN
  1810. result := set; constant := TRUE; s := {}; elements := set.elements;
  1811. IF elements # NIL THEN
  1812. FOR i := 0 TO elements.Length()-1 DO
  1813. element := elements.GetExpression(i);
  1814. element := CheckElement(element);
  1815. IF element = SyntaxTree.invalidExpression THEN
  1816. result := SyntaxTree.invalidExpression
  1817. END;
  1818. elements.SetExpression(i,element);
  1819. END;
  1820. END;
  1821. IF constant THEN
  1822. value := SyntaxTree.NewSetValue(set.position,s);
  1823. value.SetType(system.setType);
  1824. result.SetResolved(value);
  1825. END;
  1826. (* optimization possible
  1827. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1828. left this to the programmer...
  1829. *)
  1830. result.SetType(system.setType);
  1831. resolvedExpression := result;
  1832. END VisitSet;
  1833. (*
  1834. old variant: quite generic but needs better conversion handling, do this?
  1835. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1836. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1837. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1838. BEGIN
  1839. type := NIL;
  1840. numberElements := x.elements.Length();
  1841. FOR i := 0 TO numberElements-1 DO
  1842. expression := x.elements.GetExpression(i);
  1843. position := expression.position;
  1844. expression := ResolveExpression(x.elements.GetExpression(i));
  1845. x.elements.SetExpression(i,de);
  1846. IF type = NIL THEN
  1847. type := expression.type;
  1848. ELSIF CompatibleTo(system,expression.type,type) THEN
  1849. (* ok *)
  1850. ELSIF CompatibleTo(system,type,expression.type) THEN
  1851. type := expression.type
  1852. ELSE
  1853. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1854. type := SyntaxTree.invalidType;
  1855. END;
  1856. END;
  1857. isValue := TRUE;
  1858. FOR i := 0 TO numberElements-1 DO
  1859. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1860. x.elements.SetExpression(i,expression);
  1861. isValue := isValue & (expression.resolved # NIL);
  1862. END;
  1863. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1864. arrayType.SetArrayBase(type);
  1865. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1866. IF isValue THEN
  1867. value := SyntaxTree.NewMathArrayValue(position);
  1868. value.SetElements(x.elements);
  1869. x.SetResolved(value);
  1870. END;
  1871. x.SetType(arrayType);
  1872. resolvedExpression := x;
  1873. END VisitMathArrayExpression;
  1874. *)
  1875. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1876. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1877. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1878. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1879. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1880. BEGIN
  1881. numberElements := x.elements.Length();
  1882. FOR i := 0 TO numberElements-1 DO
  1883. expression := x.elements.GetExpression(i);
  1884. IF expression IS SyntaxTree.MathArrayExpression THEN
  1885. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1886. ELSE
  1887. position := expression.position;
  1888. expression := ResolveExpression(x.elements.GetExpression(i));
  1889. x.elements.SetExpression(i,expression);
  1890. IF type = NIL THEN
  1891. type := expression.type;
  1892. ELSIF CompatibleTo(system,expression.type,type) THEN
  1893. (* ok *)
  1894. ELSIF CompatibleTo(system,type,expression.type) THEN
  1895. type := expression.type
  1896. ELSE
  1897. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1898. type := SyntaxTree.invalidType;
  1899. END;
  1900. END;
  1901. END;
  1902. END RecursivelyFindType;
  1903. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1904. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1905. BEGIN
  1906. numberElements := x.elements.Length();
  1907. FOR i := 0 TO numberElements-1 DO
  1908. expression := x.elements.GetExpression(i);
  1909. IF expression IS SyntaxTree.MathArrayExpression THEN
  1910. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1911. ELSE
  1912. position := expression.position;
  1913. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1914. x.elements.SetExpression(i,expression);
  1915. isValue := isValue & (expression.resolved # NIL);
  1916. END;
  1917. END;
  1918. END RecursivelySetExpression;
  1919. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1920. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1921. arrayType: SyntaxTree.MathArrayType;
  1922. BEGIN
  1923. numberElements := x.elements.Length();
  1924. baseType := NIL;
  1925. gsize := 0;
  1926. FOR i := 0 TO numberElements-1 DO
  1927. expression := x.elements.GetExpression(i);
  1928. IF expression IS SyntaxTree.MathArrayExpression THEN
  1929. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1930. IF i=0 THEN
  1931. gsize := size;
  1932. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1933. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1934. ELSE expression.SetType(baseType)
  1935. END;
  1936. ELSIF baseType = NIL THEN baseType := type;
  1937. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1938. END;
  1939. END;
  1940. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1941. arrayType.SetArrayBase(baseType);
  1942. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1943. RETURN ResolveType(arrayType);
  1944. END RecursivelySetType;
  1945. BEGIN
  1946. type := NIL;
  1947. RecursivelyFindType(x);
  1948. isValue := TRUE;
  1949. RecursivelySetExpression(x);
  1950. arrayType := RecursivelySetType(x);
  1951. x.SetType(arrayType);
  1952. IF isValue THEN
  1953. value := SyntaxTree.NewMathArrayValue(x.position);
  1954. value.SetArray(x);
  1955. x.SetResolved(value);
  1956. value.SetType(arrayType);
  1957. END;
  1958. x.SetType(arrayType);
  1959. resolvedExpression := x;
  1960. END VisitMathArrayExpression;
  1961. (** check and resolve unary expression **)
  1962. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1963. VAR
  1964. left: SyntaxTree.Expression;
  1965. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1966. bool: BOOLEAN;
  1967. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1968. value: SyntaxTree.Value;
  1969. BEGIN
  1970. type := SyntaxTree.invalidType;
  1971. left := ResolveExpression(unaryExpression.left);
  1972. unaryExpression.SetLeft(left);
  1973. operator := unaryExpression.operator;
  1974. result := unaryExpression;
  1975. IF ~system.operatorDefined[operator] THEN
  1976. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1977. RETURN
  1978. ELSIF left.type = NIL THEN
  1979. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1980. resolvedExpression := SyntaxTree.invalidExpression;
  1981. RETURN
  1982. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1983. RETURN
  1984. END;
  1985. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1986. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1987. END;
  1988. IF operatorCall # NIL THEN
  1989. result := operatorCall;
  1990. type := operatorCall.type;
  1991. (* admissible operators
  1992. Minus number, set
  1993. Not boolean
  1994. *)
  1995. ELSE
  1996. CASE unaryExpression.operator OF
  1997. |Scanner.Minus:
  1998. IF IsIntegerType(left.type.resolved) THEN
  1999. IF left.resolved # NIL THEN
  2000. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2001. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2002. result.SetResolved(value);
  2003. type := Global.GetIntegerType(system,int);
  2004. value.SetType(type);
  2005. ELSE
  2006. type := left.type
  2007. END
  2008. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2009. IF IsRealValue(left,real) THEN
  2010. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2011. result.SetResolved(value);
  2012. type := left.type;
  2013. value.SetType(type);
  2014. ELSE
  2015. type := left.type;
  2016. END;
  2017. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2018. IF IsSetValue(left,set) THEN
  2019. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2020. result.SetResolved(value);
  2021. type := left.type;
  2022. value.SetType(type);
  2023. ELSE
  2024. type := left.type;
  2025. END;
  2026. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2027. IF IsComplexValue(left, real, imaginary) THEN
  2028. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2029. result.SetResolved(value);
  2030. type := left.type;
  2031. value.SetType(type);
  2032. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2033. ELSE
  2034. type := left.type;
  2035. END
  2036. ELSE
  2037. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2038. END;
  2039. |Scanner.Not:
  2040. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2041. IF IsBooleanValue(left,bool) THEN
  2042. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2043. result.SetResolved(value);
  2044. type := system.booleanType;
  2045. value.SetType(type);
  2046. ELSE
  2047. type := system.booleanType;
  2048. END;
  2049. ELSE
  2050. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2051. END;
  2052. |Scanner.Plus:
  2053. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2054. result := left; type := left.type;
  2055. ELSE
  2056. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2057. END;
  2058. (* ADDRESS OF *)
  2059. |Scanner.Address:
  2060. IF HasAddress(left) THEN
  2061. type := system.addressType;
  2062. ELSE
  2063. type := SyntaxTree.invalidType;
  2064. Error(left.position,Diagnostics.Invalid,"has no address");
  2065. Printout.Info("par", left);
  2066. END;
  2067. (* SIZE OF *)
  2068. |Scanner.Size:
  2069. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2070. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2071. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2072. value := SyntaxTree.NewIntegerValue(left.position, int);
  2073. result.SetResolved(value);
  2074. type := Global.GetIntegerType(system,int);
  2075. value.SetType(type)
  2076. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2077. ELSE
  2078. (* for variables, system sizeof could represent the physically occupied size
  2079. determined via the type descriptor, implement that ? *)
  2080. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2081. END
  2082. (* ALIAS OF *)
  2083. |Scanner.Alias:
  2084. type := left.type.resolved;
  2085. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2086. type := SyntaxTree.invalidType;
  2087. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2088. END;
  2089. ELSE
  2090. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2091. END;
  2092. END;
  2093. result.SetType(type);
  2094. resolvedExpression := result
  2095. END VisitUnaryExpression;
  2096. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2097. VAR
  2098. result: SyntaxTree.Expression;
  2099. array: SyntaxTree.MathArrayExpression;
  2100. value: SyntaxTree.MathArrayValue;
  2101. isValue: BOOLEAN;
  2102. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2103. BEGIN
  2104. type := type.resolved;
  2105. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2106. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2107. END;
  2108. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2109. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2110. END;
  2111. RETURN type
  2112. END BaseType;
  2113. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2114. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2115. BEGIN
  2116. numberElements := x.elements.Length();
  2117. FOR i := 0 TO numberElements-1 DO
  2118. expression := x.elements.GetExpression(i);
  2119. IF expression IS SyntaxTree.MathArrayExpression THEN
  2120. array := SyntaxTree.NewMathArrayExpression(position);
  2121. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2122. expression := array;
  2123. ELSE
  2124. position := expression.position;
  2125. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2126. isValue := isValue & (expression.resolved # NIL);
  2127. END;
  2128. to.elements.AddExpression(expression);
  2129. END;
  2130. END RecursivelyConvert;
  2131. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2132. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2133. arrayType: SyntaxTree.MathArrayType;
  2134. BEGIN
  2135. numberElements := x.elements.Length();
  2136. baseType := NIL;
  2137. gsize := 0;
  2138. FOR i := 0 TO numberElements-1 DO
  2139. expression := x.elements.GetExpression(i);
  2140. IF expression IS SyntaxTree.MathArrayExpression THEN
  2141. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2142. IF i=0 THEN
  2143. gsize := size;
  2144. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2145. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2146. ELSE expression.SetType(baseType)
  2147. END;
  2148. ELSIF baseType = NIL THEN baseType := type;
  2149. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2150. END;
  2151. END;
  2152. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2153. arrayType.SetArrayBase(baseType);
  2154. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2155. RETURN ResolveType(arrayType);
  2156. END RecursivelySetType;
  2157. BEGIN
  2158. result := SyntaxTree.invalidExpression;
  2159. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2160. result := expression (* do not convert *)
  2161. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2162. isValue := TRUE;
  2163. type := BaseType(type);
  2164. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2165. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2166. value := SyntaxTree.NewMathArrayValue(array.position);
  2167. value.SetArray(array);
  2168. value.SetType(RecursivelySetType(array));
  2169. result := value;
  2170. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2171. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2172. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2173. IF result = NIL THEN
  2174. result := SyntaxTree.invalidExpression;
  2175. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2176. IF VerboseErrorMessage THEN
  2177. Printout.Info("expression",expression);
  2178. Printout.Info("type",type);
  2179. END;
  2180. END;
  2181. END;
  2182. RETURN result
  2183. END MathArrayConversion;
  2184. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2185. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2186. BEGIN
  2187. result := expression; type := type.resolved;
  2188. IF (expression IS SyntaxTree.IntegerValue) THEN
  2189. int := expression(SyntaxTree.IntegerValue).hvalue;
  2190. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2191. int := Global.ConvertSigned(int,system.SizeOf(type));
  2192. result := SyntaxTree.NewIntegerValue(position,int);
  2193. result.SetType(type);
  2194. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2195. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2196. result := SyntaxTree.NewIntegerValue(position,int);
  2197. result.SetType(type);
  2198. ELSIF (type IS SyntaxTree.FloatType) THEN
  2199. result := SyntaxTree.NewRealValue(expression.position,int);
  2200. result.SetType(type);
  2201. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2202. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2203. result.SetType(type);
  2204. ELSIF (type IS SyntaxTree.SetType) THEN
  2205. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2206. result.SetType(type);
  2207. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2208. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2209. result.SetType(type);
  2210. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2211. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2212. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2213. END;
  2214. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2215. result.SetType(type);
  2216. ELSIF (type IS SyntaxTree.PortType) THEN
  2217. result := ConvertValue(position, expression, system.integerType);
  2218. ELSE
  2219. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2220. result := SyntaxTree.invalidExpression;
  2221. IF VerboseErrorMessage THEN
  2222. Printout.Info("expression",expression);
  2223. Printout.Info("type",type);
  2224. END;
  2225. END;
  2226. ELSIF IsRealValue(expression,real) THEN
  2227. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2228. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2229. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2230. result.SetType(type);
  2231. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2232. int := ENTIERH(real);
  2233. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2234. result.SetType(type);
  2235. ELSIF (type IS SyntaxTree.FloatType) THEN
  2236. result := SyntaxTree.NewRealValue(position,real);
  2237. result.SetType(type);
  2238. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2239. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2240. result.SetType(type);
  2241. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2242. ELSIF (type IS SyntaxTree.PortType) THEN
  2243. result := ConvertValue(position, expression, system.integerType);
  2244. ELSE
  2245. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2246. result := SyntaxTree.invalidExpression;
  2247. END
  2248. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2249. IF (type IS SyntaxTree.ComplexType) THEN
  2250. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2251. result.SetType(type);
  2252. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2253. ELSE
  2254. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2255. result := SyntaxTree.invalidExpression;
  2256. END
  2257. ELSIF IsSetValue(expression,set) THEN
  2258. IF (type IS SyntaxTree.IntegerType) THEN
  2259. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2260. result.SetType(type);
  2261. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2262. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2263. result.SetType(type);
  2264. ELSIF (type IS SyntaxTree.PortType) THEN
  2265. result := ConvertValue(position, expression, system.integerType);
  2266. ELSE
  2267. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2268. result := SyntaxTree.invalidExpression;
  2269. END;
  2270. ELSIF IsStringValue(expression,string) THEN
  2271. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2272. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2273. result.SetType(type);
  2274. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2275. ELSE
  2276. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2277. result := SyntaxTree.invalidExpression;
  2278. END;
  2279. ELSIF IsCharacterValue(expression,char) THEN
  2280. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2281. string[0] := char; string[1] := 0X;
  2282. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2283. result := SyntaxTree.NewStringValue(expression.position,string);
  2284. result.SetType(type);
  2285. ELSIF (type IS SyntaxTree.ByteType) THEN
  2286. (* do not simply set the new type as this could invalidate types of constants *)
  2287. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2288. result.SetType(type)
  2289. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2290. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2291. result.SetType(type);
  2292. ELSIF (type IS SyntaxTree.SetType) THEN
  2293. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2294. result.SetType(type);
  2295. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2296. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2297. result.SetType(type);
  2298. ELSIF (type IS SyntaxTree.PortType) THEN
  2299. result := ConvertValue(position, expression, system.integerType);
  2300. ELSE
  2301. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2302. result := SyntaxTree.invalidExpression;
  2303. END;
  2304. ELSIF expression IS SyntaxTree.NilValue THEN
  2305. IF type IS SyntaxTree.AddressType THEN
  2306. result := SyntaxTree.NewIntegerValue(position,0);
  2307. result.SetType(type);
  2308. ELSE
  2309. result := expression;
  2310. END;
  2311. (* nothing to be done *)
  2312. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2313. result := MathArrayConversion(position, expression,type);
  2314. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2315. int := expression(SyntaxTree.EnumerationValue).value;
  2316. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2317. int := Global.ConvertSigned(int,system.SizeOf(type));
  2318. result := SyntaxTree.NewIntegerValue(position,int);
  2319. result.SetType(type);
  2320. ELSE
  2321. result := expression;
  2322. END;
  2323. (* nothing to be done *)
  2324. ELSE
  2325. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2326. IF VerboseErrorMessage THEN
  2327. Printout.Info("expression",expression);
  2328. Printout.Info("type",type);
  2329. END;
  2330. result := SyntaxTree.invalidExpression;
  2331. END;
  2332. RETURN result
  2333. END ConvertValue;
  2334. (**
  2335. return a conversion of an expression to a given type
  2336. - if expression is already of same type then return expression
  2337. - if incompatible conversion then report error and return invalidExpression
  2338. **)
  2339. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2340. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2341. BEGIN
  2342. type := type.resolved;
  2343. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2344. result := expression;
  2345. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2346. ELSIF expression = NIL THEN (* NIL expression *)
  2347. ELSIF expression.type = NIL THEN
  2348. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2349. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2350. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2351. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2352. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2353. ELSIF expression.resolved # NIL THEN (* value *)
  2354. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2355. IF value IS SyntaxTree.Value THEN
  2356. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2357. result.SetResolved(value(SyntaxTree.Value));
  2358. result.SetType(value.type);
  2359. ELSE
  2360. result := value
  2361. END;
  2362. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2363. expressionList := SyntaxTree.NewExpressionList();
  2364. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2365. typeDeclaration.SetDeclaredType(type);
  2366. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2367. typeSymbol.SetType(typeDeclaration.type);
  2368. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2369. expressionList.AddExpression(expression);
  2370. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2371. result.SetType(type);
  2372. ELSIF IsArrayStructuredObjectType(type) THEN
  2373. (* no type can be converted to an array-structured object type *)
  2374. HALT(100)
  2375. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2376. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2377. result := MathArrayConversion(position, expression,type);
  2378. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2379. expression := ConvertToMathArray(expression);
  2380. type := MathArrayStructureOfType(type);
  2381. result := MathArrayConversion(position, expression, type)
  2382. ELSE
  2383. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2384. END;
  2385. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2386. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2387. OR ~(type IS SyntaxTree.ArrayType) THEN
  2388. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2389. END;
  2390. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2391. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2392. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2393. (*skip, no conversion*)
  2394. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2395. (* skip, no conversion *)
  2396. ELSE
  2397. ASSERT(~(type IS SyntaxTree.RangeType));
  2398. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2399. ASSERT(type # NIL);
  2400. END;
  2401. RETURN result
  2402. END NewConversion;
  2403. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2404. BEGIN
  2405. IF CompatibleTo(system,expression.type, type) THEN
  2406. RETURN NewConversion(position, expression, type, NIL);
  2407. ELSE
  2408. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2409. RETURN SyntaxTree.invalidExpression
  2410. END;
  2411. END CompatibleConversion;
  2412. (**
  2413. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2414. **)
  2415. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2416. VAR leftType,rightType: SyntaxTree.Type;
  2417. BEGIN
  2418. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2419. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2420. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2421. (* error already handled *)
  2422. ELSE
  2423. leftType := left.type.resolved; rightType := right.type.resolved;
  2424. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2425. right := NewConversion(right.position, right, leftType, NIL);
  2426. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2427. left := NewConversion(left.position,left,rightType,NIL);
  2428. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2429. right := NewConversion(right.position, right, leftType, NIL);
  2430. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2431. left := NewConversion(left.position,left,rightType,NIL);
  2432. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2433. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2434. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2435. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2436. ELSIF
  2437. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2438. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2439. (* must be the case LONGREAL / COMPLEX ) *)
  2440. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2441. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2442. ELSE
  2443. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2444. END;
  2445. END;
  2446. END ConvertOperands;
  2447. (** find and return best operator matching to parameter list (nil, if none)
  2448. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2449. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2450. **)
  2451. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2452. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2453. identifier: SyntaxTree.Identifier;
  2454. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2455. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2456. BEGIN
  2457. operator := scope.firstOperator;
  2458. WHILE(operator # NIL) DO
  2459. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2460. procedureType := operator.type(SyntaxTree.ProcedureType);
  2461. distance := Distance(system, procedureType,actualParameters);
  2462. IF (distance < Infinity) THEN
  2463. IF returnType # NIL THEN
  2464. IF procedureType.returnType = NIL THEN
  2465. distance := Infinity
  2466. ELSE
  2467. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2468. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2469. END;
  2470. END;
  2471. END;
  2472. (*
  2473. IF distance < Infinity THEN
  2474. TRACE(distance, operator);
  2475. Printout.Info("potential operator",operator);
  2476. ELSE
  2477. Printout.Info("operator not possible",operator);
  2478. END;
  2479. *)
  2480. IF distance < bestDistance THEN
  2481. bestDistance := distance;
  2482. bestOperator := operator;
  2483. END;
  2484. END;
  2485. operator := operator.nextOperator;
  2486. END;
  2487. (*
  2488. Printout.Info("taken operator",bestOperator);
  2489. *)
  2490. END FindInScope;
  2491. BEGIN
  2492. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2493. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2494. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2495. import := currentScope.ownerModule.moduleScope.firstImport;
  2496. WHILE (bestDistance > 0) & (import # NIL) DO
  2497. IF import.module # NIL THEN
  2498. identifier := Global.GetIdentifier(operator,import.module.case);
  2499. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2500. END;
  2501. import := import.nextImport;
  2502. END;
  2503. RETURN bestOperator
  2504. END FindOperator;
  2505. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2506. BEGIN
  2507. currentScope := scope;
  2508. END SetCurrentScope;
  2509. (**
  2510. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2511. - handle LEN and DIM operator for array-structured object types
  2512. - find operator, if found then
  2513. - if in other module then add import designator
  2514. - create symbol designator for operator
  2515. - if error then return invalidExpression, if no operator then return NIL
  2516. **)
  2517. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2518. VAR
  2519. operator: SyntaxTree.Operator;
  2520. import: SyntaxTree.Import;
  2521. expression, result: SyntaxTree.Expression;
  2522. designator: SyntaxTree.Designator;
  2523. actualParameters, tempList: SyntaxTree.ExpressionList;
  2524. recordType: SyntaxTree.RecordType;
  2525. castReturnType : SyntaxTree.MathArrayType;
  2526. BEGIN
  2527. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2528. result := SyntaxTree.invalidExpression
  2529. ELSIF leftExpression = NIL THEN
  2530. result := NIL
  2531. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2532. (* LEN or DIM operator on array-structured object type *)
  2533. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2534. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2535. IF recordType.arrayAccessOperators.len = NIL THEN
  2536. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2537. result := SyntaxTree.invalidExpression
  2538. ELSE
  2539. ASSERT(leftExpression IS SyntaxTree.Designator);
  2540. designator := leftExpression(SyntaxTree.Designator);
  2541. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2542. ASSERT(expression IS SyntaxTree.Designator);
  2543. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2544. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2545. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2546. result := designator
  2547. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2548. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2549. tempList := SyntaxTree.NewExpressionList();
  2550. tempList.AddExpression(rightExpression);
  2551. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2552. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2553. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2554. tempList := SyntaxTree.NewExpressionList();
  2555. tempList.AddExpression(designator);
  2556. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2557. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2558. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2559. END
  2560. END;
  2561. ELSE
  2562. (* import OCArrayBase if needed *)
  2563. IF ~arrayBaseImported THEN
  2564. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2565. (* operators on complex numbers *)
  2566. ImportModule(Global.ArrayBaseName,position);
  2567. arrayBaseImported := TRUE;
  2568. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2569. IF op = Global.Dim THEN
  2570. (* not existing in OCArrayBase *)
  2571. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2572. (* not existing in OCArrayBase *)
  2573. ELSE
  2574. ImportModule(Global.ArrayBaseName,position);
  2575. arrayBaseImported := TRUE;
  2576. END
  2577. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2578. ImportModule(Global.ArrayBaseName,position);
  2579. arrayBaseImported := TRUE
  2580. END;
  2581. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2582. (* LEN(RANGE) *)
  2583. ImportModule(Global.ArrayBaseName,position);
  2584. arrayBaseImported := TRUE;
  2585. END;
  2586. END;
  2587. actualParameters := SyntaxTree.NewExpressionList();
  2588. actualParameters.AddExpression(leftExpression);
  2589. IF rightExpression # NIL THEN
  2590. actualParameters.AddExpression(rightExpression)
  2591. END;
  2592. operator := FindOperator(system,op,actualParameters,resultType);
  2593. IF operator # NIL THEN
  2594. designator := NIL;
  2595. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2596. import := currentScope.ownerModule.moduleScope.firstImport;
  2597. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2598. import := import.nextImport;
  2599. END;
  2600. expression := NewSymbolDesignator(position,NIL,import);
  2601. designator := expression(SyntaxTree.Designator);
  2602. END;
  2603. expression := NewSymbolDesignator(position,designator,operator);
  2604. designator := expression(SyntaxTree.Designator);
  2605. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2606. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2607. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2608. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2609. result.SetType(castReturnType);
  2610. END;
  2611. ELSE
  2612. result := NIL;
  2613. END;
  2614. END;
  2615. RETURN result
  2616. END NewOperatorCall;
  2617. (** check and resolve binary expression **)
  2618. (*! clean up *)
  2619. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2620. VAR left,right,result: SyntaxTree.Expression;
  2621. leftType, rightType: SyntaxTree.Type;
  2622. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2623. cl,cr: CHAR;
  2624. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2625. type: SyntaxTree.Type;
  2626. value: SyntaxTree.Value;
  2627. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2628. integerConstantFolding: BOOLEAN;
  2629. list: SyntaxTree.ExpressionList;
  2630. PROCEDURE NewBool(v: BOOLEAN);
  2631. BEGIN
  2632. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2633. value.SetType(system.booleanType);
  2634. result.SetResolved(value);
  2635. type := system.booleanType
  2636. END NewBool;
  2637. PROCEDURE NewSet(v: SET);
  2638. BEGIN
  2639. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2640. value.SetType(system.setType);
  2641. result.SetResolved(value);
  2642. type := system.setType;
  2643. END NewSet;
  2644. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2645. BEGIN
  2646. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2647. (* type cast to "larger" type only if the value is still in the range *)
  2648. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2649. value.SetType(t);
  2650. END;
  2651. result.SetResolved(value);
  2652. type := value.type;
  2653. END NewInteger;
  2654. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2655. BEGIN
  2656. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2657. value.SetType(t);
  2658. result.SetResolved(value);
  2659. type := t;
  2660. END NewReal;
  2661. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2662. BEGIN
  2663. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2664. value.SetType(t);
  2665. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2666. result.SetResolved(value);
  2667. type := t;
  2668. END NewComplex;
  2669. BEGIN
  2670. type := SyntaxTree.invalidType;
  2671. left := ResolveExpression(binaryExpression.left);
  2672. right := ResolveExpression(binaryExpression.right);
  2673. binaryExpression.SetLeft(left);
  2674. binaryExpression.SetRight(right);
  2675. result := binaryExpression;
  2676. operator := binaryExpression.operator;
  2677. IF ~system.operatorDefined[operator] THEN
  2678. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2679. result := SyntaxTree.invalidExpression;
  2680. RETURN
  2681. END;
  2682. IF left.type = NIL THEN
  2683. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2684. result := SyntaxTree.invalidExpression;
  2685. RETURN;
  2686. END;
  2687. IF right.type = NIL THEN
  2688. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2689. result := SyntaxTree.invalidExpression;
  2690. RETURN;
  2691. END;
  2692. leftType := left.type.resolved; rightType := right.type.resolved;
  2693. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2694. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2695. END;
  2696. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2697. list := SyntaxTree.NewExpressionList();
  2698. list.AddExpression(right);
  2699. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2700. END;
  2701. IF operatorCall # NIL THEN
  2702. result := operatorCall;
  2703. type := operatorCall.type;
  2704. (* admissible operators:
  2705. Times, Plus, Minus numeric numeric numeric
  2706. set set set
  2707. Slash numeric numeric real /complex
  2708. set set set
  2709. Div , Mod integer integer integer
  2710. And, Or bool bool bool
  2711. Equal, Unequal basic basic bool
  2712. pointer pointer bool
  2713. object object bool
  2714. record record bool
  2715. string string bool
  2716. enumerator enumerator bool
  2717. Less, LessEqual,
  2718. Greater, GreaterEqual integer/real integer/real bool
  2719. enumerator enumerator bool
  2720. In integer set bool
  2721. Is pointer type bool
  2722. object type bool
  2723. record type bool
  2724. Upto: special abbreviation for a..b
  2725. *)
  2726. ELSIF (left.type = NIL) THEN
  2727. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2728. D.Str("nil type in "); D.Type(left); D.Ln;
  2729. result := SyntaxTree.invalidExpression;
  2730. ELSIF (right.type = NIL) THEN
  2731. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2732. result := SyntaxTree.invalidExpression;
  2733. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2734. result := SyntaxTree.invalidExpression;
  2735. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2736. HALT(100);
  2737. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2738. type := system.booleanType;
  2739. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2740. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2741. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2742. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2743. IF VerboseErrorMessage THEN
  2744. Printout.Info("left",left);
  2745. Printout.Info("right",right);
  2746. END;
  2747. ELSIF IsUnsafePointer(left.type) THEN
  2748. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2749. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2750. NewBool(TRUE)
  2751. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2752. NewBool(TRUE);
  2753. ELSIF IsUnextensibleRecord(left) THEN
  2754. NewBool(FALSE)
  2755. END
  2756. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2757. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2758. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2759. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2760. ELSIF operator = Scanner.In THEN (* left IN right *)
  2761. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2762. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2763. NewBool(il IN sr);
  2764. ELSE
  2765. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2766. left := NewConversion(left.position, left, system.longintType,NIL);
  2767. binaryExpression.SetLeft(left)
  2768. END;
  2769. type := system.booleanType;
  2770. END
  2771. ELSE
  2772. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2773. END
  2774. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2775. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2776. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2777. END;
  2778. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2779. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2780. END
  2781. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2782. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2783. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2784. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2785. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2786. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2787. ConvertOperands(left, right);
  2788. binaryExpression.SetLeft(left);
  2789. binaryExpression.SetRight(right);
  2790. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2791. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2792. END;
  2793. type := system.booleanType;
  2794. ELSE
  2795. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2796. END
  2797. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2798. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2799. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2800. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2801. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2802. CASE operator OF
  2803. |Scanner.Equal: NewBool(strl^=strr^);
  2804. |Scanner.Unequal:NewBool(strl^#strr^);
  2805. |Scanner.Less: NewBool(strl^<strr^);
  2806. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2807. |Scanner.Greater: NewBool(strl^>strr^);
  2808. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2809. ELSE
  2810. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2811. END;
  2812. END;
  2813. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2814. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2815. type := system.booleanType
  2816. ELSE
  2817. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2818. END;
  2819. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2820. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2821. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2822. type := system.booleanType;
  2823. ELSE
  2824. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2825. END
  2826. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2827. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2828. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2829. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2830. type := system.booleanType
  2831. ELSE
  2832. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2833. END;
  2834. ELSE
  2835. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2836. END;
  2837. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2838. type := system.booleanType;
  2839. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2840. type := system.booleanType;
  2841. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2842. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2843. THEN
  2844. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2845. IF (leftType # rightType) THEN
  2846. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2847. ConvertOperands(left,right); (* operands must be of the same type here *)
  2848. END;
  2849. binaryExpression.SetLeft(left);
  2850. binaryExpression.SetRight(right);
  2851. leftType := left.type.resolved;
  2852. rightType := right.type.resolved;
  2853. END;
  2854. type := leftType;
  2855. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2856. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2857. IF VerboseErrorMessage THEN
  2858. Printout.Info("left",left);
  2859. Printout.Info("right",right);
  2860. END;
  2861. ELSIF IsIntegerType(leftType) THEN
  2862. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2863. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2864. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2865. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2866. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2867. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2868. END;
  2869. END;
  2870. (* constant folding *)
  2871. (* bootstrap64
  2872. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2873. CASE operator OF
  2874. |Scanner.Plus: NewInteger(il+ir,left.type);
  2875. |Scanner.Minus: NewInteger(il-ir,left.type);
  2876. |Scanner.Times: NewInteger(il*ir,left.type);
  2877. |Scanner.Slash:
  2878. IF ir # 0 THEN
  2879. NewReal(il/ir, system.realType);
  2880. END;
  2881. |Scanner.Mod:
  2882. IF ir > 0 THEN
  2883. NewInteger(il MOD ir,left.type);
  2884. END;
  2885. |Scanner.Div:
  2886. IF ir > 0 THEN
  2887. NewInteger(il DIV ir,left.type);
  2888. END;
  2889. |Scanner.Equal: NewBool(il=ir);
  2890. |Scanner.Unequal:NewBool(il#ir);
  2891. |Scanner.Less: NewBool(il<ir);
  2892. |Scanner.LessEqual: NewBool(il<=ir);
  2893. |Scanner.Greater: NewBool(il>ir);
  2894. |Scanner.GreaterEqual: NewBool(il>=ir);
  2895. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2896. END;
  2897. ELS*)
  2898. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2899. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2900. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2901. CASE operator OF
  2902. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2903. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2904. |Scanner.Times: NewInteger(hl*hr,left.type);
  2905. |Scanner.Slash:
  2906. IF hr = 0 THEN
  2907. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2908. ELSE
  2909. IF type.sizeInBits = 64 THEN
  2910. NewReal(hl/hr,system.longrealType);
  2911. ELSE
  2912. NewReal(hl/hr,system.realType)
  2913. END
  2914. END;
  2915. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2916. |Scanner.Mod:
  2917. IF hr = 0 THEN
  2918. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2919. ELSE
  2920. NewInteger(hl MOD hr, left.type);
  2921. (* bootstrap64
  2922. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2923. *)
  2924. END;
  2925. |Scanner.Div:
  2926. IF hr = 0 THEN
  2927. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2928. ELSE
  2929. NewInteger(hl DIV hr, left.type);
  2930. (* bootstrap64
  2931. NewInteger(Machine.DivH(hl,hr),left.type);
  2932. *)
  2933. END;
  2934. (* *)
  2935. |Scanner.Equal: NewBool(hl=hr);
  2936. |Scanner.Unequal: NewBool(hl#hr);
  2937. |Scanner.Less: NewBool(hl<hr);
  2938. |Scanner.LessEqual: NewBool(hl<=hr);
  2939. |Scanner.Greater: NewBool(hl>hr);
  2940. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2941. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2942. END;
  2943. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2944. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2945. type := left.type
  2946. ELSIF (operator = Scanner.Slash) THEN
  2947. left := NewConversion(left.position,left,system.realType,NIL);
  2948. right := NewConversion(right.position,right,system.realType,NIL);
  2949. binaryExpression.SetLeft(left);
  2950. binaryExpression.SetRight(right);
  2951. type := system.realType
  2952. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2953. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2954. type := system.booleanType
  2955. ELSE
  2956. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2957. END;
  2958. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2959. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2960. CASE operator OF
  2961. |Scanner.Plus: NewReal(rl+rr,leftType);
  2962. |Scanner.Minus: NewReal(rl-rr,leftType);
  2963. |Scanner.Times:NewReal(rl*rr,leftType);
  2964. |Scanner.Slash:
  2965. IF rr = 0 THEN
  2966. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2967. ELSE
  2968. NewReal(rl/rr,leftType);
  2969. END
  2970. |Scanner.Equal: NewBool(rl=rr);
  2971. |Scanner.Unequal: NewBool(rl#rr);
  2972. |Scanner.Less: NewBool(rl<rr);
  2973. |Scanner.LessEqual: NewBool(rl<=rr);
  2974. |Scanner.Greater: NewBool(rl>rr);
  2975. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2976. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2977. END;
  2978. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2979. type := left.type
  2980. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2981. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2982. type := system.booleanType
  2983. ELSE
  2984. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2985. IF VerboseErrorMessage THEN
  2986. Printout.Info("left",left);
  2987. Printout.Info("right",right);
  2988. END;
  2989. END;
  2990. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2991. CASE operator OF
  2992. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2993. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2994. ELSE
  2995. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2996. IF VerboseErrorMessage THEN
  2997. Printout.Info("left", left);
  2998. Printout.Info("right", right)
  2999. END;
  3000. END;
  3001. IF ~error THEN
  3002. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3003. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  3004. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3005. (* do constant folding *)
  3006. CASE operator OF
  3007. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3008. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3009. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3010. |Scanner.Slash:
  3011. divisor := c * c + d * d;
  3012. ASSERT(divisor # 0);
  3013. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3014. |Scanner.Equal: NewBool((a = c) & (b = d))
  3015. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3016. END
  3017. END
  3018. END
  3019. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3020. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3021. CASE operator OF
  3022. |Scanner.And: NewBool(bl & br);
  3023. |Scanner.Or: NewBool(bl OR br);
  3024. |Scanner.Equal: NewBool(bl = br);
  3025. |Scanner.Unequal: NewBool(bl # br);
  3026. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3027. END;
  3028. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3029. type := system.booleanType
  3030. ELSE
  3031. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3032. END;
  3033. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3034. (* constant folding *)
  3035. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3036. IF operator = Scanner.Equal THEN
  3037. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3038. ELSIF operator = Scanner.Unequal THEN
  3039. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3040. END;
  3041. END;
  3042. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3043. type := system.booleanType;
  3044. ELSE
  3045. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3046. END;
  3047. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3048. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3049. CASE operator OF
  3050. |Scanner.Plus: NewSet(sl + sr);
  3051. |Scanner.Minus: NewSet(sl - sr);
  3052. |Scanner.Times: NewSet(sl * sr);
  3053. |Scanner.Slash: NewSet(sl / sr);
  3054. |Scanner.Equal: NewBool(sl=sr);
  3055. |Scanner.Unequal: NewBool(sl#sr);
  3056. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3057. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3058. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3059. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3060. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3061. END;
  3062. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3063. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3064. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3065. THEN
  3066. type := system.booleanType
  3067. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3068. type := left.type
  3069. ELSE
  3070. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3071. END;
  3072. ELSIF IsCharacterType(left.type) THEN
  3073. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3074. CASE operator OF
  3075. |Scanner.Equal: NewBool(cl=cr);
  3076. |Scanner.Unequal: NewBool(cl#cr);
  3077. |Scanner.Less: NewBool(cl<cr);
  3078. |Scanner.LessEqual: NewBool(cl<=cr);
  3079. |Scanner.Greater: NewBool(cl>cr);
  3080. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3081. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3082. END;
  3083. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3084. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3085. type := system.booleanType
  3086. ELSE
  3087. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3088. END;
  3089. ELSE
  3090. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3091. END;
  3092. ELSE
  3093. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3094. END;
  3095. IF type = SyntaxTree.invalidType THEN
  3096. result := SyntaxTree.invalidExpression
  3097. ELSE
  3098. result.SetType(type)
  3099. END;
  3100. resolvedExpression := result
  3101. END VisitBinaryExpression;
  3102. (** resolve a range expression of the from <<first .. last BY step>>
  3103. - depending on the context different things are checked:
  3104. ArrayIndex:
  3105. - components must be integers
  3106. - replace missing lower bound with 0
  3107. - replace missing upper bound with MAX(LONGINT)
  3108. - replace missing step size with 1
  3109. SetElement:
  3110. - components must be integers
  3111. - replace missing lower bound with 0
  3112. - replace missing upper bound with MAX(SET)
  3113. - must not have step size
  3114. CaseGuard:
  3115. - components must be constant
  3116. - components must be integers or characters
  3117. - must have lower and upper bound present
  3118. - components are made compatible
  3119. - must not have step size
  3120. - if error: return invalidExpression
  3121. **)
  3122. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3123. VAR
  3124. hasError: BOOLEAN;
  3125. first, last, step: SyntaxTree.Expression;
  3126. BEGIN
  3127. hasError := FALSE;
  3128. first := x.first;
  3129. last := x.last;
  3130. step := x.step;
  3131. (* check lower bound *)
  3132. IF x.context = SyntaxTree.CaseGuard THEN
  3133. IF first = NIL THEN
  3134. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3135. hasError := TRUE
  3136. ELSE
  3137. first := ResolveExpression(first);
  3138. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3139. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3140. hasError := TRUE
  3141. ELSE
  3142. IF first IS SyntaxTree.StringValue THEN
  3143. (* add conversion from string to character *)
  3144. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3145. END
  3146. END;
  3147. (* check if expression is constant *)
  3148. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3149. (* error already reported *)
  3150. hasError := TRUE
  3151. END
  3152. END
  3153. ELSE (* ArrayIndex, SetElement *)
  3154. IF first = NIL THEN
  3155. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3156. END;
  3157. first := ResolveExpression(first);
  3158. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3159. first := NewConversion(first.position, first, system.longintType, NIL)
  3160. ELSE
  3161. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3162. hasError := TRUE
  3163. END
  3164. END;
  3165. (* check upper bound *)
  3166. IF x.context = SyntaxTree.CaseGuard THEN
  3167. IF last = NIL THEN
  3168. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3169. hasError := TRUE
  3170. ELSE
  3171. last := ResolveExpression(last);
  3172. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3173. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3174. hasError := TRUE
  3175. ELSE
  3176. IF last IS SyntaxTree.StringValue THEN
  3177. (* add conversion from string to character *)
  3178. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3179. END
  3180. END;
  3181. (* check if expression is constant *)
  3182. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3183. (* error already reported *)
  3184. hasError := TRUE
  3185. ELSE
  3186. (* try to make lower and upper bound compatible *)
  3187. ConvertOperands(first, last);
  3188. IF first.type.resolved # last.type.resolved THEN
  3189. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3190. hasError := TRUE
  3191. END
  3192. END
  3193. END
  3194. ELSE (* ArrayIndex, SetElement *)
  3195. IF last = NIL THEN
  3196. IF x.context = SyntaxTree.ArrayIndex THEN
  3197. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3198. ELSE
  3199. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3200. END
  3201. END;
  3202. last := ResolveExpression(last);
  3203. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3204. last := NewConversion(last.position, last, system.longintType, NIL)
  3205. ELSE
  3206. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3207. hasError := TRUE
  3208. END
  3209. END;
  3210. (* check step size *)
  3211. IF x.context = SyntaxTree.ArrayIndex THEN
  3212. IF step = NIL THEN
  3213. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3214. END;
  3215. step := ResolveExpression(step);
  3216. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3217. step := NewConversion(step.position, step, system.longintType, NIL)
  3218. ELSE
  3219. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3220. hasError := TRUE
  3221. END
  3222. ELSE (* SetElement, CaseGuard *)
  3223. IF step # NIL THEN
  3224. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3225. hasError := TRUE
  3226. END
  3227. END;
  3228. IF hasError THEN
  3229. resolvedExpression := SyntaxTree.invalidExpression
  3230. ELSE
  3231. x.SetFirst(first);
  3232. x.SetLast(last);
  3233. x.SetStep(step);
  3234. x.SetType(system.rangeType);
  3235. resolvedExpression := x;
  3236. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3237. END
  3238. END VisitRangeExpression;
  3239. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3240. BEGIN
  3241. x.SetType(NIL);
  3242. resolvedExpression := x;
  3243. END VisitTensorRangeExpression;
  3244. (** resolve the expression d and return result as designator
  3245. - resolve expression
  3246. - if expression is a designator then return designator else error message and return invalidDesignator
  3247. **)
  3248. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3249. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3250. BEGIN
  3251. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3252. resolved := ResolveExpression(d);
  3253. IF resolved = SyntaxTree.invalidExpression THEN
  3254. (* error should already have been reported *)
  3255. result := SyntaxTree.invalidDesignator;
  3256. ELSIF resolved IS SyntaxTree.Designator THEN
  3257. result := resolved(SyntaxTree.Designator);
  3258. ELSE
  3259. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3260. result := SyntaxTree.invalidDesignator;
  3261. END;
  3262. (* result.type might be nil. *)
  3263. RETURN result
  3264. END ResolveDesignator;
  3265. (**
  3266. symbol designator generated in this module
  3267. nothing to be resolved
  3268. **)
  3269. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3270. BEGIN
  3271. resolvedExpression := x;
  3272. END VisitSymbolDesignator;
  3273. (**
  3274. self designator generated in this module
  3275. nothing to be resolved
  3276. **)
  3277. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3278. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3279. BEGIN
  3280. (* check if in record scope *)
  3281. scope := currentScope;
  3282. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3283. scope := scope.outerScope;
  3284. END;
  3285. IF scope = NIL THEN (* in module scope *)
  3286. x.SetType(system.anyType);
  3287. ELSIF scope IS SyntaxTree.CellScope THEN
  3288. cell := scope(SyntaxTree.CellScope).ownerCell;
  3289. x.SetType(cell);
  3290. ELSE (* in record scope *)
  3291. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3292. IF (record # NIL) & (record.pointerType # NIL) THEN
  3293. type := ResolveType(record.pointerType);
  3294. x.SetType(type);
  3295. ELSE
  3296. x.SetType(record);
  3297. END;
  3298. END;
  3299. resolvedExpression := x;
  3300. END VisitSelfDesignator;
  3301. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3302. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3303. BEGIN
  3304. scope := currentScope;
  3305. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3306. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3307. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3308. returnType := procedureType.returnType;
  3309. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3310. THEN
  3311. x.SetType(returnType);
  3312. ELSE
  3313. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3314. x.SetType(SyntaxTree.invalidType);
  3315. END;
  3316. ELSE
  3317. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3318. x.SetType(SyntaxTree.invalidType);
  3319. END;
  3320. x.SetAssignable(TRUE);
  3321. resolvedExpression := x;
  3322. END VisitResultDesignator;
  3323. (**
  3324. return symbol designator as an expression
  3325. - if symbol is a constant then return the constant value expression
  3326. - else
  3327. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3328. - if symbol is a guarded variable then return a TypeGuardDesignator
  3329. - else return a symbol designator
  3330. **)
  3331. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3332. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3333. guardType: SyntaxTree.Type;
  3334. BEGIN
  3335. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3336. result := SyntaxTree.invalidExpression;
  3337. ASSERT(symbol # NIL);
  3338. (*
  3339. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3340. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3341. Error(position,Diagnostics.Invalid,"type not allowed here");
  3342. ELS *)
  3343. (* not needed any more as values are stored in the expression
  3344. IF symbol IS SyntaxTree.Constant THEN
  3345. result := symbol(SyntaxTree.Constant).value
  3346. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3347. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3348. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3349. ELSE
  3350. result := symbol(SyntaxTree.Constant).value
  3351. END;
  3352. ELSE
  3353. *)
  3354. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3355. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3356. THEN
  3357. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3358. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3359. left := NewDereferenceDesignator(position,left);
  3360. left.SetHidden(TRUE);
  3361. END;
  3362. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3363. scope := currentScope;
  3364. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3365. scope := scope.outerScope;
  3366. END;
  3367. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3368. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3369. END;
  3370. END;
  3371. 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);
  3372. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3373. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3374. ELSE
  3375. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3376. END;
  3377. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3378. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3379. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3380. result.SetType(symbol.type);
  3381. result.SetAssignable(assignable);
  3382. symbol.MarkUsed;
  3383. IF symbol IS SyntaxTree.Constant THEN
  3384. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3385. END;
  3386. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3387. variableAccessed := TRUE
  3388. END;
  3389. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3390. IF GetGuard(symbol,guardType) THEN
  3391. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3392. END;
  3393. END;
  3394. ASSERT(result.type # NIL);
  3395. RETURN result
  3396. END NewSymbolDesignator;
  3397. (** check and resolve an identifier designator "identifier"
  3398. - if identifier = self then return SelfDesignator
  3399. - else find symbol in current scope
  3400. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3401. **)
  3402. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3403. VAR symbol: SyntaxTree.Symbol;
  3404. BEGIN
  3405. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3406. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3407. IF symbol # NIL THEN
  3408. ResolveSymbol(symbol);
  3409. ASSERT(symbol.type # NIL);
  3410. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3411. ELSE
  3412. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3413. IF VerboseErrorMessage THEN
  3414. Printout.Info("undeclared identifier designator",identifierDesignator);
  3415. END;
  3416. resolvedExpression := SyntaxTree.invalidDesignator;
  3417. END;
  3418. END VisitIdentifierDesignator;
  3419. (** check and resolve a selector designator of the form left.designator
  3420. - if left is a pointer type then do auto dereferenciation
  3421. - left denotes a search scope:
  3422. - if left type is import type then set search scope to respective module
  3423. - if left type is enumeration type then set search scope to respective enumeration scope
  3424. - elsif left type is record type then set search scope to record scope
  3425. - search symbol in computed scope
  3426. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3427. **)
  3428. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3429. VAR
  3430. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3431. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3432. BEGIN
  3433. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3434. left := ResolveDesignator(selectorDesignator.left);
  3435. result := SyntaxTree.invalidDesignator;
  3436. IF left # NIL THEN
  3437. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3438. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3439. END;
  3440. scope := NIL;
  3441. IF left.type = NIL THEN
  3442. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3443. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3444. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3445. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3446. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3447. module := symbol(SyntaxTree.Import).module;
  3448. IF module # NIL THEN
  3449. scope := module.moduleScope
  3450. ELSE
  3451. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3452. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3453. END;
  3454. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3455. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3456. ASSERT(scope # NIL)
  3457. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3458. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3459. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3460. IF type IS SyntaxTree.EnumerationType THEN
  3461. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3462. ELSE
  3463. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3464. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3465. END;
  3466. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3467. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3468. ELSE
  3469. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3470. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3471. END;
  3472. symbol := NIL;
  3473. IF scope # NIL THEN
  3474. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3475. IF symbol # NIL THEN
  3476. ResolveSymbol(symbol);
  3477. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3478. symbol.MarkUsed
  3479. ELSE
  3480. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3481. IF VerboseErrorMessage THEN
  3482. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3483. Printout.Info("scope", scope);
  3484. Printout.Info("left", left);
  3485. Printout.Info("undeclared identifier",selectorDesignator);
  3486. Printout.Info("left resolved designator",left);
  3487. END
  3488. END;
  3489. END;
  3490. END;
  3491. resolvedExpression := result;
  3492. END VisitSelectorDesignator;
  3493. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3494. VAR len,idx: LONGINT;
  3495. BEGIN
  3496. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3497. IF idx < 0 THEN
  3498. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3499. ELSE
  3500. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3501. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3502. END;
  3503. END;
  3504. END;
  3505. END IndexCheck;
  3506. (*
  3507. - if index designator has not type, use newBaseType as its type
  3508. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3509. - special rule: if static array of dynamic array occurs, make it all dynamic
  3510. index designator type: new base type: new index designator type:
  3511. NIL z z
  3512. ARRAY [x, y] z ARRAY [x, y] OF z
  3513. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3514. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3515. *)
  3516. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3517. VAR
  3518. mathArrayType: SyntaxTree.MathArrayType;
  3519. makeDynamic: BOOLEAN;
  3520. BEGIN
  3521. IF indexDesignator.type = NIL THEN
  3522. indexDesignator.SetType(newBaseType)
  3523. ELSE
  3524. (* index designator must be a of math array type *)
  3525. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3526. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3527. (* determine if all arrays have to be made dynamic *)
  3528. makeDynamic :=
  3529. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3530. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3531. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3532. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3533. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3534. END;
  3535. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3536. mathArrayType.SetArrayBase(newBaseType)
  3537. END
  3538. END SetIndexBaseType;
  3539. (** check and append index list element to index designator of math array
  3540. - check validity of single index or array range
  3541. - compute new type
  3542. - if range then create new array type (calculate length of resulting array)
  3543. - otherwise take sourceArray.arrayBase as new type
  3544. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3545. **)
  3546. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3547. VAR
  3548. targetArray: SyntaxTree.MathArrayType;
  3549. first, last, step: SyntaxTree.Expression;
  3550. firstValue, lastValue, stepValue, length: LONGINT;
  3551. rangeExpression: SyntaxTree.RangeExpression;
  3552. BEGIN
  3553. IF indexListItem.type = SyntaxTree.invalidType THEN
  3554. (* error already handled *)
  3555. indexDesignator.parameters.AddExpression(indexListItem)
  3556. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3557. indexDesignator.HasRange;
  3558. indexDesignator.HasTensorRange;
  3559. indexDesignator.parameters.AddExpression(indexListItem);
  3560. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3561. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3562. IndexCheck(indexListItem, sourceArray.length);
  3563. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3564. indexDesignator.parameters.AddExpression(indexListItem)
  3565. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3566. indexDesignator.HasRange;
  3567. (* if the range is given as an array range expression, check the validity of its components *)
  3568. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3569. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3570. first := rangeExpression.first;
  3571. last := rangeExpression.last;
  3572. step := rangeExpression.step;
  3573. (* perform static checks on range components *)
  3574. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3575. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3576. END;
  3577. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3578. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3579. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3580. END
  3581. END;
  3582. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3583. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3584. END;
  3585. (* add conversions to size type *)
  3586. (* TODO: needed? *)
  3587. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3588. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3589. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3590. END;
  3591. IF indexDesignator.hasTensorRange THEN
  3592. (* the index designator's base type is a tensor: leave it as is *)
  3593. ELSE
  3594. (* append a new math array to the index designator's base type *)
  3595. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3596. IF ~error THEN
  3597. (*
  3598. (* optimization: calculate length of target array for static ranges *)
  3599. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3600. IF IsStaticallyOpenRange(rangeExpression) THEN
  3601. (* range is open ('*'): reuse source array length as target array length *)
  3602. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3603. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3604. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3605. IF lastValue = MAX(LONGINT) THEN
  3606. IF IsIntegerValue(sourceArray.length, length) THEN
  3607. lastValue := length - 1;
  3608. isStaticTargetArrayLength := TRUE
  3609. ELSE
  3610. isStaticTargetArrayLength := FALSE
  3611. END
  3612. ELSE
  3613. isStaticTargetArrayLength := TRUE
  3614. END;
  3615. IF isStaticTargetArrayLength THEN
  3616. (* calculate static target array length *)
  3617. IF firstValue > lastValue THEN
  3618. length := 0
  3619. ELSE
  3620. length := 1 + lastValue - firstValue;
  3621. IF length MOD stepValue = 0 THEN
  3622. length := length DIV stepValue
  3623. ELSE
  3624. length := length DIV stepValue + 1
  3625. END
  3626. END;
  3627. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3628. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3629. ASSERT(targetArray.form = SyntaxTree.Static)
  3630. END
  3631. END
  3632. END
  3633. *)
  3634. END;
  3635. SetIndexBaseType(indexDesignator, targetArray)
  3636. END;
  3637. indexDesignator.parameters.AddExpression(indexListItem)
  3638. ELSE
  3639. Error(position, Diagnostics.Invalid,"invalid index list item");
  3640. END;
  3641. END AppendMathIndex;
  3642. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3643. VAR parameters: SyntaxTree.ExpressionList;
  3644. BEGIN
  3645. parameters := index.parameters;
  3646. IF (expression.type = NIL) THEN
  3647. Error(position,Diagnostics.Invalid,"invalid index");
  3648. ELSIF IsIntegerType(expression.type.resolved) THEN
  3649. IF over IS SyntaxTree.ArrayType THEN
  3650. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3651. ELSIF over IS SyntaxTree.StringType THEN
  3652. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3653. END;
  3654. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3655. parameters.AddExpression(expression);
  3656. ELSE
  3657. Error(position,Diagnostics.Invalid,"invalid index");
  3658. END;
  3659. END AppendIndex;
  3660. (** convert an expression to math array type
  3661. if expression is of math array type: return expression itself
  3662. if expression is of array-structured object type: return an index operator call on it
  3663. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3664. otherwise: return invalid expression
  3665. **)
  3666. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3667. VAR
  3668. result: SyntaxTree.Expression;
  3669. mathArrayType: SyntaxTree.MathArrayType;
  3670. BEGIN
  3671. IF expression.type = NIL THEN
  3672. result := SyntaxTree.invalidExpression
  3673. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3674. (* expression of math array type *)
  3675. result := expression
  3676. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3677. (* expression of array-structured object type *)
  3678. mathArrayType := MathArrayStructureOfType(expression.type);
  3679. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3680. ELSE
  3681. result := SyntaxTree.invalidExpression
  3682. END;
  3683. RETURN result
  3684. END ConvertToMathArray;
  3685. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3686. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3687. VAR
  3688. result: SyntaxTree.ExpressionList;
  3689. i: LONGINT;
  3690. BEGIN
  3691. result := SyntaxTree.NewExpressionList();
  3692. FOR i := 1 TO itemCount DO
  3693. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3694. END;
  3695. RETURN result
  3696. END ListOfOpenRanges;
  3697. (** create a procedure call designator for an index operator call on an array-structured object type
  3698. - use given index list as actual parameters
  3699. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3700. **)
  3701. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3702. VAR
  3703. operator: SyntaxTree.Operator;
  3704. expression: SyntaxTree.Expression;
  3705. actualParameters, tempList: SyntaxTree.ExpressionList;
  3706. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3707. result, tempDesignator: SyntaxTree.Designator;
  3708. recordType: SyntaxTree.RecordType;
  3709. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3710. i, hashValue, indexListSize, indexListKind: LONGINT;
  3711. castReturnType: SyntaxTree.MathArrayType;
  3712. BEGIN
  3713. ASSERT(IsArrayStructuredObjectType(left.type));
  3714. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3715. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3716. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3717. indexListSize := indexList.Length();
  3718. indexListKind := 0;
  3719. containsNonRange := FALSE;
  3720. FOR i := 0 TO indexList.Length() - 1 DO
  3721. indexListKind := indexListKind * 2;
  3722. expression := indexList.GetExpression(i);
  3723. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3724. INC(indexListKind)
  3725. ELSE
  3726. containsNonRange := TRUE
  3727. END
  3728. END;
  3729. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3730. (* select applicable index operator
  3731. - try to look up optimal index operator
  3732. - if not present, use operator on ranges
  3733. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3734. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3735. *)
  3736. usesGeneralOperator := FALSE;
  3737. IF rhs # NIL THEN
  3738. (* write operator *)
  3739. IF hashValue = -1 THEN
  3740. operator := NIL
  3741. ELSE
  3742. operator := recordType.arrayAccessOperators.write[hashValue];
  3743. END;
  3744. IF operator = NIL THEN
  3745. usesPureRangeOperator := TRUE;
  3746. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3747. operator := recordType.arrayAccessOperators.generalWrite;
  3748. usesGeneralOperator := TRUE
  3749. ELSE
  3750. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3751. operator := recordType.arrayAccessOperators.write[hashValue];
  3752. END
  3753. END
  3754. ELSE
  3755. (* read operator *)
  3756. IF hashValue = -1 THEN
  3757. operator := NIL
  3758. ELSE
  3759. operator := recordType.arrayAccessOperators.read[hashValue];
  3760. END;
  3761. IF operator = NIL THEN
  3762. usesPureRangeOperator := TRUE;
  3763. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3764. operator := recordType.arrayAccessOperators.generalRead;
  3765. usesGeneralOperator := TRUE
  3766. ELSE
  3767. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3768. operator := recordType.arrayAccessOperators.read[hashValue];
  3769. END
  3770. END
  3771. END;
  3772. IF operator = NIL THEN
  3773. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3774. result := SyntaxTree.invalidDesignator;
  3775. ELSE
  3776. (* determine if reshaping is needed *)
  3777. needsReshaping := containsNonRange & usesPureRangeOperator;
  3778. (* import OCArrayBase if reshaping is needed *)
  3779. IF needsReshaping & ~arrayBaseImported THEN
  3780. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3781. arrayBaseImported := TRUE
  3782. END;
  3783. (* add the index list item to the list of actual parameters
  3784. - for general operators: add a single inline array containing the index list items as parameter
  3785. - otherwise: add all index list items as individual parameters
  3786. *)
  3787. actualParameters := SyntaxTree.NewExpressionList();
  3788. IF usesGeneralOperator THEN
  3789. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3790. END;
  3791. FOR i := 0 TO indexListSize - 1 DO
  3792. expression := indexList.GetExpression(i);
  3793. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3794. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3795. tempList := SyntaxTree.NewExpressionList();
  3796. tempList.AddExpression(expression);
  3797. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3798. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3799. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3800. END;
  3801. IF usesGeneralOperator THEN
  3802. tempMathArrayExpression.elements.AddExpression(expression);
  3803. ELSE
  3804. actualParameters.AddExpression(expression)
  3805. END
  3806. END;
  3807. IF usesGeneralOperator THEN
  3808. actualParameters.AddExpression(tempMathArrayExpression)
  3809. END;
  3810. IF rhs # NIL THEN
  3811. (* add actual parameter for RHS *)
  3812. IF needsReshaping THEN
  3813. (* reshape using OCArrayBase.ExpandDimensions *)
  3814. tempList := SyntaxTree.NewExpressionList();
  3815. (* source array *)
  3816. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3817. tempList.AddExpression(rhs);
  3818. ELSE
  3819. (* convert scalar to one-dimensional array *)
  3820. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3821. tempMathArrayExpression.elements.AddExpression(rhs);
  3822. tempList.AddExpression(tempMathArrayExpression)
  3823. END;
  3824. (* list of kept dimensions *)
  3825. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3826. FOR i := 0 TO indexListSize - 1 DO
  3827. expression := indexList.GetExpression(i);
  3828. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3829. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3830. ELSE
  3831. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3832. END
  3833. END;
  3834. tempList.AddExpression(tempMathArrayExpression);
  3835. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3836. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3837. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3838. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3839. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3840. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3841. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3842. expression.SetType(castReturnType);
  3843. ELSE
  3844. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3845. END;
  3846. actualParameters.AddExpression(expression)
  3847. ELSE
  3848. actualParameters.AddExpression(rhs)
  3849. END
  3850. END;
  3851. (* add dereference operator and create procedure call designator *)
  3852. ASSERT(left IS SyntaxTree.Designator);
  3853. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3854. ASSERT(expression IS SyntaxTree.Designator);
  3855. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3856. IF (rhs = NIL) & needsReshaping THEN
  3857. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3858. tempList := SyntaxTree.NewExpressionList();
  3859. FOR i := 0 TO indexList.Length() - 1 DO
  3860. expression := indexList.GetExpression(i);
  3861. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3862. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3863. ELSE
  3864. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3865. END
  3866. END;
  3867. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3868. END;
  3869. IF rhs = NIL THEN
  3870. (* special rule: index read operator calls are considered to be assignable *)
  3871. result.SetAssignable(TRUE)
  3872. END;
  3873. (* put information about this index operator call into the resulting designator *)
  3874. result.SetRelatedAsot(left);
  3875. result.SetRelatedIndexList(indexList)
  3876. END;
  3877. RETURN result
  3878. END NewIndexOperatorCall;
  3879. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3880. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3881. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3882. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3883. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3884. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3885. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3886. CONST trace = FALSE;
  3887. BEGIN
  3888. IF trace THEN
  3889. FOR i := 0 TO actualParameters.Length()-1 DO
  3890. Printout.Info("par", actualParameters.GetExpression(i));
  3891. END;
  3892. END;
  3893. operator := scope.firstOperator;
  3894. WHILE(operator # NIL) DO
  3895. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3896. procedureType := operator.type(SyntaxTree.ProcedureType);
  3897. distance := Distance(system, procedureType,actualParameters);
  3898. IF trace THEN Printout.Info("check op ",operator) END;
  3899. IF distance < bestDistance THEN
  3900. IF trace THEN Printout.Info("taken op",operator) END;
  3901. bestDistance := distance;
  3902. bestOperator := operator;
  3903. END;
  3904. END;
  3905. operator := operator.nextOperator;
  3906. END;
  3907. END FindInScope;
  3908. BEGIN
  3909. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3910. IF oper = 0 THEN (* index *)
  3911. identifier := SyntaxTree.NewIdentifier("[]");
  3912. ELSE
  3913. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3914. END;
  3915. WHILE (recordType # NIL) DO
  3916. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3917. recordType := recordType.GetBaseRecord();
  3918. END;
  3919. RETURN bestOperator
  3920. END FindOperator;
  3921. BEGIN
  3922. type := left.type.resolved;
  3923. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3924. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3925. actualParameters := SyntaxTree.NewExpressionList();
  3926. IF parameters # NIL THEN
  3927. FOR i := 0 TO parameters.Length()-1 DO
  3928. expression := ResolveExpression(parameters.GetExpression(i));
  3929. actualParameters.AddExpression(expression);
  3930. END;
  3931. END;
  3932. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3933. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3934. IF op # NIL THEN
  3935. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3936. ASSERT(expression IS SyntaxTree.Designator);
  3937. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3938. result.SetRelatedAsot(left);
  3939. result.SetRelatedIndexList(parameters);
  3940. (* check if write operator exists, for var parameters *)
  3941. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3942. actualParameters := SyntaxTree.NewExpressionList();
  3943. FOR i := 0 TO parameters.Length()-1 DO
  3944. expression := ResolveExpression(parameters.GetExpression(i));
  3945. actualParameters.AddExpression(expression);
  3946. END;
  3947. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3948. actualParameters.AddExpression(rhs);
  3949. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3950. IF op = NIL THEN rhs := NIL END;
  3951. END;
  3952. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3953. ELSE
  3954. result := NIL;
  3955. END;
  3956. RETURN result;
  3957. END NewObjectOperatorCall;
  3958. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3959. 1. convert bracket designator chains into a single one that contains separators
  3960. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3961. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3962. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3963. - if an array or math array is indexed over, create index designator
  3964. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3965. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3966. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3967. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3968. - if an array-structured object type is indexed over, create procedure call designator
  3969. e.g.: a[x, y] -> a^."[]"(x, y)
  3970. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3971. - a[i, *] = a[i][*]
  3972. - a[*, i] # a[*][i]
  3973. Because:
  3974. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3975. - 'i-th column' = a[*, i]
  3976. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3977. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3978. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3979. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3980. **)
  3981. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3982. VAR
  3983. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3984. indexDesignator: SyntaxTree.IndexDesignator;
  3985. designator: SyntaxTree.Designator;
  3986. type: SyntaxTree.Type;
  3987. recordType: SyntaxTree.RecordType;
  3988. expression, rhs: SyntaxTree.Expression;
  3989. indexList: SyntaxTree.ExpressionList;
  3990. i: LONGINT;
  3991. hasError, done: BOOLEAN;
  3992. PROCEDURE FinalizeIndexDesignator;
  3993. BEGIN
  3994. IF indexDesignator # NIL THEN
  3995. (* the end of a tensor has been reached: *)
  3996. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3997. SetIndexBaseType(indexDesignator, type);
  3998. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3999. designator := indexDesignator;
  4000. type := designator.type.resolved;
  4001. indexDesignator := NIL;
  4002. ASSERT(SyntaxTree.Resolved IN type.state)
  4003. END
  4004. END FinalizeIndexDesignator;
  4005. BEGIN
  4006. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4007. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4008. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4009. (* copy all index list entries including a separator to the left bracket designator *)
  4010. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4011. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4012. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4013. END;
  4014. (* propagate the related RHS *)
  4015. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4016. (* only resolve left bracket designator and use as final result *)
  4017. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4018. ELSE
  4019. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4020. designator := ResolveDesignator(bracketDesignator.left);
  4021. type := designator.type.resolved;
  4022. indexDesignator := NIL;
  4023. (*!!! clean up *)
  4024. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4025. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4026. IF resolvedExpression = NIL THEN
  4027. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4028. resolvedExpression := SyntaxTree.invalidDesignator
  4029. END;
  4030. RETURN;
  4031. END;
  4032. i := 0;
  4033. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4034. expression := bracketDesignator.parameters.GetExpression(i);
  4035. expression := ResolveExpression(expression);
  4036. bracketDesignator.parameters.SetExpression(i, expression);
  4037. IF expression = SyntaxTree.indexListSeparator THEN
  4038. (* finalize an existing index designator if needed *)
  4039. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4040. INC(i)
  4041. ELSE
  4042. (* do auto-dereferencing if needed *)
  4043. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4044. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4045. & (i=0)*)
  4046. THEN
  4047. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4048. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4049. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4050. designator := SyntaxTree.invalidDesignator;
  4051. type := SyntaxTree.invalidType
  4052. ELSE
  4053. FinalizeIndexDesignator;
  4054. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4055. type := designator.type.resolved
  4056. END
  4057. END;
  4058. (* create a new index designator, if needed *)
  4059. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4060. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4061. indexDesignator.SetAssignable(designator.assignable);
  4062. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4063. (* designator := indexDesignator *)
  4064. END;
  4065. IF type = SyntaxTree.invalidType THEN
  4066. (* error already handled *)
  4067. INC(i)
  4068. ELSIF type IS SyntaxTree.ArrayType THEN
  4069. (* indexing over an array *)
  4070. ASSERT(indexDesignator # NIL);
  4071. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4072. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4073. INC(i)
  4074. ELSIF type IS SyntaxTree.StringType THEN
  4075. (* indexing over an array *)
  4076. ASSERT(indexDesignator # NIL);
  4077. AppendIndex(expression.position, indexDesignator, expression, type);
  4078. type := type(SyntaxTree.StringType).baseType.resolved;
  4079. INC(i)
  4080. ELSIF type IS SyntaxTree.MathArrayType THEN
  4081. (* indexing over a math array *)
  4082. ASSERT(indexDesignator # NIL);
  4083. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4084. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4085. INC(i)
  4086. ELSIF IsArrayStructuredObjectType(type) THEN
  4087. (* indexing over ASOTs *)
  4088. FinalizeIndexDesignator;
  4089. ASSERT(type IS SyntaxTree.PointerType);
  4090. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4091. (*
  4092. - collect index list items from bracket designator that belong to ASOT
  4093. - check for errors
  4094. *)
  4095. indexList := SyntaxTree.NewExpressionList();
  4096. hasError := FALSE;
  4097. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4098. (* indexing over tensor ASOT:
  4099. - stop at index list end or separator
  4100. - dimensionality is given by number of index list items
  4101. *)
  4102. done := FALSE;
  4103. WHILE ~done DO
  4104. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4105. done := TRUE;
  4106. ELSE
  4107. expression := bracketDesignator.parameters.GetExpression(i);
  4108. IF expression = SyntaxTree.indexListSeparator THEN
  4109. done := TRUE;
  4110. ELSE
  4111. expression := ResolveExpression(expression);
  4112. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4113. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4114. hasError := TRUE
  4115. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4116. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4117. expression := SyntaxTree.invalidExpression;
  4118. hasError := TRUE
  4119. END;
  4120. indexList.AddExpression(expression)
  4121. END;
  4122. INC(i)
  4123. END
  4124. END
  4125. ELSE
  4126. (* indexing over non-tensor ASOT:
  4127. - ignore separators
  4128. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4129. *)
  4130. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4131. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4132. expression := bracketDesignator.parameters.GetExpression(i);
  4133. ELSE
  4134. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4135. END;
  4136. IF expression # SyntaxTree.indexListSeparator THEN
  4137. expression := ResolveExpression(expression);
  4138. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4139. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4140. expression := SyntaxTree.invalidExpression;
  4141. hasError := TRUE
  4142. END;
  4143. indexList.AddExpression(expression)
  4144. END;
  4145. INC(i)
  4146. END;
  4147. END;
  4148. IF hasError THEN
  4149. designator := SyntaxTree.invalidDesignator;
  4150. type := SyntaxTree.invalidType;
  4151. ELSE
  4152. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4153. and the last entry in the index list belongs to the array-structured object type in question.
  4154. E.g.: for a 2-dimensional array-structured object type:
  4155. - 'lhs := asot[1, 2]' -> read mode
  4156. - 'asot[1, 2] := rhs' -> write mode
  4157. - 'asot[1, 2, 3] := rhs' -> read mode
  4158. *)
  4159. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4160. rhs := bracketDesignator.relatedRhs
  4161. ELSE
  4162. rhs := NIL
  4163. END;
  4164. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4165. type := designator.type
  4166. END
  4167. ELSE
  4168. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4169. designator := SyntaxTree.invalidDesignator;
  4170. type := SyntaxTree.invalidType;
  4171. INC(i)
  4172. END
  4173. END
  4174. END;
  4175. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4176. resolvedExpression := designator
  4177. END
  4178. END VisitBracketDesignator;
  4179. (** check and resolve expression list
  4180. - resolve each expression in an expression list
  4181. - returns true if and only if all statements could have successfully been resolved
  4182. **)
  4183. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4184. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4185. BEGIN
  4186. result := TRUE;
  4187. FOR i := 0 TO expressionList.Length()-1 DO
  4188. expression := ResolveExpression(expressionList.GetExpression(i));
  4189. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4190. expressionList.SetExpression(i,expression);
  4191. END;
  4192. RETURN result
  4193. END ExpressionList;
  4194. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4195. BEGIN
  4196. type := type.resolved;
  4197. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4198. RETURN TRUE
  4199. ELSIF system.CanPassInRegister # NIL THEN
  4200. RETURN system.CanPassInRegister(type);
  4201. ELSE
  4202. RETURN FALSE
  4203. END;
  4204. END CanPassInRegister;
  4205. (** return procedure call designator left(actualParameters)
  4206. - check realtime procedure call in realtime procedure
  4207. - check number of parameters
  4208. - check parameter compatibility
  4209. return invalidDesignator if error
  4210. **)
  4211. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4212. VAR result: SyntaxTree.Designator;
  4213. numberFormalParameters, numberActualParameters: LONGINT;
  4214. formalType: SyntaxTree.ProcedureType;
  4215. formalParameter: SyntaxTree.Parameter;
  4216. actualParameter: SyntaxTree.Expression;
  4217. i: LONGINT;
  4218. BEGIN
  4219. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4220. result := SyntaxTree.invalidDesignator;
  4221. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4222. numberFormalParameters := formalType.numberParameters;
  4223. numberActualParameters := actualParameters.Length();
  4224. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4225. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4226. END;
  4227. IF ~ExpressionList(actualParameters) THEN
  4228. result := SyntaxTree.invalidDesignator
  4229. ELSE
  4230. IF numberActualParameters <= numberFormalParameters THEN
  4231. formalParameter := formalType.firstParameter;
  4232. FOR i := 0 TO numberActualParameters-1 DO
  4233. actualParameter := actualParameters.GetExpression(i);
  4234. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4235. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4236. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4237. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4238. ELSE
  4239. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4240. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4241. END;
  4242. actualParameters.SetExpression(i,actualParameter);
  4243. END;
  4244. formalParameter := formalParameter.nextParameter;
  4245. END;
  4246. WHILE (formalParameter # NIL) DO
  4247. IF formalParameter.defaultValue # NIL THEN
  4248. actualParameters.AddExpression(formalParameter.defaultValue);
  4249. formalParameter := formalParameter.nextParameter
  4250. ELSE
  4251. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4252. formalParameter := NIL;
  4253. END;
  4254. END;
  4255. ELSE
  4256. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4257. END;
  4258. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4259. result.SetAssignable(FALSE);
  4260. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4261. END;
  4262. RETURN result
  4263. END NewProcedureCallDesignator;
  4264. (**
  4265. builtin call designator generated in VisitParameterDesignator
  4266. -> nothing to be resolved
  4267. **)
  4268. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4269. BEGIN
  4270. resolvedExpression := x;
  4271. END VisitTypeGuardDesignator;
  4272. (**
  4273. builtin call designator generated in VisitParameterDesignator
  4274. -> nothing to be resolved
  4275. **)
  4276. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4277. BEGIN
  4278. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4279. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4280. ASSERT(resolvedExpression.type # NIL);
  4281. ELSIF ExpressionList(x.parameters) THEN
  4282. resolvedExpression := x;
  4283. END;
  4284. END VisitBuiltinCallDesignator;
  4285. (**
  4286. procedure call designator generated in VisitParameterDesignator
  4287. -> nothing to be resolved
  4288. **)
  4289. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4290. BEGIN
  4291. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4292. resolvedExpression := x;
  4293. END VisitProcedureCallDesignator;
  4294. (** return true if x is a variable else return false and report error **)
  4295. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4296. VAR result: BOOLEAN;
  4297. BEGIN
  4298. result := TRUE;
  4299. IF x = SyntaxTree.invalidExpression THEN
  4300. result := FALSE;
  4301. ELSIF ~IsVariable(x) THEN
  4302. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4303. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4304. result := FALSE;
  4305. END;
  4306. RETURN result
  4307. END CheckVariable;
  4308. (**
  4309. if expression x is of basic type then return true else report error and return false
  4310. **)
  4311. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4312. VAR result: BOOLEAN;
  4313. BEGIN
  4314. result := FALSE;
  4315. IF x = SyntaxTree.invalidExpression THEN
  4316. ELSIF ~IsBasicType(x.type) THEN
  4317. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4318. result := FALSE
  4319. ELSE result := TRUE
  4320. END;
  4321. RETURN result
  4322. END CheckBasicType;
  4323. (**
  4324. if expression x is of number type then return true else report error and return false
  4325. **)
  4326. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4327. VAR result: BOOLEAN;
  4328. BEGIN
  4329. result := FALSE;
  4330. IF x = SyntaxTree.invalidExpression THEN
  4331. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4332. Error(x.position,Diagnostics.Invalid,"is non number type");
  4333. ELSE result := TRUE
  4334. END;
  4335. RETURN result
  4336. END CheckNumberType;
  4337. (**
  4338. if expression x is of number or size type but not complex then return true else report error and return false
  4339. **)
  4340. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4341. VAR result: BOOLEAN;
  4342. BEGIN
  4343. result := FALSE;
  4344. IF x = SyntaxTree.invalidExpression THEN
  4345. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4346. Error(x.position,Diagnostics.Invalid,"is complex type");
  4347. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4348. Error(x.position,Diagnostics.Invalid,"is non number type");
  4349. ELSE result := TRUE
  4350. END;
  4351. RETURN result
  4352. END CheckNonComplexNumberSizeType;
  4353. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4354. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4355. BEGIN
  4356. result := FALSE; type := x.type.resolved;
  4357. IF x = SyntaxTree.invalidExpression THEN
  4358. 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
  4359. TRACE(type.sizeInBits);
  4360. TRACE(system.addressType.sizeInBits);
  4361. Error(x.position,Diagnostics.Invalid,"is no address type");
  4362. ELSE result := TRUE
  4363. END;
  4364. RETURN result
  4365. END CheckAddressType;
  4366. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4367. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4368. BEGIN
  4369. result := FALSE; type := x.type.resolved;
  4370. IF x = SyntaxTree.invalidExpression THEN
  4371. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4372. Error(x.position,Diagnostics.Invalid,"is no size type");
  4373. ELSE result := TRUE
  4374. END;
  4375. RETURN result
  4376. END CheckSizeType;
  4377. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4378. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4379. BEGIN
  4380. result := FALSE; type := x.type.resolved;
  4381. IF x = SyntaxTree.invalidExpression THEN
  4382. 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
  4383. Error(x.position,Diagnostics.Invalid,"is no object type");
  4384. ELSE result := TRUE
  4385. END;
  4386. RETURN result
  4387. END CheckObjectType;
  4388. (**
  4389. if expression x is of integer type then return true else report error and return false
  4390. **)
  4391. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4392. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4393. BEGIN
  4394. result := FALSE; type := x.type.resolved;
  4395. IF x = SyntaxTree.invalidExpression THEN
  4396. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4397. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4398. ELSE result := TRUE
  4399. END;
  4400. RETURN result
  4401. END CheckIntegerType;
  4402. (**
  4403. if expression x is of character type then return true else report error and return false
  4404. **)
  4405. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4406. VAR result: BOOLEAN;
  4407. BEGIN
  4408. result := FALSE;
  4409. IF x = SyntaxTree.invalidExpression THEN
  4410. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4411. Error(x.position,Diagnostics.Invalid,"is no character type");
  4412. ELSE result := TRUE
  4413. END;
  4414. RETURN result
  4415. END CheckCharacterType;
  4416. (**
  4417. if expression x is of real type then return true else report error and return false
  4418. **)
  4419. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4420. VAR result: BOOLEAN;
  4421. BEGIN
  4422. result := FALSE;
  4423. IF x = SyntaxTree.invalidExpression THEN
  4424. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4425. Error(x.position,Diagnostics.Invalid,"is no float type");
  4426. ELSE result := TRUE
  4427. END;
  4428. RETURN result
  4429. END CheckRealType;
  4430. (**
  4431. if expression x is of range type then return true else report error and return false
  4432. **)
  4433. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4434. VAR result: BOOLEAN;
  4435. BEGIN
  4436. result := FALSE;
  4437. IF x = SyntaxTree.invalidExpression THEN
  4438. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4439. Error(x.position,Diagnostics.Invalid,"is no range type");
  4440. ELSE result := TRUE
  4441. END;
  4442. RETURN result
  4443. END CheckRangeType;
  4444. (**
  4445. if expression x is of boolean type then return true else report error and return false
  4446. **)
  4447. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4448. VAR result: BOOLEAN;
  4449. BEGIN
  4450. result := FALSE;
  4451. IF x = SyntaxTree.invalidExpression THEN
  4452. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4453. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4454. ELSE result := TRUE
  4455. END;
  4456. RETURN result
  4457. END CheckBooleanType;
  4458. (**
  4459. if expression x is of set type then return true else report error and return false
  4460. **)
  4461. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4462. VAR result: BOOLEAN;
  4463. BEGIN
  4464. result := FALSE;
  4465. IF x = SyntaxTree.invalidExpression THEN
  4466. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4467. Error(x.position,Diagnostics.Invalid,"is no set type");
  4468. ELSE result := TRUE
  4469. END;
  4470. RETURN result
  4471. END CheckSetType;
  4472. (**
  4473. if expression x is of string or array of character type then return true else report error and return false
  4474. **)
  4475. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4476. VAR result: BOOLEAN;
  4477. BEGIN
  4478. result := FALSE;
  4479. IF x = SyntaxTree.invalidExpression THEN
  4480. ELSIF ~IsStringType(x.type.resolved) THEN
  4481. Error(x.position,Diagnostics.Invalid,"is no string type");
  4482. ELSE result := TRUE
  4483. END;
  4484. RETURN result
  4485. END CheckStringType;
  4486. (**
  4487. if expression x is a type declaration type return true else report error and return false
  4488. **)
  4489. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4490. VAR result: BOOLEAN;
  4491. BEGIN
  4492. result := FALSE;
  4493. IF x = SyntaxTree.invalidExpression THEN
  4494. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4495. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4496. ELSE result := TRUE
  4497. END;
  4498. RETURN result
  4499. END CheckTypeDeclarationType;
  4500. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4501. VAR result: BOOLEAN;
  4502. BEGIN
  4503. result := FALSE;
  4504. IF x = SyntaxTree.invalidExpression THEN
  4505. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4506. result := TRUE;
  4507. value := x.resolved(SyntaxTree.IntegerValue).value;
  4508. ELSE
  4509. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4510. END;
  4511. RETURN result;
  4512. END CheckIntegerValue;
  4513. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4514. VAR result: BOOLEAN;
  4515. BEGIN
  4516. result := FALSE;
  4517. IF x = SyntaxTree.invalidExpression THEN
  4518. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4519. result := TRUE;
  4520. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4521. ELSE
  4522. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4523. END;
  4524. RETURN result;
  4525. END CheckStringValue;
  4526. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4527. BEGIN
  4528. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4529. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4530. ELSE
  4531. RETURN FALSE
  4532. END;
  4533. END IsUnsignedValue;
  4534. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4535. BEGIN
  4536. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4537. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4538. ELSE
  4539. RETURN FALSE
  4540. END
  4541. END IsAddressValue;
  4542. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4543. BEGIN
  4544. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4545. END IsAddressExpression;
  4546. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4547. BEGIN
  4548. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4549. END IsSizeExpression;
  4550. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4551. VAR result: BOOLEAN;
  4552. BEGIN
  4553. result := FALSE;
  4554. IF x = SyntaxTree.invalidExpression THEN
  4555. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4556. result := TRUE;
  4557. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4558. ELSE
  4559. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4560. END;
  4561. RETURN result;
  4562. END CheckEnumerationValue;
  4563. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4564. VAR result: BOOLEAN;
  4565. BEGIN
  4566. result := FALSE;
  4567. IF x = SyntaxTree.invalidExpression THEN
  4568. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4569. result := TRUE;
  4570. value := x.resolved(SyntaxTree.CharacterValue).value;
  4571. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4572. result := TRUE;
  4573. value := x.resolved(SyntaxTree.StringValue).value[0];
  4574. ELSE
  4575. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4576. END;
  4577. RETURN result;
  4578. END CheckCharacterValue;
  4579. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4580. VAR result: BOOLEAN;
  4581. BEGIN
  4582. result := FALSE;
  4583. IF x = SyntaxTree.invalidExpression THEN
  4584. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4585. value := x.resolved(SyntaxTree.IntegerValue).value;
  4586. IF (value > 0) OR includeZero & (value = 0) THEN
  4587. result := TRUE;
  4588. ELSE
  4589. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4590. END
  4591. ELSE
  4592. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4593. END;
  4594. RETURN result;
  4595. END CheckPositiveIntegerValue;
  4596. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4597. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4598. BEGIN
  4599. result := FALSE;
  4600. IF x = SyntaxTree.invalidExpression THEN
  4601. ELSE
  4602. type := x.type.resolved;
  4603. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4604. portType := type(SyntaxTree.PortType);
  4605. result := TRUE
  4606. ELSE
  4607. Error(x.position,Diagnostics.Invalid,"no port type");
  4608. END;
  4609. END;
  4610. RETURN result
  4611. END CheckPortType;
  4612. (* move to builtin procedure call statement ?
  4613. remove builtin procedure call designator ?
  4614. *)
  4615. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4616. VAR
  4617. numberActualParameters,numberFormalParameters: LONGINT;
  4618. formalParameter: SyntaxTree.Parameter;
  4619. actualParameter: SyntaxTree.Expression;
  4620. procedureType: SyntaxTree.ProcedureType;
  4621. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4622. inPort, outPort: SyntaxTree.PortType;
  4623. constructor: SyntaxTree.Procedure;
  4624. type0,type1,type2: SyntaxTree.Type;
  4625. type,base,parameterType: SyntaxTree.Type;
  4626. arrayType: SyntaxTree.ArrayType;
  4627. i,i0,i1: LONGINT;
  4628. r,r0,r1,im: LONGREAL;
  4629. c: CHAR;
  4630. id: LONGINT;
  4631. b: BOOLEAN;
  4632. first: LONGINT;
  4633. mathArrayType: SyntaxTree.MathArrayType;
  4634. customBuiltin: SyntaxTree.CustomBuiltin;
  4635. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4636. VAR resultB: BOOLEAN;
  4637. BEGIN
  4638. IF numberActualParameters < from THEN
  4639. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4640. result := SyntaxTree.invalidExpression;
  4641. resultB := FALSE;
  4642. ELSIF numberActualParameters > to THEN
  4643. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4644. result := SyntaxTree.invalidExpression;
  4645. resultB := FALSE;
  4646. ELSE
  4647. resultB := TRUE;
  4648. END;
  4649. RETURN resultB
  4650. END CheckArity;
  4651. BEGIN
  4652. type := NIL; result := NIL;
  4653. type0 := NIL; type1 := NIL; type2 := NIL;
  4654. numberActualParameters := actualParameters.Length();
  4655. IF numberActualParameters>0 THEN
  4656. parameter0 := actualParameters.GetExpression(0);
  4657. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4658. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4659. result := SyntaxTree.invalidExpression
  4660. END
  4661. END;
  4662. IF numberActualParameters >1 THEN
  4663. parameter1 := actualParameters.GetExpression(1);
  4664. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4665. ELSE
  4666. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4667. result := SyntaxTree.invalidExpression
  4668. END
  4669. END;
  4670. IF numberActualParameters >2 THEN
  4671. parameter2 := actualParameters.GetExpression(2);
  4672. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4673. ELSE
  4674. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4675. result := SyntaxTree.invalidExpression
  4676. END
  4677. END;
  4678. IF returnType # NIL THEN
  4679. id := Global.New;
  4680. result := NIL;
  4681. ELSE
  4682. id := builtin.id;
  4683. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4684. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4685. END;
  4686. END;
  4687. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4688. ELSIF result # NIL THEN type := result.type (* operator *)
  4689. ELSE
  4690. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4691. result(SyntaxTree.Designator).SetLeft(left);
  4692. IF returnType # NIL THEN
  4693. type := returnType;
  4694. END;
  4695. (* ---- ASSERT ----- *)
  4696. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4697. IF CheckBooleanType(parameter0) THEN
  4698. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4699. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4700. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4701. *)
  4702. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4703. Error(position,Diagnostics.Invalid,"assert failed");
  4704. END;
  4705. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4706. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4707. rules imposed by the architecture / current runtime
  4708. *)
  4709. END;
  4710. END;
  4711. (* ---- COPY ----- *)
  4712. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4713. IF~IsStringType(type0) THEN
  4714. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4715. END;
  4716. IF ~IsStringType(type1) THEN
  4717. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4718. ELSIF CheckVariable(parameter1) THEN
  4719. IF (type0 IS SyntaxTree.StringType) THEN
  4720. arrayType := type1(SyntaxTree.ArrayType);
  4721. IF arrayType.form = SyntaxTree.Static THEN
  4722. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4723. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4724. END;
  4725. END;
  4726. END;
  4727. END;
  4728. (* ---- INC, DEC----- *)
  4729. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4730. IF numberActualParameters = 1 THEN
  4731. parameter1 :=Global.NewIntegerValue(system,position,1);
  4732. actualParameters.AddExpression(parameter1);
  4733. END;
  4734. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4735. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4736. Error(position,Diagnostics.Invalid,"incompatible increment");
  4737. ELSE
  4738. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4739. actualParameters.SetExpression(1,parameter1);
  4740. END;
  4741. END;
  4742. (* ---- EXCL, INCL----- *)
  4743. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4744. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4745. IF IsIntegerValue(parameter1,i0) THEN
  4746. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4747. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4748. END;
  4749. END;
  4750. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4751. actualParameters.SetExpression(1,parameter1);
  4752. END;
  4753. (* ---- HALT, SYSTEM.HALT ----- *)
  4754. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4755. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4756. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4757. rules imposed by the architecture / current runtime
  4758. *)
  4759. END;
  4760. (* ---- WAIT ----- *)
  4761. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4762. IF CheckObjectType(parameter0) THEN
  4763. END;
  4764. (* ---- NEW ----- *)
  4765. ELSIF (id = Global.New) THEN
  4766. IF returnType # NIL THEN
  4767. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4768. ELSE
  4769. first := 1;
  4770. END;
  4771. IF CheckArity(first,Infinity) THEN
  4772. IF currentIsRealtime THEN
  4773. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4774. END;
  4775. (* check constructor *)
  4776. IF (first =0) OR CheckVariable(parameter0) THEN
  4777. IF type0 IS SyntaxTree.PointerType THEN
  4778. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4779. ELSIF type0 IS SyntaxTree.CellType THEN
  4780. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4781. ELSE
  4782. Error(position, Diagnostics.Invalid, "forbidden new on value type");
  4783. END;
  4784. IF type0 IS SyntaxTree.ArrayType THEN
  4785. arrayType := type0(SyntaxTree.ArrayType);
  4786. IF arrayType.form = SyntaxTree.Static THEN
  4787. i := first
  4788. ELSIF arrayType.form = SyntaxTree.Open THEN
  4789. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4790. ELSE HALT(100)
  4791. END;
  4792. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4793. i := first;
  4794. REPEAT
  4795. actualParameter := actualParameters.GetExpression(i);
  4796. IF CheckSizeType(actualParameter) THEN
  4797. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4798. actualParameters.SetExpression(i,actualParameter);
  4799. END;
  4800. INC(i);
  4801. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4802. END;
  4803. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4804. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4805. IF constructor = NIL THEN
  4806. IF CheckArity(first,first) THEN END;
  4807. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4808. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4809. ELSE
  4810. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4811. numberFormalParameters := procedureType.numberParameters;
  4812. IF numberActualParameters-first <= numberFormalParameters THEN
  4813. formalParameter := procedureType.firstParameter;
  4814. FOR i := first TO numberActualParameters-1 DO
  4815. actualParameter := actualParameters.GetExpression(i);
  4816. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4817. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4818. ELSE
  4819. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4820. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4821. END;
  4822. actualParameters.SetExpression(i,actualParameter);
  4823. END;
  4824. formalParameter := formalParameter.nextParameter;
  4825. END;
  4826. WHILE (formalParameter # NIL) DO
  4827. IF formalParameter.defaultValue # NIL THEN
  4828. actualParameters.AddExpression(formalParameter.defaultValue);
  4829. formalParameter := formalParameter.nextParameter
  4830. ELSE
  4831. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4832. formalParameter := NIL;
  4833. END;
  4834. END;
  4835. ELSE
  4836. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4837. END;
  4838. END;
  4839. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4840. mathArrayType := type0(SyntaxTree.MathArrayType);
  4841. IF mathArrayType.form = SyntaxTree.Static THEN
  4842. Error(position,Diagnostics.Invalid,"new on static array");
  4843. ELSE
  4844. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4845. i0 := first+1; i1 := Infinity;
  4846. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4847. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4848. i1 := i0;
  4849. ELSE HALT(100);
  4850. END;
  4851. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4852. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4853. base := ArrayBase(type0,MAX(LONGINT));
  4854. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4855. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4856. IF ~CompatibleTo(system,type0,parameterType) THEN
  4857. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4858. result := SyntaxTree.invalidExpression;
  4859. ELSE
  4860. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4861. END;
  4862. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4863. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4864. IF ~CompatibleTo(system,type1,parameterType) THEN
  4865. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4866. result := SyntaxTree.invalidExpression;
  4867. ELSE
  4868. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4869. END;
  4870. ELSE
  4871. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4872. i := first;
  4873. REPEAT
  4874. actualParameter := actualParameters.GetExpression(i);
  4875. IF CheckSizeType(actualParameter) THEN
  4876. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4877. actualParameters.SetExpression(i,actualParameter);
  4878. END;
  4879. INC(i);
  4880. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4881. END;
  4882. END;
  4883. END;
  4884. ELSIF type0 IS SyntaxTree.CellType THEN
  4885. IF ~(currentIsCellNet) THEN
  4886. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4887. ELSE
  4888. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4889. IF (constructor = NIL) & CheckArity(1,1) THEN
  4890. (* ok *)
  4891. ELSE
  4892. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4893. numberFormalParameters := procedureType.numberParameters;
  4894. DEC(numberActualParameters);
  4895. IF numberActualParameters <= numberFormalParameters THEN
  4896. formalParameter := procedureType.firstParameter;
  4897. FOR i := first TO numberActualParameters DO
  4898. actualParameter := actualParameters.GetExpression(i);
  4899. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4900. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4901. ELSE
  4902. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4903. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4904. END;
  4905. actualParameters.SetExpression(i,actualParameter);
  4906. END;
  4907. formalParameter := formalParameter.nextParameter;
  4908. END;
  4909. WHILE (formalParameter # NIL) DO
  4910. IF formalParameter.defaultValue # NIL THEN
  4911. actualParameters.AddExpression(formalParameter.defaultValue);
  4912. formalParameter := formalParameter.nextParameter
  4913. ELSE
  4914. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4915. formalParameter := NIL;
  4916. END;
  4917. END;
  4918. ELSE
  4919. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4920. END;
  4921. END;
  4922. END;
  4923. activeCellsStatement := TRUE;
  4924. ELSE
  4925. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4926. END;
  4927. END;
  4928. END;
  4929. (* ---- DISPOSE ----- *)
  4930. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4931. IF ~IsPointerType(parameter0.type) THEN
  4932. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4933. ELSIF ~IsDisposable(parameter0.type) THEN
  4934. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4935. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4936. END
  4937. (* ---- GETPROCEDURE ----- *)
  4938. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4939. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4940. IF CheckVariable(parameter2) THEN
  4941. IF ~GetProcedureAllowed(parameter2.type) THEN
  4942. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4943. END;
  4944. END;
  4945. END;
  4946. (* ---- ABS ----- *)
  4947. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4948. (* note: ABS on complex numbers is done using overloading *)
  4949. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4950. type := type0;
  4951. IF IsIntegerValue(parameter0,i0) THEN
  4952. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4953. type := Global.GetIntegerType(system,ABS(i0));
  4954. ELSIF IsRealValue(parameter0,r) THEN
  4955. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4956. END;
  4957. ELSE
  4958. type := SyntaxTree.invalidType;
  4959. END;
  4960. (* ---- ASH, ASR ----- *)
  4961. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4962. type := type0;
  4963. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4964. (*
  4965. ConvertOperands(parameter0,parameter1); (* same type *)
  4966. *)
  4967. type := parameter0.type;
  4968. IF IsIntegerValue(parameter0,i0) THEN
  4969. IF IsIntegerValue(parameter1,i1) THEN
  4970. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4971. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4972. result := ResolveExpression(result);
  4973. type := Global.GetIntegerType(system,i0);
  4974. END;
  4975. END;
  4976. IF type.resolved.sizeInBits < 32 THEN
  4977. type := system.longintType;
  4978. END;
  4979. (*!compatibility with release, remove when resolved
  4980. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4981. *)
  4982. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4983. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4984. actualParameters.SetExpression(0,parameter0);
  4985. actualParameters.SetExpression(1,parameter1);
  4986. END;
  4987. (* ---- CAP ----- *)
  4988. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4989. type := system.characterType;
  4990. IF CheckCharacterType (parameter0) THEN
  4991. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4992. actualParameters.SetExpression(0,parameter0);
  4993. IF IsCharacterValue(parameter0,c) THEN
  4994. IF (c <= "z") & (c >= "a") THEN
  4995. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4996. ELSE
  4997. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4998. END;
  4999. END;
  5000. END;
  5001. (* ---- CHR ----- *)
  5002. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5003. IF id = Global.Chr THEN
  5004. type := system.characterType
  5005. ELSE
  5006. type := system.characterType32
  5007. END;
  5008. IF CheckIntegerType(parameter0) THEN
  5009. IF IsIntegerValue(parameter0,i0) THEN
  5010. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5011. result := ResolveExpression(result);
  5012. ELSE
  5013. (*
  5014. result := NewConversion(parameter0.position,parameter0,type);
  5015. *)
  5016. END;
  5017. END
  5018. (* ---- ENTIER ----- *)
  5019. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5020. type := system.longintType;
  5021. IF CheckRealType(parameter0) THEN
  5022. IF IsRealValue(parameter0,r) THEN
  5023. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5024. type := Global.GetIntegerType(system,ENTIER(r));
  5025. END
  5026. END;
  5027. (* ---- ENTIERH ----- *)
  5028. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5029. type := system.hugeintType;
  5030. IF CheckRealType(parameter0) THEN
  5031. IF IsRealValue(parameter0,r) THEN
  5032. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5033. END
  5034. END;
  5035. (* ---- LEN ----- *)
  5036. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5037. type := system.longintType;
  5038. base := type0;
  5039. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5040. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5041. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5042. END;
  5043. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5044. actualParameters.SetExpression(0,parameter0);
  5045. type0 := parameter0.type.resolved;
  5046. base := type0;
  5047. END;
  5048. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5049. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5050. IF i1 < 0 THEN
  5051. Error(position,Diagnostics.Invalid,"invalid dimension");
  5052. base := SyntaxTree.invalidType;
  5053. ELSE
  5054. base := ArrayBase(base,i1);
  5055. IF (base # NIL) & Indexable(base) THEN
  5056. ELSE
  5057. Error(position,Diagnostics.Invalid,"len on no array");
  5058. IF VerboseErrorMessage THEN
  5059. Printout.Info("base",base);
  5060. END;
  5061. base := SyntaxTree.invalidType;
  5062. END;
  5063. END;
  5064. IF numberActualParameters=2 THEN
  5065. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5066. actualParameters.SetExpression(1,parameter1);
  5067. ELSIF base IS SyntaxTree.MathArrayType THEN
  5068. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5069. END;
  5070. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5071. IF base IS SyntaxTree.ArrayType THEN
  5072. arrayType := base(SyntaxTree.ArrayType);
  5073. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5074. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5075. result := Global.NewIntegerValue(system,position,i);
  5076. type := result.type;(* arrayType.length.type;*)
  5077. ASSERT(type # NIL);
  5078. END;
  5079. ELSIF base IS SyntaxTree.MathArrayType THEN
  5080. mathArrayType := base(SyntaxTree.MathArrayType);
  5081. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5082. result := Global.NewIntegerValue(system,position,i);
  5083. type := result.type;
  5084. (*
  5085. type := mathArrayType.length.type;
  5086. *)
  5087. ASSERT(type # NIL);
  5088. END;
  5089. END;
  5090. END;
  5091. ELSE
  5092. type := system.longintType;
  5093. END;
  5094. (* ---- FIRST ---- *)
  5095. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5096. type := system.longintType;
  5097. IF CheckRangeType(parameter0) THEN END;
  5098. result.SetAssignable(parameter0.assignable)
  5099. (* ---- LAST ---- *)
  5100. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5101. type := system.longintType;
  5102. IF CheckRangeType(parameter0) THEN END;
  5103. result.SetAssignable(parameter0.assignable)
  5104. (* ---- STEP ---- *)
  5105. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5106. type := system.longintType;
  5107. IF CheckRangeType(parameter0) THEN END;
  5108. result.SetAssignable(parameter0.assignable)
  5109. (* ---- RE ---- *)
  5110. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5111. IF CheckNumberType(parameter0) THEN
  5112. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5113. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5114. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5115. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5116. type := parameter0.type
  5117. ELSE
  5118. type := system.realType
  5119. END
  5120. END;
  5121. result.SetAssignable(parameter0.assignable)
  5122. (* ---- IM ---- *)
  5123. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5124. IF CheckNumberType(parameter0) THEN
  5125. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5126. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5127. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5128. ELSE
  5129. type := system.realType;
  5130. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5131. END
  5132. END;
  5133. result.SetAssignable(parameter0.assignable)
  5134. (* ---- MAX ----- *)
  5135. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5136. IF numberActualParameters = 1 THEN
  5137. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5138. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5139. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5140. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5141. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5142. *)
  5143. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5144. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5145. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5146. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5147. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5148. END;
  5149. ELSE
  5150. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5151. END
  5152. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5153. ConvertOperands(parameter0,parameter1);
  5154. actualParameters.SetExpression(0,parameter0);
  5155. actualParameters.SetExpression(1,parameter1);
  5156. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5157. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5158. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5159. END;
  5160. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5161. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5162. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5163. END;
  5164. END;
  5165. type := parameter0.type;
  5166. ELSE type := SyntaxTree.invalidType;
  5167. END;
  5168. (* ---- MIN ----- *)
  5169. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5170. IF numberActualParameters = 1 THEN
  5171. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5172. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5173. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5174. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5175. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5176. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5177. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5178. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5179. END;
  5180. ELSE
  5181. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5182. END
  5183. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5184. ConvertOperands(parameter0,parameter1);
  5185. actualParameters.SetExpression(0,parameter0);
  5186. actualParameters.SetExpression(1,parameter1);
  5187. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5188. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5189. ELSE result.SetResolved(parameter1.resolved)
  5190. END;
  5191. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5192. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5193. ELSE result.SetResolved(parameter1.resolved)
  5194. END;
  5195. END;
  5196. type := parameter0.type;
  5197. ELSE type := SyntaxTree.invalidType;
  5198. END;
  5199. (* ---- ODD ----- *)
  5200. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5201. type := system.booleanType;
  5202. IF CheckIntegerType(parameter0) THEN
  5203. IF IsIntegerValue(parameter0,i0) THEN
  5204. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5205. type := system.booleanType;
  5206. END;
  5207. END;
  5208. (* ---- ORD ----- *)
  5209. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5210. IF id = Global.Ord THEN
  5211. type := system.integerType;
  5212. ELSE
  5213. type := system.longintType;
  5214. END;
  5215. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5216. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5217. actualParameters.SetExpression(0,parameter0);
  5218. (* IF CheckCharacterType(parameter0) THEN*)
  5219. IF IsCharacterValue(parameter0,c)THEN
  5220. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5221. type := Global.GetSignedIntegerType(system,ORD(c));
  5222. END;
  5223. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5224. END;
  5225. (* ---- SHORT ----- *)
  5226. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5227. type := type0;
  5228. IF IsSignedIntegerType(type) THEN
  5229. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5230. ELSIF type = system.integerType THEN type := system.shortintType
  5231. ELSIF type = system.longintType THEN type := system.integerType
  5232. ELSIF type = system.hugeintType THEN type:= system.longintType
  5233. ELSE
  5234. CASE type.sizeInBits OF
  5235. 16: type := Global.Integer8
  5236. |32: type := Global.Integer16
  5237. |64: type := Global.Integer32
  5238. END;
  5239. END;
  5240. ELSIF type IS SyntaxTree.FloatType THEN
  5241. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5242. ELSIF type = system.longrealType THEN type := system.realType
  5243. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5244. END;
  5245. ELSIF type IS SyntaxTree.ComplexType THEN
  5246. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5247. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5248. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5249. END;
  5250. ELSE
  5251. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5252. END;
  5253. IF (parameter0.resolved # NIL) THEN
  5254. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5255. IF parameter0 IS SyntaxTree.Value THEN
  5256. result.SetResolved(parameter0(SyntaxTree.Value));
  5257. END;
  5258. END;
  5259. (* ---- LONG ----- *)
  5260. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5261. type := type0;
  5262. IF IsSignedIntegerType(type) THEN
  5263. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5264. ELSIF type = system.longintType THEN type := system.hugeintType
  5265. ELSIF type = system.integerType THEN type := system.longintType
  5266. ELSIF type = system.shortintType THEN type := system.integerType
  5267. ELSE
  5268. CASE type.sizeInBits OF
  5269. 8: type := Global.Integer16
  5270. |16: type := Global.Integer32
  5271. |32: type := Global.Integer64
  5272. END;
  5273. END;
  5274. ELSIF type IS SyntaxTree.FloatType THEN
  5275. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5276. ELSIF type= system.realType THEN type := system.longrealType
  5277. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5278. END;
  5279. ELSIF type IS SyntaxTree.ComplexType THEN
  5280. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5281. ELSIF type = system.complexType THEN type := system.longcomplexType
  5282. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5283. END;
  5284. ELSE
  5285. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5286. END;
  5287. IF (parameter0.resolved # NIL) THEN
  5288. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5289. IF parameter0 IS SyntaxTree.Value THEN
  5290. result.SetResolved(parameter0(SyntaxTree.Value));
  5291. END;
  5292. END;
  5293. (* ---- SIZE OF ----- *)
  5294. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5295. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5296. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5297. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5298. type := system.integerType;
  5299. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5300. ELSE
  5301. (* for variables, system sizeof could represent the physically occupied size
  5302. determined via the type descriptor, implement that ? *)
  5303. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5304. END
  5305. (* ---- SYSTEM.TRACE -----*)
  5306. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5307. FOR i := 0 TO numberActualParameters-1 DO
  5308. parameter0 := actualParameters.GetExpression(i);
  5309. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5310. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5311. END;
  5312. END;
  5313. (* remaining issues can only be tested in backend *)
  5314. (* ---- ADDRESSOF----- *)
  5315. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5316. IF HasAddress(parameter0) THEN
  5317. type := system.addressType;
  5318. ELSE
  5319. type := SyntaxTree.invalidType;
  5320. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5321. END;
  5322. (* ---- BIT ----- *)
  5323. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5324. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5325. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5326. actualParameters.SetExpression(0,parameter0);
  5327. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5328. actualParameters.SetExpression(1,parameter1);
  5329. END;
  5330. type := system.booleanType;
  5331. (* ----- MSK ---- *)
  5332. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5333. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5334. ConvertOperands(parameter0,parameter1);
  5335. actualParameters.SetExpression(0,parameter0);
  5336. actualParameters.SetExpression(1,parameter1);
  5337. END;
  5338. type := parameter0.type;
  5339. (* ---- SYSTEM.GET64 ----- *)
  5340. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5341. IF CheckAddressType(parameter0) THEN
  5342. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5343. actualParameters.SetExpression(0,parameter0);
  5344. END;
  5345. type := system.hugeintType;
  5346. (* ---- SYSTEM.GET32 ----- *)
  5347. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5348. IF CheckAddressType(parameter0) THEN
  5349. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5350. actualParameters.SetExpression(0,parameter0);
  5351. END;
  5352. type := system.longintType;
  5353. (* ---- SYSTEM.GET16 ----- *)
  5354. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5355. IF CheckAddressType(parameter0) THEN
  5356. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5357. actualParameters.SetExpression(0,parameter0);
  5358. END;
  5359. type := system.integerType;
  5360. (* ---- SYSTEM.GET8 ----- *)
  5361. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5362. IF CheckAddressType(parameter0) THEN
  5363. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5364. actualParameters.SetExpression(0,parameter0);
  5365. END;
  5366. type := system.shortintType;
  5367. (* ---- SYSTEM.GetStackPointer ----- *)
  5368. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5369. type := system.addressType;
  5370. (* ---- SYSTEM.GetFramePointer ----- *)
  5371. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5372. type := system.addressType;
  5373. (* ---- SYSTEM.GetActivity ----- *)
  5374. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5375. type := system.objectType;
  5376. (* ---- SYSTEM.SetStackPointer ----- *)
  5377. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5378. IF CheckAddressType(parameter0) THEN
  5379. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5380. actualParameters.SetExpression(0,parameter0);
  5381. END;
  5382. (* ---- SYSTEM.SetFramePointer ----- *)
  5383. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5384. IF CheckAddressType(parameter0) THEN
  5385. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5386. actualParameters.SetExpression(0,parameter0);
  5387. END;
  5388. (* ---- SYSTEM.SetActivity ----- *)
  5389. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5390. IF CheckObjectType(parameter0) THEN
  5391. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5392. actualParameters.SetExpression(0,parameter0);
  5393. END;
  5394. (* ---- LSH, LSL, ROT, ROR ----- *)
  5395. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5396. type := type0;
  5397. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5398. actualParameters.SetExpression(1, parameter1);
  5399. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5400. IF id = Global.Lsh THEN
  5401. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5402. ELSIF id = Global.Rot THEN
  5403. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5404. ELSIF id = Global.Ror THEN
  5405. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5406. END;
  5407. END;
  5408. (* ---- SYSTEM.VAL ----- *)
  5409. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5410. IF CheckTypeDeclarationType(parameter0) THEN
  5411. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5412. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5413. result := SyntaxTree.invalidExpression;
  5414. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5415. ELSE
  5416. IF (parameter1.resolved # NIL) THEN
  5417. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5418. IF parameter0 IS SyntaxTree.Value THEN
  5419. result.SetResolved(parameter0(SyntaxTree.Value));
  5420. END;
  5421. END;
  5422. result.SetAssignable(parameter1.assignable);
  5423. END;
  5424. END;
  5425. (* ---- SYSTEM.GET ----- *)
  5426. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5427. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5428. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5429. actualParameters.SetExpression(0,parameter0);
  5430. END;
  5431. (* ---- SYSTEM.PUT ----- *)
  5432. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5433. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5434. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5435. actualParameters.SetExpression(0,parameter0);
  5436. END;
  5437. (* ---- SYSTEM.PUT64 ----- *)
  5438. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5439. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5440. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5441. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5442. actualParameters.SetExpression(0,parameter0);
  5443. actualParameters.SetExpression(1,parameter1);
  5444. END;
  5445. (* ---- SYSTEM.PUT32 ----- *)
  5446. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5447. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5448. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5449. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5450. actualParameters.SetExpression(0,parameter0);
  5451. actualParameters.SetExpression(1,parameter1);
  5452. END;
  5453. (* ---- SYSTEM.PUT16 ----- *)
  5454. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5455. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5456. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5457. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5458. actualParameters.SetExpression(0,parameter0);
  5459. actualParameters.SetExpression(1,parameter1);
  5460. END;
  5461. (* ---- SYSTEM.PUT8 ----- *)
  5462. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5463. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5464. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5465. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5466. actualParameters.SetExpression(0,parameter0);
  5467. actualParameters.SetExpression(1,parameter1);
  5468. END;
  5469. (* ---- SYSTEM.MOVE ----- *)
  5470. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5471. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5472. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5473. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5474. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5475. actualParameters.SetExpression(0,parameter0);
  5476. actualParameters.SetExpression(1,parameter1);
  5477. actualParameters.SetExpression(2,parameter2);
  5478. END;
  5479. (* ---- SYSTEM.NEW ----- *)
  5480. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5481. IF ~IsPointerType(parameter0.type) THEN
  5482. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5483. ELSIF CheckSizeType(parameter1) THEN
  5484. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5485. actualParameters.SetExpression(1,parameter1);
  5486. END;
  5487. (* ----SYSTEM.REF ---- *)
  5488. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5489. type := system.addressType
  5490. (* ---- INCR ----- *)
  5491. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5492. type := system.sizeType;
  5493. base := type0;
  5494. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5495. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5496. IF i1 < 0 THEN
  5497. Error(position,Diagnostics.Invalid,"invalid dimension");
  5498. base := SyntaxTree.invalidType;
  5499. ELSE
  5500. base := ArrayBase(base,i1);
  5501. IF (base # NIL) & Indexable(base) THEN
  5502. ELSE
  5503. Error(position,Diagnostics.Invalid,"len on no array");
  5504. IF VerboseErrorMessage THEN
  5505. Printout.Info("base",base);
  5506. END;
  5507. base := SyntaxTree.invalidType;
  5508. END;
  5509. END;
  5510. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5511. actualParameters.SetExpression(1,parameter1);
  5512. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5513. mathArrayType := base(SyntaxTree.MathArrayType);
  5514. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5515. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5516. type := system.longintType;
  5517. END;
  5518. END;
  5519. ELSE
  5520. type := system.longintType;
  5521. END;
  5522. (* ---- SUM ----- *)
  5523. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5524. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5525. (* ---- ALL ----- *)
  5526. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5527. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5528. (* ---- DIM ----- *)
  5529. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5530. type := system.sizeType;
  5531. IF type0 IS SyntaxTree.MathArrayType THEN
  5532. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5533. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5534. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5535. END;
  5536. ELSE
  5537. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5538. END;
  5539. (* ---- CAS ----- *)
  5540. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5541. IF type0.IsComposite () THEN
  5542. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5543. result := SyntaxTree.invalidExpression;
  5544. ELSIF ~IsVariable (parameter0) THEN
  5545. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5546. result := SyntaxTree.invalidExpression;
  5547. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5548. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5549. result := SyntaxTree.invalidExpression;
  5550. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5551. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5552. result := SyntaxTree.invalidExpression;
  5553. ELSE
  5554. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5555. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5556. type := type0;
  5557. END;
  5558. (* ---- RESHAPE ----- *)
  5559. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5560. IF type0 IS SyntaxTree.MathArrayType THEN
  5561. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5562. base := ArrayBase(type0,MAX(LONGINT));
  5563. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5564. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5565. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5566. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5567. IF ~CompatibleTo(system,type0,parameterType) THEN
  5568. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5569. result := SyntaxTree.invalidExpression;
  5570. ELSE
  5571. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5572. END;
  5573. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5574. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5575. IF ~CompatibleTo(system,type1,parameterType) THEN
  5576. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5577. result := SyntaxTree.invalidExpression;
  5578. ELSE
  5579. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5580. END;
  5581. ELSE
  5582. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5583. result := SyntaxTree.invalidExpression;
  5584. END;
  5585. (* ---- SYSTEM.TYPECODE ----- *)
  5586. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5587. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5588. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5589. type := type.resolved;
  5590. IF type IS SyntaxTree.PointerType THEN
  5591. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5592. END;
  5593. IF ~(type IS SyntaxTree.RecordType) THEN
  5594. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5595. END;
  5596. ELSE
  5597. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5598. END;
  5599. type := system.addressType;
  5600. (* -------- FLT --------- *)
  5601. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5602. type := system.realType;
  5603. IF IsRealValue(parameter0, r) THEN
  5604. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5605. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5606. i0 := i; i := ABS(i);
  5607. IF i # 0 THEN
  5608. i1 := 23;
  5609. IF i >= 2*800000H THEN
  5610. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5611. ELSIF i < 800000H THEN
  5612. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5613. END;
  5614. i := (i1 + 127)*800000H - 800000H + i;
  5615. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5616. END;
  5617. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5618. END;
  5619. (* ------- CONNECT -------*)
  5620. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5621. IF ~(currentIsCellNet) THEN
  5622. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5623. END;
  5624. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5625. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5626. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5627. END;
  5628. IF numberActualParameters = 3 THEN
  5629. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5630. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5631. END;
  5632. *)
  5633. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5634. actualParameters.SetExpression(2,parameter2);
  5635. END;
  5636. activeCellsStatement := TRUE;
  5637. (* ---------- DELEGATE --------*)
  5638. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5639. IF ~(currentIsCellNet) THEN
  5640. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5641. END;
  5642. IF ~CheckPortType(parameter1, inPort) THEN
  5643. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5644. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5645. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5646. ELSIF (outPort.direction # inPort.direction) THEN
  5647. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5648. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5649. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5650. END;
  5651. activeCellsStatement := TRUE;
  5652. (* --------- RECEIVE ---------*)
  5653. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5654. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5655. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5656. IF inPort.direction # SyntaxTree.InPort THEN
  5657. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5658. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5659. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5660. END;
  5661. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5662. IF ~SameType(parameter2.type, system.integerType) THEN
  5663. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5664. END;
  5665. END;
  5666. END;
  5667. (* --------- SEND ---------*)
  5668. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5669. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5670. IF CheckPortType(parameter0,outPort) THEN
  5671. IF outPort.direction # SyntaxTree.OutPort THEN
  5672. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5673. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5674. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5675. ELSE
  5676. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5677. actualParameters.SetExpression(1,parameter1);
  5678. END;
  5679. END;
  5680. (* ------- custom builtins ----- *)
  5681. ELSIF id = Global.systemSpecial THEN
  5682. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5683. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5684. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5685. type := procedureType.returnType;
  5686. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5687. (* go through all formal parameters *)
  5688. formalParameter := procedureType.firstParameter;
  5689. FOR i := 0 TO actualParameters.Length() - 1 DO
  5690. actualParameter := actualParameters.GetExpression(i);
  5691. IF actualParameter = SyntaxTree.invalidExpression THEN
  5692. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5693. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5694. ELSE
  5695. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5696. END;
  5697. actualParameters.SetExpression(i, actualParameter);
  5698. formalParameter := formalParameter.nextParameter
  5699. END
  5700. END
  5701. ELSE
  5702. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5703. result := SyntaxTree.invalidExpression;
  5704. END;
  5705. END;
  5706. IF result # SyntaxTree.invalidExpression THEN
  5707. type := ResolveType(type);
  5708. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5709. result.SetType(type);
  5710. END;
  5711. RETURN result
  5712. END NewBuiltinCallDesignator;
  5713. (** return type guard designator left(type)
  5714. - check if type can be extended (i.e. is no static record)
  5715. - check if type is a type extension of left.type
  5716. - returns new type guard designator
  5717. returns invalidDesignator = invalidExpression if error
  5718. **)
  5719. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5720. VAR result: SyntaxTree.Designator;
  5721. BEGIN
  5722. result := SyntaxTree.invalidDesignator;
  5723. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5724. Error(position,Diagnostics.Invalid,"no type extension of type");
  5725. IF VerboseErrorMessage THEN
  5726. Printout.Info("left",left);
  5727. Printout.Info("type",type);
  5728. END;
  5729. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5730. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5731. ELSIF IsUnsafePointer(left.type) THEN
  5732. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5733. ELSE
  5734. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5735. result.SetType(type);
  5736. result.SetAssignable(left.assignable);
  5737. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5738. END;
  5739. RETURN result
  5740. END NewTypeGuardDesignator;
  5741. (** check and resolve parameter designator left(expression list)
  5742. - check expression list
  5743. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5744. - elsif left is a procedure type then
  5745. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5746. - else return is a procedure call then return ProcedureCallDesignator
  5747. returns invalidDesignator = invalidExpression if error
  5748. **)
  5749. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5750. VAR
  5751. parameters: SyntaxTree.ExpressionList;
  5752. left: SyntaxTree.Designator;
  5753. result,expression: SyntaxTree.Expression;
  5754. typeDeclaration: SyntaxTree.TypeDeclaration;
  5755. type, expressionType: SyntaxTree.Type;
  5756. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5757. BEGIN
  5758. type := type.resolved;
  5759. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5760. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5761. END;
  5762. RETURN type
  5763. END BaseType;
  5764. BEGIN
  5765. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5766. result := SyntaxTree.invalidDesignator;
  5767. left := ResolveDesignator(designator.left);
  5768. IF left # SyntaxTree.invalidDesignator THEN
  5769. parameters := designator.parameters;
  5770. IF ExpressionList(parameters) THEN
  5771. IF (left.type = NIL) THEN
  5772. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5773. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5774. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5775. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5776. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5777. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5778. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5779. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5780. ELSE
  5781. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5782. END
  5783. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5784. expression := parameters.GetExpression(0);
  5785. type := typeDeclaration.declaredType.resolved;
  5786. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5787. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5788. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5789. OR (expressionType IS SyntaxTree.EnumerationType)
  5790. ) THEN
  5791. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5792. ELSE
  5793. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5794. END;
  5795. ELSE
  5796. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5797. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5798. result := SyntaxTree.invalidDesignator;
  5799. END;
  5800. ELSE
  5801. result := SyntaxTree.invalidDesignator
  5802. END;
  5803. END;
  5804. resolvedExpression := result;
  5805. END VisitParameterDesignator;
  5806. (** check dereference designator left^
  5807. - check if left is pointer type or left is object type
  5808. - return new dereference designator with type = left.baseType.type (if appropriate)
  5809. with error handling
  5810. returns invalidDesignator = invalidExpression if error
  5811. **)
  5812. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5813. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5814. BEGIN
  5815. result := SyntaxTree.invalidDesignator;
  5816. type := left.type;
  5817. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5818. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5819. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5820. result.SetAssignable(TRUE);
  5821. result.SetType(type);
  5822. result.SetHidden(left.isHidden);
  5823. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5824. type := type.resolved;
  5825. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5826. result.SetAssignable(TRUE);
  5827. result.SetType(type);
  5828. result.SetHidden(left.isHidden);
  5829. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5830. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5831. result.SetAssignable(TRUE);
  5832. result.SetType(type);
  5833. result.SetHidden(left.isHidden);
  5834. ELSE
  5835. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5836. IF VerboseErrorMessage THEN
  5837. Printout.Info("pointer", type);
  5838. Printout.Info("scope", currentScope);
  5839. END;
  5840. END;
  5841. RETURN result
  5842. END NewDereferenceDesignator;
  5843. (** check supercall designator left^
  5844. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5845. - return new supercall designator with type = left.type
  5846. with error handling
  5847. **)
  5848. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5849. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5850. objectScope: SyntaxTree.Scope;
  5851. BEGIN
  5852. result := SyntaxTree.invalidDesignator;
  5853. IF left = SyntaxTree.invalidDesignator THEN
  5854. (* error already handled *)
  5855. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5856. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5857. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5858. IF symbol IS SyntaxTree.Procedure THEN
  5859. procedure := symbol(SyntaxTree.Procedure);
  5860. objectScope := currentScope;
  5861. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5862. objectScope := objectScope.outerScope;
  5863. END;
  5864. IF (left.left = NIL) OR ~
  5865. (
  5866. (left.left IS SyntaxTree.SelfDesignator) OR
  5867. (left.left IS SyntaxTree.DereferenceDesignator)
  5868. & (left.left(SyntaxTree.Designator).left # NIL)
  5869. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5870. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5871. IF VerboseErrorMessage THEN
  5872. Printout.Info("left.left",left.left);
  5873. END;
  5874. ELSIF procedure.super # NIL THEN
  5875. result := SyntaxTree.NewSupercallDesignator(position,left);
  5876. result.SetType(left.type.resolved)
  5877. ELSE
  5878. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5879. END;
  5880. ELSE
  5881. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5882. END;
  5883. ELSE
  5884. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5885. END;
  5886. RETURN result
  5887. END NewSupercallDesignator;
  5888. (** check and semantically resolve arrow designator left^
  5889. - if left is procedure type -> result := SupercallDesignator
  5890. - else result := DereferenceDesignator
  5891. returns result via global variable resolvedExpression
  5892. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5893. **)
  5894. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5895. VAR left: SyntaxTree.Designator;
  5896. BEGIN
  5897. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5898. left := ResolveDesignator(arrowDesignator.left);
  5899. IF left # NIL THEN
  5900. IF (left.type = NIL) THEN
  5901. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5902. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5903. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5904. ELSE
  5905. IF IsPointerToObject(left.type) THEN
  5906. Warning(arrowDesignator.position, "forbidden dereference on object");
  5907. END;
  5908. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5909. END
  5910. END
  5911. END VisitArrowDesignator;
  5912. (** check and return expression
  5913. - if expression has no type then resolve expression
  5914. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5915. - return result
  5916. **)
  5917. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5918. VAR result,prev: SyntaxTree.Expression;
  5919. BEGIN
  5920. IF expression = NIL THEN result := NIL
  5921. ELSIF (expression.type = NIL) THEN
  5922. prev := resolvedExpression;
  5923. resolvedExpression := SyntaxTree.invalidExpression;
  5924. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5925. expression.SetType(SyntaxTree.invalidType);
  5926. END;
  5927. expression.Accept(SELF);
  5928. result := resolvedExpression;
  5929. IF currentIsRealtime THEN
  5930. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5931. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5932. END;
  5933. END;
  5934. (* designator modifiers for backends if they support it ...*)
  5935. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5936. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5937. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5938. END;
  5939. resolvedExpression := prev
  5940. ELSE
  5941. result := expression
  5942. END;
  5943. RETURN result
  5944. END ResolveExpression;
  5945. (**
  5946. check expression to be constant expression
  5947. - resolve expression
  5948. - if valid then check that of value type
  5949. report error and return invalidExpression if anything fails
  5950. **)
  5951. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5952. VAR position: LONGINT;
  5953. BEGIN
  5954. position := expression.position;
  5955. expression := ResolveExpression(expression);
  5956. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5957. ELSIF (expression.resolved = NIL) THEN
  5958. Error(position,Diagnostics.Invalid,"expression is not constant");
  5959. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5960. expression := SyntaxTree.invalidExpression;
  5961. END;
  5962. RETURN expression
  5963. END ConstantExpression;
  5964. (** check expression to be constant integer
  5965. - resolve expresssion
  5966. - if valid then check that of integer value type
  5967. report error and return invalidExpression if anything fails
  5968. **)
  5969. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5970. VAR position: LONGINT;
  5971. BEGIN
  5972. position := expression.position;
  5973. expression := ResolveExpression(expression);
  5974. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5975. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5976. expression := SyntaxTree.invalidExpression;
  5977. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5978. END;
  5979. RETURN expression
  5980. END ConstantInteger;
  5981. (** check expression as positive (>=0) constant integer
  5982. - resolve expression
  5983. - if valid then check that integer value
  5984. - if integer value then check that value >= 0
  5985. report error and return invalidExpression if anything fails
  5986. **)
  5987. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5988. VAR position: LONGINT;
  5989. BEGIN
  5990. position := expression.position;
  5991. expression := ConstantExpression(expression);
  5992. IF expression = SyntaxTree.invalidExpression THEN
  5993. (* error already reported *)
  5994. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5995. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5996. expression := SyntaxTree.invalidExpression
  5997. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5998. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5999. END;
  6000. RETURN expression
  6001. END ConstantIntegerGeq0;
  6002. (** check expression as condition
  6003. - resolve expression
  6004. - if valid expression then check that result type is boolean
  6005. report error and return invalidExpression if anything fails
  6006. **)
  6007. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6008. VAR position: LONGINT;
  6009. BEGIN
  6010. position := expression.position;
  6011. expression := ResolveExpression(expression);
  6012. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6013. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6014. expression := SyntaxTree.invalidExpression;
  6015. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6016. END;
  6017. RETURN expression
  6018. END ResolveCondition;
  6019. (*** symbols ***)
  6020. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6021. BEGIN
  6022. x.Accept(SELF);
  6023. END ResolveSymbol;
  6024. (** check a symbol
  6025. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6026. **)
  6027. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6028. VAR scope: SyntaxTree.Scope;
  6029. BEGIN
  6030. (* visibility *)
  6031. scope := symbol.scope;
  6032. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6033. scope := scope.outerScope;
  6034. END;
  6035. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6036. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6037. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6038. IF VerboseErrorMessage THEN
  6039. Printout.Info("symbol",symbol);
  6040. END;
  6041. END;
  6042. END;
  6043. END CheckSymbolVisibility;
  6044. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6045. If node is currently being resolved then emit a cyclic definition error.
  6046. Return TRUE only if node is fully resolved.
  6047. **)
  6048. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6049. VAR result: BOOLEAN;
  6050. BEGIN
  6051. IF SyntaxTree.Resolved IN x.state THEN
  6052. result := FALSE
  6053. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6054. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6055. result := FALSE;
  6056. ELSE
  6057. result := TRUE;
  6058. x.SetState(SyntaxTree.BeingResolved)
  6059. END;
  6060. RETURN result
  6061. END SymbolNeedsResolution;
  6062. (** check and resolve a type declaration symbol = Type
  6063. - set type to declaration type
  6064. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6065. This is so because the type declaration itself does not have a type but it only stands for a type.
  6066. In the implementation of the compiler this made a lot much easier.
  6067. - resolve and set declared type
  6068. - check symbol
  6069. **)
  6070. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6071. VAR prevScope: SyntaxTree.Scope;
  6072. BEGIN
  6073. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6074. IF SymbolNeedsResolution(typeDeclaration) THEN
  6075. prevScope := currentScope;
  6076. currentScope := typeDeclaration.scope;
  6077. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6078. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6079. CheckSymbolVisibility(typeDeclaration);
  6080. typeDeclaration.SetState(SyntaxTree.Resolved);
  6081. currentScope := prevScope;
  6082. END;
  6083. END VisitTypeDeclaration;
  6084. (** check and resolve a constant declaration symbol = (constant) expression
  6085. - check expression
  6086. - set type and value
  6087. - check symbol
  6088. **)
  6089. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6090. VAR
  6091. expression: SyntaxTree.Expression;
  6092. type: SyntaxTree.Type;
  6093. name: Basic.SegmentedName;
  6094. replacement: Replacement;
  6095. BEGIN
  6096. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6097. IF SymbolNeedsResolution(constant) THEN
  6098. expression := constant.value;
  6099. IF replacements # NIL THEN
  6100. Global.GetSymbolSegmentedName(constant, name);
  6101. replacement := replacements;
  6102. WHILE (replacement # NIL) & (replacement.name # name) DO
  6103. replacement := replacement.next;
  6104. END;
  6105. IF replacement # NIL THEN
  6106. InfoSS(constant.position, "replacing constant", constant.name);
  6107. (*
  6108. NEW(stringReader, Strings.Length(replacement.string^));
  6109. stringReader.Set(replacement.string^);
  6110. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6111. NEW(parser, scanner, diagnostics);
  6112. expression := parser.Expression();
  6113. *)
  6114. expression := replacement.expression;
  6115. replacement.used := TRUE;
  6116. END;
  6117. END;
  6118. constant.SetType(SyntaxTree.invalidType);
  6119. expression := ConstantExpression(expression);
  6120. ASSERT(expression.type # NIL);
  6121. type := expression.type.resolved;
  6122. constant.SetType(type);
  6123. constant.SetValue(expression);
  6124. CheckSymbolVisibility(constant);
  6125. constant.SetState(SyntaxTree.Resolved);
  6126. END;
  6127. END VisitConstant;
  6128. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6129. VAR procedureAlignment: LONGINT;
  6130. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6131. (* least common multiple *)
  6132. VAR a,b: LONGINT;
  6133. BEGIN
  6134. a := a0; b := b0;
  6135. WHILE (a # b) DO
  6136. IF a < b THEN a := a+a0
  6137. ELSE b := b + b0
  6138. END;
  6139. END;
  6140. RETURN a
  6141. END LCM;
  6142. BEGIN
  6143. IF alignment > 1 THEN
  6144. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6145. IF (procedureAlignment > 1) THEN
  6146. alignment := LCM(alignment, procedureAlignment);
  6147. END;
  6148. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6149. END;
  6150. END AdaptStackAlignment;
  6151. (** check and resolve a variable / field
  6152. - check and set type
  6153. - negative check on open array type
  6154. - check symbol
  6155. **)
  6156. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6157. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6158. BEGIN
  6159. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6160. IF SymbolNeedsResolution(variable) THEN
  6161. modifiers := variable.modifiers;
  6162. (*
  6163. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6164. variable.AddFlags(flags);
  6165. *)
  6166. variable.SetType(ResolveType(variable.type));
  6167. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6168. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6169. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6170. END;
  6171. END;
  6172. CheckSymbolVisibility(variable);
  6173. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6174. variable.SetUntraced(TRUE);
  6175. IF ~ContainsPointer(variable.type) THEN
  6176. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6177. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6178. END;
  6179. END;
  6180. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6181. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6182. IF ~PowerOf2(value) THEN
  6183. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6184. ELSE
  6185. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6186. END;
  6187. END;
  6188. variable.SetAlignment(FALSE,value);
  6189. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6190. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6191. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6192. END;
  6193. variable.SetAlignment(TRUE, value);
  6194. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6195. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6196. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6197. END;
  6198. variable.SetFictive(value);
  6199. variable.SetOffset(value*system.dataUnit);
  6200. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6201. END;
  6202. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6203. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6204. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6205. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6206. END;
  6207. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6208. IF variable.initializer # NIL THEN
  6209. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6210. END;
  6211. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6212. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6213. pointerType.SetPointerBase(variable.type);
  6214. pointerType.SetHidden(TRUE);
  6215. variable.SetType(ResolveType(pointerType));
  6216. END;
  6217. variable.SetState(SyntaxTree.Resolved);
  6218. END;
  6219. END VisitVariable;
  6220. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6221. BEGIN
  6222. VisitVariable(property)
  6223. END VisitProperty;
  6224. (** check and resolve a (procedure) parameter
  6225. - check and set type
  6226. - check symbol
  6227. - check parameter kind and set read-only flags if appropriate
  6228. **)
  6229. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6230. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6231. BEGIN
  6232. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6233. IF SymbolNeedsResolution(parameter) THEN
  6234. modifiers := parameter.modifiers;
  6235. parameter.SetType(ResolveType(parameter.type));
  6236. ASSERT(parameter.type.resolved # NIL);
  6237. CheckSymbolVisibility(parameter);
  6238. IF parameter.defaultValue # NIL THEN
  6239. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6240. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6241. ELSE
  6242. expression := ConstantExpression(parameter.defaultValue);
  6243. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6244. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6245. parameter.SetDefaultValue(expression);
  6246. END;
  6247. END;
  6248. END;
  6249. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6250. parameter.SetUntraced(TRUE);
  6251. IF ~ContainsPointer(parameter.type) THEN
  6252. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6253. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6254. END;
  6255. END;
  6256. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6257. parameter.SetMoveable(TRUE);
  6258. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6259. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6260. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6261. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6262. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6263. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6264. END;
  6265. END;
  6266. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6267. parameter.SetState(SyntaxTree.Resolved);
  6268. END;
  6269. END VisitParameter;
  6270. (** check and resolve a procedure (with declaration and implementation scope)
  6271. - check the procedure type
  6272. - check if method (i.e. in record scope), if so then
  6273. - check if (unique) constructor
  6274. - check if (unique) finalizer
  6275. - check if super method available, if so then check signature
  6276. - of not in record scope then negative check on constructor flag
  6277. - of not in record scope then negative check on finalizer flag
  6278. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6279. - check procedure symbol
  6280. **)
  6281. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6282. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6283. procedureType: SyntaxTree.ProcedureType;
  6284. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6285. qualifiedType: SyntaxTree.QualifiedType;
  6286. value: LONGINT;
  6287. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6288. position: LONGINT;
  6289. BEGIN
  6290. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6291. IF IsOberonInline(procedure) THEN
  6292. IF SyntaxTree.Public * procedure.access # {} THEN
  6293. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6294. END;
  6295. procedure.SetInline(FALSE);
  6296. procedure.SetOberonInline(TRUE);
  6297. END;
  6298. IF SymbolNeedsResolution(procedure) THEN
  6299. recentIsRealtime := currentIsRealtime;
  6300. recentIsBodyProcedure := currentIsBodyProcedure;
  6301. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6302. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6303. modifiers := procedureType.modifiers;
  6304. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6305. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6306. IF useDarwinCCalls THEN (*fld*)
  6307. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6308. ELSE
  6309. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6310. END
  6311. END;
  6312. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6313. procedureType.SetInterrupt(TRUE);
  6314. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6315. END;
  6316. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6317. procedureType.SetNoReturn(TRUE);
  6318. END;
  6319. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6320. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6321. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6322. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6323. END;
  6324. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6325. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6326. END;
  6327. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6328. IF ~PowerOf2(value) THEN
  6329. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6330. ELSE
  6331. procedureType.SetStackAlignment(value)
  6332. END;
  6333. END;
  6334. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6335. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6336. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6337. END;
  6338. CheckModifiers(modifiers, TRUE);
  6339. modifiers := procedureType.returnTypeModifiers;
  6340. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6341. CheckModifiers(modifiers, TRUE);
  6342. FixProcedureType(procedureType);
  6343. currentIsRealtime := procedureType.isRealtime;
  6344. currentIsBodyProcedure := procedure.isBodyProcedure;
  6345. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6346. THEN
  6347. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6348. END;
  6349. CheckSymbolVisibility(procedure);
  6350. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6351. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6352. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6353. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6354. END;
  6355. END;
  6356. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6357. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6358. procedureType.SetDelegate(TRUE);
  6359. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6360. IF record.pointerType.typeDeclaration = NIL THEN
  6361. selfParameter.SetType(record.pointerType);
  6362. ELSE
  6363. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6364. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6365. qualifiedType.SetResolved(record.pointerType);
  6366. selfParameter.SetType(qualifiedType);
  6367. END;
  6368. selfParameter.SetAccess(SyntaxTree.Hidden);
  6369. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6370. IF procedure.isConstructor THEN
  6371. (*! constructor is always visible, compatibility to paco
  6372. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6373. *)
  6374. procedure.MarkUsed;
  6375. IF procedureType.returnType # NIL THEN
  6376. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6377. END;
  6378. proc := procedure.scope.firstProcedure;
  6379. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6380. proc := proc.nextProcedure;
  6381. END;
  6382. IF proc # NIL THEN
  6383. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6384. ELSE
  6385. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6386. END;
  6387. END;
  6388. IF procedure.isFinalizer THEN
  6389. procedure.MarkUsed;
  6390. IF procedureType.returnType # NIL THEN
  6391. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6392. END;
  6393. IF procedureType.numberParameters # 0 THEN
  6394. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6395. END;
  6396. proc := procedure.scope.firstProcedure;
  6397. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6398. proc := proc.nextProcedure;
  6399. END;
  6400. IF proc # NIL THEN
  6401. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6402. ELSE
  6403. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6404. END;
  6405. END;
  6406. super := FindSuperProcedure(record.recordScope, procedure);
  6407. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6408. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6409. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6410. END;
  6411. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6412. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6413. END;
  6414. IF super.isFinal THEN
  6415. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6416. END;
  6417. procedure.SetSuper(super);
  6418. super.SetOverwritten(TRUE);
  6419. procedure.SetAccess(procedure.access+super.access);
  6420. procedure.MarkUsed;
  6421. END;
  6422. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6423. THEN
  6424. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6425. END;
  6426. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6427. IF cellsAreObjects THEN
  6428. procedureType.SetDelegate(TRUE);
  6429. END;
  6430. IF procedure.isConstructor THEN
  6431. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6432. END;
  6433. ELSIF procedure.isConstructor THEN
  6434. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6435. END;
  6436. Declarations(procedure.procedureScope, FALSE);
  6437. (* body resolution part done as late fix of the procedure type *)
  6438. procedure.SetState(SyntaxTree.Resolved);
  6439. currentIsRealtime := recentIsRealtime;
  6440. currentIsBodyProcedure := recentIsBodyProcedure;
  6441. END;
  6442. END VisitProcedure;
  6443. (**
  6444. a builtin procedure is a global item that may not be modified locally
  6445. instead the resolving of builtin procedure calls are done in the esignator
  6446. **)
  6447. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6448. VAR type: SyntaxTree.Type;
  6449. BEGIN
  6450. type := ResolveType(builtinProcedure.type);
  6451. END VisitBuiltin;
  6452. (* nopov *)
  6453. (** check and resolve operator
  6454. - operators are first checked as procedures
  6455. - then additional operator-specific checks are done
  6456. - note that only module-scope operators are checked here
  6457. (operators in a record scope are only allowed in the context of
  6458. array-structured object types and checked in 'ResolveArrayStructure')
  6459. - also note that inter-operator conformity is not checked here
  6460. **)
  6461. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6462. VAR
  6463. procedureType: SyntaxTree.ProcedureType;
  6464. leftType, rightType: SyntaxTree.Type;
  6465. identifierNumber, position: LONGINT;
  6466. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6467. modifiers: SyntaxTree.Modifier;
  6468. (** whether a type is locally defined in the current module scope
  6469. for arrays, the base type must be locally defined **)
  6470. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6471. BEGIN
  6472. IF type = NIL THEN
  6473. RETURN FALSE
  6474. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6475. RETURN TRUE
  6476. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6477. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6478. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6479. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6480. ELSE
  6481. RETURN FALSE
  6482. END
  6483. END IsLocallyDefined;
  6484. BEGIN
  6485. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6486. procedureType := operator.type(SyntaxTree.ProcedureType);
  6487. modifiers := procedureType.modifiers;
  6488. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6489. CheckModifiers(modifiers, TRUE);
  6490. VisitProcedure(operator);
  6491. IF operator.scope IS SyntaxTree.RecordScope THEN
  6492. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6493. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6494. IF identifierNumber = -1 THEN
  6495. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6496. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6497. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6498. ELSE
  6499. IF procedureType.numberParameters < 1 THEN
  6500. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6501. ELSIF procedureType.numberParameters > 2 THEN
  6502. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6503. ELSE
  6504. (* determine operand types *)
  6505. leftType := procedureType.firstParameter.type;
  6506. IF procedureType.numberParameters > 1 THEN
  6507. rightType := procedureType.firstParameter.nextParameter.type
  6508. ELSE
  6509. rightType := NIL
  6510. END;
  6511. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6512. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6513. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6514. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6515. END
  6516. END;
  6517. (* TODO: refine the checks, think about how restrictive the checks should be
  6518. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6519. They might be used for intersection, union, complement of custom object types *)
  6520. (* defaults *)
  6521. hasReturnType := TRUE;
  6522. mustBeUnary := FALSE;
  6523. mustBeBinary := FALSE;
  6524. mustReturnBoolean := FALSE;
  6525. mustReturnInteger := FALSE;
  6526. mustHaveEquitypedOperands := FALSE;
  6527. (* operator-specific exceptions *)
  6528. CASE identifierNumber OF
  6529. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6530. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6531. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6532. mustBeBinary := TRUE
  6533. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6534. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6535. | Scanner.Times: mustBeBinary := TRUE
  6536. | Scanner.TimesTimes: mustBeBinary := TRUE
  6537. | Scanner.DotTimes: mustBeBinary := TRUE
  6538. | Scanner.PlusTimes: mustBeBinary := TRUE
  6539. | Scanner.Slash: mustBeBinary := TRUE
  6540. | Scanner.Backslash: mustBeBinary := TRUE
  6541. | Scanner.DotSlash: mustBeBinary := TRUE
  6542. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6543. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6544. | Scanner.Not: mustBeUnary := TRUE
  6545. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6546. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6547. | Scanner.Transpose: mustBeUnary := TRUE;
  6548. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6549. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6550. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6551. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6552. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6553. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6554. | Global.Abs: mustBeUnary := TRUE;
  6555. | Global.Ash: (* TODO: arity? *)
  6556. | Global.Cap: (* TODO: arity? *)
  6557. | Global.Chr: mustBeUnary := TRUE;
  6558. | Global.Entier: (* TODO: arity? *)
  6559. | Global.EntierH: (* TODO: arity? *)
  6560. | Global.Len: (* unary and binary *)
  6561. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6562. | Global.Max, Global.Min: (* unary and binary *)
  6563. | Global.Odd: (* TODO: arity? *)
  6564. | Global.Sum: (* TODO: arity? *)
  6565. | Global.All: (* TODO: arity? *)
  6566. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6567. | Scanner.Alias:
  6568. | Scanner.GreaterGreater, Scanner.LessLess:
  6569. mustBeBinary := TRUE; hasReturnType := FALSE;
  6570. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6571. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6572. END;
  6573. (* check parameter count *)
  6574. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6575. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6576. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6577. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6578. END;
  6579. (* check parameter types *)
  6580. (* TODO: is this used at all? *)
  6581. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6582. leftType := procedureType.firstParameter.type;
  6583. rightType := procedureType.firstParameter.nextParameter.type;
  6584. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6585. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6586. END
  6587. END;
  6588. (* check return type *)
  6589. IF hasReturnType THEN
  6590. IF procedureType.returnType = NIL THEN
  6591. Error(operator.position, Diagnostics.Invalid, "return type required")
  6592. ELSIF mustReturnBoolean THEN
  6593. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6594. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6595. END
  6596. ELSIF mustReturnInteger THEN
  6597. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6598. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6599. END
  6600. END
  6601. ELSIF procedureType.returnType # NIL THEN
  6602. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6603. END
  6604. END
  6605. END
  6606. END
  6607. END VisitOperator;
  6608. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6609. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6610. BEGIN
  6611. IF error THEN RETURN FALSE END;
  6612. prevScope := currentScope;
  6613. prevDiagnostics := diagnostics;
  6614. diagnostics := NIL; (* suppress error output *)
  6615. currentScope := module.moduleScope;
  6616. VisitImport(x);
  6617. IF ~error THEN
  6618. module.moduleScope.AddImport(x);
  6619. x.SetScope(module.moduleScope);
  6620. END;
  6621. currentScope := prevScope;
  6622. diagnostics := prevDiagnostics;
  6623. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6624. END AddImport;
  6625. (** check and resolve import
  6626. - check for name = SYSTEM
  6627. - check for forbidden self import
  6628. - search through global import cache: already imported?
  6629. - check if already imported indirectly
  6630. - import if necessary -> set module and enter into import cache
  6631. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6632. - after this import this direct import and all indirect imports are stored in the current module's import list
  6633. **)
  6634. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6635. VAR
  6636. module: SyntaxTree.Module;
  6637. moduleScope: SyntaxTree.ModuleScope;
  6638. import,reimport: SyntaxTree.Import;
  6639. filename: FileName;
  6640. prevScope: SyntaxTree.Scope;
  6641. BEGIN
  6642. IF SymbolNeedsResolution(x) THEN
  6643. prevScope := currentScope;
  6644. x.SetType(SyntaxTree.importType);
  6645. moduleScope := currentScope.ownerModule.moduleScope;
  6646. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6647. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6648. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6649. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6650. ELSE
  6651. (* search through global import list: already imported ? *)
  6652. IF (x.module = NIL) & (importCache # NIL) THEN
  6653. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6654. ELSE import := NIL
  6655. END;
  6656. IF x.module # NIL THEN (* already imported indirectly *)
  6657. module := x.module;
  6658. ELSIF import # NIL THEN (* already in module list *)
  6659. module := import.module;
  6660. ASSERT(module # NIL);
  6661. x.SetModule(module);
  6662. ELSE (* must be imported *)
  6663. Global.ModuleFileName(x.moduleName,x.context,filename);
  6664. IF symbolFileFormat # NIL THEN
  6665. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6666. IF module = NIL THEN
  6667. ErrorSS(x.position,"could not import",filename);
  6668. IF VerboseErrorMessage THEN
  6669. Printout.Info("import",x)
  6670. END
  6671. ELSE
  6672. (*
  6673. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6674. (*! should rather be done by importer *)
  6675. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6676. checker.importCache := importCache;
  6677. checker.arrayBaseImported := arrayBaseImported;
  6678. checker.global := global;
  6679. checker.Module(module); (* semantic check *)
  6680. error := error OR checker.error;
  6681. END;
  6682. *)
  6683. (*
  6684. ASSERT(SyntaxTree.Resolved IN module.state);
  6685. *)
  6686. x.SetModule(module);
  6687. IF importCache # NIL THEN
  6688. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6689. import.SetContext(x.context);
  6690. import.SetModule(module);
  6691. importCache.AddImport(import);
  6692. END;
  6693. END;
  6694. ELSE
  6695. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6696. END;
  6697. END;
  6698. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6699. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6700. END;
  6701. import := module.moduleScope.firstImport;
  6702. WHILE(import # NIL) DO
  6703. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6704. ASSERT(currentScope # NIL);
  6705. ASSERT(currentScope.ownerModule # NIL);
  6706. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6707. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6708. Error(x.position,Diagnostics.Invalid,"recursive import");
  6709. ELSE
  6710. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6711. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6712. IF reimport = NIL THEN (* indirect import *)
  6713. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6714. reimport.SetContext(import.context);
  6715. reimport.SetModule(import.module);
  6716. moduleScope.AddImport(reimport);
  6717. reimport.SetScope(moduleScope);
  6718. ELSE
  6719. ASSERT(import.module # NIL);
  6720. reimport.SetModule(import.module); (* direct or indirect import *)
  6721. END;
  6722. END;
  6723. import := import.nextImport;
  6724. END;
  6725. END;
  6726. END;
  6727. currentScope := prevScope;
  6728. (* ELSE nothing to be done *)
  6729. x.SetState(SyntaxTree.Resolved);
  6730. END;
  6731. END VisitImport;
  6732. (*** statements ***)
  6733. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6734. VAR prev,resolved: SyntaxTree.Statement;
  6735. BEGIN
  6736. prev := resolvedStatement;
  6737. resolvedStatement := x;
  6738. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6739. activeCellsStatement := FALSE;
  6740. x.Accept(SELF);
  6741. (* removed this, implementation restriction should be resolved by backend
  6742. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6743. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6744. END;
  6745. *)
  6746. resolved := resolvedStatement;
  6747. resolvedStatement := prev;
  6748. RETURN resolved
  6749. END ResolveStatement;
  6750. (** check and resolve statement sequence
  6751. - check all statements, replace if necessary
  6752. **)
  6753. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6754. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6755. BEGIN
  6756. IF statementSequence # NIL THEN (* else empty *)
  6757. FOR i := 0 TO statementSequence.Length()-1 DO
  6758. statement := statementSequence.GetStatement(i);
  6759. resolved := ResolveStatement(statement);
  6760. IF (resolved # statement) THEN
  6761. statementSequence.SetStatement(i,resolved);
  6762. END;
  6763. END;
  6764. END;
  6765. END StatementSequence;
  6766. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6767. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6768. - check if procedure is callable
  6769. - check return type = NIL (otherwise must be assignment statement)
  6770. **)
  6771. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6772. VAR call: SyntaxTree.Designator;
  6773. BEGIN
  6774. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6775. call := procedureCall.call;
  6776. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6777. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6778. END;
  6779. call := ResolveDesignator(call);
  6780. IF call = SyntaxTree.invalidDesignator THEN
  6781. (* error already handled *)
  6782. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6783. (* inline call in a statement *)
  6784. ELSIF ~IsCallable(call) THEN
  6785. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6786. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6787. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6788. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6789. END;
  6790. procedureCall.SetCall(call);
  6791. (*
  6792. IF call = SyntaxTree.invalidDesignator THEN
  6793. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6794. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6795. IF IsOberonInline(procedure) THEN
  6796. Warning(procedure.position,"call to inline proc");
  6797. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6798. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6799. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6800. resolvedStatement := block;
  6801. RETURN;
  6802. END;
  6803. END;
  6804. *)
  6805. END VisitProcedureCallStatement;
  6806. (** check and resolve assignment LHS := RHS
  6807. - resolve LHS and RHS
  6808. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6809. - check if assignment is compatible
  6810. - check if LHS is variable (i.e. assignable)
  6811. - convert RHS if necessary
  6812. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6813. - assignment between different ASOTs
  6814. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6815. - assignment to ASOT elements:
  6816. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6817. **)
  6818. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6819. VAR
  6820. left: SyntaxTree.Designator;
  6821. right, expression: SyntaxTree.Expression;
  6822. designator: SyntaxTree.Designator;
  6823. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6824. mathArrayType: SyntaxTree.MathArrayType;
  6825. BEGIN
  6826. right := ResolveExpression(assignment.right);
  6827. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6828. left := ResolveDesignator(assignment.left);
  6829. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6830. (* error already handled *)
  6831. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6832. (* LHS is index write operator call on ASOT *)
  6833. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6834. (* necessary ?
  6835. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6836. type := procedureType.firstParameter.type;
  6837. expression := procedureCallDesignator.parameters.GetExpression(0);
  6838. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6839. *)
  6840. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6841. ELSIF CheckVariable(left) THEN
  6842. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6843. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6844. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6845. (* conversion done by procedure call
  6846. (* try to convert to left argument *)
  6847. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6848. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6849. procedureCallDesignator.parameters.SetExpression(1, right);
  6850. END;
  6851. *)
  6852. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6853. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6854. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6855. ELSIF AssignmentCompatible(left, right) THEN
  6856. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6857. mathArrayType := MathArrayStructureOfType(left.type);
  6858. right := NewConversion(right.position, right, mathArrayType, NIL);
  6859. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6860. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6861. ELSE
  6862. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6863. assignment.SetLeft(left);
  6864. assignment.SetRight(right);
  6865. resolvedStatement := assignment
  6866. END
  6867. END
  6868. END
  6869. END VisitAssignment;
  6870. (** check and resolve assignment LHS := RHS
  6871. - resolve LHS and RHS
  6872. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6873. - check if assignment is compatible
  6874. - check if LHS is variable (i.e. assignable)
  6875. - convert RHS if necessary
  6876. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6877. - assignment between different ASOTs
  6878. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6879. - assignment to ASOT elements:
  6880. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6881. **)
  6882. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6883. VAR
  6884. left: SyntaxTree.Designator;
  6885. right: SyntaxTree.Expression;
  6886. inPort, outPort: SyntaxTree.PortType;
  6887. expression: SyntaxTree.Expression;
  6888. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6889. BEGIN
  6890. right := ResolveExpression(communication.right);
  6891. left := ResolveDesignator(communication.left);
  6892. communication.SetLeft(left);
  6893. communication.SetRight(right);
  6894. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6895. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6896. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6897. (* conversion done by procedure call
  6898. (* try to convert to left argument *)
  6899. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6900. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6901. procedureCallDesignator.parameters.SetExpression(1, right);
  6902. END;
  6903. *)
  6904. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6905. ELSE
  6906. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6907. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6908. (* error already handled *)
  6909. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6910. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6911. IF outPort.direction # SyntaxTree.OutPort THEN
  6912. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6913. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6914. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6915. ELSE
  6916. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6917. communication.SetRight(right)
  6918. END;
  6919. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6920. IF CheckVariable(left) THEN
  6921. IF inPort.direction # SyntaxTree.InPort THEN
  6922. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6923. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6924. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6925. END;
  6926. END;
  6927. ELSE
  6928. Error(communication.position, -1, "unsupported stream operation");
  6929. END;
  6930. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6931. IF outPort.direction # SyntaxTree.OutPort THEN
  6932. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6933. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6934. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6935. ELSE
  6936. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6937. communication.SetRight(right)
  6938. END;
  6939. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6940. IF CheckVariable(right) THEN
  6941. IF inPort.direction # SyntaxTree.InPort THEN
  6942. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6943. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6944. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6945. END;
  6946. END;
  6947. ELSE
  6948. Error(communication.position, -1, "unsupported operation");
  6949. END;
  6950. END;
  6951. END VisitCommunicationStatement;
  6952. (** check and resolve if/eslif part
  6953. - check condition
  6954. - check statement sequence
  6955. **)
  6956. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6957. VAR prevUnreachable, b: BOOLEAN;
  6958. BEGIN
  6959. prevUnreachable := currentIsUnreachable;
  6960. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6961. IF IsBooleanValue(ifPart.condition,b) THEN
  6962. IF b=FALSE THEN
  6963. currentIsUnreachable := TRUE
  6964. ELSIF b=TRUE THEN
  6965. true := TRUE
  6966. END;
  6967. END;
  6968. StatementSequence(ifPart.statements);
  6969. currentIsUnreachable := prevUnreachable;
  6970. END IfPart;
  6971. (** check and resolve if statement
  6972. - check if parts and else part statement sequence
  6973. **)
  6974. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6975. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6976. BEGIN
  6977. prevUnreachable := currentIsUnreachable;
  6978. ifPartTrue := FALSE;
  6979. IfPart(ifStatement.ifPart,ifPartTrue);
  6980. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6981. elsif := ifStatement.GetElsifPart(i);
  6982. IfPart(elsif,ifPartTrue);
  6983. END;
  6984. IF ifStatement.elsePart # NIL THEN
  6985. IF ifPartTrue THEN
  6986. currentIsUnreachable := TRUE
  6987. END;
  6988. StatementSequence(ifStatement.elsePart)
  6989. END;
  6990. currentIsUnreachable := prevUnreachable;
  6991. END VisitIfStatement;
  6992. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6993. VAR variable: SyntaxTree.Designator;
  6994. type,variableType: SyntaxTree.Type;
  6995. withEntry: WithEntry;
  6996. BEGIN
  6997. variable := ResolveDesignator(withPart.variable);
  6998. variableType := variable.type.resolved;
  6999. withPart.SetVariable(variable);
  7000. type := ResolveType(withPart.type);
  7001. withPart.SetType(type);
  7002. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7003. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7004. END;
  7005. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7006. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7007. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  7008. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7009. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7010. IF VerboseErrorMessage THEN
  7011. Printout.Info("variable",variable)
  7012. END;
  7013. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7014. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7015. IF VerboseErrorMessage THEN
  7016. Printout.Info("variable",variable);
  7017. Printout.Info("type",type);
  7018. END;
  7019. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7020. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7021. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7022. IF VerboseErrorMessage THEN
  7023. Printout.Info("variable",variable);
  7024. Printout.Info("type",type);
  7025. END;
  7026. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7027. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7028. ELSE
  7029. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7030. NEW(withEntry);
  7031. withEntry.previous := withEntries;
  7032. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7033. withEntry.type := type;
  7034. withEntries := withEntry;
  7035. StatementSequence(withPart.statements);
  7036. withEntries := withEntries.previous;
  7037. END;
  7038. END WithPart;
  7039. (** check and resolve with statement WITH variable: type DO ... END;
  7040. - check type and variable
  7041. - check that variable type is type extension of type
  7042. - check that variable is a variable
  7043. - enter new with scope and enter guardedVariable with same name and reference to variable
  7044. - create if statement:
  7045. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7046. **)
  7047. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7048. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7049. BEGIN
  7050. prevScope := currentScope; symbol := NIL;
  7051. FOR i := 0 TO withStatement.WithParts()-1 DO
  7052. WithPart(withStatement.GetWithPart(i),symbol);
  7053. END;
  7054. IF withStatement.elsePart # NIL THEN
  7055. StatementSequence(withStatement.elsePart)
  7056. END;
  7057. currentScope := prevScope;
  7058. END VisitWithStatement;
  7059. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7060. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7061. - check 'first' < 'last' and no overlaps between different case labels
  7062. - check statement sequence
  7063. **)
  7064. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7065. VAR
  7066. i: LONGINT;
  7067. position: LONGINT;
  7068. expression, left, right: SyntaxTree.Expression;
  7069. expressionType: SyntaxTree.Type;
  7070. l, r: LONGINT;
  7071. cl, cr: CHAR;
  7072. thiscases: SyntaxTree.CaseConstant;
  7073. BEGIN
  7074. thiscases := NIL;
  7075. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7076. expression := casePart.elements.GetExpression(i);
  7077. position := expression.position;
  7078. (* set context of range *)
  7079. IF expression IS SyntaxTree.RangeExpression THEN
  7080. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7081. END;
  7082. expression := ResolveExpression(expression);
  7083. IF expression = SyntaxTree.invalidExpression THEN
  7084. (* error already reported *)
  7085. expressionType := SyntaxTree.invalidType;
  7086. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7087. (* read out 'first' and 'last' *)
  7088. left := expression(SyntaxTree.RangeExpression).first;
  7089. right := expression(SyntaxTree.RangeExpression).last;
  7090. (* guaranteed by VisitRangeExpression: *)
  7091. ASSERT((left # NIL) & (right # NIL));
  7092. ASSERT(left.type.resolved = right.type.resolved);
  7093. left := CompatibleConversion(left.position, left, type);
  7094. right := CompatibleConversion(right.position, right, type);
  7095. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7096. expression(SyntaxTree.RangeExpression).SetLast(right);
  7097. expressionType := RegularType(position,left.type);
  7098. ELSE
  7099. expression := ConstantExpression(expression);
  7100. expression := CompatibleConversion(expression.position, expression, type);
  7101. (*
  7102. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7103. left := Global.NewCharacterValue(system,expression.position,cl);
  7104. expression := casePart.elements.GetExpression(i);
  7105. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7106. expression := left
  7107. END;
  7108. *)
  7109. casePart.elements.SetExpression(i,expression);
  7110. left := expression; right := expression;
  7111. expressionType := RegularType(position,expression.type)
  7112. END;
  7113. IF (expressionType = SyntaxTree.invalidType) THEN
  7114. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7115. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7116. expression := SyntaxTree.invalidExpression;
  7117. ELSE
  7118. l := 0; r := 0;
  7119. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7120. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7121. l := ORD(cl); r := ORD(cr);
  7122. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7123. ELSE
  7124. expression := SyntaxTree.invalidExpression
  7125. END;
  7126. IF expression # SyntaxTree.invalidExpression THEN
  7127. IF l>r THEN
  7128. Error(position,Diagnostics.Invalid,"empty case label")
  7129. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7130. Error(position,Diagnostics.Invalid,"duplicate case label");
  7131. ELSE
  7132. IF l < min THEN min := l END;
  7133. IF r > max THEN max := r END;
  7134. END;
  7135. END;
  7136. END;
  7137. casePart.elements.SetExpression(i,expression);
  7138. END;
  7139. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7140. casePart.SetConstants(thiscases);
  7141. StatementSequence(casePart.statements);
  7142. END CasePart;
  7143. (** check and resolve case statement CASE variable OF ... END;
  7144. - check variable
  7145. - check case parts
  7146. **)
  7147. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7148. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7149. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7150. BEGIN
  7151. expression := ResolveExpression(caseStatement.variable);
  7152. type := RegularType(expression.position,expression.type);
  7153. IF type = SyntaxTree.invalidType THEN
  7154. expression := SyntaxTree.invalidExpression;
  7155. ELSIF IsIntegerType(type) THEN
  7156. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7157. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7158. (*
  7159. expression := Global.NewCharacterValue(system,expression.position,ch);
  7160. *)
  7161. type := expression.type;
  7162. ELSIF IsCharacterType(type) THEN
  7163. ELSIF IsEnumerationType(type) THEN
  7164. ELSE
  7165. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7166. expression := SyntaxTree.invalidExpression;
  7167. END;
  7168. caseStatement.SetVariable(expression);
  7169. caseList := NIL;
  7170. min := MAX(LONGINT); max := MIN(LONGINT);
  7171. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7172. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7173. END;
  7174. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7175. msg := "huge sparse case table ";
  7176. Strings.AppendInt(msg, max-min);
  7177. Strings.Append(msg,"/");
  7178. Strings.AppendInt(msg, caseStatement.CaseParts());
  7179. Warning(caseStatement.position,msg);
  7180. END;
  7181. caseStatement.SetMinMax(min,max);
  7182. StatementSequence(caseStatement.elsePart);
  7183. IF expression.resolved # NIL THEN
  7184. IF IsCharacterValue(expression,ch) THEN
  7185. l := ORD(ch)
  7186. ELSIF IsIntegerValue(expression,l) THEN
  7187. END;
  7188. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7189. END;
  7190. END VisitCaseStatement;
  7191. (** check and resolve while statement
  7192. - check condition
  7193. - check statement sequence
  7194. **)
  7195. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7196. VAR prevIsUnreachable,b: BOOLEAN;
  7197. BEGIN
  7198. prevIsUnreachable := currentIsUnreachable;
  7199. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7200. IF IsBooleanValue(whileStatement.condition,b) THEN
  7201. IF b=FALSE THEN
  7202. currentIsUnreachable := TRUE
  7203. END;
  7204. END;
  7205. StatementSequence(whileStatement.statements);
  7206. currentIsUnreachable := prevIsUnreachable
  7207. END VisitWhileStatement;
  7208. (** check and resolve repeat statement
  7209. - check condition
  7210. - check statement sequence
  7211. **)
  7212. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7213. BEGIN
  7214. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7215. StatementSequence(repeatStatement.statements);
  7216. END VisitRepeatStatement;
  7217. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7218. VAR withEntry: WithEntry;
  7219. BEGIN
  7220. withEntry := withEntries;
  7221. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7222. withEntry := withEntry.previous
  7223. END;
  7224. IF withEntry = NIL THEN RETURN FALSE
  7225. ELSE
  7226. type := withEntry.type;
  7227. RETURN TRUE
  7228. END;
  7229. END GetGuard;
  7230. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7231. - check that variable is an integer variable
  7232. - check that from is integer typed with compatible type
  7233. - check that to has compatible type
  7234. - check that by is constant integer with compatible type
  7235. **)
  7236. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7237. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7238. BEGIN
  7239. designator := ResolveDesignator(forStatement.variable);
  7240. type := SyntaxTree.invalidType;
  7241. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7242. designator := SyntaxTree.invalidDesignator;
  7243. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7244. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7245. designator := SyntaxTree.invalidDesignator;
  7246. ELSIF CheckVariable(designator) THEN
  7247. type := designator.type;
  7248. END;
  7249. forStatement.SetVariable(designator);
  7250. expression := ResolveExpression(forStatement.from);
  7251. IF expression = SyntaxTree.invalidExpression THEN
  7252. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7253. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7254. expression := SyntaxTree.invalidExpression;
  7255. ELSIF type # SyntaxTree.invalidType THEN
  7256. expression := NewConversion(expression.position,expression,type,NIL)
  7257. END;
  7258. forStatement.SetFrom(expression);
  7259. expression := ResolveExpression(forStatement.to);
  7260. IF expression = SyntaxTree.invalidExpression THEN
  7261. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7262. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7263. expression := SyntaxTree.invalidExpression;
  7264. ELSIF type # SyntaxTree.invalidType THEN
  7265. expression := NewConversion(expression.position,expression,type,NIL)
  7266. END;
  7267. forStatement.SetTo(expression);
  7268. IF forStatement.by # NIL THEN
  7269. expression := ConstantInteger(forStatement.by);
  7270. ELSE
  7271. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7272. END;
  7273. IF expression = SyntaxTree.invalidExpression THEN
  7274. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7275. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7276. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7277. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7278. ELSIF type # SyntaxTree.invalidType THEN
  7279. expression := NewConversion(expression.position,expression,type,NIL)
  7280. END;
  7281. forStatement.SetBy(expression);
  7282. StatementSequence(forStatement.statements);
  7283. END VisitForStatement;
  7284. (** check and resolve loop statement LOOP StatementSequence END
  7285. - check statement sequence
  7286. **)
  7287. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7288. BEGIN
  7289. StatementSequence(loopStatement.statements)
  7290. END VisitLoopStatement;
  7291. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7292. BEGIN
  7293. StatementSequence(exitableBlock.statements);
  7294. END VisitExitableBlock;
  7295. (** check and resolve exit statement EXIT
  7296. - check that exit is within LOOP statement block
  7297. **)
  7298. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7299. VAR outer: SyntaxTree.Statement;
  7300. BEGIN
  7301. outer := exitStatement.outer;
  7302. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7303. outer := outer.outer;
  7304. END;
  7305. IF outer = NIL THEN
  7306. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7307. END;
  7308. END VisitExitStatement;
  7309. (** check and resolve return statement RETURN [expression]
  7310. - check expression (if any)
  7311. - check if in procedure scope
  7312. - if in procedure scope then check expression compatibility
  7313. - if not in procecdure scope then check on return without expression
  7314. **)
  7315. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7316. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7317. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7318. BEGIN
  7319. position := returnStatement.position;
  7320. expression := returnStatement.returnValue;
  7321. IF expression # NIL THEN
  7322. expression := ResolveExpression(expression);
  7323. returnStatement.SetReturnValue(expression);
  7324. END;
  7325. outer := returnStatement.outer;
  7326. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7327. outer := outer.outer
  7328. END;
  7329. IF (outer # NIL) THEN
  7330. scope := outer(SyntaxTree.Body).inScope;
  7331. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7332. IF (expression # NIL) THEN
  7333. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7334. END;
  7335. ELSE
  7336. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7337. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7338. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7339. END;
  7340. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7341. IF returnType # NIL THEN
  7342. returnType := returnType.resolved;
  7343. IF expression = NIL THEN
  7344. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7345. ELSIF expression.type = NIL THEN
  7346. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7347. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7348. Error(position,Diagnostics.Invalid,"return type not compatible");
  7349. IF VerboseErrorMessage THEN
  7350. Printout.Info("returnType",returnType);
  7351. Printout.Info("expression",expression);
  7352. END;
  7353. ELSE
  7354. expression := NewConversion(expression.position,expression,returnType,NIL);
  7355. returnStatement.SetReturnValue(expression);
  7356. END;
  7357. ELSIF expression # NIL THEN
  7358. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7359. END;
  7360. END;
  7361. END;
  7362. END VisitReturnStatement;
  7363. (** check and resolve await statement AWAIT(condition: Expression)
  7364. - check await condition
  7365. **)
  7366. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7367. VAR condition: SyntaxTree.Expression;
  7368. BEGIN
  7369. condition := ResolveCondition(awaitStatement.condition);
  7370. IF currentIsRealtime THEN
  7371. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7372. END;
  7373. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7374. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7375. END;
  7376. awaitStatement.SetCondition(condition);
  7377. END VisitAwaitStatement;
  7378. PROCEDURE CheckSystemImport(position: LONGINT);
  7379. VAR import: SyntaxTree.Import;
  7380. BEGIN
  7381. import := currentScope.ownerModule.moduleScope.firstImport;
  7382. WHILE(import # NIL) DO
  7383. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7384. RETURN;
  7385. END;
  7386. import := import.nextImport;
  7387. END;
  7388. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7389. END CheckSystemImport;
  7390. (** check and resolve code statement: do nothing, must be done by assembler
  7391. **)
  7392. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7393. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7394. BEGIN
  7395. CheckSystemImport(code.position);
  7396. FOR i := 0 TO code.inRules.Length()-1 DO
  7397. statement := code.inRules.GetStatement(i);
  7398. IF statement IS SyntaxTree.Assignment THEN
  7399. WITH statement: SyntaxTree.Assignment DO
  7400. statement.SetRight(ResolveExpression(statement.right));
  7401. END;
  7402. ELSE
  7403. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7404. END;
  7405. END;
  7406. FOR i := 0 TO code.outRules.Length()-1 DO
  7407. statement := code.outRules.GetStatement(i);
  7408. IF statement IS SyntaxTree.Assignment THEN
  7409. WITH statement: SyntaxTree.Assignment DO
  7410. statement.SetLeft(ResolveDesignator(statement.left));
  7411. END;
  7412. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7413. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7414. ELSE
  7415. Printout.Info("out statement ", statement);
  7416. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7417. END;
  7418. END;
  7419. END VisitCode;
  7420. (** check and set flags of a statement block
  7421. - check for multiply occurence of a flag
  7422. - check and set priority only in bodies
  7423. - check for valid names
  7424. **)
  7425. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7426. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7427. flag: LONGINT; recordBody: SyntaxTree.Body;
  7428. PROCEDURE SetProtectedRecord;
  7429. VAR scope: SyntaxTree.Scope;
  7430. BEGIN
  7431. scope := currentScope;
  7432. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7433. scope := scope.outerScope
  7434. END;
  7435. IF scope # NIL THEN
  7436. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7437. END;
  7438. END SetProtectedRecord;
  7439. BEGIN
  7440. flags := {};
  7441. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7442. recordBody := block(SyntaxTree.Body)
  7443. ELSE
  7444. recordBody := NIL
  7445. END;
  7446. blockModifier := block.blockModifiers;
  7447. WHILE(blockModifier # NIL) DO
  7448. name := blockModifier.identifier;
  7449. expression := blockModifier.expression;
  7450. position := blockModifier.position;
  7451. flag := -1;
  7452. IF name=Global.NamePriority THEN
  7453. IF expression = NIL THEN
  7454. Error(position,Diagnostics.Invalid,"missing priority expression");
  7455. ELSIF recordBody = NIL THEN
  7456. Error(position,Diagnostics.Invalid,"priority not on record body");
  7457. ELSIF recordBody.priority # NIL THEN
  7458. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7459. ELSE
  7460. recordBody.SetPriority(expression);
  7461. END;
  7462. ELSIF expression # NIL THEN
  7463. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7464. ELSIF name=Global.NameExclusive THEN
  7465. IF block.isExclusive THEN
  7466. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7467. END;
  7468. block.SetExclusive(TRUE); SetProtectedRecord;
  7469. ELSIF name=Global.NameActive THEN
  7470. IF recordBody = NIL THEN
  7471. Error(position,Diagnostics.Invalid,"active not in record body");
  7472. ELSIF recordBody.isActive THEN
  7473. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7474. ELSE
  7475. recordBody.SetActive(TRUE); SetProtectedRecord;
  7476. END;
  7477. ELSIF name=Global.NameSafe THEN
  7478. IF recordBody = NIL THEN
  7479. Error(position,Diagnostics.Invalid,"safe not in record body");
  7480. ELSIF recordBody.isSafe THEN
  7481. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7482. ELSE
  7483. recordBody.SetSafe(TRUE);
  7484. SetProtectedRecord;
  7485. END;
  7486. ELSIF name=Global.NameRealtime THEN
  7487. IF recordBody = NIL THEN
  7488. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7489. ELSIF recordBody.isRealtime THEN
  7490. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7491. ELSE
  7492. recordBody.SetRealtime(TRUE);
  7493. block.SetRealtime(TRUE);
  7494. END;
  7495. ELSIF name=Global.NameUnchecked THEN
  7496. IF block.isUnchecked THEN
  7497. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7498. ELSE
  7499. block.SetUnchecked(TRUE);
  7500. END;
  7501. ELSIF (name=Global.NameUncooperative) THEN
  7502. IF block.isUncooperative THEN
  7503. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7504. ELSE
  7505. block.SetUncooperative(TRUE);
  7506. END;
  7507. ELSE
  7508. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7509. END;
  7510. blockModifier := blockModifier.nextModifier;
  7511. END;
  7512. END BlockFlags;
  7513. (** check and resolve statement block
  7514. - check flags (exclusive)
  7515. - check statement sequence
  7516. **)
  7517. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7518. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7519. BEGIN
  7520. BlockFlags(statementBlock);
  7521. IF statementBlock.isExclusive THEN
  7522. (* check that not in exclusive block *)
  7523. IF currentIsExclusive THEN
  7524. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7525. ELSIF currentIsRealtime THEN
  7526. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7527. END;
  7528. END;
  7529. recentExclusive := currentIsExclusive;
  7530. recentUnreachable := currentIsUnreachable;
  7531. recentRealtime := currentIsRealtime;
  7532. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7533. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7534. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7535. StatementSequence(statementBlock.statements);
  7536. currentIsRealtime := recentRealtime;
  7537. currentIsExclusive := recentExclusive;
  7538. currentIsUnreachable := recentUnreachable;
  7539. END VisitStatementBlock;
  7540. (** check and resolve body
  7541. - check flags (active, priority, safe)
  7542. - check body and finally part
  7543. **)
  7544. PROCEDURE Body(body: SyntaxTree.Body);
  7545. BEGIN
  7546. VisitStatementBlock(body);
  7547. IF body.isActive THEN
  7548. IF ~currentIsBodyProcedure THEN
  7549. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7550. ELSIF body.priority # NIL THEN
  7551. body.SetPriority(ConstantInteger(body.priority));
  7552. END;
  7553. ELSIF body.isSafe THEN
  7554. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7555. ELSIF body.priority # NIL THEN
  7556. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7557. END;
  7558. IF body.code # NIL THEN
  7559. CheckSystemImport(body.position);
  7560. END;
  7561. StatementSequence(body.finally)
  7562. END Body;
  7563. (*** scopes ***)
  7564. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7565. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7566. VAR duplicateSymbol: BOOLEAN;
  7567. BEGIN
  7568. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7569. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7570. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7571. END;
  7572. scope.EnterSymbol(symbol,duplicateSymbol);
  7573. IF ~allowDuplicate & duplicateSymbol THEN
  7574. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7575. IF VerboseErrorMessage THEN
  7576. Printout.Info("multiply defined identifier",symbol);
  7577. Printout.Info("in scope",scope);
  7578. END;
  7579. END;
  7580. END Register;
  7581. (**
  7582. implementation: check and resolve an implementation part
  7583. **)
  7584. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7585. move implementation checker to a separate object ? *)
  7586. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7587. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7588. BEGIN
  7589. prevIsRealtime := currentIsRealtime;
  7590. prevIsBodyProcedure := currentIsBodyProcedure;
  7591. prevIsCellNet := currentIsCellNet;
  7592. prevScope := currentScope;
  7593. currentScope := scope;
  7594. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7595. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7596. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7597. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7598. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7599. (*
  7600. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7601. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7602. END;
  7603. *)
  7604. END;
  7605. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7606. (* module body, record bodies are wrapped into an artifical procedure *)
  7607. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7608. Body(scope(SyntaxTree.ProcedureScope).body)
  7609. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7610. Body(scope(SyntaxTree.ProcedureScope).body)
  7611. END;
  7612. END;
  7613. currentScope := prevScope;
  7614. currentIsRealtime := prevIsRealtime;
  7615. currentIsBodyProcedure := prevIsBodyProcedure;
  7616. currentIsCellNet := prevIsCellNet;
  7617. END Implementation;
  7618. (** implementation phase:
  7619. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7620. **)
  7621. PROCEDURE Implementations(x: SyntaxTree.Module);
  7622. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7623. BEGIN
  7624. prevPhase := phase;
  7625. phase := InlinePhase;
  7626. scope := x.firstScope;
  7627. WHILE(scope # NIL) DO
  7628. Implementation(scope);
  7629. scope := scope.nextScope;
  7630. END;
  7631. phase := ImplementationPhase;
  7632. scope := x.firstScope;
  7633. WHILE(scope # NIL) DO
  7634. Implementation(scope);
  7635. scope := scope.nextScope;
  7636. END;
  7637. phase := prevPhase;
  7638. END Implementations;
  7639. (** declaration phase:
  7640. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7641. - import lists (for module scopes)
  7642. - parameter list (for procedure scopes)
  7643. - constant declarations
  7644. - type declarations
  7645. - variable declarations
  7646. - procedure declarations
  7647. preformed in two stages:
  7648. - first all symbols are entered into the symbol table (with uniqueness check),
  7649. - then all symbols are resolved
  7650. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7651. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7652. **)
  7653. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7654. VAR
  7655. constant: SyntaxTree.Constant;
  7656. typeDeclaration: SyntaxTree.TypeDeclaration;
  7657. variable: SyntaxTree.Variable;
  7658. procedure: SyntaxTree.Procedure;
  7659. prevScope: SyntaxTree.Scope;
  7660. parameter: SyntaxTree.Parameter;
  7661. import: SyntaxTree.Import;
  7662. symbol: SyntaxTree.Symbol;
  7663. prevPhase: LONGINT;
  7664. prevError : BOOLEAN;
  7665. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7666. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7667. BEGIN
  7668. IF type.baseType # NIL THEN
  7669. baseType := type.baseType.resolved;
  7670. IF baseType IS SyntaxTree.PointerType THEN
  7671. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7672. END;
  7673. IF baseType IS SyntaxTree.CellType THEN
  7674. DeclareCell(baseType(SyntaxTree.CellType));
  7675. END;
  7676. END;
  7677. parameter := type.firstParameter;
  7678. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7679. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7680. variable.SetType(parameter.type);
  7681. variable.SetAccess(SyntaxTree.Hidden);
  7682. variable.SetModifiers(parameter.modifiers);
  7683. currentScope.PushVariable(variable);
  7684. (*
  7685. Register(parameter,scope, FALSE);
  7686. *)
  7687. parameter := parameter.nextParameter;
  7688. END;
  7689. property := type.firstProperty;
  7690. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7691. variable := currentScope.FindVariable(property.name);
  7692. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7693. prop := variable(SyntaxTree.Property);
  7694. ELSE (* add, duplicate symbols detection later *)
  7695. prop := SyntaxTree.NewProperty(property.position, property.name);
  7696. currentScope.PushVariable(prop);
  7697. END;
  7698. prop.SetType(property.type);
  7699. prop.SetValue(property.value);
  7700. prop.SetAccess(SyntaxTree.Hidden);
  7701. property := property.nextProperty;
  7702. END;
  7703. END DeclareCell;
  7704. BEGIN
  7705. prevError := error;
  7706. prevPhase := phase;
  7707. phase := DeclarationPhase;
  7708. prevScope := currentScope;
  7709. currentScope := scope;
  7710. error := FALSE;
  7711. (* first enter all symbols in scope *)
  7712. IF scope IS SyntaxTree.ModuleScope THEN
  7713. (* treat imports first for a module scope, , set default context if necessary *)
  7714. import := scope(SyntaxTree.ModuleScope).firstImport;
  7715. WHILE(import # NIL) DO
  7716. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7717. Register(import, currentScope, FALSE);
  7718. import := import.nextImport;
  7719. END;
  7720. import := scope(SyntaxTree.ModuleScope).firstImport;
  7721. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7722. ResolveSymbol(import);
  7723. import := import.nextImport;
  7724. END;
  7725. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7726. (* enter parameters for a procedure scope *)
  7727. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7728. WHILE(parameter # NIL) DO
  7729. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7730. END;
  7731. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7732. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7733. ELSIF scope IS SyntaxTree.CellScope THEN
  7734. IF~skipImplementation THEN
  7735. import := scope(SyntaxTree.CellScope).firstImport;
  7736. WHILE(import # NIL) DO
  7737. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7738. Register(import, currentScope, FALSE);
  7739. import := import.nextImport;
  7740. END;
  7741. import := scope(SyntaxTree.CellScope).firstImport;
  7742. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7743. ResolveSymbol(import);
  7744. import := import.nextImport;
  7745. END;
  7746. END;
  7747. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7748. END;
  7749. IF error THEN RETURN END;
  7750. (* constants *)
  7751. constant := scope.firstConstant;
  7752. WHILE (constant # NIL) DO
  7753. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7754. END;
  7755. (* type declarations *)
  7756. typeDeclaration := scope.firstTypeDeclaration;
  7757. WHILE (typeDeclaration # NIL) DO
  7758. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7759. END;
  7760. (* variables *)
  7761. variable := scope.firstVariable;
  7762. WHILE (variable # NIL) DO
  7763. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7764. END;
  7765. (* procedures *)
  7766. procedure := scope.firstProcedure;
  7767. WHILE (procedure # NIL) DO
  7768. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7769. END;
  7770. IF ~skipImplementation THEN
  7771. (* now process all symbols without any presumption on the order *)
  7772. symbol := scope.firstSymbol;
  7773. WHILE(symbol # NIL) DO
  7774. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7775. ResolveSymbol(symbol);
  7776. END;
  7777. symbol := symbol.nextSymbol;
  7778. END;
  7779. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7780. symbol := scope.firstSymbol;
  7781. WHILE symbol # NIL DO
  7782. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7783. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7784. pointerFixes.Add(symbol, currentScope);
  7785. END;
  7786. IF ~symbol.type.resolved.isRealtime THEN
  7787. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7788. END;
  7789. END;
  7790. symbol := symbol.nextSymbol
  7791. END;
  7792. END;
  7793. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7794. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7795. END;
  7796. IF (scope.ownerModule # NIL) THEN
  7797. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7798. scope.ownerModule.AddScope(scope);
  7799. END;
  7800. END;
  7801. phase := prevPhase;
  7802. currentScope := prevScope;
  7803. error := error OR prevError;
  7804. END Declarations;
  7805. (* nopov *)
  7806. (** check if all operators from one module are compatible to the ones in the other module
  7807. - check if there are not multiple operators with the same signature
  7808. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7809. - check for all operators whose signatures are compatible, whether the return types are compatible
  7810. note that:
  7811. - the return type is not considered to be part of the signature
  7812. - two signatures are considered compatible, if all of the operands are compatible
  7813. **)
  7814. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7815. VAR
  7816. thisOperator, thatOperator: SyntaxTree.Operator;
  7817. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7818. thisParameter, thatParameter: SyntaxTree.Parameter;
  7819. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7820. i: LONGINT;
  7821. BEGIN
  7822. currentScope := thisModuleScope;
  7823. hasError := FALSE;
  7824. (* go through all operators in the other module *)
  7825. thatOperator := thatModuleScope.firstOperator;
  7826. WHILE (thatOperator # NIL) & ~hasError DO
  7827. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7828. (* the other operator is accessible *)
  7829. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7830. (* the other operator is not the conversion operator *)
  7831. (* go through all operators in this module *)
  7832. thisOperator := thisModuleScope.firstOperator;
  7833. WHILE (thisOperator # NIL) & ~hasError DO
  7834. IF thisOperator # thatOperator THEN
  7835. (* the operators are not the same *)
  7836. IF thisOperator.name = thatOperator.name THEN
  7837. (* the operators share the same identifier *)
  7838. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7839. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7840. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7841. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7842. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7843. (* both operators have the same paramter count *)
  7844. thisParameter := thisProcedureType.firstParameter;
  7845. thatParameter := thatProcedureType.firstParameter;
  7846. operandsAreEqual := TRUE;
  7847. operandsAreCompatible := TRUE;
  7848. (* go through all parameters *)
  7849. FOR i := 1 TO thisProcedureType.numberParameters DO
  7850. ASSERT(thatParameter # NIL);
  7851. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7852. operandsAreEqual := FALSE;
  7853. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7854. operandsAreCompatible := FALSE
  7855. END
  7856. END;
  7857. thisParameter := thisParameter.nextParameter;
  7858. thatParameter := thatParameter.nextParameter
  7859. END;
  7860. IF operandsAreEqual THEN
  7861. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7862. hasError := TRUE
  7863. ELSIF operandsAreCompatible THEN
  7864. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7865. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7866. hasError := TRUE
  7867. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7868. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7869. hasError := TRUE
  7870. END
  7871. END
  7872. END
  7873. END
  7874. END;
  7875. thisOperator := thisOperator.nextOperator
  7876. END
  7877. END
  7878. END;
  7879. thatOperator := thatOperator.nextOperator
  7880. END
  7881. END CheckInterOperatorConformity;
  7882. (** check module:
  7883. - check module declaration
  7884. - add context, if necessary
  7885. - remove module from import cache, if necessary
  7886. - check declarations
  7887. - resolve all type fixes
  7888. - check implementation (bodies)
  7889. **)
  7890. PROCEDURE Module*(x: SyntaxTree.Module);
  7891. VAR (* nopov *)
  7892. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7893. BEGIN
  7894. prevScope := currentScope;
  7895. prevIsCellNet := currentIsCellNet;
  7896. module := x;
  7897. ASSERT(x # NIL);
  7898. global := system.globalScope[x.case];
  7899. x.moduleScope.SetGlobalScope(global);
  7900. currentScope := global;
  7901. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7902. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7903. RemoveModuleFromCache(importCache,x);
  7904. Declarations(x.moduleScope, FALSE);
  7905. FixTypes();
  7906. IF module.isCellNet THEN
  7907. currentIsCellNet := TRUE;
  7908. modifier := x.modifiers;
  7909. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7910. CheckModifiers(modifier, FALSE);
  7911. END;
  7912. (* nopov *)
  7913. IF ~error THEN
  7914. (* check if operators conform to each other within this module *)
  7915. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7916. (* go through all imports *)
  7917. import := x.moduleScope.firstImport;
  7918. WHILE import # NIL DO
  7919. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7920. (* check if all operators in this module conform to the ones of the imported module *)
  7921. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7922. END;
  7923. import := import.nextImport
  7924. END;
  7925. END;
  7926. Implementations(x);
  7927. module := NIL;
  7928. currentIsCellNet := prevIsCellNet;
  7929. currentScope := prevScope;
  7930. END Module;
  7931. END Checker;
  7932. Warnings*=OBJECT (SyntaxTree.Visitor)
  7933. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7934. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7935. BEGIN
  7936. SELF.diagnostics := diagnostics
  7937. END InitWarnings;
  7938. (** types *)
  7939. PROCEDURE Type(x: SyntaxTree.Type);
  7940. BEGIN x.Accept(SELF)
  7941. END Type;
  7942. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7943. BEGIN END VisitType;
  7944. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7945. BEGIN END VisitBasicType;
  7946. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7947. BEGIN END VisitCharacterType;
  7948. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7949. BEGIN END VisitIntegerType;
  7950. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7951. BEGIN END VisitFloatType;
  7952. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7953. BEGIN END VisitQualifiedType;
  7954. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7955. BEGIN END VisitStringType;
  7956. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7957. BEGIN END VisitEnumerationType;
  7958. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7959. BEGIN END VisitRangeType;
  7960. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7961. BEGIN
  7962. IF ~(SyntaxTree.Warned IN x.state) THEN
  7963. x.SetState(SyntaxTree.Warned);
  7964. Type(x.arrayBase);
  7965. END;
  7966. END VisitArrayType;
  7967. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7968. BEGIN
  7969. IF ~(SyntaxTree.Warned IN x.state) THEN
  7970. x.SetState(SyntaxTree.Warned);
  7971. Type(x.arrayBase);
  7972. END;
  7973. END VisitMathArrayType;
  7974. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7975. BEGIN
  7976. IF ~(SyntaxTree.Warned IN x.state) THEN
  7977. x.SetState(SyntaxTree.Warned);
  7978. Type(x.pointerBase);
  7979. END;
  7980. END VisitPointerType;
  7981. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7982. BEGIN Scope(x.recordScope) END VisitRecordType;
  7983. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7984. BEGIN Scope(x.cellScope) END VisitCellType;
  7985. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7986. BEGIN END VisitProcedureType;
  7987. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7988. VAR msg: ARRAY 256 OF CHAR;
  7989. BEGIN
  7990. Global.GetSymbolName(x,msg);
  7991. Strings.Append(msg," ");
  7992. Strings.Append(msg,text);
  7993. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7994. END Warning;
  7995. (** symbols *)
  7996. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7997. BEGIN
  7998. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7999. IF ~(x IS SyntaxTree.Parameter) THEN
  8000. Warning(x,"never used");
  8001. END;
  8002. END;
  8003. x.Accept(SELF);
  8004. END Symbol;
  8005. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8006. BEGIN END VisitSymbol;
  8007. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8008. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8009. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8010. BEGIN END VisitConstant;
  8011. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8012. BEGIN END VisitVariable;
  8013. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8014. BEGIN END VisitProperty;
  8015. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8016. BEGIN END VisitParameter;
  8017. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8018. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8019. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8020. BEGIN END VisitOperator;
  8021. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8022. BEGIN END VisitImport;
  8023. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8024. VAR
  8025. symbol: SyntaxTree.Symbol;
  8026. BEGIN
  8027. symbol := scope.firstSymbol;
  8028. WHILE(symbol # NIL) DO
  8029. Symbol(symbol);
  8030. symbol := symbol.nextSymbol;
  8031. END;
  8032. END Scope;
  8033. PROCEDURE Module*(x: SyntaxTree.Module);
  8034. BEGIN
  8035. SELF.module := x;
  8036. Scope(x.moduleScope);
  8037. END Module;
  8038. END Warnings;
  8039. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8040. BEGIN
  8041. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8042. END IsOberonInline;
  8043. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8044. BEGIN
  8045. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8046. END Resolved;
  8047. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8048. VAR i: LONGINT;
  8049. BEGIN
  8050. i := 1;
  8051. WHILE i < x DO
  8052. i := i *2
  8053. END;
  8054. RETURN i=x
  8055. END PowerOf2;
  8056. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8057. BEGIN
  8058. RETURN
  8059. (scope # NIL) &
  8060. (scope IS SyntaxTree.ModuleScope)
  8061. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8062. OR
  8063. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8064. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8065. END IsCellNetScope;
  8066. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8067. BEGIN
  8068. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8069. END IsCellScope;
  8070. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8071. BEGIN
  8072. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8073. RETURN (scope # NIL) & IsCellNetScope(scope)
  8074. END InCellNetScope;
  8075. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8076. BEGIN
  8077. ASSERT(size MOD system.dataUnit = 0);
  8078. RETURN size DIV system.dataUnit
  8079. END ToMemoryUnits;
  8080. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8081. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8082. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8083. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8084. BEGIN
  8085. IF t = NIL THEN
  8086. RETURN TRUE
  8087. ELSE
  8088. t := t.resolved;
  8089. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8090. END;
  8091. END TypeAllowed;
  8092. BEGIN
  8093. type := type.resolved;
  8094. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8095. RETURN FALSE
  8096. ELSE
  8097. procedureType := type(SyntaxTree.ProcedureType);
  8098. numberParameters := procedureType.numberParameters;
  8099. RETURN
  8100. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8101. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8102. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8103. END;
  8104. END GetProcedureAllowed;
  8105. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8106. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8107. VAR import: SyntaxTree.Import;
  8108. BEGIN
  8109. import := importCache.ImportByModuleName(x.name,x.context);
  8110. IF import # NIL THEN
  8111. importCache.RemoveImporters(x.name,x.context);
  8112. END;
  8113. END RemoveModuleFromCache;
  8114. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8115. (* to <- this assignment compatibility *)
  8116. VAR result: BOOLEAN;
  8117. BEGIN
  8118. IF this= NIL THEN result := (to=NIL)
  8119. ELSIF to=NIL THEN result := FALSE
  8120. ELSE
  8121. (*! will be replaced by this:
  8122. ELSE result := this.CompatibleTo(to.resolved);
  8123. *)
  8124. this := this.resolved; to := to.resolved;
  8125. IF to=SyntaxTree.invalidType THEN result := FALSE
  8126. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8127. ELSIF to = this THEN
  8128. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8129. ELSIF to IS SyntaxTree.BasicType THEN
  8130. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8131. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8132. result := this.CompatibleTo(to.resolved)
  8133. ELSE
  8134. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8135. END
  8136. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8137. result := to.sizeInBits = this.sizeInBits;
  8138. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8139. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8140. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8141. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8142. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8143. result := TRUE;
  8144. ELSIF to IS SyntaxTree.AnyType THEN
  8145. 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);
  8146. ELSIF to IS SyntaxTree.ObjectType THEN
  8147. 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 *) ;
  8148. ELSIF to IS SyntaxTree.ByteType THEN
  8149. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8150. ELSIF to IS SyntaxTree.CharacterType THEN
  8151. result := IsCharacterType(this)
  8152. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8153. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8154. 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
  8155. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8156. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8157. result := TRUE;
  8158. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8159. result := TRUE;
  8160. ELSE
  8161. result := FALSE
  8162. END;
  8163. ELSIF to IS SyntaxTree.PointerType THEN
  8164. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8165. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8166. & (~to.isRealtime OR this.isRealtime);
  8167. ELSIF to IS SyntaxTree.ProcedureType THEN
  8168. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8169. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8170. & (~to.isRealtime OR this.isRealtime)
  8171. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8172. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8173. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8174. ELSIF to IS SyntaxTree.RecordType THEN
  8175. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8176. ELSIF to IS SyntaxTree.ArrayType THEN
  8177. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8178. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8179. ELSIF StaticArrayCompatible(to, this) THEN
  8180. result := TRUE
  8181. ELSE
  8182. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8183. END;
  8184. ELSIF to IS SyntaxTree.MathArrayType THEN
  8185. IF this IS SyntaxTree.MathArrayType THEN
  8186. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8187. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8188. result := TRUE;
  8189. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8190. result := TRUE;
  8191. ELSE
  8192. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8193. END;
  8194. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8195. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8196. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8197. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8198. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8199. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8200. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8201. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8202. ELSE
  8203. result := FALSE
  8204. END;
  8205. (* an array-structured object type is compatible to the type of its array structure *)
  8206. ELSIF IsArrayStructuredObjectType(this) THEN
  8207. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8208. ELSE
  8209. result := FALSE;
  8210. END;
  8211. ELSIF to IS SyntaxTree.StringType THEN
  8212. result := FALSE;
  8213. ELSIF to IS SyntaxTree.EnumerationType THEN
  8214. result := IsEnumerationExtension(this,to);
  8215. ELSIF to IS SyntaxTree.PortType THEN
  8216. result := SameType(to, this)
  8217. ELSE
  8218. Printout.Info("CompatibleTo",to);
  8219. HALT(100); (* implement missing type check *)
  8220. END;
  8221. END;
  8222. RETURN result
  8223. END CompatibleTo;
  8224. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8225. VAR actualBase, formalBase: SyntaxTree.Type;
  8226. BEGIN
  8227. IF SameType(formal,actual) THEN
  8228. RETURN TRUE
  8229. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8230. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8231. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8232. RETURN
  8233. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8234. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8235. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8236. & StaticArrayCompatible(formalBase,actualBase)
  8237. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8238. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8239. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8240. RETURN
  8241. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8242. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8243. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8244. & StaticArrayCompatible(formalBase,actualBase)
  8245. ELSE RETURN FALSE
  8246. END;
  8247. END StaticArrayCompatible;
  8248. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8249. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8250. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8251. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8252. BEGIN
  8253. result := SameType(formal,actual);
  8254. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8255. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8256. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8257. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8258. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8259. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8260. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8261. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8262. & TC(formalBase, actualBase);
  8263. END;
  8264. RETURN result
  8265. END TC;
  8266. BEGIN
  8267. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8268. ELSE
  8269. arrayBase := formalType.arrayBase.resolved;
  8270. IF (actualType IS SyntaxTree.StringType) THEN
  8271. result := arrayBase IS SyntaxTree.CharacterType
  8272. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8273. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8274. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8275. result := TC(formalType, actualType);
  8276. ELSE
  8277. result := (arrayBase IS SyntaxTree.ByteType)
  8278. END;
  8279. END;
  8280. RETURN result
  8281. END OpenArrayCompatible;
  8282. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8283. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8284. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8285. BEGIN
  8286. IF actualType IS SyntaxTree.MathArrayType THEN
  8287. actualArray := actualType(SyntaxTree.MathArrayType);
  8288. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8289. (*
  8290. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8291. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8292. *)
  8293. actualBase := ArrayBase(actualType,Infinity);
  8294. formalBase := ArrayBase(formalType,Infinity);
  8295. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8296. ELSE
  8297. (*
  8298. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8299. *)
  8300. formalBase := Resolved(formalType.arrayBase);
  8301. actualBase := Resolved(actualArray.arrayBase);
  8302. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8303. (*
  8304. ARRAY [k] -> ARRAY [n]
  8305. *)
  8306. result := (formalType.staticLength = actualArray.staticLength)
  8307. ELSE
  8308. result := TRUE
  8309. END;
  8310. IF ~result THEN
  8311. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8312. ELSIF actualBase = NIL THEN result := FALSE
  8313. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8314. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8315. ELSE
  8316. result := SameType(formalBase,actualBase)
  8317. END;
  8318. END;
  8319. ELSE
  8320. result := FALSE
  8321. END;
  8322. RETURN result
  8323. END MathArrayCompatible;
  8324. (**
  8325. Math Array Type distance for assignments / parameter passings of the form
  8326. from -> to
  8327. variants:
  8328. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8329. allowed:
  8330. static -> static (& size match)
  8331. static -> open
  8332. static -> tensor
  8333. open -> open
  8334. open -> tensor
  8335. open -> static
  8336. tensor -> tensor
  8337. tensor -> open
  8338. tensor -> static
  8339. **)
  8340. (*! think about the metric here: is form matching more important than element type matching? *)
  8341. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8342. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8343. BEGIN
  8344. fromBase := Resolved(from.arrayBase);
  8345. toBase := Resolved(to.arrayBase);
  8346. i := Infinity;
  8347. IF from = to THEN
  8348. i := 0;
  8349. ELSIF (from.form = to.form) THEN
  8350. (* static -> static, open -> open, tensor -> tensor *)
  8351. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8352. IF fromBase = toBase THEN i := 0
  8353. ELSIF toBase = NIL THEN i := 1
  8354. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8355. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8356. ELSE
  8357. i := TypeDistance(system,fromBase, toBase, varpar);
  8358. END;
  8359. END;
  8360. ELSIF (to.form = SyntaxTree.Static) THEN
  8361. (* forbidden *)
  8362. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8363. (* static -> tensor, open -> tensor, tensor -> open *)
  8364. IF toBase=fromBase THEN i := 0;
  8365. ELSIF toBase = NIL THEN i := 1;
  8366. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8367. toBase := ArrayBase(toBase,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. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8374. fromBase := ArrayBase(fromBase,Infinity);
  8375. IF (fromBase=toBase) THEN i := 0
  8376. ELSIF (toBase = NIL) THEN i := 1
  8377. ELSIF (fromBase = NIL) THEN i := Infinity;
  8378. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8379. END;
  8380. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8381. END;
  8382. IF i # Infinity THEN INC(i,2) END;
  8383. ELSIF (from.form = SyntaxTree.Static) THEN
  8384. (* static -> open *)
  8385. IF toBase=fromBase THEN i := 0
  8386. ELSIF toBase = NIL THEN i := 1
  8387. ELSIF fromBase = NIL THEN i := Infinity
  8388. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8389. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8390. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8391. END;
  8392. IF i # Infinity THEN INC(i,1) END;
  8393. ELSE HALT(100); (* unknown case *)
  8394. END;
  8395. RETURN i;
  8396. END MathArrayTypeDistance;
  8397. (** compute and return the distance of two array types
  8398. - return the distance of the base types
  8399. **)
  8400. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8401. VAR i: LONGINT;
  8402. BEGIN
  8403. i := Infinity;
  8404. IF from = to THEN
  8405. i := 0
  8406. ELSE
  8407. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8408. (*
  8409. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8410. i := TypeDistance(from.base, to.base);
  8411. IF i >= 0 THEN INC(i) END
  8412. ELSIF (from.mode = open) & (to.mode = open) THEN
  8413. i := TypeDistance(from.base, to.base);
  8414. *)
  8415. END;
  8416. RETURN i
  8417. END ArrayTypeDistance;
  8418. (** compute the signature distance of a procedure and an actual parameter list
  8419. - if any of the parameters are not compatible, the result is infinite
  8420. - add up and return the distance over all parameters
  8421. **)
  8422. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8423. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8424. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8425. BEGIN
  8426. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8427. result := Infinity
  8428. ELSE
  8429. formalParameter := procedureType.firstParameter;
  8430. i := 0;
  8431. result := 0;
  8432. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8433. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8434. actualParameter := actualParameters.GetExpression(i);
  8435. ASSERT(formalParameter.type # NIL);
  8436. IF (actualParameter.type = NIL) THEN distance := Infinity
  8437. ELSE
  8438. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8439. END;
  8440. IF distance = Infinity THEN
  8441. result := Infinity;
  8442. ELSE
  8443. to := formalParameter.type.resolved;
  8444. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8445. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8446. (* already handled varpar *)
  8447. (*
  8448. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8449. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8450. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8451. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8452. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8453. END;
  8454. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8455. result := Infinity
  8456. END;
  8457. *)
  8458. INC(result, distance);
  8459. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8460. INC(result, distance);
  8461. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8462. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8463. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8464. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8465. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8466. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8467. END;
  8468. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8469. result := Infinity
  8470. END;
  8471. ELSE
  8472. result := Infinity
  8473. END;
  8474. ELSE
  8475. INC(result,distance);
  8476. END;
  8477. END;
  8478. (*
  8479. Printout.Info("actual=", actualParameter);
  8480. Printout.Info("formal=", formalParameter);
  8481. TRACE(result);
  8482. *)
  8483. formalParameter := formalParameter.nextParameter; INC(i);
  8484. END;
  8485. END;
  8486. ASSERT(result >= 0);
  8487. RETURN result
  8488. END Distance;
  8489. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8490. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8491. BEGIN
  8492. IF right.numberParameters # (procedureType.numberParameters) THEN
  8493. result := Infinity
  8494. ELSE
  8495. formalParameter := procedureType.firstParameter;
  8496. rightParameter := right.firstParameter;
  8497. i := 0;
  8498. result := 0;
  8499. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8500. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8501. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8502. IF distance = Infinity THEN
  8503. result := Infinity;
  8504. ELSE
  8505. INC(result,distance);
  8506. END;
  8507. formalParameter := formalParameter.nextParameter;
  8508. rightParameter := rightParameter.nextParameter;
  8509. END;
  8510. END;
  8511. ASSERT(result >= 0);
  8512. RETURN result
  8513. END ProcedureTypeDistance;
  8514. (** compute and return the distance between two types, used for computation of signature distance
  8515. from -> to
  8516. **)
  8517. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8518. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8519. BEGIN
  8520. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8521. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8522. END;
  8523. i := Infinity;
  8524. IF from = to THEN
  8525. i := 0
  8526. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8527. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8528. i := Infinity;
  8529. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8530. i := 10;
  8531. ELSIF (from IS SyntaxTree.StringType) THEN
  8532. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8533. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8534. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8535. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8536. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8537. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8538. i := 1
  8539. ELSIF (from IS SyntaxTree.NilType) THEN
  8540. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8541. (*
  8542. ELSIF (from = NoType) THEN
  8543. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8544. *)
  8545. ELSIF (from IS SyntaxTree.BasicType) THEN
  8546. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8547. IF varpar & (i # 0) THEN i := Infinity END;
  8548. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8549. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8550. ELSIF (from IS SyntaxTree.RecordType) THEN
  8551. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8552. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8553. IF to IS SyntaxTree.MathArrayType THEN
  8554. (*
  8555. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8556. i := Infinity;
  8557. ELSE
  8558. *)
  8559. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8560. (*
  8561. END;
  8562. *)
  8563. END
  8564. ELSIF (from IS SyntaxTree.PointerType) THEN
  8565. ptr := from(SyntaxTree.PointerType);
  8566. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8567. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8568. (* ELSE i := TypeDistance(ptr.base, to); *)
  8569. END
  8570. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8571. IF (to IS SyntaxTree.ProcedureType) THEN
  8572. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8573. END;
  8574. ELSIF (from IS SyntaxTree.PortType) THEN
  8575. IF (to IS SyntaxTree.PortType) THEN
  8576. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8577. i := 0;
  8578. END;
  8579. END;
  8580. (*no procedure test, procedure must be the same*)
  8581. END;
  8582. RETURN i
  8583. END TypeDistance;
  8584. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8585. BEGIN
  8586. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8587. END IsIntegerType;
  8588. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8589. BEGIN
  8590. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8591. END IsAddressType;
  8592. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8593. BEGIN
  8594. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8595. END IsSizeType;
  8596. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8597. BEGIN
  8598. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8599. END IsSignedIntegerType;
  8600. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8601. BEGIN
  8602. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8603. END IsUnsignedIntegerType;
  8604. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8605. VAR result: BOOLEAN;
  8606. BEGIN
  8607. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8608. value := x.resolved(SyntaxTree.IntegerValue).value;
  8609. result := TRUE
  8610. ELSE
  8611. result := FALSE
  8612. END;
  8613. RETURN result
  8614. END IsIntegerValue;
  8615. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8616. VAR result: BOOLEAN;
  8617. BEGIN
  8618. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8619. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8620. result := TRUE
  8621. ELSE
  8622. result := FALSE
  8623. END;
  8624. RETURN result
  8625. END IsEnumerationValue;
  8626. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8627. VAR result: BOOLEAN;
  8628. BEGIN
  8629. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8630. value := x.resolved(SyntaxTree.RealValue).value;
  8631. result := TRUE
  8632. ELSE
  8633. result := FALSE
  8634. END;
  8635. RETURN result
  8636. END IsRealValue;
  8637. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8638. VAR result: BOOLEAN;
  8639. BEGIN
  8640. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8641. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8642. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8643. result := TRUE
  8644. ELSE
  8645. result := FALSE
  8646. END;
  8647. RETURN result
  8648. END IsComplexValue;
  8649. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8650. VAR result: BOOLEAN;
  8651. BEGIN
  8652. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8653. value := x.resolved(SyntaxTree.CharacterValue).value;
  8654. result := TRUE
  8655. ELSE
  8656. result := FALSE
  8657. END;
  8658. RETURN result
  8659. END IsCharacterValue;
  8660. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8661. VAR result: BOOLEAN;
  8662. BEGIN
  8663. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8664. value := x.resolved(SyntaxTree.BooleanValue).value;
  8665. result := TRUE
  8666. ELSE
  8667. result := FALSE
  8668. END;
  8669. RETURN result
  8670. END IsBooleanValue;
  8671. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8672. VAR result: BOOLEAN;
  8673. BEGIN
  8674. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8675. value := x.resolved(SyntaxTree.SetValue).value;
  8676. result := TRUE
  8677. ELSE
  8678. result := FALSE
  8679. END;
  8680. RETURN result
  8681. END IsSetValue;
  8682. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8683. VAR result: BOOLEAN;
  8684. BEGIN
  8685. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8686. value := x.resolved(SyntaxTree.StringValue).value;
  8687. result := TRUE
  8688. ELSE
  8689. result := FALSE
  8690. END;
  8691. RETURN result
  8692. END IsStringValue;
  8693. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8694. BEGIN
  8695. x := x.resolved;
  8696. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8697. END Indexable;
  8698. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8699. BEGIN
  8700. RETURN t1.SameType(t2.resolved);
  8701. END SameType;
  8702. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8703. BEGIN
  8704. IF t IS SyntaxTree.MathArrayType THEN
  8705. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8706. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8707. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8708. DEC(max);
  8709. END;
  8710. ELSIF t IS SyntaxTree.ArrayType THEN
  8711. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8712. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8713. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8714. END;
  8715. END;
  8716. RETURN t;
  8717. END ArrayBase;
  8718. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8719. BEGIN
  8720. type := type.resolved;
  8721. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8722. base := type(SyntaxTree.ArrayType).arrayBase;
  8723. RETURN TRUE;
  8724. END;
  8725. RETURN FALSE;
  8726. END IsOpenArray;
  8727. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8728. BEGIN
  8729. type := type.resolved;
  8730. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8731. base := type(SyntaxTree.ArrayType).arrayBase;
  8732. dim := type(SyntaxTree.ArrayType).staticLength;
  8733. RETURN TRUE
  8734. ELSE
  8735. RETURN FALSE
  8736. END;
  8737. END IsStaticArray;
  8738. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8739. BEGIN
  8740. type := type.resolved;
  8741. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8742. base := type(SyntaxTree.ArrayType).arrayBase;
  8743. RETURN TRUE
  8744. ELSE
  8745. RETURN FALSE
  8746. END;
  8747. END IsDynamicArray;
  8748. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8749. VAR i: LONGINT;
  8750. BEGIN
  8751. i := 0;
  8752. t := t.resolved;
  8753. IF t IS SyntaxTree.MathArrayType THEN
  8754. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8755. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8756. END;
  8757. ELSIF t IS SyntaxTree.ArrayType THEN
  8758. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8759. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8760. END;
  8761. END;
  8762. RETURN i
  8763. END Dimension;
  8764. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8765. BEGIN
  8766. RETURN expression.assignable;
  8767. END IsVariable;
  8768. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8769. BEGIN
  8770. IF (symbol IS SyntaxTree.Parameter) THEN
  8771. WITH symbol: SyntaxTree.Parameter DO
  8772. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8773. END;
  8774. ELSE
  8775. RETURN FALSE
  8776. END;
  8777. END IsVariableParameter;
  8778. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8779. VAR result: BOOLEAN;
  8780. BEGIN
  8781. IF type = NIL THEN result := FALSE
  8782. ELSE
  8783. type := type.resolved;
  8784. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8785. END;
  8786. RETURN result
  8787. END IsPointerType;
  8788. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8789. VAR result: BOOLEAN;
  8790. BEGIN
  8791. IF type = NIL THEN result := FALSE
  8792. ELSE
  8793. type := type.resolved;
  8794. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8795. END;
  8796. RETURN result
  8797. END IsUnsafePointer;
  8798. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8799. BEGIN
  8800. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8801. END IsDisposable;
  8802. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8803. VAR result: BOOLEAN;
  8804. BEGIN
  8805. IF type = NIL THEN result := FALSE
  8806. ELSE
  8807. type := type.resolved;
  8808. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8809. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8810. result := result OR (type IS SyntaxTree.ObjectType);
  8811. END;
  8812. RETURN result
  8813. END IsPointerToRecord;
  8814. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8815. VAR result: BOOLEAN;
  8816. BEGIN
  8817. IF type = NIL THEN result := FALSE
  8818. ELSE
  8819. type := type.resolved;
  8820. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8821. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8822. ;
  8823. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8824. result := result OR (type IS SyntaxTree.ObjectType);
  8825. END;
  8826. RETURN result
  8827. END IsPointerToObject;
  8828. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8829. BEGIN
  8830. IF type # NIL THEN
  8831. RETURN type.resolved.hasPointers
  8832. ELSE
  8833. RETURN FALSE
  8834. END;
  8835. END ContainsPointer;
  8836. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8837. BEGIN
  8838. IF type = NIL THEN RETURN FALSE END;
  8839. type := type.resolved;
  8840. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8841. END IsStringType;
  8842. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8843. BEGIN
  8844. IF type = NIL THEN RETURN FALSE END;
  8845. type := type.resolved;
  8846. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8847. END IsCharacterType;
  8848. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8849. BEGIN
  8850. IF type = NIL THEN RETURN FALSE END;
  8851. type := type.resolved;
  8852. RETURN (type IS SyntaxTree.EnumerationType)
  8853. END IsEnumerationType;
  8854. (** cf. section "Type extension (base type)" in the language report **)
  8855. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8856. VAR result: BOOLEAN;
  8857. BEGIN
  8858. ASSERT(base # NIL); ASSERT(extension # NIL);
  8859. base := base.resolved; extension := extension.resolved;
  8860. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8861. result := TRUE;
  8862. ELSE
  8863. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8864. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8865. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8866. END;
  8867. WHILE (extension # NIL) & (extension # base) DO
  8868. IF extension IS SyntaxTree.RecordType THEN
  8869. extension := extension(SyntaxTree.RecordType).baseType;
  8870. IF (extension # NIL) THEN extension := extension.resolved END;
  8871. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8872. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8873. END;
  8874. ELSE extension := NIL;
  8875. END;
  8876. END;
  8877. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8878. END;
  8879. RETURN result
  8880. END IsTypeExtension;
  8881. (** check if base is the base enumeration type of extension **)
  8882. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8883. BEGIN
  8884. base := base.resolved; extension := extension.resolved;
  8885. WHILE (extension # NIL) & (extension # base) DO
  8886. IF extension IS SyntaxTree.EnumerationType THEN
  8887. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8888. IF extension # NIL THEN extension := extension.resolved END;
  8889. ELSE
  8890. extension := NIL
  8891. END;
  8892. END;
  8893. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8894. END IsEnumerationExtension;
  8895. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8896. BEGIN
  8897. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8898. RETURN TRUE
  8899. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8900. RETURN TRUE
  8901. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8902. RETURN TRUE
  8903. ELSE
  8904. RETURN FALSE
  8905. END
  8906. END IsCallable;
  8907. (** compute and return the distance of two record types
  8908. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8909. **)
  8910. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8911. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8912. BEGIN
  8913. i := 0;
  8914. WHILE (from # NIL) & (from # to) DO
  8915. baseType := from.baseType;
  8916. IF (baseType # NIL) THEN
  8917. baseType := baseType.resolved;
  8918. IF baseType IS SyntaxTree.PointerType THEN
  8919. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8920. END;
  8921. IF baseType IS SyntaxTree.RecordType THEN
  8922. from := baseType(SyntaxTree.RecordType);
  8923. ELSE
  8924. from := NIL;
  8925. END;
  8926. ELSE
  8927. from := NIL
  8928. END;
  8929. INC(i)
  8930. END;
  8931. IF from = NIL THEN i := Infinity END;
  8932. RETURN i
  8933. END RecordTypeDistance;
  8934. (** compute and return the distance of two pointer types **)
  8935. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8936. BEGIN
  8937. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8938. RETURN Infinity;
  8939. ELSE
  8940. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8941. END;
  8942. END PointerTypeDistance;
  8943. (** check if expression contains a symbol designator pointing to a type declaration.
  8944. - if so then enter type declaration into typeDeclaration and return true else return false
  8945. **)
  8946. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8947. VAR result: BOOLEAN;
  8948. BEGIN
  8949. result := FALSE;
  8950. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8951. result := TRUE;
  8952. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8953. END;
  8954. RETURN result
  8955. END IsTypeDesignator;
  8956. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8957. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8958. VAR result: BOOLEAN;
  8959. BEGIN
  8960. type := type.resolved;
  8961. IF type IS SyntaxTree.PointerType THEN
  8962. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8963. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8964. result := TRUE
  8965. ELSE
  8966. result := type IS SyntaxTree.RecordType
  8967. END;
  8968. RETURN result
  8969. END IsExtensibleType;
  8970. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8971. BEGIN
  8972. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8973. (d IS SyntaxTree.SymbolDesignator) &
  8974. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8975. OR
  8976. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8977. END IsUnextensibleRecord;
  8978. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8979. BEGIN
  8980. IF IsUnextensibleRecord(d) THEN
  8981. RETURN FALSE
  8982. ELSE RETURN IsExtensibleType(d.type.resolved)
  8983. END;
  8984. END IsExtensibleDesignator;
  8985. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8986. BEGIN
  8987. type := type.resolved;
  8988. IF (type IS SyntaxTree.PointerType) THEN
  8989. RETURN TRUE
  8990. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8991. RETURN TRUE
  8992. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8993. RETURN TRUE
  8994. ELSIF (type IS SyntaxTree.BasicType) THEN
  8995. RETURN TRUE
  8996. END;
  8997. RETURN FALSE
  8998. END IsBasicType;
  8999. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9000. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9001. BEGIN
  9002. baseType := record.baseType;
  9003. IF (baseType # NIL) THEN
  9004. baseType := baseType.resolved;
  9005. IF (baseType IS SyntaxTree.PointerType) THEN
  9006. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9007. END;
  9008. END;
  9009. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9010. recordType := baseType(SyntaxTree.RecordType);
  9011. ELSE
  9012. recordType := NIL;
  9013. END;
  9014. RETURN recordType
  9015. END RecordBase;
  9016. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9017. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9018. BEGIN
  9019. baseRecord := RecordBase(scope.ownerRecord);
  9020. IF baseRecord = NIL THEN RETURN NIL END;
  9021. scope := baseRecord.recordScope;
  9022. procedureType := procedure.type.resolved;
  9023. IF procedure IS SyntaxTree.Operator THEN
  9024. operator := scope.firstOperator;
  9025. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9026. (*
  9027. Printout.Info("not same ",procedureType);
  9028. Printout.Info("with ",operator.type);
  9029. *)
  9030. operator := operator.nextOperator;
  9031. END;
  9032. super := operator;
  9033. ELSE
  9034. super := scope.firstProcedure;
  9035. WHILE (super # NIL) & (super.name # procedure.name) DO
  9036. super := super.nextProcedure;
  9037. END;
  9038. END;
  9039. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9040. RETURN super
  9041. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9042. RETURN super
  9043. ELSE
  9044. RETURN FindSuperProcedure(scope,procedure);
  9045. END;
  9046. END FindSuperProcedure;
  9047. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9048. VAR procedure: SyntaxTree.Procedure;
  9049. BEGIN
  9050. procedure := record.recordScope.constructor;
  9051. IF procedure = NIL THEN
  9052. record := RecordBase(record);
  9053. IF record # NIL THEN
  9054. procedure := GetConstructor(record)
  9055. END;
  9056. END;
  9057. RETURN procedure;
  9058. END GetConstructor;
  9059. (* enter a case into a list of cases in a sorted way and check for collision *)
  9060. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9061. VAR prev,this,new: SyntaxTree.CaseConstant;
  9062. BEGIN
  9063. this := root;
  9064. prev := NIL;
  9065. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9066. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9067. RETURN FALSE
  9068. ELSE
  9069. IF (this # NIL) & (this.min = max+1) THEN
  9070. this.min := min
  9071. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9072. prev.max := min
  9073. ELSE
  9074. NEW(new); new.min := min; new.max := max;
  9075. new.next := this;
  9076. IF prev = NIL THEN
  9077. root := new;
  9078. ELSE
  9079. prev.next := new
  9080. END
  9081. END;
  9082. RETURN TRUE
  9083. END;
  9084. END EnterCase;
  9085. (** generate and return a new checker object, errors are entered into diagnostics **)
  9086. 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;
  9087. VAR checker: Checker;
  9088. BEGIN
  9089. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9090. RETURN checker
  9091. END NewChecker;
  9092. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9093. VAR warnings: Warnings;
  9094. BEGIN
  9095. NEW(warnings, diagnostics); RETURN warnings;
  9096. END NewWarnings;
  9097. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9098. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9099. END IsRangeType;
  9100. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9101. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9102. END IsMathArrayType;
  9103. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9104. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9105. END IsArrayType;
  9106. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9107. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9108. END IsComplexType;
  9109. (** if a type is an array-structured object type *)
  9110. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9111. VAR recordType: SyntaxTree.RecordType;
  9112. BEGIN
  9113. IF type = NIL THEN
  9114. RETURN FALSE
  9115. ELSE
  9116. type := type.resolved;
  9117. IF type IS SyntaxTree.PointerType THEN
  9118. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9119. IF type IS SyntaxTree.RecordType THEN
  9120. recordType := type(SyntaxTree.RecordType);
  9121. RETURN recordType.isObject & recordType.HasArrayStructure()
  9122. ELSE
  9123. RETURN FALSE
  9124. END
  9125. ELSE
  9126. RETURN FALSE
  9127. END
  9128. END
  9129. END IsArrayStructuredObjectType;
  9130. (** the math array structure of a type
  9131. - for math arrays: the array itself
  9132. - for pointers: the math array structure of the pointer base
  9133. - for array-structured object types: the underlying structure
  9134. - for non-math arrays and all other types: NIL
  9135. **)
  9136. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9137. VAR
  9138. result: SyntaxTree.MathArrayType;
  9139. BEGIN
  9140. IF type = NIL THEN
  9141. result := NIL
  9142. ELSE
  9143. type := type.resolved;
  9144. IF type IS SyntaxTree.PointerType THEN
  9145. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9146. END;
  9147. IF type IS SyntaxTree.MathArrayType THEN
  9148. result := type(SyntaxTree.MathArrayType)
  9149. ELSIF type IS SyntaxTree.RecordType THEN
  9150. result := type(SyntaxTree.RecordType).arrayStructure
  9151. ELSE
  9152. result := NIL
  9153. END
  9154. END;
  9155. RETURN result
  9156. END MathArrayStructureOfType;
  9157. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9158. VAR
  9159. result: BOOLEAN;
  9160. rangeExpression: SyntaxTree.RangeExpression;
  9161. BEGIN
  9162. IF x IS SyntaxTree.RangeExpression THEN
  9163. rangeExpression := x(SyntaxTree.RangeExpression);
  9164. result := TRUE;
  9165. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9166. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9167. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9168. ELSE
  9169. result := FALSE
  9170. END;
  9171. RETURN result
  9172. END IsStaticRange;
  9173. (** whether a type is a math array of tensor form **)
  9174. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9175. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9176. END IsTensor;
  9177. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9178. BEGIN
  9179. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9180. length := type(SyntaxTree.MathArrayType).staticLength;
  9181. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9182. RETURN TRUE
  9183. ELSE
  9184. RETURN FALSE
  9185. END;
  9186. END IsStaticMathArray;
  9187. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9188. BEGIN
  9189. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9190. END SymbolHasAddress;
  9191. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9192. BEGIN
  9193. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9194. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9195. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9196. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9197. END HasAddress;
  9198. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9199. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9200. BEGIN
  9201. IF (e IS SyntaxTree.Designator) THEN
  9202. d := e(SyntaxTree.Designator);
  9203. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9204. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9205. e := d.left;
  9206. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9207. END;
  9208. IF d # NIL THEN
  9209. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9210. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9211. END;
  9212. END;
  9213. RETURN FALSE;
  9214. END IsLocalVariable;
  9215. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9216. BEGIN
  9217. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9218. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9219. ELSE
  9220. RETURN TRUE
  9221. END;
  9222. END IsStaticProcedure;
  9223. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9224. CONST OptimizeMethodTable = FALSE;
  9225. BEGIN
  9226. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9227. END InMethodTable;
  9228. END FoxSemanticChecker.
  9229. SystemTools.FreeDownTo FoxSemanticChecker ~