FoxSemanticChecker.Mod 400 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  127. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  128. BEGIN
  129. IF diagnostics # NIL THEN
  130. Basic.GetErrorMessage(code,message,errorMessage);
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  134. END;
  135. error := TRUE;
  136. END Error;
  137. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  138. VAR errModule: SyntaxTree.Module;
  139. BEGIN
  140. IF diagnostics # NIL THEN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  143. END;
  144. END Warning;
  145. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  146. VAR errorMessage: ARRAY 256 OF CHAR;
  147. BEGIN
  148. IF diagnostics # NIL THEN
  149. Basic.Concat(errorMessage,msg," ", msg2);
  150. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  151. END;
  152. error := TRUE;
  153. END ErrorSS;
  154. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  155. VAR msg, msg2: ARRAY 256 OF CHAR;
  156. BEGIN
  157. IF diagnostics # NIL THEN
  158. COPY(msg1, msg);
  159. Strings.Append(msg, " = ");
  160. Basic.GetString(s, msg2);
  161. Strings.Append(msg, msg2);
  162. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  163. END;
  164. END InfoSS;
  165. (*** symbol lookup ***)
  166. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  167. **)
  168. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  169. VAR
  170. scope,baseScope: SyntaxTree.Scope;
  171. symbol, s: SyntaxTree.Symbol;
  172. ownerRecord,base: SyntaxTree.RecordType;
  173. BEGIN
  174. scope := inScope;
  175. symbol := NIL;
  176. WHILE (scope # NIL) & (symbol = NIL) DO
  177. symbol := scope.FindSymbol(name);
  178. s := NIL;
  179. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  180. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  181. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  182. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  183. END;
  184. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  185. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  186. ELSE
  187. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  188. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  189. symbol.MarkUsed;
  190. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  191. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  192. base := RecordBase(ownerRecord);
  193. IF (base # NIL) THEN
  194. baseScope := base.recordScope;
  195. symbol := Find(baseScope,name,FALSE);
  196. ELSE
  197. symbol := NIL;
  198. END;
  199. ELSE
  200. symbol := NIL;
  201. END;
  202. END;
  203. END;
  204. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  205. END;
  206. IF (symbol # NIL) THEN
  207. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  208. ASSERT(phase = DeclarationPhase);
  209. ResolveSymbol(symbol)
  210. END;
  211. symbol.MarkUsed;
  212. END;
  213. RETURN symbol
  214. END Find;
  215. (*** types ***)
  216. (** find type declaration with name qualifiedIdentifier and return resolved type
  217. - check qualified identifier prefix, set scope to module scope if appropriate
  218. - check suffix in scope
  219. **)
  220. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  221. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  222. BEGIN
  223. result := NIL;
  224. prevScope := currentScope;
  225. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  226. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  227. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  228. IF symbol(SyntaxTree.Import).module = NIL THEN
  229. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  230. result := SyntaxTree.invalidType;
  231. symbol := NIL;
  232. ELSE
  233. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  234. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  235. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  236. IF VerboseErrorMessage THEN
  237. Printout.Info("scope", currentScope);
  238. Printout.Info("symbol", symbol);
  239. END;
  240. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  241. END;
  242. END;
  243. ELSE
  244. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  245. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  246. symbol := NIL;
  247. END;
  248. ELSE
  249. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  250. IF symbol = NIL THEN
  251. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  252. IF VerboseErrorMessage THEN
  253. Printout.Info("Qualident",qualifiedIdentifier);
  254. Printout.Info("in scope",currentScope) ;
  255. END;
  256. END;
  257. END;
  258. IF symbol = NIL THEN (* error already handled *)
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  262. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  263. typeDeclaration := NIL;
  264. result := SyntaxTree.invalidType;
  265. ELSE
  266. currentScope := symbol.scope;
  267. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  268. result := ResolveType(typeDeclaration.declaredType);
  269. symbol.MarkUsed;
  270. ASSERT(result # NIL);
  271. END;
  272. currentScope := prevScope;
  273. RETURN result
  274. END ResolveNamedType;
  275. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  276. If node is currently being resolved then emit a cyclic definition error.
  277. Return TRUE only if node is fully resolved.
  278. **)
  279. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  280. VAR result: BOOLEAN;
  281. BEGIN
  282. IF SyntaxTree.Resolved IN x.state THEN
  283. result := FALSE
  284. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  285. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  286. result := FALSE;
  287. ELSE
  288. result := TRUE;
  289. x.SetState(SyntaxTree.BeingResolved)
  290. END;
  291. RETURN result
  292. END TypeNeedsResolution;
  293. (** Return invalid type if x is currently being resolved, return x otherwise**)
  294. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  295. BEGIN
  296. IF SyntaxTree.Resolved IN x.state THEN
  297. RETURN x
  298. ELSE
  299. RETURN SyntaxTree.invalidType
  300. END;
  301. END ResolvedType;
  302. PROCEDURE VisitType(x: SyntaxTree.Type);
  303. BEGIN
  304. ASSERT(x = SyntaxTree.invalidType);
  305. END VisitType;
  306. (** resolve basic type **)
  307. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  308. BEGIN
  309. IF TypeNeedsResolution(x) THEN
  310. x.SetState(SyntaxTree.Resolved);
  311. END;
  312. resolvedType := ResolvedType(x)
  313. END VisitBasicType;
  314. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitByteType;
  318. (** resolve character type **)
  319. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitCharacterType;
  323. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitBooleanType;
  327. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitSetType;
  331. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitAddressType;
  335. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitSizeType;
  339. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitAnyType;
  343. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitObjectType;
  347. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitNilType;
  351. (** resolve integer type **)
  352. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitIntegerType;
  356. (** resolve real type **)
  357. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitFloatType;
  361. (** resolve complex type **)
  362. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  363. BEGIN
  364. VisitBasicType(x);
  365. END VisitComplexType;
  366. (**
  367. resolve string type: nothing to be done
  368. **)
  369. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  370. BEGIN
  371. IF TypeNeedsResolution(x) THEN
  372. x.SetState(SyntaxTree.Resolved);
  373. END;
  374. resolvedType := ResolvedType(x)
  375. END VisitStringType;
  376. (**
  377. check enumeration scope: enter symbols and check for duplicate names
  378. **)
  379. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  380. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  381. BEGIN
  382. prevScope := currentScope;
  383. currentScope := x;
  384. e := x.firstConstant;
  385. WHILE (e # NIL) DO
  386. Register(e,x,FALSE);
  387. IF SymbolNeedsResolution(e) THEN
  388. IF e.value # NIL THEN
  389. value := ConstantExpression(e.value);
  390. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  391. ELSE
  392. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  393. value.SetType(x.ownerEnumeration);
  394. END;
  395. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  396. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  397. IF nextHighest > highest THEN highest := nextHighest END;
  398. END;
  399. e.SetValue(value);
  400. CheckSymbolVisibility(e);
  401. e.SetType(x.ownerEnumeration);
  402. e.SetState(SyntaxTree.Resolved);
  403. END;
  404. e := e.nextConstant;
  405. END;
  406. currentScope := prevScope;
  407. END CheckEnumerationScope;
  408. (**
  409. resolve enumeration type: check enumeration scope
  410. **)
  411. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  412. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  413. lowest, highest: LONGINT;
  414. BEGIN
  415. IF TypeNeedsResolution(x) THEN
  416. IF x.enumerationBase # NIL THEN
  417. position := x.enumerationBase.position;
  418. baseType := ResolveType(x.enumerationBase);
  419. resolved := baseType.resolved;
  420. baseScope := NIL;
  421. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  422. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  423. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  424. ELSE
  425. enumerationBase := resolved(SyntaxTree.EnumerationType);
  426. lowest := enumerationBase.rangeHighest+1;
  427. END;
  428. x.SetEnumerationBase(baseType);
  429. ELSE lowest := 0;
  430. END;
  431. highest := lowest-1;
  432. CheckEnumerationScope(x.enumerationScope, highest);
  433. x.SetRange(lowest, highest);
  434. x.SetState(SyntaxTree.Resolved);
  435. END;
  436. resolvedType := ResolvedType(x);
  437. END VisitEnumerationType;
  438. (**
  439. resolve range type: nothing to be done
  440. **)
  441. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  442. BEGIN
  443. IF TypeNeedsResolution(x) THEN
  444. x.SetState(SyntaxTree.Resolved);
  445. END;
  446. resolvedType := ResolvedType(x)
  447. END VisitRangeType;
  448. (**
  449. resolve qualified type
  450. - find and resolve named type and set resolved type
  451. **)
  452. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  453. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  454. BEGIN
  455. IF TypeNeedsResolution(x) THEN
  456. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  457. x.SetResolved(type.resolved);
  458. x.SetState(SyntaxTree.Resolved);
  459. x.SetTypeDeclaration (typeDeclaration);
  460. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  461. x.SetResolved(SyntaxTree.invalidType);
  462. END;
  463. resolvedType := x;
  464. END VisitQualifiedType;
  465. (**
  466. resolve array type
  467. - check base type
  468. - array of math array forbidden
  469. - static array of open array forbidden
  470. **)
  471. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  472. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  473. BEGIN
  474. IF TypeNeedsResolution(x) THEN
  475. x.SetArrayBase(ResolveType(x.arrayBase));
  476. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  477. arrayBase := x.arrayBase.resolved;
  478. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  479. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  480. pointerType.SetPointerBase(arrayBase);
  481. pointerType.SetHidden(TRUE);
  482. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  483. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  484. ELSE
  485. x.SetArrayBase(pointerType);
  486. END;
  487. END;
  488. IF x.length # NIL THEN
  489. variableAccessed := FALSE;
  490. e := ResolveExpression(x.length);
  491. IF (e.resolved = NIL) THEN
  492. IF variableAccessed THEN
  493. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  494. END;
  495. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  496. ELSE
  497. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  498. END;
  499. END;
  500. IF arrayBase IS SyntaxTree.ArrayType THEN
  501. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  502. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  503. END;
  504. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  505. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  506. END;
  507. x.SetHasPointers(arrayBase.hasPointers);
  508. x.SetState(SyntaxTree.Resolved);
  509. END;
  510. resolvedType := ResolvedType(x);
  511. END VisitArrayType;
  512. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  513. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  514. BEGIN
  515. module := currentScope.ownerModule;
  516. IF module.name=name THEN
  517. (* do nothing *)
  518. ELSE
  519. moduleScope := module.moduleScope;
  520. import := moduleScope.FindImport(name);
  521. IF import = NIL THEN
  522. import := SyntaxTree.NewImport(position,name,name,TRUE);
  523. moduleScope.AddImport(import);
  524. Register(import,moduleScope,FALSE);
  525. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  526. VisitImport(import);
  527. ELSIF import.direct=FALSE THEN
  528. import.SetScope(module.moduleScope);
  529. import.SetDirect(TRUE);
  530. IF moduleScope.FindSymbol(import.name) = NIL THEN
  531. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  532. duplicate.SetContext(import.context);
  533. duplicate.SetModule(import.module);
  534. Register(duplicate,moduleScope,TRUE);
  535. VisitImport(duplicate);
  536. END;
  537. END;
  538. import.MarkUsed
  539. END;
  540. END ImportModule;
  541. (**
  542. resolve math array type
  543. - check base type
  544. - open math array of array forbidden
  545. - math array of tensor forbidden
  546. - static array of open array forbidden
  547. **)
  548. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  549. VAR arrayBase: SyntaxTree.Type;
  550. BEGIN
  551. IF TypeNeedsResolution(x) THEN
  552. x.SetArrayBase(ResolveType(x.arrayBase));
  553. IF x.length # NIL THEN
  554. x.SetLength(ConstantIntegerGeq0(x.length));
  555. END;
  556. arrayBase := x.arrayBase;
  557. IF arrayBase # NIL THEN
  558. arrayBase := arrayBase.resolved;
  559. IF arrayBase = SyntaxTree.invalidType THEN
  560. (* error already handled *)
  561. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  562. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  563. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  564. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  565. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  566. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  567. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  568. END;
  569. END;
  570. IF x.form = SyntaxTree.Static THEN
  571. x.SetIncrement(system.SizeOf(arrayBase));
  572. END;
  573. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  574. END;
  575. x.SetState(SyntaxTree.Resolved);
  576. END;
  577. resolvedType := ResolvedType(x);
  578. END VisitMathArrayType;
  579. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  580. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  581. (1) Rec = RECORD ... END; Ptr <---> Rec
  582. Ptr = POINTER TO Rec; ^ |
  583. | |
  584. TypeDesc TypeDesc
  585. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  586. ^ /
  587. | /
  588. TypeDesc <-- /
  589. *)
  590. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  591. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  592. BEGIN
  593. Strings.IntToStr(x.position,number);
  594. COPY(prefix,name);
  595. Strings.Append(name,"@");
  596. Strings.Append(name,number);
  597. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  598. typeDeclaration.SetDeclaredType(x);
  599. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  600. x.SetTypeDeclaration(typeDeclaration);
  601. currentScope.AddTypeDeclaration(typeDeclaration);
  602. typeDeclaration.SetScope(currentScope);
  603. END AnonymousTypeDeclaration;
  604. (**
  605. deferred pointer type resolving
  606. - resolve base type
  607. - check that base type is a record or array type
  608. - if error then set base type to invalid type
  609. **)
  610. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  611. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  612. BEGIN
  613. ASSERT(type.pointerBase # NIL);
  614. position := type.pointerBase.position;
  615. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  616. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  617. (* not for pointers, a type is needed for the records only
  618. IF type.typeDeclaration = NIL THEN
  619. AnonymousTypeDeclaration(type);
  620. END;
  621. *)
  622. END;
  623. resolved := ResolveType(type.pointerBase);
  624. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  625. type.SetPointerBase(resolved);
  626. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  627. recordType := resolved.resolved(SyntaxTree.RecordType);
  628. IF recordType.isObject & (recordType.baseType # NIL) THEN
  629. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  630. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  631. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  632. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  633. END;
  634. END;
  635. END;
  636. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  637. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  638. END;
  639. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  640. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  641. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  642. ELS
  643. *)
  644. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  645. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  646. END;
  647. END;
  648. ELSE
  649. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  650. type.SetPointerBase(SyntaxTree.invalidType)
  651. END
  652. END FixPointerType;
  653. (**
  654. resolve pointer type
  655. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  656. **)
  657. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  658. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  659. modifiers: SyntaxTree.Modifier; position: LONGINT;
  660. BEGIN
  661. IF TypeNeedsResolution(x) THEN
  662. modifiers := x.modifiers;
  663. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  664. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  665. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  666. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  667. (* inheritance cycle check
  668. example:
  669. A=POINTER TO RECORD(B) END;
  670. B=POINTER TO RECORD(A) END;
  671. *)
  672. IF x.pointerBase IS SyntaxTree.RecordType THEN
  673. recordType := x.pointerBase(SyntaxTree.RecordType);
  674. recordBaseType := ResolveType(recordType.baseType);
  675. recordType.SetBaseType(recordBaseType);
  676. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  677. END;
  678. CheckModifiers(modifiers, TRUE);
  679. typeFixes.Add(x,currentScope);
  680. x.SetState(SyntaxTree.Resolved);
  681. END;
  682. resolvedType := ResolvedType(x)
  683. END VisitPointerType;
  684. (**
  685. resolve port type
  686. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  687. **)
  688. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  689. VAR value: LONGINT;
  690. BEGIN
  691. IF TypeNeedsResolution(x) THEN
  692. x.SetCellsAreObjects(cellsAreObjects);
  693. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  694. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  695. x.SetSize(value)
  696. ELSE
  697. x.SetSize(system.SizeOf(system.longintType));
  698. END;
  699. x.SetState(SyntaxTree.Resolved);
  700. END;
  701. resolvedType := ResolvedType(x)
  702. END VisitPortType;
  703. (**
  704. deferred procedure type resolving
  705. - resolve return type
  706. - traverse and resolve parameters
  707. **)
  708. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  709. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  710. BEGIN
  711. resolved := ResolveType(procedureType.returnType);
  712. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  713. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  714. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  715. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  716. END;
  717. procedureType.SetReturnType(resolved);
  718. IF (resolved # NIL) THEN
  719. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  720. parameter.SetType(procedureType.returnType);
  721. parameter.SetAccess(SyntaxTree.Hidden);
  722. parameter.SetUntraced(procedureType.hasUntracedReturn);
  723. VisitParameter(parameter);
  724. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  725. END;
  726. (* process parameters *)
  727. parameter :=procedureType.firstParameter;
  728. WHILE (parameter # NIL) DO
  729. VisitParameter(parameter);
  730. parameter := parameter.nextParameter;
  731. END;
  732. END FixProcedureType;
  733. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  734. VAR prev,this: SyntaxTree.Modifier;
  735. BEGIN
  736. this := modifiers;prev := NIL;
  737. WHILE (this # NIL) & (this.identifier # name) DO
  738. prev := this; this := this.nextModifier;
  739. END;
  740. IF this # NIL THEN
  741. IF this.expression # NIL THEN
  742. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  743. END;
  744. this.Resolved;
  745. position := this.position;
  746. RETURN TRUE
  747. ELSE
  748. RETURN FALSE
  749. END;
  750. END HasFlag;
  751. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  752. VAR prev,this: SyntaxTree.Modifier;
  753. BEGIN
  754. this := modifiers;prev := NIL;
  755. WHILE (this # NIL) & (this.identifier # name) DO
  756. prev := this; this := this.nextModifier;
  757. END;
  758. IF this # NIL THEN
  759. IF this.expression = NIL THEN
  760. Error(this.position,Diagnostics.Invalid,"expected expression value");
  761. ELSE
  762. this.SetExpression(ConstantExpression(this.expression));
  763. IF CheckIntegerValue(this.expression,value) THEN END;
  764. END;
  765. this.Resolved;
  766. position := this.position;
  767. RETURN TRUE
  768. ELSE RETURN FALSE
  769. END;
  770. END HasValue;
  771. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: ARRAY OF CHAR): BOOLEAN;
  772. VAR prev,this: SyntaxTree.Modifier;
  773. BEGIN
  774. this := modifiers;prev := NIL;
  775. WHILE (this # NIL) & (this.identifier # name) DO
  776. prev := this; this := this.nextModifier;
  777. END;
  778. IF this # NIL THEN
  779. IF this.expression = NIL THEN
  780. Error(this.position,Diagnostics.Invalid,"expected expression value");
  781. ELSE
  782. this.SetExpression(ConstantExpression(this.expression));
  783. IF CheckStringValue(this.expression,value) THEN END;
  784. END;
  785. this.Resolved;
  786. position := this.position;
  787. RETURN TRUE
  788. ELSE RETURN FALSE
  789. END;
  790. END HasStringValue;
  791. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  792. VAR svalue: ARRAY 32 OF CHAR; position: LONGINT;
  793. BEGIN
  794. IF cellsAreObjects THEN RETURN FALSE END;
  795. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  796. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  797. IF svalue[0] = "~" THEN
  798. Strings.TrimLeft(svalue, "~");
  799. IF svalue = backendName THEN
  800. RETURN TRUE;
  801. END;
  802. ELSIF svalue # backendName THEN
  803. RETURN TRUE;
  804. END;
  805. END;
  806. IF x.baseType # NIL THEN
  807. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  808. END;
  809. RETURN FALSE;
  810. END SkipImplementation;
  811. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  812. VAR this: SyntaxTree.Modifier;
  813. BEGIN
  814. this := modifiers;
  815. WHILE this # NIL DO
  816. IF ~this.resolved THEN
  817. IF checkUse THEN
  818. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  819. ELSE
  820. this.SetExpression(ResolveExpression(this.expression));
  821. this.Resolved;
  822. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  823. END;
  824. END;
  825. this := this.nextModifier
  826. END;
  827. END CheckModifiers;
  828. (**
  829. resolve procedure type
  830. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  831. **)
  832. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  833. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  834. BEGIN
  835. IF TypeNeedsResolution(procedureType) THEN
  836. modifiers := procedureType.modifiers;
  837. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  838. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  839. procedureType.SetInterrupt(TRUE);
  840. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  841. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  842. IF useDarwinCCalls THEN (*fld*)
  843. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  844. ELSE
  845. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  846. END
  847. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  848. procedureType.SetNoReturn(TRUE);
  849. END;
  850. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  851. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  852. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  853. CheckModifiers(modifiers, TRUE);
  854. modifiers := procedureType.returnTypeModifiers;
  855. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  856. CheckModifiers(modifiers, TRUE);
  857. typeFixes.Add(procedureType,currentScope);
  858. procedureType.SetHasPointers(procedureType.isDelegate);
  859. procedureType.SetState(SyntaxTree.Resolved);
  860. END;
  861. resolvedType := ResolvedType(procedureType)
  862. END VisitProcedureType;
  863. (** check and resolve record type
  864. - check base type: must be record, math array or array-structured object type
  865. - check declarations
  866. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  867. **)
  868. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  869. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  870. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  871. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  872. hasPointers: BOOLEAN;
  873. modifiers: SyntaxTree.Modifier;
  874. value: LONGINT;
  875. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  876. BEGIN
  877. type := type.resolved;
  878. IF (type IS SyntaxTree.PointerType) &
  879. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  880. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  881. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  882. RETURN TRUE
  883. ELSE
  884. RETURN FALSE
  885. END;
  886. END IsPointerToRecord;
  887. BEGIN
  888. IF TypeNeedsResolution(x) THEN
  889. hasPointers := FALSE;
  890. modifiers := x.modifiers;
  891. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  892. CheckModifiers(modifiers, TRUE);
  893. IF x.baseType # NIL THEN
  894. position := x.baseType.position;
  895. baseType := ResolveType(x.baseType);
  896. resolved := baseType.resolved;
  897. hasPointers := hasPointers OR resolved.hasPointers;
  898. IF x.isObject THEN (* object *)
  899. ASSERT(x.pointerType # NIL);
  900. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  901. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  902. baseType := NIL
  903. ELSIF IsPointerToRecord(resolved,recordType) THEN
  904. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  905. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  906. ELSE
  907. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  908. END;
  909. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  910. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  911. ELSIF IsPointerToRecord(resolved,recordType) THEN
  912. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  913. ELSIF resolved IS SyntaxTree.RecordType THEN
  914. ELSE
  915. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  916. END;
  917. ELSE
  918. IF resolved IS SyntaxTree.RecordType THEN
  919. ELSE
  920. Error(position, Diagnostics.Invalid,"record does not extend record")
  921. END;
  922. END;
  923. x.SetBaseType(baseType);
  924. IF x.Level() > 15 THEN
  925. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  926. (* note:
  927. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  928. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  929. inheritance history of a type.
  930. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  931. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  932. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  933. *)
  934. END;
  935. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  936. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  937. END;
  938. END;
  939. Declarations(x.recordScope, FALSE);
  940. ResolveArrayStructure(x);
  941. (* computation of sizes and offsets skipped -> done in backend / system *)
  942. recordBase := x.GetBaseRecord();
  943. IF recordBase = NIL THEN numberMethods := 0
  944. ELSE numberMethods := recordBase.recordScope.numberMethods
  945. END;
  946. isRealtime := TRUE;
  947. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  948. WHILE symbol # NIL DO
  949. IF symbol IS SyntaxTree.Variable THEN
  950. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  951. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  952. END;
  953. IF symbol IS SyntaxTree.Procedure THEN
  954. procedure := symbol(SyntaxTree.Procedure);
  955. IF procedure.super # NIL THEN
  956. procedure.SetMethodNumber(procedure.super.methodNumber)
  957. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  958. procedure.SetMethodNumber(numberMethods);
  959. INC(numberMethods);
  960. END;
  961. END;
  962. symbol := symbol.nextSymbol;
  963. END;
  964. IF isRealtime THEN x.SetRealtime(TRUE) END;
  965. x.recordScope.SetNumberMethods(numberMethods);
  966. (* TODO: is this needed anymore? *)
  967. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  968. Error(x.position,Diagnostics.Invalid,"object extends a record")
  969. END;
  970. IF (x.typeDeclaration = NIL) THEN
  971. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  972. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  973. (*
  974. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  975. AnonymousTypeDeclaration(x,name);
  976. *)
  977. ELSE
  978. AnonymousTypeDeclaration(x,"Anonymous");
  979. END;
  980. END;
  981. x.SetHasPointers(hasPointers);
  982. x.SetState(SyntaxTree.Resolved);
  983. END;
  984. resolvedType := ResolvedType(x);
  985. END VisitRecordType;
  986. (** check and resolve cell type
  987. - check base type: must be cell
  988. - check declarations
  989. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  990. **)
  991. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  992. VAR
  993. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  994. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  995. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  996. recordBase: SyntaxTree.RecordType;
  997. numberMethods, int: LONGINT;
  998. real: LONGREAL;
  999. bool: BOOLEAN;
  1000. set: SET;
  1001. v: SyntaxTree.Expression;
  1002. str: Scanner.StringType;
  1003. atype: SyntaxTree.ArrayType;
  1004. prev: SyntaxTree.Scope;
  1005. skip: BOOLEAN;
  1006. svalue: ARRAY 32 OF CHAR;
  1007. BEGIN
  1008. IF TypeNeedsResolution(x) THEN
  1009. recordBase := NIL;
  1010. IF cellsAreObjects THEN
  1011. IF x.baseType = NIL THEN
  1012. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1013. ImportModule(qualifiedIdentifier.prefix, x.position);
  1014. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1015. x.SetBaseType(ResolveType(x.baseType));
  1016. recordBase := x.GetBaseRecord();
  1017. IF recordBase = NIL THEN
  1018. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1019. END;
  1020. ELSE
  1021. x.SetBaseType(ResolveType(x.baseType));
  1022. END;
  1023. ELSE
  1024. x.SetBaseType(ResolveType(x.baseType));
  1025. END;
  1026. IF recordBase = NIL THEN numberMethods := 0
  1027. ELSE numberMethods := recordBase.recordScope.numberMethods
  1028. END;
  1029. modifier := x.modifiers;
  1030. (*IF ~x.isCellNet THEN*)
  1031. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1032. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1033. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1034. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1035. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1036. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1037. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1038. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1039. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1040. symbol := system.activeCellsCapabilities;
  1041. WHILE symbol # NIL DO
  1042. IF HasFlag(modifier, symbol.name, position) THEN END;
  1043. symbol := symbol.nextSymbol;
  1044. END;
  1045. modifier := x.modifiers;
  1046. WHILE (modifier # NIL) DO
  1047. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1048. IF modifier.expression # NIL THEN
  1049. v := ConstantExpression(modifier.expression);
  1050. property.SetValue(v);
  1051. IF IsIntegerValue(modifier.expression, int) THEN
  1052. (*property.SetValue(modifier.expression);*)
  1053. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1054. property.SetType(system.longintType);
  1055. ELSIF IsRealValue(modifier.expression, real) THEN
  1056. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1057. property.SetType(system.longrealType);
  1058. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1059. property.SetType(system.booleanType);
  1060. ELSIF IsSetValue(modifier.expression, set) THEN
  1061. property.SetType(system.setType);
  1062. ELSIF IsStringValue(modifier.expression, str) THEN
  1063. (*property.SetValue(modifier.expression);*)
  1064. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1065. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1066. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1067. property.SetType(atype);
  1068. ELSE
  1069. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1070. END;
  1071. ELSE (* flag property *)
  1072. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1073. property.SetType(system.booleanType);
  1074. END;
  1075. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1076. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1077. x.AddProperty(property);
  1078. modifier := modifier.nextModifier;
  1079. END;
  1080. CheckModifiers(modifier, FALSE);
  1081. Declarations(x.cellScope, SkipImplementation(x));
  1082. (* process parameters *)
  1083. prev := currentScope;
  1084. currentScope := x.cellScope;
  1085. parameter :=x.firstParameter;
  1086. WHILE (parameter # NIL) DO
  1087. VisitParameter(parameter);
  1088. type := parameter.type.resolved;
  1089. IF ~(type IS SyntaxTree.PortType) THEN
  1090. WHILE IsStaticArray(type, type, len) DO
  1091. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1092. END;
  1093. WHILE IsDynamicArray(type, type) DO
  1094. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1095. END;
  1096. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1097. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1098. END;
  1099. END;
  1100. parameter := parameter.nextParameter;
  1101. END;
  1102. currentScope := prev;
  1103. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1104. WHILE symbol # NIL DO
  1105. IF symbol IS SyntaxTree.Variable THEN
  1106. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1107. END;
  1108. symbol := symbol.nextSymbol;
  1109. END;
  1110. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1111. IF (x.typeDeclaration = NIL) THEN
  1112. AnonymousTypeDeclaration(x,"Anonymous");
  1113. END;
  1114. x.SetState(SyntaxTree.Resolved);
  1115. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1116. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1117. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1118. Warning(x.position, "Non-empty body for an engine?");
  1119. END;
  1120. END;
  1121. resolvedType := ResolvedType(x);
  1122. END VisitCellType;
  1123. (* check if an object is an array-structured object type
  1124. - determine the array structure
  1125. - collect operators from top to bottom in the inheritance hierarchy
  1126. - check if LEN operator is declared
  1127. - determine number of possible index operators
  1128. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1129. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1130. *)
  1131. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1132. VAR
  1133. indexOperatorCount, i: LONGINT;
  1134. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1135. isTensor: BOOLEAN;
  1136. BEGIN
  1137. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1138. (* determine array structure *)
  1139. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1140. END;
  1141. IF recordType.HasArrayStructure() THEN
  1142. (* the object is an ASOT *)
  1143. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1144. (* reset array access operators *)
  1145. arrayAccessOperators.len := NIL;
  1146. arrayAccessOperators.generalRead := NIL;
  1147. arrayAccessOperators.generalWrite := NIL;
  1148. IF isTensor THEN
  1149. (* all operators of dimensionalities 1 to max *)
  1150. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1151. ELSE
  1152. (* all operators of certain dimensionality *)
  1153. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1154. END;
  1155. NEW(arrayAccessOperators.read, indexOperatorCount);
  1156. NEW(arrayAccessOperators.write, indexOperatorCount);
  1157. FOR i := 0 TO indexOperatorCount - 1 DO
  1158. arrayAccessOperators.read[i] := NIL;
  1159. arrayAccessOperators.write[i] := NIL
  1160. END;
  1161. (* collect access operators in the record scope *)
  1162. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1163. IF arrayAccessOperators.len = NIL THEN
  1164. (* TODO: think about making this operator optional for static array structures *)
  1165. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1166. END;
  1167. (* show error messages *)
  1168. IF isTensor THEN
  1169. (* require ARRAY [*] OF RANGE *)
  1170. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1171. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1172. ELSE
  1173. (* forbid ARRAY [*] OF RANGE *)
  1174. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1175. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1176. (* require RANGE, RANGE, ... RANGE *)
  1177. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1178. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1179. END;
  1180. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1181. ELSE
  1182. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1183. IF recordType.recordScope.firstOperator # NIL THEN
  1184. RETURN;
  1185. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1186. END
  1187. END
  1188. END ResolveArrayStructure;
  1189. (** collect array access operators in a record scope **)
  1190. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1191. VAR
  1192. baseType: SyntaxTree.Type;
  1193. operator: SyntaxTree.Operator;
  1194. isReadOperator, isGeneralOperator: BOOLEAN;
  1195. indexListSize, indexListKind, hashValue: LONGINT;
  1196. BEGIN
  1197. (* if a parent record scope exists, collect the operators there first *)
  1198. baseType := recordScope.ownerRecord.baseType;
  1199. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1200. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1201. END;
  1202. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1203. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1204. END;
  1205. (* go through all operators in the current record scope *)
  1206. operator := recordScope.firstOperator;
  1207. WHILE operator # NIL DO
  1208. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1209. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1210. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1211. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1212. IF isGeneralOperator THEN
  1213. IF isReadOperator THEN
  1214. arrayAccessOperators.generalRead := operator
  1215. ELSE
  1216. arrayAccessOperators.generalWrite := operator
  1217. END
  1218. ELSE
  1219. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1220. IF isReadOperator THEN
  1221. arrayAccessOperators.read[hashValue] := operator
  1222. ELSE
  1223. arrayAccessOperators.write[hashValue] := operator
  1224. END
  1225. END
  1226. END
  1227. ELSE
  1228. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1229. END;
  1230. operator := operator.nextOperator
  1231. END
  1232. END CollectArrayAccessOperators;
  1233. (** the hash value of an index operator **)
  1234. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1235. VAR result: LONGINT;
  1236. BEGIN
  1237. IF isTensor THEN
  1238. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1239. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1240. ELSE
  1241. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1242. END
  1243. ELSE
  1244. result := indexListKind
  1245. END;
  1246. RETURN result
  1247. END IndexOperatorHash;
  1248. (** 2 to the power of exponent **)
  1249. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1250. VAR result, i: LONGINT;
  1251. BEGIN
  1252. result := 1;
  1253. FOR i := 1 TO exponent DO
  1254. result := result * 2;
  1255. END;
  1256. RETURN result
  1257. END TwoToThePowerOf;
  1258. (** check if a LEN operator has a correct signature. i.e.
  1259. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1260. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1261. **)
  1262. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1263. VAR
  1264. procedureType: SyntaxTree.ProcedureType;
  1265. returnedArrayType: SyntaxTree.MathArrayType;
  1266. result: BOOLEAN;
  1267. BEGIN
  1268. result := FALSE;
  1269. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1270. IF (procedureType.numberParameters = 0) THEN
  1271. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1272. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1273. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1274. IF returnedArrayType.form = SyntaxTree.Open THEN
  1275. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1276. result := TRUE
  1277. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1278. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1279. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1280. result := TRUE
  1281. END
  1282. END
  1283. END
  1284. END
  1285. END;
  1286. IF result THEN
  1287. (* export symbol automatically *)
  1288. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1289. ELSE
  1290. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1291. END;
  1292. RETURN result
  1293. END CheckLenOperator;
  1294. (** check if an index operator has a correct signature. i.e.
  1295. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1296. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1297. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1298. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1299. - determine if it is a read or write operator (existance of return type)
  1300. - check index parameters
  1301. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1302. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1303. [LONGINT] -> binary 0 -> 0
  1304. [RANGE] -> binary 1 -> 1
  1305. [LONGINT, LONGINT] -> binary 00 -> 0
  1306. [LONGINT, RANGE] -> binary 01 -> 1
  1307. [RANGE, LONGINT] -> binary 10 -> 2
  1308. [RANGE, RANGE] -> binary 11 -> 3
  1309. etc.
  1310. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1311. - for read operators, check if return type matches the type of data that is read
  1312. - for write operators, check if last parameter type matches the type of data that is written
  1313. **)
  1314. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1315. VAR
  1316. elementType, otherElementType, dataType: SyntaxTree.Type;
  1317. procedureType: SyntaxTree.ProcedureType;
  1318. mathArrayType: SyntaxTree.MathArrayType;
  1319. parameter: SyntaxTree.Parameter;
  1320. parameterCount, rangeCount, i: LONGINT;
  1321. hasTypeError: BOOLEAN;
  1322. BEGIN
  1323. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1324. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1325. (* determine if it is a read or write operator *)
  1326. isReadOperator := (procedureType.returnType # NIL);
  1327. IF isReadOperator THEN
  1328. indexListSize := parameterCount;
  1329. ELSE
  1330. indexListSize := parameterCount - 1;
  1331. END;
  1332. IF indexListSize < 1 THEN
  1333. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1334. RETURN FALSE
  1335. END;
  1336. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1337. (* general operator *)
  1338. isGeneralOperator := TRUE;
  1339. IF indexListSize > 1 THEN
  1340. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1341. RETURN FALSE
  1342. END;
  1343. (* ARRAY [*] OF RANGE*)
  1344. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1345. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1346. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1347. RETURN FALSE
  1348. END;
  1349. parameter := procedureType.firstParameter.nextParameter
  1350. ELSE
  1351. (* fixed-dim. operator *)
  1352. isGeneralOperator := FALSE;
  1353. (* check number of index parameters *)
  1354. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1355. (* for tensors, limited to a certain size *)
  1356. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1357. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1358. RETURN FALSE
  1359. END
  1360. ELSE
  1361. (* for non-tensors, depends on dimensionality *)
  1362. IF indexListSize # arrayStructure.Dimensionality() THEN
  1363. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1364. RETURN FALSE
  1365. END
  1366. END;
  1367. (* go through all index parameters
  1368. - count the number of ranges
  1369. - determine the index list kind number
  1370. *)
  1371. indexListKind := 0;
  1372. rangeCount := 0;
  1373. parameter := procedureType.firstParameter;
  1374. FOR i := 1 TO indexListSize DO
  1375. indexListKind := indexListKind * 2;
  1376. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1377. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1378. INC(indexListKind);
  1379. INC(rangeCount)
  1380. ELSE
  1381. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1382. RETURN FALSE
  1383. END;
  1384. parameter := parameter.nextParameter
  1385. END;
  1386. END;
  1387. (*
  1388. - for read operators: check type of last parameter
  1389. - for write operators: check return type
  1390. *)
  1391. IF isReadOperator THEN
  1392. dataType := procedureType.returnType (* the return type *)
  1393. ELSE
  1394. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1395. END;
  1396. elementType := arrayStructure.ElementType();
  1397. hasTypeError := FALSE;
  1398. IF isGeneralOperator THEN
  1399. (* ARRAY [?] OF <Element> *)
  1400. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1401. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1402. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1403. hasTypeError := TRUE
  1404. END
  1405. ELSE
  1406. hasTypeError := TRUE
  1407. END
  1408. ELSE
  1409. IF rangeCount = 0 THEN
  1410. (* <Element> *)
  1411. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1412. ELSE
  1413. (* ARRAY [*, *, ..., *] OF <Element> *)
  1414. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1415. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1416. IF mathArrayType.IsFullyDynamic() THEN
  1417. IF mathArrayType.Dimensionality() = rangeCount THEN
  1418. otherElementType := mathArrayType.ElementType();
  1419. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1420. ELSE
  1421. hasTypeError := TRUE
  1422. END
  1423. ELSE
  1424. hasTypeError := TRUE
  1425. END
  1426. ELSE
  1427. hasTypeError := TRUE
  1428. END
  1429. END
  1430. END;
  1431. IF hasTypeError THEN
  1432. IF isReadOperator THEN
  1433. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1434. ELSE
  1435. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1436. END;
  1437. RETURN FALSE
  1438. END;
  1439. (* export symbol automatically *)
  1440. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1441. RETURN TRUE
  1442. END CheckIndexOperator;
  1443. (** resolve all pending types (late resolving).
  1444. - type fixes are resolved at the end of the declaration phase
  1445. - type fixes may imply new type fixes that are also entered at the end of the list
  1446. **)
  1447. PROCEDURE FixTypes;
  1448. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1449. BEGIN
  1450. prevScope := currentScope;
  1451. p := typeFixes.Get(currentScope);
  1452. WHILE p # NIL DO
  1453. ASSERT(currentScope # NIL);
  1454. ASSERT(p IS SyntaxTree.Type);
  1455. IF p IS SyntaxTree.PointerType THEN
  1456. FixPointerType(p(SyntaxTree.PointerType))
  1457. ELSIF p IS SyntaxTree.ProcedureType THEN
  1458. FixProcedureType(p(SyntaxTree.ProcedureType))
  1459. ELSE
  1460. HALT(100);
  1461. END;
  1462. p := typeFixes.Get(currentScope);
  1463. END;
  1464. currentScope :=prevScope;
  1465. END FixTypes;
  1466. (**
  1467. resolve type x
  1468. - if x is nil then return nil
  1469. - if x cannot be resolved then the result is invalidType else the result is x
  1470. - the resolved type is entered into x.resolved
  1471. **)
  1472. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1473. VAR prev,resolved: SyntaxTree.Type;
  1474. BEGIN
  1475. prev := resolvedType;
  1476. resolvedType := SyntaxTree.invalidType;
  1477. IF x = NIL THEN resolvedType := NIL
  1478. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1479. END;
  1480. resolved := resolvedType;
  1481. resolvedType := prev;
  1482. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1483. RETURN resolved
  1484. END ResolveType;
  1485. (*** compatibility rules ***)
  1486. (**
  1487. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1488. **)
  1489. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1490. VAR result: SyntaxTree.Type;
  1491. BEGIN
  1492. result := SyntaxTree.invalidType;
  1493. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1494. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1495. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1496. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1497. ELSE result := type.resolved
  1498. END;
  1499. RETURN result
  1500. END RegularType;
  1501. (** returns signature compatibility of procedure types this and to
  1502. - if not compatible then error is reported
  1503. - compatibility means type equality
  1504. **)
  1505. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1506. VAR result: BOOLEAN;
  1507. BEGIN
  1508. result := SameType(to,this);
  1509. IF ~result THEN
  1510. Error(position,Diagnostics.Invalid,"signature incompatible");
  1511. IF VerboseErrorMessage THEN
  1512. Printout.Info("this",this);
  1513. Printout.Info("to",to);
  1514. END;
  1515. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1516. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1517. END;
  1518. RETURN result
  1519. END SignatureCompatible;
  1520. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1521. - for var parameters compatibility means same type except for
  1522. - formal is of open array of system byte
  1523. - formal is of record type
  1524. - formal is of open array type
  1525. - formal is of open math array type
  1526. - for value parameters compatibllity means assignment compatibility except for
  1527. - formal is of open array type
  1528. if compatible the return true else report error and return false
  1529. **)
  1530. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1531. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1532. BEGIN
  1533. formalType := RegularType(formal.position,formal.type);
  1534. actualType := RegularType(actual.position,actual.type);
  1535. error := FALSE;
  1536. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1537. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1538. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1539. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1540. END;
  1541. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1542. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1543. ELSIF ~IsVariable(actual) THEN
  1544. result := FALSE; error := TRUE;
  1545. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1546. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1547. ELSE
  1548. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1549. END;
  1550. IF VerboseErrorMessage THEN
  1551. Printout.Info("actual",actual);
  1552. Printout.Info("formal",formal);
  1553. END;
  1554. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1555. result := CompatibleTo(system,actualType,formalType);
  1556. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1557. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1558. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1559. IF IsArrayStructuredObjectType(actualType) THEN
  1560. actualType := MathArrayStructureOfType(actualType)
  1561. END;
  1562. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1563. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1564. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1565. END;
  1566. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1567. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1568. END;
  1569. ELSE
  1570. result := SameType(actualType,formalType)
  1571. END
  1572. ELSE
  1573. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1574. actualType := system.characterType;
  1575. END;
  1576. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1577. result := TRUE; (* special rule for WINAPI parameters *)
  1578. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1579. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1580. ELSE
  1581. result := CompatibleTo(system,actualType,formalType);
  1582. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1583. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1584. END;
  1585. END;
  1586. END;
  1587. IF ~result & ~error THEN
  1588. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1589. IF VerboseErrorMessage THEN
  1590. Printout.Info("actual",actual);
  1591. Printout.Info("formal",formal);
  1592. END;
  1593. END;
  1594. RETURN result
  1595. END ParameterCompatible;
  1596. (** check compatibility for expressions of the form left := right
  1597. - if compatible then return true else error report and return false
  1598. - check if left is variable
  1599. - check compatiblity
  1600. **)
  1601. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1602. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1603. BEGIN
  1604. result := FALSE;
  1605. leftType := RegularType(left.position,left.type);
  1606. rightType := RegularType(right.position,right.type);
  1607. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1608. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1609. END;
  1610. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1611. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1612. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1613. (* error already handled *)
  1614. result := TRUE;
  1615. ELSIF ~IsVariable(left) THEN
  1616. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1617. IF VerboseErrorMessage THEN
  1618. Printout.Info("left",left);
  1619. Printout.Info("right",right);
  1620. END;
  1621. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1622. result := TRUE;
  1623. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1624. result := TRUE
  1625. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1626. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1627. IF VerboseErrorMessage THEN
  1628. Printout.Info("left",left);
  1629. Printout.Info("right",right);
  1630. END;
  1631. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1632. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1633. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1634. ELSE
  1635. result := TRUE
  1636. END;
  1637. RETURN result
  1638. END AssignmentCompatible;
  1639. (*** values ***)
  1640. (** check and resolve integer value **)
  1641. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1642. VAR hugeint: HUGEINT;
  1643. BEGIN
  1644. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1645. value.SetType(Global.GetIntegerType(system,hugeint));
  1646. resolvedExpression := value
  1647. END VisitIntegerValue;
  1648. (** check and resolve real value **)
  1649. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1650. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1651. BEGIN
  1652. subtype := value(SyntaxTree.RealValue).subtype;
  1653. IF subtype = Scanner.Real THEN
  1654. type := system.realType
  1655. ELSIF subtype = Scanner.Longreal THEN
  1656. type := system.longrealType
  1657. ELSE
  1658. HALT(100)
  1659. END;
  1660. value.SetType(type);
  1661. resolvedExpression := value
  1662. END VisitRealValue;
  1663. (** check and resolve complex value **)
  1664. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1665. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1666. BEGIN
  1667. subtype := value(SyntaxTree.ComplexValue).subtype;
  1668. IF subtype = Scanner.Real THEN
  1669. type := system.complexType
  1670. ELSIF subtype = Scanner.Longreal THEN
  1671. type := system.longcomplexType
  1672. ELSE
  1673. HALT(100)
  1674. END;
  1675. value.SetType(type);
  1676. resolvedExpression := value
  1677. END VisitComplexValue;
  1678. (** check and resolve set value **)
  1679. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1680. BEGIN
  1681. value.SetType(system.setType);
  1682. resolvedExpression := value
  1683. END VisitSetValue;
  1684. (** check and resolve set value **)
  1685. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1686. BEGIN
  1687. value.SetType(SyntaxTree.invalidType);
  1688. resolvedExpression := value
  1689. END VisitMathArrayValue;
  1690. (** check and resolve boolean value **)
  1691. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1692. BEGIN
  1693. value.SetType(system.booleanType);
  1694. resolvedExpression := value
  1695. END VisitBooleanValue;
  1696. (** check and resolve string value **)
  1697. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1698. BEGIN
  1699. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1700. resolvedExpression := value
  1701. END VisitStringValue;
  1702. (** check and resolve character value **)
  1703. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1704. BEGIN
  1705. value.SetType(system.characterType);
  1706. resolvedExpression := value
  1707. END VisitCharacterValue;
  1708. (** check and resolve nil value **)
  1709. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1710. BEGIN
  1711. value.SetType(system.nilType);
  1712. resolvedExpression := value
  1713. END VisitNilValue;
  1714. (** check and resolve enumerator value **)
  1715. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1716. BEGIN
  1717. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1718. ASSERT(value.type # NIL);
  1719. resolvedExpression := value
  1720. END VisitEnumerationValue;
  1721. (*** expressions ***)
  1722. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1723. - check all elements on integer type
  1724. - if element range is constant, then check lower and upper bound
  1725. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1726. if an error occurs then report error and return invalidExpression
  1727. **)
  1728. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1729. VAR
  1730. i: LONGINT;
  1731. element: SyntaxTree.Expression;
  1732. constant: BOOLEAN;
  1733. elements: SyntaxTree.ExpressionList;
  1734. s: SET;
  1735. result: SyntaxTree.Expression;
  1736. value: SyntaxTree.Value;
  1737. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1738. VAR
  1739. left, right: SyntaxTree.Expression;
  1740. elementResult: SyntaxTree.Expression;
  1741. leftInteger, rightInteger, temp: LONGINT;
  1742. BEGIN
  1743. (* set context of range *)
  1744. IF element IS SyntaxTree.RangeExpression THEN
  1745. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1746. END;
  1747. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1748. IF elementResult = SyntaxTree.invalidExpression THEN
  1749. (* error already reported *)
  1750. constant := FALSE
  1751. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1752. (* the element is a range expression *)
  1753. (* extract left and right hand side of range *)
  1754. left := elementResult(SyntaxTree.RangeExpression).first;
  1755. right := elementResult(SyntaxTree.RangeExpression).last;
  1756. (* guaranteed by VisitRangeExpression: *)
  1757. ASSERT((left # NIL) & (right # NIL));
  1758. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1759. ELSE
  1760. (* the element is not a range expression *)
  1761. (* check type and add conversion if needed *)
  1762. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1763. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1764. ELSE
  1765. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1766. elementResult := SyntaxTree.invalidExpression;
  1767. constant := FALSE
  1768. END;
  1769. left := elementResult;
  1770. right := elementResult
  1771. END;
  1772. IF elementResult # SyntaxTree.invalidExpression THEN
  1773. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1774. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1775. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1776. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1777. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1778. END
  1779. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1780. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1781. ELSE
  1782. IF leftInteger > rightInteger THEN
  1783. temp := leftInteger; leftInteger := rightInteger; rightInteger := temp
  1784. END;
  1785. IF leftInteger > MAX(SET) THEN leftInteger := MAX(SET) END;
  1786. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1787. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1788. extends the range x..31 to x..63 !!!!!! *)
  1789. s := s + {leftInteger..rightInteger};
  1790. (* same but more explicit:
  1791. WHILE (leftInteger <= rightInteger) DO
  1792. INCL(s,leftInteger);
  1793. INC(leftInteger);
  1794. END;
  1795. *)
  1796. END;
  1797. ELSE
  1798. constant := FALSE;
  1799. END
  1800. END;
  1801. RETURN elementResult
  1802. END CheckElement;
  1803. BEGIN
  1804. result := set; constant := TRUE; s := {}; elements := set.elements;
  1805. IF elements # NIL THEN
  1806. FOR i := 0 TO elements.Length()-1 DO
  1807. element := elements.GetExpression(i);
  1808. element := CheckElement(element);
  1809. IF element = SyntaxTree.invalidExpression THEN
  1810. result := SyntaxTree.invalidExpression
  1811. END;
  1812. elements.SetExpression(i,element);
  1813. END;
  1814. END;
  1815. IF constant THEN
  1816. value := SyntaxTree.NewSetValue(set.position,s);
  1817. value.SetType(system.setType);
  1818. result.SetResolved(value);
  1819. END;
  1820. (* optimization possible
  1821. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1822. left this to the programmer...
  1823. *)
  1824. result.SetType(system.setType);
  1825. resolvedExpression := result;
  1826. END VisitSet;
  1827. (*
  1828. old variant: quite generic but needs better conversion handling, do this?
  1829. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1830. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1831. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1832. BEGIN
  1833. type := NIL;
  1834. numberElements := x.elements.Length();
  1835. FOR i := 0 TO numberElements-1 DO
  1836. expression := x.elements.GetExpression(i);
  1837. position := expression.position;
  1838. expression := ResolveExpression(x.elements.GetExpression(i));
  1839. x.elements.SetExpression(i,de);
  1840. IF type = NIL THEN
  1841. type := expression.type;
  1842. ELSIF CompatibleTo(system,expression.type,type) THEN
  1843. (* ok *)
  1844. ELSIF CompatibleTo(system,type,expression.type) THEN
  1845. type := expression.type
  1846. ELSE
  1847. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1848. type := SyntaxTree.invalidType;
  1849. END;
  1850. END;
  1851. isValue := TRUE;
  1852. FOR i := 0 TO numberElements-1 DO
  1853. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1854. x.elements.SetExpression(i,expression);
  1855. isValue := isValue & (expression.resolved # NIL);
  1856. END;
  1857. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1858. arrayType.SetArrayBase(type);
  1859. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1860. IF isValue THEN
  1861. value := SyntaxTree.NewMathArrayValue(position);
  1862. value.SetElements(x.elements);
  1863. x.SetResolved(value);
  1864. END;
  1865. x.SetType(arrayType);
  1866. resolvedExpression := x;
  1867. END VisitMathArrayExpression;
  1868. *)
  1869. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1870. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1871. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1872. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1873. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1874. BEGIN
  1875. numberElements := x.elements.Length();
  1876. FOR i := 0 TO numberElements-1 DO
  1877. expression := x.elements.GetExpression(i);
  1878. IF expression IS SyntaxTree.MathArrayExpression THEN
  1879. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1880. ELSE
  1881. position := expression.position;
  1882. expression := ResolveExpression(x.elements.GetExpression(i));
  1883. x.elements.SetExpression(i,expression);
  1884. IF type = NIL THEN
  1885. type := expression.type;
  1886. ELSIF CompatibleTo(system,expression.type,type) THEN
  1887. (* ok *)
  1888. ELSIF CompatibleTo(system,type,expression.type) THEN
  1889. type := expression.type
  1890. ELSE
  1891. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1892. type := SyntaxTree.invalidType;
  1893. END;
  1894. END;
  1895. END;
  1896. END RecursivelyFindType;
  1897. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1898. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1899. BEGIN
  1900. numberElements := x.elements.Length();
  1901. FOR i := 0 TO numberElements-1 DO
  1902. expression := x.elements.GetExpression(i);
  1903. IF expression IS SyntaxTree.MathArrayExpression THEN
  1904. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1905. ELSE
  1906. position := expression.position;
  1907. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1908. x.elements.SetExpression(i,expression);
  1909. isValue := isValue & (expression.resolved # NIL);
  1910. END;
  1911. END;
  1912. END RecursivelySetExpression;
  1913. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1914. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1915. arrayType: SyntaxTree.MathArrayType;
  1916. BEGIN
  1917. numberElements := x.elements.Length();
  1918. baseType := NIL;
  1919. gsize := 0;
  1920. FOR i := 0 TO numberElements-1 DO
  1921. expression := x.elements.GetExpression(i);
  1922. IF expression IS SyntaxTree.MathArrayExpression THEN
  1923. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1924. IF i=0 THEN
  1925. gsize := size;
  1926. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1927. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1928. ELSE expression.SetType(baseType)
  1929. END;
  1930. ELSIF baseType = NIL THEN baseType := type;
  1931. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1932. END;
  1933. END;
  1934. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1935. arrayType.SetArrayBase(baseType);
  1936. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1937. RETURN ResolveType(arrayType);
  1938. END RecursivelySetType;
  1939. BEGIN
  1940. type := NIL;
  1941. RecursivelyFindType(x);
  1942. isValue := TRUE;
  1943. RecursivelySetExpression(x);
  1944. arrayType := RecursivelySetType(x);
  1945. x.SetType(arrayType);
  1946. IF isValue THEN
  1947. value := SyntaxTree.NewMathArrayValue(x.position);
  1948. value.SetArray(x);
  1949. x.SetResolved(value);
  1950. value.SetType(arrayType);
  1951. END;
  1952. x.SetType(arrayType);
  1953. resolvedExpression := x;
  1954. END VisitMathArrayExpression;
  1955. (** check and resolve unary expression **)
  1956. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1957. VAR
  1958. left: SyntaxTree.Expression;
  1959. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1960. bool: BOOLEAN;
  1961. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1962. value: SyntaxTree.Value;
  1963. BEGIN
  1964. type := SyntaxTree.invalidType;
  1965. left := ResolveExpression(unaryExpression.left);
  1966. unaryExpression.SetLeft(left);
  1967. operator := unaryExpression.operator;
  1968. result := unaryExpression;
  1969. IF ~system.operatorDefined[operator] THEN
  1970. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1971. RETURN
  1972. ELSIF left.type = NIL THEN
  1973. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1974. resolvedExpression := SyntaxTree.invalidExpression;
  1975. RETURN
  1976. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1977. RETURN
  1978. END;
  1979. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1980. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1981. END;
  1982. IF operatorCall # NIL THEN
  1983. result := operatorCall;
  1984. type := operatorCall.type;
  1985. (* admissible operators
  1986. Minus number, set
  1987. Not boolean
  1988. *)
  1989. ELSE
  1990. CASE unaryExpression.operator OF
  1991. |Scanner.Minus:
  1992. IF IsIntegerType(left.type.resolved) THEN
  1993. IF left.resolved # NIL THEN
  1994. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1995. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1996. result.SetResolved(value);
  1997. type := Global.GetIntegerType(system,int);
  1998. value.SetType(type);
  1999. ELSE
  2000. type := left.type
  2001. END
  2002. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2003. IF IsRealValue(left,real) THEN
  2004. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2005. result.SetResolved(value);
  2006. type := left.type;
  2007. value.SetType(type);
  2008. ELSE
  2009. type := left.type;
  2010. END;
  2011. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2012. IF IsSetValue(left,set) THEN
  2013. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2014. result.SetResolved(value);
  2015. type := left.type;
  2016. value.SetType(type);
  2017. ELSE
  2018. type := left.type;
  2019. END;
  2020. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2021. IF IsComplexValue(left, real, imaginary) THEN
  2022. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2023. result.SetResolved(value);
  2024. type := left.type;
  2025. value.SetType(type);
  2026. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2027. ELSE
  2028. type := left.type;
  2029. END
  2030. ELSE
  2031. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2032. END;
  2033. |Scanner.Not:
  2034. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2035. IF IsBooleanValue(left,bool) THEN
  2036. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2037. result.SetResolved(value);
  2038. type := system.booleanType;
  2039. value.SetType(type);
  2040. ELSE
  2041. type := system.booleanType;
  2042. END;
  2043. ELSE
  2044. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2045. END;
  2046. |Scanner.Plus:
  2047. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2048. result := left; type := left.type;
  2049. ELSE
  2050. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2051. END;
  2052. (* ADDRESS OF *)
  2053. |Scanner.Address:
  2054. IF HasAddress(left) THEN
  2055. type := system.addressType;
  2056. ELSE
  2057. type := SyntaxTree.invalidType;
  2058. Error(left.position,Diagnostics.Invalid,"has no address");
  2059. Printout.Info("par", left);
  2060. END;
  2061. (* SIZE OF *)
  2062. |Scanner.Size:
  2063. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2064. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2065. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2066. value := SyntaxTree.NewIntegerValue(left.position, int);
  2067. result.SetResolved(value);
  2068. type := Global.GetIntegerType(system,int);
  2069. value.SetType(type)
  2070. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2071. ELSE
  2072. (* for variables, system sizeof could represent the physically occupied size
  2073. determined via the type descriptor, implement that ? *)
  2074. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2075. END
  2076. (* ALIAS OF *)
  2077. |Scanner.Alias:
  2078. type := left.type.resolved;
  2079. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2080. type := SyntaxTree.invalidType;
  2081. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2082. END;
  2083. ELSE
  2084. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2085. END;
  2086. END;
  2087. result.SetType(type);
  2088. resolvedExpression := result
  2089. END VisitUnaryExpression;
  2090. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2091. VAR
  2092. result: SyntaxTree.Expression;
  2093. array: SyntaxTree.MathArrayExpression;
  2094. value: SyntaxTree.MathArrayValue;
  2095. isValue: BOOLEAN;
  2096. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2097. BEGIN
  2098. type := type.resolved;
  2099. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2100. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2101. END;
  2102. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2103. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2104. END;
  2105. RETURN type
  2106. END BaseType;
  2107. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2108. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2109. BEGIN
  2110. numberElements := x.elements.Length();
  2111. FOR i := 0 TO numberElements-1 DO
  2112. expression := x.elements.GetExpression(i);
  2113. IF expression IS SyntaxTree.MathArrayExpression THEN
  2114. array := SyntaxTree.NewMathArrayExpression(position);
  2115. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2116. expression := array;
  2117. ELSE
  2118. position := expression.position;
  2119. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2120. isValue := isValue & (expression.resolved # NIL);
  2121. END;
  2122. to.elements.AddExpression(expression);
  2123. END;
  2124. END RecursivelyConvert;
  2125. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2126. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2127. arrayType: SyntaxTree.MathArrayType;
  2128. BEGIN
  2129. numberElements := x.elements.Length();
  2130. baseType := NIL;
  2131. gsize := 0;
  2132. FOR i := 0 TO numberElements-1 DO
  2133. expression := x.elements.GetExpression(i);
  2134. IF expression IS SyntaxTree.MathArrayExpression THEN
  2135. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2136. IF i=0 THEN
  2137. gsize := size;
  2138. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2139. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2140. ELSE expression.SetType(baseType)
  2141. END;
  2142. ELSIF baseType = NIL THEN baseType := type;
  2143. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2144. END;
  2145. END;
  2146. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2147. arrayType.SetArrayBase(baseType);
  2148. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2149. RETURN ResolveType(arrayType);
  2150. END RecursivelySetType;
  2151. BEGIN
  2152. result := SyntaxTree.invalidExpression;
  2153. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2154. result := expression (* do not convert *)
  2155. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2156. isValue := TRUE;
  2157. type := BaseType(type);
  2158. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2159. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2160. value := SyntaxTree.NewMathArrayValue(array.position);
  2161. value.SetArray(array);
  2162. value.SetType(RecursivelySetType(array));
  2163. result := value;
  2164. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2165. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2166. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2167. IF result = NIL THEN
  2168. result := SyntaxTree.invalidExpression;
  2169. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2170. IF VerboseErrorMessage THEN
  2171. Printout.Info("expression",expression);
  2172. Printout.Info("type",type);
  2173. END;
  2174. END;
  2175. END;
  2176. RETURN result
  2177. END MathArrayConversion;
  2178. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2179. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2180. BEGIN
  2181. result := expression; type := type.resolved;
  2182. IF (expression IS SyntaxTree.IntegerValue) THEN
  2183. int := expression(SyntaxTree.IntegerValue).hvalue;
  2184. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2185. int := Global.ConvertSigned(int,system.SizeOf(type));
  2186. result := SyntaxTree.NewIntegerValue(position,int);
  2187. result.SetType(type);
  2188. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2189. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2190. result := SyntaxTree.NewIntegerValue(position,int);
  2191. result.SetType(type);
  2192. ELSIF (type IS SyntaxTree.FloatType) THEN
  2193. result := SyntaxTree.NewRealValue(expression.position,int);
  2194. result.SetType(type);
  2195. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2196. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2197. result.SetType(type);
  2198. ELSIF (type IS SyntaxTree.SetType) THEN
  2199. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2200. result.SetType(type);
  2201. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2202. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2203. result.SetType(type);
  2204. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2205. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2206. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2207. END;
  2208. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2209. result.SetType(type);
  2210. ELSIF (type IS SyntaxTree.PortType) THEN
  2211. result := ConvertValue(position, expression, system.integerType);
  2212. ELSE
  2213. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2214. result := SyntaxTree.invalidExpression;
  2215. IF VerboseErrorMessage THEN
  2216. Printout.Info("expression",expression);
  2217. Printout.Info("type",type);
  2218. END;
  2219. END;
  2220. ELSIF IsRealValue(expression,real) THEN
  2221. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2222. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2223. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2224. result.SetType(type);
  2225. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2226. int := ENTIERH(real);
  2227. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2228. result.SetType(type);
  2229. ELSIF (type IS SyntaxTree.FloatType) THEN
  2230. result := SyntaxTree.NewRealValue(position,real);
  2231. result.SetType(type);
  2232. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2233. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2234. result.SetType(type);
  2235. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2236. ELSIF (type IS SyntaxTree.PortType) THEN
  2237. result := ConvertValue(position, expression, system.integerType);
  2238. ELSE
  2239. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2240. result := SyntaxTree.invalidExpression;
  2241. END
  2242. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2243. IF (type IS SyntaxTree.ComplexType) THEN
  2244. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2245. result.SetType(type);
  2246. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2247. ELSE
  2248. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2249. result := SyntaxTree.invalidExpression;
  2250. END
  2251. ELSIF IsSetValue(expression,set) THEN
  2252. IF (type IS SyntaxTree.IntegerType) THEN
  2253. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2254. result.SetType(type);
  2255. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2256. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2257. result.SetType(type);
  2258. ELSIF (type IS SyntaxTree.PortType) THEN
  2259. result := ConvertValue(position, expression, system.integerType);
  2260. ELSE
  2261. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2262. result := SyntaxTree.invalidExpression;
  2263. END;
  2264. ELSIF IsStringValue(expression,string) THEN
  2265. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2266. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2267. result.SetType(type);
  2268. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2269. ELSE
  2270. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2271. result := SyntaxTree.invalidExpression;
  2272. END;
  2273. ELSIF IsCharacterValue(expression,char) THEN
  2274. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2275. string[0] := char; string[1] := 0X;
  2276. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2277. result := SyntaxTree.NewStringValue(expression.position,string);
  2278. result.SetType(type);
  2279. ELSIF (type IS SyntaxTree.ByteType) THEN
  2280. (* do not simply set the new type as this could invalidate types of constants *)
  2281. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2282. result.SetType(type)
  2283. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2284. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2285. result.SetType(type);
  2286. ELSIF (type IS SyntaxTree.SetType) THEN
  2287. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2288. result.SetType(type);
  2289. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2290. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2291. result.SetType(type);
  2292. ELSIF (type IS SyntaxTree.PortType) THEN
  2293. result := ConvertValue(position, expression, system.integerType);
  2294. ELSE
  2295. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2296. result := SyntaxTree.invalidExpression;
  2297. END;
  2298. ELSIF expression IS SyntaxTree.NilValue THEN
  2299. IF type IS SyntaxTree.AddressType THEN
  2300. result := SyntaxTree.NewIntegerValue(position,0);
  2301. result.SetType(type);
  2302. ELSE
  2303. result := expression;
  2304. END;
  2305. (* nothing to be done *)
  2306. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2307. result := MathArrayConversion(position, expression,type);
  2308. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2309. int := expression(SyntaxTree.EnumerationValue).value;
  2310. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2311. int := Global.ConvertSigned(int,system.SizeOf(type));
  2312. result := SyntaxTree.NewIntegerValue(position,int);
  2313. result.SetType(type);
  2314. ELSE
  2315. result := expression;
  2316. END;
  2317. (* nothing to be done *)
  2318. ELSE
  2319. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2320. IF VerboseErrorMessage THEN
  2321. Printout.Info("expression",expression);
  2322. Printout.Info("type",type);
  2323. END;
  2324. result := SyntaxTree.invalidExpression;
  2325. END;
  2326. RETURN result
  2327. END ConvertValue;
  2328. (**
  2329. return a conversion of an expression to a given type
  2330. - if expression is already of same type then return expression
  2331. - if incompatible conversion then report error and return invalidExpression
  2332. **)
  2333. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2334. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2335. BEGIN
  2336. type := type.resolved;
  2337. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2338. result := expression;
  2339. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2340. ELSIF expression = NIL THEN (* NIL expression *)
  2341. ELSIF expression.type = NIL THEN
  2342. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2343. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2344. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2345. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2346. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2347. ELSIF expression.resolved # NIL THEN (* value *)
  2348. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2349. IF value IS SyntaxTree.Value THEN
  2350. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2351. result.SetResolved(value(SyntaxTree.Value));
  2352. result.SetType(value.type);
  2353. ELSE
  2354. result := value
  2355. END;
  2356. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2357. expressionList := SyntaxTree.NewExpressionList();
  2358. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2359. typeDeclaration.SetDeclaredType(type);
  2360. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2361. typeSymbol.SetType(typeDeclaration.type);
  2362. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2363. expressionList.AddExpression(expression);
  2364. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2365. result.SetType(type);
  2366. ELSIF IsArrayStructuredObjectType(type) THEN
  2367. (* no type can be converted to an array-structured object type *)
  2368. HALT(100)
  2369. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2370. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2371. result := MathArrayConversion(position, expression,type);
  2372. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2373. expression := ConvertToMathArray(expression);
  2374. type := MathArrayStructureOfType(type);
  2375. result := MathArrayConversion(position, expression, type)
  2376. ELSE
  2377. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2378. END;
  2379. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2380. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2381. OR ~(type IS SyntaxTree.ArrayType) THEN
  2382. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2383. END;
  2384. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2385. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2386. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2387. (*skip, no conversion*)
  2388. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2389. (* skip, no conversion *)
  2390. ELSE
  2391. ASSERT(~(type IS SyntaxTree.RangeType));
  2392. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2393. ASSERT(type # NIL);
  2394. END;
  2395. RETURN result
  2396. END NewConversion;
  2397. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2398. BEGIN
  2399. IF CompatibleTo(system,expression.type, type) THEN
  2400. RETURN NewConversion(position, expression, type, NIL);
  2401. ELSE
  2402. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2403. RETURN SyntaxTree.invalidExpression
  2404. END;
  2405. END CompatibleConversion;
  2406. (**
  2407. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2408. **)
  2409. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2410. VAR leftType,rightType: SyntaxTree.Type;
  2411. BEGIN
  2412. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2413. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2414. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2415. (* error already handled *)
  2416. ELSE
  2417. leftType := left.type.resolved; rightType := right.type.resolved;
  2418. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2419. right := NewConversion(right.position, right, leftType, NIL);
  2420. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2421. left := NewConversion(left.position,left,rightType,NIL);
  2422. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2423. right := NewConversion(right.position, right, leftType, NIL);
  2424. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2425. left := NewConversion(left.position,left,rightType,NIL);
  2426. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2427. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2428. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2429. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2430. ELSIF
  2431. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2432. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2433. (* must be the case LONGREAL / COMPLEX ) *)
  2434. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2435. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2436. ELSE
  2437. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2438. END;
  2439. END;
  2440. END ConvertOperands;
  2441. (** find and return best operator matching to parameter list (nil, if none)
  2442. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2443. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2444. **)
  2445. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2446. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2447. identifier: SyntaxTree.Identifier;
  2448. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2449. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2450. BEGIN
  2451. operator := scope.firstOperator;
  2452. WHILE(operator # NIL) DO
  2453. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2454. procedureType := operator.type(SyntaxTree.ProcedureType);
  2455. distance := Distance(system, procedureType,actualParameters);
  2456. IF (distance < Infinity) THEN
  2457. IF returnType # NIL THEN
  2458. IF procedureType.returnType = NIL THEN
  2459. distance := Infinity
  2460. ELSE
  2461. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2462. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2463. END;
  2464. END;
  2465. END;
  2466. (*
  2467. IF distance < Infinity THEN
  2468. TRACE(distance, operator);
  2469. Printout.Info("potential operator",operator);
  2470. ELSE
  2471. Printout.Info("operator not possible",operator);
  2472. END;
  2473. *)
  2474. IF distance < bestDistance THEN
  2475. bestDistance := distance;
  2476. bestOperator := operator;
  2477. END;
  2478. END;
  2479. operator := operator.nextOperator;
  2480. END;
  2481. (*
  2482. Printout.Info("taken operator",bestOperator);
  2483. *)
  2484. END FindInScope;
  2485. BEGIN
  2486. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2487. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2488. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2489. import := currentScope.ownerModule.moduleScope.firstImport;
  2490. WHILE (bestDistance > 0) & (import # NIL) DO
  2491. IF import.module # NIL THEN
  2492. identifier := Global.GetIdentifier(operator,import.module.case);
  2493. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2494. END;
  2495. import := import.nextImport;
  2496. END;
  2497. RETURN bestOperator
  2498. END FindOperator;
  2499. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2500. BEGIN
  2501. currentScope := scope;
  2502. END SetCurrentScope;
  2503. (**
  2504. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2505. - handle LEN and DIM operator for array-structured object types
  2506. - find operator, if found then
  2507. - if in other module then add import designator
  2508. - create symbol designator for operator
  2509. - if error then return invalidExpression, if no operator then return NIL
  2510. **)
  2511. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2512. VAR
  2513. operator: SyntaxTree.Operator;
  2514. import: SyntaxTree.Import;
  2515. expression, result: SyntaxTree.Expression;
  2516. designator: SyntaxTree.Designator;
  2517. actualParameters, tempList: SyntaxTree.ExpressionList;
  2518. recordType: SyntaxTree.RecordType;
  2519. castReturnType : SyntaxTree.MathArrayType;
  2520. BEGIN
  2521. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2522. result := SyntaxTree.invalidExpression
  2523. ELSIF leftExpression = NIL THEN
  2524. result := NIL
  2525. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2526. (* LEN or DIM operator on array-structured object type *)
  2527. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2528. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2529. IF recordType.arrayAccessOperators.len = NIL THEN
  2530. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2531. result := SyntaxTree.invalidExpression
  2532. ELSE
  2533. ASSERT(leftExpression IS SyntaxTree.Designator);
  2534. designator := leftExpression(SyntaxTree.Designator);
  2535. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2536. ASSERT(expression IS SyntaxTree.Designator);
  2537. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2538. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2539. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2540. result := designator
  2541. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2542. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2543. tempList := SyntaxTree.NewExpressionList();
  2544. tempList.AddExpression(rightExpression);
  2545. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2546. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2547. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2548. tempList := SyntaxTree.NewExpressionList();
  2549. tempList.AddExpression(designator);
  2550. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2551. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2552. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2553. END
  2554. END;
  2555. ELSE
  2556. (* import OCArrayBase if needed *)
  2557. IF ~arrayBaseImported THEN
  2558. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2559. (* operators on complex numbers *)
  2560. ImportModule(Global.ArrayBaseName,position);
  2561. arrayBaseImported := TRUE;
  2562. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2563. IF op = Global.Dim THEN
  2564. (* not existing in OCArrayBase *)
  2565. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2566. (* not existing in OCArrayBase *)
  2567. ELSE
  2568. ImportModule(Global.ArrayBaseName,position);
  2569. arrayBaseImported := TRUE;
  2570. END
  2571. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2572. ImportModule(Global.ArrayBaseName,position);
  2573. arrayBaseImported := TRUE
  2574. END;
  2575. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2576. (* LEN(RANGE) *)
  2577. ImportModule(Global.ArrayBaseName,position);
  2578. arrayBaseImported := TRUE;
  2579. END;
  2580. END;
  2581. actualParameters := SyntaxTree.NewExpressionList();
  2582. actualParameters.AddExpression(leftExpression);
  2583. IF rightExpression # NIL THEN
  2584. actualParameters.AddExpression(rightExpression)
  2585. END;
  2586. operator := FindOperator(system,op,actualParameters,resultType);
  2587. IF operator # NIL THEN
  2588. designator := NIL;
  2589. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2590. import := currentScope.ownerModule.moduleScope.firstImport;
  2591. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2592. import := import.nextImport;
  2593. END;
  2594. expression := NewSymbolDesignator(position,NIL,import);
  2595. designator := expression(SyntaxTree.Designator);
  2596. END;
  2597. expression := NewSymbolDesignator(position,designator,operator);
  2598. designator := expression(SyntaxTree.Designator);
  2599. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2600. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2601. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2602. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2603. result.SetType(castReturnType);
  2604. END;
  2605. ELSE
  2606. result := NIL;
  2607. END;
  2608. END;
  2609. RETURN result
  2610. END NewOperatorCall;
  2611. (** check and resolve binary expression **)
  2612. (*! clean up *)
  2613. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2614. VAR left,right,result: SyntaxTree.Expression;
  2615. leftType, rightType: SyntaxTree.Type;
  2616. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2617. cl,cr: CHAR;
  2618. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2619. type: SyntaxTree.Type;
  2620. value: SyntaxTree.Value;
  2621. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2622. integerConstantFolding: BOOLEAN;
  2623. list: SyntaxTree.ExpressionList;
  2624. PROCEDURE NewBool(v: BOOLEAN);
  2625. BEGIN
  2626. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2627. value.SetType(system.booleanType);
  2628. result.SetResolved(value);
  2629. type := system.booleanType
  2630. END NewBool;
  2631. PROCEDURE NewSet(v: SET);
  2632. BEGIN
  2633. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2634. value.SetType(system.setType);
  2635. result.SetResolved(value);
  2636. type := system.setType;
  2637. END NewSet;
  2638. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2639. BEGIN
  2640. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2641. (* type cast to "larger" type only if the value is still in the range *)
  2642. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2643. value.SetType(t);
  2644. END;
  2645. result.SetResolved(value);
  2646. type := value.type;
  2647. END NewInteger;
  2648. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2649. BEGIN
  2650. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2651. value.SetType(t);
  2652. result.SetResolved(value);
  2653. type := t;
  2654. END NewReal;
  2655. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2656. BEGIN
  2657. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2658. value.SetType(t);
  2659. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2660. result.SetResolved(value);
  2661. type := t;
  2662. END NewComplex;
  2663. BEGIN
  2664. type := SyntaxTree.invalidType;
  2665. left := ResolveExpression(binaryExpression.left);
  2666. right := ResolveExpression(binaryExpression.right);
  2667. binaryExpression.SetLeft(left);
  2668. binaryExpression.SetRight(right);
  2669. result := binaryExpression;
  2670. operator := binaryExpression.operator;
  2671. IF ~system.operatorDefined[operator] THEN
  2672. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2673. result := SyntaxTree.invalidExpression;
  2674. RETURN
  2675. END;
  2676. IF left.type = NIL THEN
  2677. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2678. result := SyntaxTree.invalidExpression;
  2679. RETURN;
  2680. END;
  2681. IF right.type = NIL THEN
  2682. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2683. result := SyntaxTree.invalidExpression;
  2684. RETURN;
  2685. END;
  2686. leftType := left.type.resolved; rightType := right.type.resolved;
  2687. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2688. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2689. END;
  2690. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2691. list := SyntaxTree.NewExpressionList();
  2692. list.AddExpression(right);
  2693. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2694. END;
  2695. IF operatorCall # NIL THEN
  2696. result := operatorCall;
  2697. type := operatorCall.type;
  2698. (* admissible operators:
  2699. Times, Plus, Minus numeric numeric numeric
  2700. set set set
  2701. Slash numeric numeric real /complex
  2702. set set set
  2703. Div , Mod integer integer integer
  2704. And, Or bool bool bool
  2705. Equal, Unequal basic basic bool
  2706. pointer pointer bool
  2707. object object bool
  2708. record record bool
  2709. string string bool
  2710. enumerator enumerator bool
  2711. Less, LessEqual,
  2712. Greater, GreaterEqual integer/real integer/real bool
  2713. enumerator enumerator bool
  2714. In integer set bool
  2715. Is pointer type bool
  2716. object type bool
  2717. record type bool
  2718. Upto: special abbreviation for a..b
  2719. *)
  2720. ELSIF (left.type = NIL) THEN
  2721. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2722. D.Str("nil type in "); D.Type(left); D.Ln;
  2723. result := SyntaxTree.invalidExpression;
  2724. ELSIF (right.type = NIL) THEN
  2725. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2726. result := SyntaxTree.invalidExpression;
  2727. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2728. result := SyntaxTree.invalidExpression;
  2729. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2730. HALT(100);
  2731. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2732. type := system.booleanType;
  2733. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2734. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2735. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2736. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2737. IF VerboseErrorMessage THEN
  2738. Printout.Info("left",left);
  2739. Printout.Info("right",right);
  2740. END;
  2741. ELSIF IsUnsafePointer(left.type) THEN
  2742. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2743. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2744. NewBool(TRUE)
  2745. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2746. NewBool(TRUE);
  2747. ELSIF IsUnextensibleRecord(left) THEN
  2748. NewBool(FALSE)
  2749. END
  2750. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2751. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2752. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2753. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2754. ELSIF operator = Scanner.In THEN (* left IN right *)
  2755. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2756. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2757. NewBool(il IN sr);
  2758. ELSE
  2759. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2760. left := NewConversion(left.position, left, system.longintType,NIL);
  2761. binaryExpression.SetLeft(left)
  2762. END;
  2763. type := system.booleanType;
  2764. END
  2765. ELSE
  2766. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2767. END
  2768. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2769. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2770. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2771. END;
  2772. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2773. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2774. END
  2775. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2776. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2777. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2778. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2779. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2780. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2781. ConvertOperands(left, right);
  2782. binaryExpression.SetLeft(left);
  2783. binaryExpression.SetRight(right);
  2784. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2785. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2786. END;
  2787. type := system.booleanType;
  2788. ELSE
  2789. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2790. END
  2791. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2792. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2793. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2794. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2795. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2796. CASE operator OF
  2797. |Scanner.Equal: NewBool(strl^=strr^);
  2798. |Scanner.Unequal:NewBool(strl^#strr^);
  2799. |Scanner.Less: NewBool(strl^<strr^);
  2800. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2801. |Scanner.Greater: NewBool(strl^>strr^);
  2802. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2803. ELSE
  2804. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2805. END;
  2806. END;
  2807. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2808. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2809. type := system.booleanType
  2810. ELSE
  2811. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2812. END;
  2813. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2814. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2815. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2816. type := system.booleanType;
  2817. ELSE
  2818. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2819. END
  2820. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2821. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2822. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2823. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2824. type := system.booleanType
  2825. ELSE
  2826. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2827. END;
  2828. ELSE
  2829. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2830. END;
  2831. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2832. type := system.booleanType;
  2833. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2834. type := system.booleanType;
  2835. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2836. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2837. THEN
  2838. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2839. IF (leftType # rightType) THEN
  2840. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2841. ConvertOperands(left,right); (* operands must be of the same type here *)
  2842. END;
  2843. binaryExpression.SetLeft(left);
  2844. binaryExpression.SetRight(right);
  2845. leftType := left.type.resolved;
  2846. rightType := right.type.resolved;
  2847. END;
  2848. type := leftType;
  2849. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2850. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2851. IF VerboseErrorMessage THEN
  2852. Printout.Info("left",left);
  2853. Printout.Info("right",right);
  2854. END;
  2855. ELSIF IsIntegerType(leftType) THEN
  2856. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2857. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2858. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2859. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2860. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2861. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2862. END;
  2863. END;
  2864. (* constant folding *)
  2865. (* bootstrap64
  2866. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2867. CASE operator OF
  2868. |Scanner.Plus: NewInteger(il+ir,left.type);
  2869. |Scanner.Minus: NewInteger(il-ir,left.type);
  2870. |Scanner.Times: NewInteger(il*ir,left.type);
  2871. |Scanner.Slash:
  2872. IF ir # 0 THEN
  2873. NewReal(il/ir, system.realType);
  2874. END;
  2875. |Scanner.Mod:
  2876. IF ir > 0 THEN
  2877. NewInteger(il MOD ir,left.type);
  2878. END;
  2879. |Scanner.Div:
  2880. IF ir > 0 THEN
  2881. NewInteger(il DIV ir,left.type);
  2882. END;
  2883. |Scanner.Equal: NewBool(il=ir);
  2884. |Scanner.Unequal:NewBool(il#ir);
  2885. |Scanner.Less: NewBool(il<ir);
  2886. |Scanner.LessEqual: NewBool(il<=ir);
  2887. |Scanner.Greater: NewBool(il>ir);
  2888. |Scanner.GreaterEqual: NewBool(il>=ir);
  2889. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2890. END;
  2891. ELS*)
  2892. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2893. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2894. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2895. CASE operator OF
  2896. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2897. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2898. |Scanner.Times: NewInteger(hl*hr,left.type);
  2899. |Scanner.Slash:
  2900. IF hr = 0 THEN
  2901. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2902. ELSE
  2903. IF type.sizeInBits = 64 THEN
  2904. NewReal(hl/hr,system.longrealType);
  2905. ELSE
  2906. NewReal(hl/hr,system.realType)
  2907. END
  2908. END;
  2909. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2910. |Scanner.Mod:
  2911. IF hr = 0 THEN
  2912. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2913. ELSE
  2914. NewInteger(hl MOD hr, left.type);
  2915. (* bootstrap64
  2916. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2917. *)
  2918. END;
  2919. |Scanner.Div:
  2920. IF hr = 0 THEN
  2921. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2922. ELSE
  2923. NewInteger(hl DIV hr, left.type);
  2924. (* bootstrap64
  2925. NewInteger(Machine.DivH(hl,hr),left.type);
  2926. *)
  2927. END;
  2928. (* *)
  2929. |Scanner.Equal: NewBool(hl=hr);
  2930. |Scanner.Unequal: NewBool(hl#hr);
  2931. |Scanner.Less: NewBool(hl<hr);
  2932. |Scanner.LessEqual: NewBool(hl<=hr);
  2933. |Scanner.Greater: NewBool(hl>hr);
  2934. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2935. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2936. END;
  2937. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2938. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2939. type := left.type
  2940. ELSIF (operator = Scanner.Slash) THEN
  2941. left := NewConversion(left.position,left,system.realType,NIL);
  2942. right := NewConversion(right.position,right,system.realType,NIL);
  2943. binaryExpression.SetLeft(left);
  2944. binaryExpression.SetRight(right);
  2945. type := system.realType
  2946. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2947. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2948. type := system.booleanType
  2949. ELSE
  2950. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2951. END;
  2952. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2953. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2954. CASE operator OF
  2955. |Scanner.Plus: NewReal(rl+rr,leftType);
  2956. |Scanner.Minus: NewReal(rl-rr,leftType);
  2957. |Scanner.Times:NewReal(rl*rr,leftType);
  2958. |Scanner.Slash:
  2959. IF rr = 0 THEN
  2960. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2961. ELSE
  2962. NewReal(rl/rr,leftType);
  2963. END
  2964. |Scanner.Equal: NewBool(rl=rr);
  2965. |Scanner.Unequal: NewBool(rl#rr);
  2966. |Scanner.Less: NewBool(rl<rr);
  2967. |Scanner.LessEqual: NewBool(rl<=rr);
  2968. |Scanner.Greater: NewBool(rl>rr);
  2969. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2970. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2971. END;
  2972. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2973. type := left.type
  2974. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2975. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2976. type := system.booleanType
  2977. ELSE
  2978. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2979. IF VerboseErrorMessage THEN
  2980. Printout.Info("left",left);
  2981. Printout.Info("right",right);
  2982. END;
  2983. END;
  2984. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2985. CASE operator OF
  2986. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2987. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2988. ELSE
  2989. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2990. IF VerboseErrorMessage THEN
  2991. Printout.Info("left", left);
  2992. Printout.Info("right", right)
  2993. END;
  2994. END;
  2995. IF ~error THEN
  2996. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2997. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2998. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2999. (* do constant folding *)
  3000. CASE operator OF
  3001. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3002. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3003. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3004. |Scanner.Slash:
  3005. divisor := c * c + d * d;
  3006. ASSERT(divisor # 0);
  3007. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3008. |Scanner.Equal: NewBool((a = c) & (b = d))
  3009. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3010. END
  3011. END
  3012. END
  3013. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3014. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3015. CASE operator OF
  3016. |Scanner.And: NewBool(bl & br);
  3017. |Scanner.Or: NewBool(bl OR br);
  3018. |Scanner.Equal: NewBool(bl = br);
  3019. |Scanner.Unequal: NewBool(bl # br);
  3020. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3021. END;
  3022. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3023. type := system.booleanType
  3024. ELSE
  3025. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3026. END;
  3027. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3028. (* constant folding *)
  3029. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3030. IF operator = Scanner.Equal THEN
  3031. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3032. ELSIF operator = Scanner.Unequal THEN
  3033. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3034. END;
  3035. END;
  3036. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3037. type := system.booleanType;
  3038. ELSE
  3039. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3040. END;
  3041. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3042. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3043. CASE operator OF
  3044. |Scanner.Plus: NewSet(sl + sr);
  3045. |Scanner.Minus: NewSet(sl - sr);
  3046. |Scanner.Times: NewSet(sl * sr);
  3047. |Scanner.Slash: NewSet(sl / sr);
  3048. |Scanner.Equal: NewBool(sl=sr);
  3049. |Scanner.Unequal: NewBool(sl#sr);
  3050. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3051. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3052. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3053. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3054. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3055. END;
  3056. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3057. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3058. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3059. THEN
  3060. type := system.booleanType
  3061. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3062. type := left.type
  3063. ELSE
  3064. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3065. END;
  3066. ELSIF IsCharacterType(left.type) THEN
  3067. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3068. CASE operator OF
  3069. |Scanner.Equal: NewBool(cl=cr);
  3070. |Scanner.Unequal: NewBool(cl#cr);
  3071. |Scanner.Less: NewBool(cl<cr);
  3072. |Scanner.LessEqual: NewBool(cl<=cr);
  3073. |Scanner.Greater: NewBool(cl>cr);
  3074. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3075. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3076. END;
  3077. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3078. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3079. type := system.booleanType
  3080. ELSE
  3081. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3082. END;
  3083. ELSE
  3084. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3085. END;
  3086. ELSE
  3087. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3088. END;
  3089. IF type = SyntaxTree.invalidType THEN
  3090. result := SyntaxTree.invalidExpression
  3091. ELSE
  3092. result.SetType(type)
  3093. END;
  3094. resolvedExpression := result
  3095. END VisitBinaryExpression;
  3096. (** resolve a range expression of the from <<first .. last BY step>>
  3097. - depending on the context different things are checked:
  3098. ArrayIndex:
  3099. - components must be integers
  3100. - replace missing lower bound with 0
  3101. - replace missing upper bound with MAX(LONGINT)
  3102. - replace missing step size with 1
  3103. SetElement:
  3104. - components must be integers
  3105. - replace missing lower bound with 0
  3106. - replace missing upper bound with MAX(SET)
  3107. - must not have step size
  3108. CaseGuard:
  3109. - components must be constant
  3110. - components must be integers or characters
  3111. - must have lower and upper bound present
  3112. - components are made compatible
  3113. - must not have step size
  3114. - if error: return invalidExpression
  3115. **)
  3116. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3117. VAR
  3118. hasError: BOOLEAN;
  3119. first, last, step: SyntaxTree.Expression;
  3120. BEGIN
  3121. hasError := FALSE;
  3122. first := x.first;
  3123. last := x.last;
  3124. step := x.step;
  3125. (* check lower bound *)
  3126. IF x.context = SyntaxTree.CaseGuard THEN
  3127. IF first = NIL THEN
  3128. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3129. hasError := TRUE
  3130. ELSE
  3131. first := ResolveExpression(first);
  3132. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3133. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3134. hasError := TRUE
  3135. ELSE
  3136. IF first IS SyntaxTree.StringValue THEN
  3137. (* add conversion from string to character *)
  3138. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3139. END
  3140. END;
  3141. (* check if expression is constant *)
  3142. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3143. (* error already reported *)
  3144. hasError := TRUE
  3145. END
  3146. END
  3147. ELSE (* ArrayIndex, SetElement *)
  3148. IF first = NIL THEN
  3149. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3150. END;
  3151. first := ResolveExpression(first);
  3152. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3153. first := NewConversion(first.position, first, system.longintType, NIL)
  3154. ELSE
  3155. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3156. hasError := TRUE
  3157. END
  3158. END;
  3159. (* check upper bound *)
  3160. IF x.context = SyntaxTree.CaseGuard THEN
  3161. IF last = NIL THEN
  3162. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3163. hasError := TRUE
  3164. ELSE
  3165. last := ResolveExpression(last);
  3166. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3167. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3168. hasError := TRUE
  3169. ELSE
  3170. IF last IS SyntaxTree.StringValue THEN
  3171. (* add conversion from string to character *)
  3172. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3173. END
  3174. END;
  3175. (* check if expression is constant *)
  3176. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3177. (* error already reported *)
  3178. hasError := TRUE
  3179. ELSE
  3180. (* try to make lower and upper bound compatible *)
  3181. ConvertOperands(first, last);
  3182. IF first.type.resolved # last.type.resolved THEN
  3183. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3184. hasError := TRUE
  3185. END
  3186. END
  3187. END
  3188. ELSE (* ArrayIndex, SetElement *)
  3189. IF last = NIL THEN
  3190. IF x.context = SyntaxTree.ArrayIndex THEN
  3191. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3192. ELSE
  3193. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3194. END
  3195. END;
  3196. last := ResolveExpression(last);
  3197. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3198. last := NewConversion(last.position, last, system.longintType, NIL)
  3199. ELSE
  3200. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3201. hasError := TRUE
  3202. END
  3203. END;
  3204. (* check step size *)
  3205. IF x.context = SyntaxTree.ArrayIndex THEN
  3206. IF step = NIL THEN
  3207. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3208. END;
  3209. step := ResolveExpression(step);
  3210. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3211. step := NewConversion(step.position, step, system.longintType, NIL)
  3212. ELSE
  3213. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3214. hasError := TRUE
  3215. END
  3216. ELSE (* SetElement, CaseGuard *)
  3217. IF step # NIL THEN
  3218. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3219. hasError := TRUE
  3220. END
  3221. END;
  3222. IF hasError THEN
  3223. resolvedExpression := SyntaxTree.invalidExpression
  3224. ELSE
  3225. x.SetFirst(first);
  3226. x.SetLast(last);
  3227. x.SetStep(step);
  3228. x.SetType(system.rangeType);
  3229. resolvedExpression := x;
  3230. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3231. END
  3232. END VisitRangeExpression;
  3233. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3234. BEGIN
  3235. x.SetType(NIL);
  3236. resolvedExpression := x;
  3237. END VisitTensorRangeExpression;
  3238. (** resolve the expression d and return result as designator
  3239. - resolve expression
  3240. - if expression is a designator then return designator else error message and return invalidDesignator
  3241. **)
  3242. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3243. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3244. BEGIN
  3245. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3246. resolved := ResolveExpression(d);
  3247. IF resolved = SyntaxTree.invalidExpression THEN
  3248. (* error should already have been reported *)
  3249. result := SyntaxTree.invalidDesignator;
  3250. ELSIF resolved IS SyntaxTree.Designator THEN
  3251. result := resolved(SyntaxTree.Designator);
  3252. ELSE
  3253. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3254. result := SyntaxTree.invalidDesignator;
  3255. END;
  3256. (* result.type might be nil. *)
  3257. RETURN result
  3258. END ResolveDesignator;
  3259. (**
  3260. symbol designator generated in this module
  3261. nothing to be resolved
  3262. **)
  3263. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3264. BEGIN
  3265. resolvedExpression := x;
  3266. END VisitSymbolDesignator;
  3267. (**
  3268. self designator generated in this module
  3269. nothing to be resolved
  3270. **)
  3271. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3272. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3273. BEGIN
  3274. (* check if in record scope *)
  3275. scope := currentScope;
  3276. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3277. scope := scope.outerScope;
  3278. END;
  3279. IF scope = NIL THEN (* in module scope *)
  3280. x.SetType(system.anyType);
  3281. ELSIF scope IS SyntaxTree.CellScope THEN
  3282. cell := scope(SyntaxTree.CellScope).ownerCell;
  3283. x.SetType(cell);
  3284. ELSE (* in record scope *)
  3285. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3286. IF (record # NIL) & (record.pointerType # NIL) THEN
  3287. type := ResolveType(record.pointerType);
  3288. x.SetType(type);
  3289. ELSE
  3290. x.SetType(record);
  3291. END;
  3292. END;
  3293. resolvedExpression := x;
  3294. END VisitSelfDesignator;
  3295. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3296. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3297. BEGIN
  3298. scope := currentScope;
  3299. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3300. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3301. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3302. returnType := procedureType.returnType;
  3303. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3304. THEN
  3305. x.SetType(returnType);
  3306. ELSE
  3307. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3308. x.SetType(SyntaxTree.invalidType);
  3309. END;
  3310. ELSE
  3311. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3312. x.SetType(SyntaxTree.invalidType);
  3313. END;
  3314. x.SetAssignable(TRUE);
  3315. resolvedExpression := x;
  3316. END VisitResultDesignator;
  3317. (**
  3318. return symbol designator as an expression
  3319. - if symbol is a constant then return the constant value expression
  3320. - else
  3321. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3322. - if symbol is a guarded variable then return a TypeGuardDesignator
  3323. - else return a symbol designator
  3324. **)
  3325. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3326. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3327. guardType: SyntaxTree.Type;
  3328. BEGIN
  3329. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3330. result := SyntaxTree.invalidExpression;
  3331. ASSERT(symbol # NIL);
  3332. (*
  3333. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3334. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3335. Error(position,Diagnostics.Invalid,"type not allowed here");
  3336. ELS *)
  3337. (* not needed any more as values are stored in the expression
  3338. IF symbol IS SyntaxTree.Constant THEN
  3339. result := symbol(SyntaxTree.Constant).value
  3340. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3341. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3342. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3343. ELSE
  3344. result := symbol(SyntaxTree.Constant).value
  3345. END;
  3346. ELSE
  3347. *)
  3348. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3349. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3350. THEN
  3351. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3352. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3353. left := NewDereferenceDesignator(position,left);
  3354. left.SetHidden(TRUE);
  3355. END;
  3356. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3357. scope := currentScope;
  3358. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3359. scope := scope.outerScope;
  3360. END;
  3361. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3362. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3363. END;
  3364. END;
  3365. 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);
  3366. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3367. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3368. ELSE
  3369. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3370. END;
  3371. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3372. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3373. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3374. result.SetType(symbol.type);
  3375. result.SetAssignable(assignable);
  3376. symbol.MarkUsed;
  3377. IF symbol IS SyntaxTree.Constant THEN
  3378. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3379. END;
  3380. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3381. variableAccessed := TRUE
  3382. END;
  3383. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3384. IF GetGuard(symbol,guardType) THEN
  3385. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3386. END;
  3387. END;
  3388. ASSERT(result.type # NIL);
  3389. RETURN result
  3390. END NewSymbolDesignator;
  3391. (** check and resolve an identifier designator "identifier"
  3392. - if identifier = self then return SelfDesignator
  3393. - else find symbol in current scope
  3394. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3395. **)
  3396. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3397. VAR symbol: SyntaxTree.Symbol;
  3398. BEGIN
  3399. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3400. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3401. IF symbol # NIL THEN
  3402. ResolveSymbol(symbol);
  3403. ASSERT(symbol.type # NIL);
  3404. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3405. ELSE
  3406. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3407. IF VerboseErrorMessage THEN
  3408. Printout.Info("undeclared identifier designator",identifierDesignator);
  3409. END;
  3410. resolvedExpression := SyntaxTree.invalidDesignator;
  3411. END;
  3412. END VisitIdentifierDesignator;
  3413. (** check and resolve a selector designator of the form left.designator
  3414. - if left is a pointer type then do auto dereferenciation
  3415. - left denotes a search scope:
  3416. - if left type is import type then set search scope to respective module
  3417. - if left type is enumeration type then set search scope to respective enumeration scope
  3418. - elsif left type is record type then set search scope to record scope
  3419. - search symbol in computed scope
  3420. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3421. **)
  3422. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3423. VAR
  3424. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3425. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3426. BEGIN
  3427. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3428. left := ResolveDesignator(selectorDesignator.left);
  3429. result := SyntaxTree.invalidDesignator;
  3430. IF left # NIL THEN
  3431. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3432. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3433. END;
  3434. scope := NIL;
  3435. IF left.type = NIL THEN
  3436. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3437. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3438. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3439. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3440. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3441. module := symbol(SyntaxTree.Import).module;
  3442. IF module # NIL THEN
  3443. scope := module.moduleScope
  3444. ELSE
  3445. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3446. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3447. END;
  3448. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3449. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3450. ASSERT(scope # NIL)
  3451. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3452. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3453. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3454. IF type IS SyntaxTree.EnumerationType THEN
  3455. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3456. ELSE
  3457. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3458. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3459. END;
  3460. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3461. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3462. ELSE
  3463. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3464. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3465. END;
  3466. symbol := NIL;
  3467. IF scope # NIL THEN
  3468. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3469. IF symbol # NIL THEN
  3470. ResolveSymbol(symbol);
  3471. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3472. symbol.MarkUsed
  3473. ELSE
  3474. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3475. IF VerboseErrorMessage THEN
  3476. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3477. Printout.Info("scope", scope);
  3478. Printout.Info("left", left);
  3479. Printout.Info("undeclared identifier",selectorDesignator);
  3480. Printout.Info("left resolved designator",left);
  3481. END
  3482. END;
  3483. END;
  3484. END;
  3485. resolvedExpression := result;
  3486. END VisitSelectorDesignator;
  3487. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3488. VAR len,idx: LONGINT;
  3489. BEGIN
  3490. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3491. IF idx < 0 THEN
  3492. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3493. ELSE
  3494. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3495. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3496. END;
  3497. END;
  3498. END;
  3499. END IndexCheck;
  3500. (*
  3501. - if index designator has not type, use newBaseType as its type
  3502. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3503. - special rule: if static array of dynamic array occurs, make it all dynamic
  3504. index designator type: new base type: new index designator type:
  3505. NIL z z
  3506. ARRAY [x, y] z ARRAY [x, y] OF z
  3507. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3508. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3509. *)
  3510. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3511. VAR
  3512. mathArrayType: SyntaxTree.MathArrayType;
  3513. makeDynamic: BOOLEAN;
  3514. BEGIN
  3515. IF indexDesignator.type = NIL THEN
  3516. indexDesignator.SetType(newBaseType)
  3517. ELSE
  3518. (* index designator must be a of math array type *)
  3519. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3520. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3521. (* determine if all arrays have to be made dynamic *)
  3522. makeDynamic :=
  3523. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3524. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3525. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3526. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3527. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3528. END;
  3529. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3530. mathArrayType.SetArrayBase(newBaseType)
  3531. END
  3532. END SetIndexBaseType;
  3533. (** check and append index list element to index designator of math array
  3534. - check validity of single index or array range
  3535. - compute new type
  3536. - if range then create new array type (calculate length of resulting array)
  3537. - otherwise take sourceArray.arrayBase as new type
  3538. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3539. **)
  3540. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3541. VAR
  3542. targetArray: SyntaxTree.MathArrayType;
  3543. first, last, step: SyntaxTree.Expression;
  3544. firstValue, lastValue, stepValue, length: LONGINT;
  3545. rangeExpression: SyntaxTree.RangeExpression;
  3546. BEGIN
  3547. IF indexListItem.type = SyntaxTree.invalidType THEN
  3548. (* error already handled *)
  3549. indexDesignator.parameters.AddExpression(indexListItem)
  3550. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3551. indexDesignator.HasRange;
  3552. indexDesignator.HasTensorRange;
  3553. indexDesignator.parameters.AddExpression(indexListItem);
  3554. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3555. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3556. IndexCheck(indexListItem, sourceArray.length);
  3557. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3558. indexDesignator.parameters.AddExpression(indexListItem)
  3559. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3560. indexDesignator.HasRange;
  3561. (* if the range is given as an array range expression, check the validity of its components *)
  3562. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3563. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3564. first := rangeExpression.first;
  3565. last := rangeExpression.last;
  3566. step := rangeExpression.step;
  3567. (* perform static checks on range components *)
  3568. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3569. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3570. END;
  3571. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3572. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3573. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3574. END
  3575. END;
  3576. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3577. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3578. END;
  3579. (* add conversions to size type *)
  3580. (* TODO: needed? *)
  3581. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3582. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3583. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3584. END;
  3585. IF indexDesignator.hasTensorRange THEN
  3586. (* the index designator's base type is a tensor: leave it as is *)
  3587. ELSE
  3588. (* append a new math array to the index designator's base type *)
  3589. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3590. IF ~error THEN
  3591. (*
  3592. (* optimization: calculate length of target array for static ranges *)
  3593. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3594. IF IsStaticallyOpenRange(rangeExpression) THEN
  3595. (* range is open ('*'): reuse source array length as target array length *)
  3596. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3597. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3598. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3599. IF lastValue = MAX(LONGINT) THEN
  3600. IF IsIntegerValue(sourceArray.length, length) THEN
  3601. lastValue := length - 1;
  3602. isStaticTargetArrayLength := TRUE
  3603. ELSE
  3604. isStaticTargetArrayLength := FALSE
  3605. END
  3606. ELSE
  3607. isStaticTargetArrayLength := TRUE
  3608. END;
  3609. IF isStaticTargetArrayLength THEN
  3610. (* calculate static target array length *)
  3611. IF firstValue > lastValue THEN
  3612. length := 0
  3613. ELSE
  3614. length := 1 + lastValue - firstValue;
  3615. IF length MOD stepValue = 0 THEN
  3616. length := length DIV stepValue
  3617. ELSE
  3618. length := length DIV stepValue + 1
  3619. END
  3620. END;
  3621. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3622. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3623. ASSERT(targetArray.form = SyntaxTree.Static)
  3624. END
  3625. END
  3626. END
  3627. *)
  3628. END;
  3629. SetIndexBaseType(indexDesignator, targetArray)
  3630. END;
  3631. indexDesignator.parameters.AddExpression(indexListItem)
  3632. ELSE
  3633. Error(position, Diagnostics.Invalid,"invalid index list item");
  3634. END;
  3635. END AppendMathIndex;
  3636. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3637. VAR parameters: SyntaxTree.ExpressionList;
  3638. BEGIN
  3639. parameters := index.parameters;
  3640. IF (expression.type = NIL) THEN
  3641. Error(position,Diagnostics.Invalid,"invalid index");
  3642. ELSIF IsIntegerType(expression.type.resolved) THEN
  3643. IF over IS SyntaxTree.ArrayType THEN
  3644. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3645. ELSIF over IS SyntaxTree.StringType THEN
  3646. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3647. END;
  3648. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3649. parameters.AddExpression(expression);
  3650. ELSE
  3651. Error(position,Diagnostics.Invalid,"invalid index");
  3652. END;
  3653. END AppendIndex;
  3654. (** convert an expression to math array type
  3655. if expression is of math array type: return expression itself
  3656. if expression is of array-structured object type: return an index operator call on it
  3657. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3658. otherwise: return invalid expression
  3659. **)
  3660. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3661. VAR
  3662. result: SyntaxTree.Expression;
  3663. mathArrayType: SyntaxTree.MathArrayType;
  3664. BEGIN
  3665. IF expression.type = NIL THEN
  3666. result := SyntaxTree.invalidExpression
  3667. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3668. (* expression of math array type *)
  3669. result := expression
  3670. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3671. (* expression of array-structured object type *)
  3672. mathArrayType := MathArrayStructureOfType(expression.type);
  3673. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3674. ELSE
  3675. result := SyntaxTree.invalidExpression
  3676. END;
  3677. RETURN result
  3678. END ConvertToMathArray;
  3679. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3680. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3681. VAR
  3682. result: SyntaxTree.ExpressionList;
  3683. i: LONGINT;
  3684. BEGIN
  3685. result := SyntaxTree.NewExpressionList();
  3686. FOR i := 1 TO itemCount DO
  3687. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3688. END;
  3689. RETURN result
  3690. END ListOfOpenRanges;
  3691. (** create a procedure call designator for an index operator call on an array-structured object type
  3692. - use given index list as actual parameters
  3693. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3694. **)
  3695. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3696. VAR
  3697. operator: SyntaxTree.Operator;
  3698. expression: SyntaxTree.Expression;
  3699. actualParameters, tempList: SyntaxTree.ExpressionList;
  3700. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3701. result, tempDesignator: SyntaxTree.Designator;
  3702. recordType: SyntaxTree.RecordType;
  3703. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3704. i, hashValue, indexListSize, indexListKind: LONGINT;
  3705. castReturnType: SyntaxTree.MathArrayType;
  3706. BEGIN
  3707. ASSERT(IsArrayStructuredObjectType(left.type));
  3708. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3709. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3710. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3711. indexListSize := indexList.Length();
  3712. indexListKind := 0;
  3713. containsNonRange := FALSE;
  3714. FOR i := 0 TO indexList.Length() - 1 DO
  3715. indexListKind := indexListKind * 2;
  3716. expression := indexList.GetExpression(i);
  3717. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3718. INC(indexListKind)
  3719. ELSE
  3720. containsNonRange := TRUE
  3721. END
  3722. END;
  3723. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3724. (* select applicable index operator
  3725. - try to look up optimal index operator
  3726. - if not present, use operator on ranges
  3727. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3728. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3729. *)
  3730. usesGeneralOperator := FALSE;
  3731. IF rhs # NIL THEN
  3732. (* write operator *)
  3733. IF hashValue = -1 THEN
  3734. operator := NIL
  3735. ELSE
  3736. operator := recordType.arrayAccessOperators.write[hashValue];
  3737. END;
  3738. IF operator = NIL THEN
  3739. usesPureRangeOperator := TRUE;
  3740. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3741. operator := recordType.arrayAccessOperators.generalWrite;
  3742. usesGeneralOperator := TRUE
  3743. ELSE
  3744. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3745. operator := recordType.arrayAccessOperators.write[hashValue];
  3746. END
  3747. END
  3748. ELSE
  3749. (* read operator *)
  3750. IF hashValue = -1 THEN
  3751. operator := NIL
  3752. ELSE
  3753. operator := recordType.arrayAccessOperators.read[hashValue];
  3754. END;
  3755. IF operator = NIL THEN
  3756. usesPureRangeOperator := TRUE;
  3757. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3758. operator := recordType.arrayAccessOperators.generalRead;
  3759. usesGeneralOperator := TRUE
  3760. ELSE
  3761. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3762. operator := recordType.arrayAccessOperators.read[hashValue];
  3763. END
  3764. END
  3765. END;
  3766. IF operator = NIL THEN
  3767. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3768. result := SyntaxTree.invalidDesignator;
  3769. ELSE
  3770. (* determine if reshaping is needed *)
  3771. needsReshaping := containsNonRange & usesPureRangeOperator;
  3772. (* import OCArrayBase if reshaping is needed *)
  3773. IF needsReshaping & ~arrayBaseImported THEN
  3774. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3775. arrayBaseImported := TRUE
  3776. END;
  3777. (* add the index list item to the list of actual parameters
  3778. - for general operators: add a single inline array containing the index list items as parameter
  3779. - otherwise: add all index list items as individual parameters
  3780. *)
  3781. actualParameters := SyntaxTree.NewExpressionList();
  3782. IF usesGeneralOperator THEN
  3783. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3784. END;
  3785. FOR i := 0 TO indexListSize - 1 DO
  3786. expression := indexList.GetExpression(i);
  3787. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3788. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3789. tempList := SyntaxTree.NewExpressionList();
  3790. tempList.AddExpression(expression);
  3791. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3792. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3793. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3794. END;
  3795. IF usesGeneralOperator THEN
  3796. tempMathArrayExpression.elements.AddExpression(expression);
  3797. ELSE
  3798. actualParameters.AddExpression(expression)
  3799. END
  3800. END;
  3801. IF usesGeneralOperator THEN
  3802. actualParameters.AddExpression(tempMathArrayExpression)
  3803. END;
  3804. IF rhs # NIL THEN
  3805. (* add actual parameter for RHS *)
  3806. IF needsReshaping THEN
  3807. (* reshape using OCArrayBase.ExpandDimensions *)
  3808. tempList := SyntaxTree.NewExpressionList();
  3809. (* source array *)
  3810. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3811. tempList.AddExpression(rhs);
  3812. ELSE
  3813. (* convert scalar to one-dimensional array *)
  3814. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3815. tempMathArrayExpression.elements.AddExpression(rhs);
  3816. tempList.AddExpression(tempMathArrayExpression)
  3817. END;
  3818. (* list of kept dimensions *)
  3819. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3820. FOR i := 0 TO indexListSize - 1 DO
  3821. expression := indexList.GetExpression(i);
  3822. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3823. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3824. ELSE
  3825. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3826. END
  3827. END;
  3828. tempList.AddExpression(tempMathArrayExpression);
  3829. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3830. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3831. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3832. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3833. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3834. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3835. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3836. expression.SetType(castReturnType);
  3837. ELSE
  3838. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3839. END;
  3840. actualParameters.AddExpression(expression)
  3841. ELSE
  3842. actualParameters.AddExpression(rhs)
  3843. END
  3844. END;
  3845. (* add dereference operator and create procedure call designator *)
  3846. ASSERT(left IS SyntaxTree.Designator);
  3847. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3848. ASSERT(expression IS SyntaxTree.Designator);
  3849. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3850. IF (rhs = NIL) & needsReshaping THEN
  3851. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3852. tempList := SyntaxTree.NewExpressionList();
  3853. FOR i := 0 TO indexList.Length() - 1 DO
  3854. expression := indexList.GetExpression(i);
  3855. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3856. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3857. ELSE
  3858. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3859. END
  3860. END;
  3861. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3862. END;
  3863. IF rhs = NIL THEN
  3864. (* special rule: index read operator calls are considered to be assignable *)
  3865. result.SetAssignable(TRUE)
  3866. END;
  3867. (* put information about this index operator call into the resulting designator *)
  3868. result.SetRelatedAsot(left);
  3869. result.SetRelatedIndexList(indexList)
  3870. END;
  3871. RETURN result
  3872. END NewIndexOperatorCall;
  3873. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3874. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3875. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3876. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3877. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3878. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3879. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3880. CONST trace = FALSE;
  3881. BEGIN
  3882. IF trace THEN
  3883. FOR i := 0 TO actualParameters.Length()-1 DO
  3884. Printout.Info("par", actualParameters.GetExpression(i));
  3885. END;
  3886. END;
  3887. operator := scope.firstOperator;
  3888. WHILE(operator # NIL) DO
  3889. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3890. procedureType := operator.type(SyntaxTree.ProcedureType);
  3891. distance := Distance(system, procedureType,actualParameters);
  3892. IF trace THEN Printout.Info("check op ",operator) END;
  3893. IF distance < bestDistance THEN
  3894. IF trace THEN Printout.Info("taken op",operator) END;
  3895. bestDistance := distance;
  3896. bestOperator := operator;
  3897. END;
  3898. END;
  3899. operator := operator.nextOperator;
  3900. END;
  3901. END FindInScope;
  3902. BEGIN
  3903. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3904. IF oper = 0 THEN (* index *)
  3905. identifier := SyntaxTree.NewIdentifier("[]");
  3906. ELSE
  3907. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3908. END;
  3909. WHILE (recordType # NIL) DO
  3910. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3911. recordType := recordType.GetBaseRecord();
  3912. END;
  3913. RETURN bestOperator
  3914. END FindOperator;
  3915. BEGIN
  3916. type := left.type.resolved;
  3917. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3918. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3919. actualParameters := SyntaxTree.NewExpressionList();
  3920. IF parameters # NIL THEN
  3921. FOR i := 0 TO parameters.Length()-1 DO
  3922. expression := ResolveExpression(parameters.GetExpression(i));
  3923. actualParameters.AddExpression(expression);
  3924. END;
  3925. END;
  3926. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3927. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3928. IF op # NIL THEN
  3929. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3930. ASSERT(expression IS SyntaxTree.Designator);
  3931. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3932. result.SetRelatedAsot(left);
  3933. result.SetRelatedIndexList(parameters);
  3934. (* check if write operator exists, for var parameters *)
  3935. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3936. actualParameters := SyntaxTree.NewExpressionList();
  3937. FOR i := 0 TO parameters.Length()-1 DO
  3938. expression := ResolveExpression(parameters.GetExpression(i));
  3939. actualParameters.AddExpression(expression);
  3940. END;
  3941. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3942. actualParameters.AddExpression(rhs);
  3943. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3944. IF op = NIL THEN rhs := NIL END;
  3945. END;
  3946. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3947. ELSE
  3948. result := NIL;
  3949. END;
  3950. RETURN result;
  3951. END NewObjectOperatorCall;
  3952. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3953. 1. convert bracket designator chains into a single one that contains separators
  3954. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3955. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3956. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3957. - if an array or math array is indexed over, create index designator
  3958. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3959. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3960. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3961. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3962. - if an array-structured object type is indexed over, create procedure call designator
  3963. e.g.: a[x, y] -> a^."[]"(x, y)
  3964. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3965. - a[i, *] = a[i][*]
  3966. - a[*, i] # a[*][i]
  3967. Because:
  3968. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3969. - 'i-th column' = a[*, i]
  3970. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3971. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3972. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3973. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3974. **)
  3975. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3976. VAR
  3977. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3978. indexDesignator: SyntaxTree.IndexDesignator;
  3979. designator: SyntaxTree.Designator;
  3980. type: SyntaxTree.Type;
  3981. recordType: SyntaxTree.RecordType;
  3982. expression, rhs: SyntaxTree.Expression;
  3983. indexList: SyntaxTree.ExpressionList;
  3984. i: LONGINT;
  3985. hasError, done: BOOLEAN;
  3986. PROCEDURE FinalizeIndexDesignator;
  3987. BEGIN
  3988. IF indexDesignator # NIL THEN
  3989. (* the end of a tensor has been reached: *)
  3990. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3991. SetIndexBaseType(indexDesignator, type);
  3992. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3993. designator := indexDesignator;
  3994. type := designator.type.resolved;
  3995. indexDesignator := NIL;
  3996. ASSERT(SyntaxTree.Resolved IN type.state)
  3997. END
  3998. END FinalizeIndexDesignator;
  3999. BEGIN
  4000. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4001. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4002. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4003. (* copy all index list entries including a separator to the left bracket designator *)
  4004. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4005. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4006. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4007. END;
  4008. (* propagate the related RHS *)
  4009. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4010. (* only resolve left bracket designator and use as final result *)
  4011. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4012. ELSE
  4013. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4014. designator := ResolveDesignator(bracketDesignator.left);
  4015. type := designator.type.resolved;
  4016. indexDesignator := NIL;
  4017. (*!!! clean up *)
  4018. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4019. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4020. IF resolvedExpression = NIL THEN
  4021. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4022. resolvedExpression := SyntaxTree.invalidDesignator
  4023. END;
  4024. RETURN;
  4025. END;
  4026. i := 0;
  4027. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4028. expression := bracketDesignator.parameters.GetExpression(i);
  4029. expression := ResolveExpression(expression);
  4030. bracketDesignator.parameters.SetExpression(i, expression);
  4031. IF expression = SyntaxTree.indexListSeparator THEN
  4032. (* finalize an existing index designator if needed *)
  4033. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4034. INC(i)
  4035. ELSE
  4036. (* do auto-dereferencing if needed *)
  4037. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4038. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4039. & (i=0)*)
  4040. THEN
  4041. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4042. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4043. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4044. designator := SyntaxTree.invalidDesignator;
  4045. type := SyntaxTree.invalidType
  4046. ELSE
  4047. FinalizeIndexDesignator;
  4048. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4049. type := designator.type.resolved
  4050. END
  4051. END;
  4052. (* create a new index designator, if needed *)
  4053. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4054. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4055. indexDesignator.SetAssignable(designator.assignable);
  4056. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4057. (* designator := indexDesignator *)
  4058. END;
  4059. IF type = SyntaxTree.invalidType THEN
  4060. (* error already handled *)
  4061. INC(i)
  4062. ELSIF type IS SyntaxTree.ArrayType THEN
  4063. (* indexing over an array *)
  4064. ASSERT(indexDesignator # NIL);
  4065. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4066. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4067. INC(i)
  4068. ELSIF type IS SyntaxTree.StringType THEN
  4069. (* indexing over an array *)
  4070. ASSERT(indexDesignator # NIL);
  4071. AppendIndex(expression.position, indexDesignator, expression, type);
  4072. type := type(SyntaxTree.StringType).baseType.resolved;
  4073. INC(i)
  4074. ELSIF type IS SyntaxTree.MathArrayType THEN
  4075. (* indexing over a math array *)
  4076. ASSERT(indexDesignator # NIL);
  4077. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4078. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4079. INC(i)
  4080. ELSIF IsArrayStructuredObjectType(type) THEN
  4081. (* indexing over ASOTs *)
  4082. FinalizeIndexDesignator;
  4083. ASSERT(type IS SyntaxTree.PointerType);
  4084. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4085. (*
  4086. - collect index list items from bracket designator that belong to ASOT
  4087. - check for errors
  4088. *)
  4089. indexList := SyntaxTree.NewExpressionList();
  4090. hasError := FALSE;
  4091. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4092. (* indexing over tensor ASOT:
  4093. - stop at index list end or separator
  4094. - dimensionality is given by number of index list items
  4095. *)
  4096. done := FALSE;
  4097. WHILE ~done DO
  4098. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4099. done := TRUE;
  4100. ELSE
  4101. expression := bracketDesignator.parameters.GetExpression(i);
  4102. IF expression = SyntaxTree.indexListSeparator THEN
  4103. done := TRUE;
  4104. ELSE
  4105. expression := ResolveExpression(expression);
  4106. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4107. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4108. hasError := TRUE
  4109. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4110. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4111. expression := SyntaxTree.invalidExpression;
  4112. hasError := TRUE
  4113. END;
  4114. indexList.AddExpression(expression)
  4115. END;
  4116. INC(i)
  4117. END
  4118. END
  4119. ELSE
  4120. (* indexing over non-tensor ASOT:
  4121. - ignore separators
  4122. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4123. *)
  4124. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4125. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4126. expression := bracketDesignator.parameters.GetExpression(i);
  4127. ELSE
  4128. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4129. END;
  4130. IF expression # SyntaxTree.indexListSeparator THEN
  4131. expression := ResolveExpression(expression);
  4132. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4133. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4134. expression := SyntaxTree.invalidExpression;
  4135. hasError := TRUE
  4136. END;
  4137. indexList.AddExpression(expression)
  4138. END;
  4139. INC(i)
  4140. END;
  4141. END;
  4142. IF hasError THEN
  4143. designator := SyntaxTree.invalidDesignator;
  4144. type := SyntaxTree.invalidType;
  4145. ELSE
  4146. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4147. and the last entry in the index list belongs to the array-structured object type in question.
  4148. E.g.: for a 2-dimensional array-structured object type:
  4149. - 'lhs := asot[1, 2]' -> read mode
  4150. - 'asot[1, 2] := rhs' -> write mode
  4151. - 'asot[1, 2, 3] := rhs' -> read mode
  4152. *)
  4153. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4154. rhs := bracketDesignator.relatedRhs
  4155. ELSE
  4156. rhs := NIL
  4157. END;
  4158. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4159. type := designator.type
  4160. END
  4161. ELSE
  4162. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4163. designator := SyntaxTree.invalidDesignator;
  4164. type := SyntaxTree.invalidType;
  4165. INC(i)
  4166. END
  4167. END
  4168. END;
  4169. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4170. resolvedExpression := designator
  4171. END
  4172. END VisitBracketDesignator;
  4173. (** check and resolve expression list
  4174. - resolve each expression in an expression list
  4175. - returns true if and only if all statements could have successfully been resolved
  4176. **)
  4177. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4178. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4179. BEGIN
  4180. result := TRUE;
  4181. FOR i := 0 TO expressionList.Length()-1 DO
  4182. expression := ResolveExpression(expressionList.GetExpression(i));
  4183. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4184. expressionList.SetExpression(i,expression);
  4185. END;
  4186. RETURN result
  4187. END ExpressionList;
  4188. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4189. BEGIN
  4190. type := type.resolved;
  4191. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4192. RETURN TRUE
  4193. ELSIF system.CanPassInRegister # NIL THEN
  4194. RETURN system.CanPassInRegister(type);
  4195. ELSE
  4196. RETURN FALSE
  4197. END;
  4198. END CanPassInRegister;
  4199. (** return procedure call designator left(actualParameters)
  4200. - check realtime procedure call in realtime procedure
  4201. - check number of parameters
  4202. - check parameter compatibility
  4203. return invalidDesignator if error
  4204. **)
  4205. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4206. VAR result: SyntaxTree.Designator;
  4207. numberFormalParameters, numberActualParameters: LONGINT;
  4208. formalType: SyntaxTree.ProcedureType;
  4209. formalParameter: SyntaxTree.Parameter;
  4210. actualParameter: SyntaxTree.Expression;
  4211. i: LONGINT;
  4212. BEGIN
  4213. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4214. result := SyntaxTree.invalidDesignator;
  4215. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4216. numberFormalParameters := formalType.numberParameters;
  4217. numberActualParameters := actualParameters.Length();
  4218. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4219. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4220. END;
  4221. IF ~ExpressionList(actualParameters) THEN
  4222. result := SyntaxTree.invalidDesignator
  4223. ELSE
  4224. IF numberActualParameters <= numberFormalParameters THEN
  4225. formalParameter := formalType.firstParameter;
  4226. FOR i := 0 TO numberActualParameters-1 DO
  4227. actualParameter := actualParameters.GetExpression(i);
  4228. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4229. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4230. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4231. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4232. ELSE
  4233. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4234. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4235. END;
  4236. actualParameters.SetExpression(i,actualParameter);
  4237. END;
  4238. formalParameter := formalParameter.nextParameter;
  4239. END;
  4240. WHILE (formalParameter # NIL) DO
  4241. IF formalParameter.defaultValue # NIL THEN
  4242. actualParameters.AddExpression(formalParameter.defaultValue);
  4243. formalParameter := formalParameter.nextParameter
  4244. ELSE
  4245. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4246. formalParameter := NIL;
  4247. END;
  4248. END;
  4249. ELSE
  4250. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4251. END;
  4252. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4253. result.SetAssignable(FALSE);
  4254. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4255. END;
  4256. RETURN result
  4257. END NewProcedureCallDesignator;
  4258. (**
  4259. builtin call designator generated in VisitParameterDesignator
  4260. -> nothing to be resolved
  4261. **)
  4262. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4263. BEGIN
  4264. resolvedExpression := x;
  4265. END VisitTypeGuardDesignator;
  4266. (**
  4267. builtin call designator generated in VisitParameterDesignator
  4268. -> nothing to be resolved
  4269. **)
  4270. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4271. BEGIN
  4272. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4273. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4274. ASSERT(resolvedExpression.type # NIL);
  4275. ELSIF ExpressionList(x.parameters) THEN
  4276. resolvedExpression := x;
  4277. END;
  4278. END VisitBuiltinCallDesignator;
  4279. (**
  4280. procedure call designator generated in VisitParameterDesignator
  4281. -> nothing to be resolved
  4282. **)
  4283. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4284. BEGIN
  4285. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4286. resolvedExpression := x;
  4287. END VisitProcedureCallDesignator;
  4288. (** return true if x is a variable else return false and report error **)
  4289. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4290. VAR result: BOOLEAN;
  4291. BEGIN
  4292. result := TRUE;
  4293. IF x = SyntaxTree.invalidExpression THEN
  4294. result := FALSE;
  4295. ELSIF ~IsVariable(x) THEN
  4296. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4297. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4298. result := FALSE;
  4299. END;
  4300. RETURN result
  4301. END CheckVariable;
  4302. (**
  4303. if expression x is of basic type then return true else report error and return false
  4304. **)
  4305. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4306. VAR result: BOOLEAN;
  4307. BEGIN
  4308. result := FALSE;
  4309. IF x = SyntaxTree.invalidExpression THEN
  4310. ELSIF ~IsBasicType(x.type) THEN
  4311. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4312. result := FALSE
  4313. ELSE result := TRUE
  4314. END;
  4315. RETURN result
  4316. END CheckBasicType;
  4317. (**
  4318. if expression x is of number type then return true else report error and return false
  4319. **)
  4320. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4321. VAR result: BOOLEAN;
  4322. BEGIN
  4323. result := FALSE;
  4324. IF x = SyntaxTree.invalidExpression THEN
  4325. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4326. Error(x.position,Diagnostics.Invalid,"is non number type");
  4327. ELSE result := TRUE
  4328. END;
  4329. RETURN result
  4330. END CheckNumberType;
  4331. (**
  4332. if expression x is of number or size type but not complex then return true else report error and return false
  4333. **)
  4334. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4335. VAR result: BOOLEAN;
  4336. BEGIN
  4337. result := FALSE;
  4338. IF x = SyntaxTree.invalidExpression THEN
  4339. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4340. Error(x.position,Diagnostics.Invalid,"is complex type");
  4341. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4342. Error(x.position,Diagnostics.Invalid,"is non number type");
  4343. ELSE result := TRUE
  4344. END;
  4345. RETURN result
  4346. END CheckNonComplexNumberSizeType;
  4347. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4348. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4349. BEGIN
  4350. result := FALSE; type := x.type.resolved;
  4351. IF x = SyntaxTree.invalidExpression THEN
  4352. 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
  4353. TRACE(type.sizeInBits);
  4354. TRACE(system.addressType.sizeInBits);
  4355. Error(x.position,Diagnostics.Invalid,"is no address type");
  4356. ELSE result := TRUE
  4357. END;
  4358. RETURN result
  4359. END CheckAddressType;
  4360. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4361. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4362. BEGIN
  4363. result := FALSE; type := x.type.resolved;
  4364. IF x = SyntaxTree.invalidExpression THEN
  4365. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4366. Error(x.position,Diagnostics.Invalid,"is no size type");
  4367. ELSE result := TRUE
  4368. END;
  4369. RETURN result
  4370. END CheckSizeType;
  4371. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4372. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4373. BEGIN
  4374. result := FALSE; type := x.type.resolved;
  4375. IF x = SyntaxTree.invalidExpression THEN
  4376. 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
  4377. Error(x.position,Diagnostics.Invalid,"is no object type");
  4378. ELSE result := TRUE
  4379. END;
  4380. RETURN result
  4381. END CheckObjectType;
  4382. (**
  4383. if expression x is of integer type then return true else report error and return false
  4384. **)
  4385. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4386. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4387. BEGIN
  4388. result := FALSE; type := x.type.resolved;
  4389. IF x = SyntaxTree.invalidExpression THEN
  4390. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4391. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4392. ELSE result := TRUE
  4393. END;
  4394. RETURN result
  4395. END CheckIntegerType;
  4396. (**
  4397. if expression x is of character type then return true else report error and return false
  4398. **)
  4399. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4400. VAR result: BOOLEAN;
  4401. BEGIN
  4402. result := FALSE;
  4403. IF x = SyntaxTree.invalidExpression THEN
  4404. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4405. Error(x.position,Diagnostics.Invalid,"is no character type");
  4406. ELSE result := TRUE
  4407. END;
  4408. RETURN result
  4409. END CheckCharacterType;
  4410. (**
  4411. if expression x is of real type then return true else report error and return false
  4412. **)
  4413. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4414. VAR result: BOOLEAN;
  4415. BEGIN
  4416. result := FALSE;
  4417. IF x = SyntaxTree.invalidExpression THEN
  4418. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4419. Error(x.position,Diagnostics.Invalid,"is no float type");
  4420. ELSE result := TRUE
  4421. END;
  4422. RETURN result
  4423. END CheckRealType;
  4424. (**
  4425. if expression x is of range type then return true else report error and return false
  4426. **)
  4427. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4428. VAR result: BOOLEAN;
  4429. BEGIN
  4430. result := FALSE;
  4431. IF x = SyntaxTree.invalidExpression THEN
  4432. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4433. Error(x.position,Diagnostics.Invalid,"is no range type");
  4434. ELSE result := TRUE
  4435. END;
  4436. RETURN result
  4437. END CheckRangeType;
  4438. (**
  4439. if expression x is of boolean type then return true else report error and return false
  4440. **)
  4441. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4442. VAR result: BOOLEAN;
  4443. BEGIN
  4444. result := FALSE;
  4445. IF x = SyntaxTree.invalidExpression THEN
  4446. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4447. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4448. ELSE result := TRUE
  4449. END;
  4450. RETURN result
  4451. END CheckBooleanType;
  4452. (**
  4453. if expression x is of set type then return true else report error and return false
  4454. **)
  4455. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4456. VAR result: BOOLEAN;
  4457. BEGIN
  4458. result := FALSE;
  4459. IF x = SyntaxTree.invalidExpression THEN
  4460. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4461. Error(x.position,Diagnostics.Invalid,"is no set type");
  4462. ELSE result := TRUE
  4463. END;
  4464. RETURN result
  4465. END CheckSetType;
  4466. (**
  4467. if expression x is of string or array of character type then return true else report error and return false
  4468. **)
  4469. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4470. VAR result: BOOLEAN;
  4471. BEGIN
  4472. result := FALSE;
  4473. IF x = SyntaxTree.invalidExpression THEN
  4474. ELSIF ~IsStringType(x.type.resolved) THEN
  4475. Error(x.position,Diagnostics.Invalid,"is no string type");
  4476. ELSE result := TRUE
  4477. END;
  4478. RETURN result
  4479. END CheckStringType;
  4480. (**
  4481. if expression x is a type declaration type return true else report error and return false
  4482. **)
  4483. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4484. VAR result: BOOLEAN;
  4485. BEGIN
  4486. result := FALSE;
  4487. IF x = SyntaxTree.invalidExpression THEN
  4488. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4489. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4490. ELSE result := TRUE
  4491. END;
  4492. RETURN result
  4493. END CheckTypeDeclarationType;
  4494. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4495. VAR result: BOOLEAN;
  4496. BEGIN
  4497. result := FALSE;
  4498. IF x = SyntaxTree.invalidExpression THEN
  4499. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4500. result := TRUE;
  4501. value := x.resolved(SyntaxTree.IntegerValue).value;
  4502. ELSE
  4503. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4504. END;
  4505. RETURN result;
  4506. END CheckIntegerValue;
  4507. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4508. VAR result: BOOLEAN;
  4509. BEGIN
  4510. result := FALSE;
  4511. IF x = SyntaxTree.invalidExpression THEN
  4512. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4513. result := TRUE;
  4514. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4515. ELSE
  4516. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4517. END;
  4518. RETURN result;
  4519. END CheckStringValue;
  4520. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4521. BEGIN
  4522. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4523. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4524. ELSE
  4525. RETURN FALSE
  4526. END;
  4527. END IsUnsignedValue;
  4528. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4529. BEGIN
  4530. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4531. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4532. ELSE
  4533. RETURN FALSE
  4534. END
  4535. END IsAddressValue;
  4536. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4537. BEGIN
  4538. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4539. END IsAddressExpression;
  4540. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4541. BEGIN
  4542. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4543. END IsSizeExpression;
  4544. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4545. VAR result: BOOLEAN;
  4546. BEGIN
  4547. result := FALSE;
  4548. IF x = SyntaxTree.invalidExpression THEN
  4549. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4550. result := TRUE;
  4551. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4552. ELSE
  4553. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4554. END;
  4555. RETURN result;
  4556. END CheckEnumerationValue;
  4557. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4558. VAR result: BOOLEAN;
  4559. BEGIN
  4560. result := FALSE;
  4561. IF x = SyntaxTree.invalidExpression THEN
  4562. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4563. result := TRUE;
  4564. value := x.resolved(SyntaxTree.CharacterValue).value;
  4565. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4566. result := TRUE;
  4567. value := x.resolved(SyntaxTree.StringValue).value[0];
  4568. ELSE
  4569. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4570. END;
  4571. RETURN result;
  4572. END CheckCharacterValue;
  4573. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4574. VAR result: BOOLEAN;
  4575. BEGIN
  4576. result := FALSE;
  4577. IF x = SyntaxTree.invalidExpression THEN
  4578. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4579. value := x.resolved(SyntaxTree.IntegerValue).value;
  4580. IF (value > 0) OR includeZero & (value = 0) THEN
  4581. result := TRUE;
  4582. ELSE
  4583. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4584. END
  4585. ELSE
  4586. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4587. END;
  4588. RETURN result;
  4589. END CheckPositiveIntegerValue;
  4590. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4591. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4592. BEGIN
  4593. result := FALSE;
  4594. IF x = SyntaxTree.invalidExpression THEN
  4595. ELSE
  4596. type := x.type.resolved;
  4597. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4598. portType := type(SyntaxTree.PortType);
  4599. result := TRUE
  4600. ELSE
  4601. Error(x.position,Diagnostics.Invalid,"no port type");
  4602. END;
  4603. END;
  4604. RETURN result
  4605. END CheckPortType;
  4606. (* move to builtin procedure call statement ?
  4607. remove builtin procedure call designator ?
  4608. *)
  4609. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4610. VAR
  4611. numberActualParameters,numberFormalParameters: LONGINT;
  4612. formalParameter: SyntaxTree.Parameter;
  4613. actualParameter: SyntaxTree.Expression;
  4614. procedureType: SyntaxTree.ProcedureType;
  4615. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4616. inPort, outPort: SyntaxTree.PortType;
  4617. constructor: SyntaxTree.Procedure;
  4618. type0,type1,type2: SyntaxTree.Type;
  4619. type,base,parameterType: SyntaxTree.Type;
  4620. arrayType: SyntaxTree.ArrayType;
  4621. i,i0,i1: LONGINT;
  4622. r,r0,r1,im: LONGREAL;
  4623. c: CHAR;
  4624. id: LONGINT;
  4625. b: BOOLEAN;
  4626. first: LONGINT;
  4627. mathArrayType: SyntaxTree.MathArrayType;
  4628. customBuiltin: SyntaxTree.CustomBuiltin;
  4629. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4630. VAR resultB: BOOLEAN;
  4631. BEGIN
  4632. IF numberActualParameters < from THEN
  4633. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4634. result := SyntaxTree.invalidExpression;
  4635. resultB := FALSE;
  4636. ELSIF numberActualParameters > to THEN
  4637. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4638. result := SyntaxTree.invalidExpression;
  4639. resultB := FALSE;
  4640. ELSE
  4641. resultB := TRUE;
  4642. END;
  4643. RETURN resultB
  4644. END CheckArity;
  4645. BEGIN
  4646. type := NIL; result := NIL;
  4647. type0 := NIL; type1 := NIL; type2 := NIL;
  4648. numberActualParameters := actualParameters.Length();
  4649. IF numberActualParameters>0 THEN
  4650. parameter0 := actualParameters.GetExpression(0);
  4651. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4652. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4653. result := SyntaxTree.invalidExpression
  4654. END
  4655. END;
  4656. IF numberActualParameters >1 THEN
  4657. parameter1 := actualParameters.GetExpression(1);
  4658. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4659. ELSE
  4660. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4661. result := SyntaxTree.invalidExpression
  4662. END
  4663. END;
  4664. IF numberActualParameters >2 THEN
  4665. parameter2 := actualParameters.GetExpression(2);
  4666. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4667. ELSE
  4668. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4669. result := SyntaxTree.invalidExpression
  4670. END
  4671. END;
  4672. IF returnType # NIL THEN
  4673. id := Global.New;
  4674. result := NIL;
  4675. ELSE
  4676. id := builtin.id;
  4677. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4678. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4679. END;
  4680. END;
  4681. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4682. ELSIF result # NIL THEN type := result.type (* operator *)
  4683. ELSE
  4684. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4685. result(SyntaxTree.Designator).SetLeft(left);
  4686. IF returnType # NIL THEN
  4687. type := returnType;
  4688. END;
  4689. (* ---- ASSERT ----- *)
  4690. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4691. IF CheckBooleanType(parameter0) THEN
  4692. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4693. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4694. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4695. *)
  4696. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4697. Error(position,Diagnostics.Invalid,"assert failed");
  4698. END;
  4699. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4700. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4701. rules imposed by the architecture / current runtime
  4702. *)
  4703. END;
  4704. END;
  4705. (* ---- COPY ----- *)
  4706. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4707. IF~IsStringType(type0) THEN
  4708. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4709. END;
  4710. IF ~IsStringType(type1) THEN
  4711. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4712. ELSIF CheckVariable(parameter1) THEN
  4713. IF (type0 IS SyntaxTree.StringType) THEN
  4714. arrayType := type1(SyntaxTree.ArrayType);
  4715. IF arrayType.form = SyntaxTree.Static THEN
  4716. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4717. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4718. END;
  4719. END;
  4720. END;
  4721. END;
  4722. (* ---- INC, DEC----- *)
  4723. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4724. IF numberActualParameters = 1 THEN
  4725. parameter1 :=Global.NewIntegerValue(system,position,1);
  4726. actualParameters.AddExpression(parameter1);
  4727. END;
  4728. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4729. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4730. Error(position,Diagnostics.Invalid,"incompatible increment");
  4731. ELSE
  4732. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4733. actualParameters.SetExpression(1,parameter1);
  4734. END;
  4735. END;
  4736. (* ---- EXCL, INCL----- *)
  4737. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4738. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4739. IF IsIntegerValue(parameter1,i0) THEN
  4740. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4741. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4742. END;
  4743. END;
  4744. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4745. actualParameters.SetExpression(1,parameter1);
  4746. END;
  4747. (* ---- HALT, SYSTEM.HALT ----- *)
  4748. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4749. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4750. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4751. rules imposed by the architecture / current runtime
  4752. *)
  4753. END;
  4754. (* ---- WAIT ----- *)
  4755. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4756. IF CheckObjectType(parameter0) THEN
  4757. END;
  4758. (* ---- NEW ----- *)
  4759. ELSIF (id = Global.New) THEN
  4760. IF returnType # NIL THEN
  4761. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4762. ELSE
  4763. first := 1;
  4764. END;
  4765. IF CheckArity(first,Infinity) THEN
  4766. IF currentIsRealtime THEN
  4767. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4768. END;
  4769. (* check constructor *)
  4770. IF (first =0) OR CheckVariable(parameter0) THEN
  4771. IF type0 IS SyntaxTree.PointerType THEN
  4772. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4773. END;
  4774. IF type0 IS SyntaxTree.ArrayType THEN
  4775. arrayType := type0(SyntaxTree.ArrayType);
  4776. IF arrayType.form = SyntaxTree.Static THEN
  4777. i := first
  4778. ELSIF arrayType.form = SyntaxTree.Open THEN
  4779. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4780. ELSE HALT(100)
  4781. END;
  4782. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4783. i := first;
  4784. REPEAT
  4785. actualParameter := actualParameters.GetExpression(i);
  4786. IF CheckSizeType(actualParameter) THEN
  4787. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4788. actualParameters.SetExpression(i,actualParameter);
  4789. END;
  4790. INC(i);
  4791. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4792. END;
  4793. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4794. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4795. IF constructor = NIL THEN
  4796. IF CheckArity(first,first) THEN END;
  4797. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4798. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4799. ELSE
  4800. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4801. numberFormalParameters := procedureType.numberParameters;
  4802. IF numberActualParameters-first <= numberFormalParameters THEN
  4803. formalParameter := procedureType.firstParameter;
  4804. FOR i := first TO numberActualParameters-1 DO
  4805. actualParameter := actualParameters.GetExpression(i);
  4806. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4807. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4808. ELSE
  4809. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4810. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4811. END;
  4812. actualParameters.SetExpression(i,actualParameter);
  4813. END;
  4814. formalParameter := formalParameter.nextParameter;
  4815. END;
  4816. WHILE (formalParameter # NIL) DO
  4817. IF formalParameter.defaultValue # NIL THEN
  4818. actualParameters.AddExpression(formalParameter.defaultValue);
  4819. formalParameter := formalParameter.nextParameter
  4820. ELSE
  4821. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4822. formalParameter := NIL;
  4823. END;
  4824. END;
  4825. ELSE
  4826. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4827. END;
  4828. END;
  4829. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4830. mathArrayType := type0(SyntaxTree.MathArrayType);
  4831. IF mathArrayType.form = SyntaxTree.Static THEN
  4832. Error(position,Diagnostics.Invalid,"new on static array");
  4833. ELSE
  4834. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4835. i0 := first+1; i1 := Infinity;
  4836. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4837. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4838. i1 := i0;
  4839. ELSE HALT(100);
  4840. END;
  4841. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4842. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4843. base := ArrayBase(type0,MAX(LONGINT));
  4844. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4845. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4846. IF ~CompatibleTo(system,type0,parameterType) THEN
  4847. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4848. result := SyntaxTree.invalidExpression;
  4849. ELSE
  4850. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4851. END;
  4852. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4853. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4854. IF ~CompatibleTo(system,type1,parameterType) THEN
  4855. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4856. result := SyntaxTree.invalidExpression;
  4857. ELSE
  4858. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4859. END;
  4860. ELSE
  4861. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4862. i := first;
  4863. REPEAT
  4864. actualParameter := actualParameters.GetExpression(i);
  4865. IF CheckSizeType(actualParameter) THEN
  4866. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4867. actualParameters.SetExpression(i,actualParameter);
  4868. END;
  4869. INC(i);
  4870. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4871. END;
  4872. END;
  4873. END;
  4874. ELSIF type0 IS SyntaxTree.CellType THEN
  4875. IF ~(currentIsCellNet) THEN
  4876. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4877. ELSE
  4878. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4879. IF (constructor = NIL) & CheckArity(1,1) THEN
  4880. (* ok *)
  4881. ELSE
  4882. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4883. numberFormalParameters := procedureType.numberParameters;
  4884. DEC(numberActualParameters);
  4885. IF numberActualParameters <= numberFormalParameters THEN
  4886. formalParameter := procedureType.firstParameter;
  4887. FOR i := first TO numberActualParameters DO
  4888. actualParameter := actualParameters.GetExpression(i);
  4889. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4890. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4891. ELSE
  4892. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4893. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4894. END;
  4895. actualParameters.SetExpression(i,actualParameter);
  4896. END;
  4897. formalParameter := formalParameter.nextParameter;
  4898. END;
  4899. WHILE (formalParameter # NIL) DO
  4900. IF formalParameter.defaultValue # NIL THEN
  4901. actualParameters.AddExpression(formalParameter.defaultValue);
  4902. formalParameter := formalParameter.nextParameter
  4903. ELSE
  4904. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4905. formalParameter := NIL;
  4906. END;
  4907. END;
  4908. ELSE
  4909. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4910. END;
  4911. END;
  4912. END;
  4913. activeCellsStatement := TRUE;
  4914. ELSE
  4915. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4916. END;
  4917. END;
  4918. END;
  4919. (* ---- DISPOSE ----- *)
  4920. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4921. IF ~IsPointerType(parameter0.type) THEN
  4922. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4923. ELSIF ~IsDisposable(parameter0.type) THEN
  4924. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4925. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4926. END
  4927. (* ---- GETPROCEDURE ----- *)
  4928. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4929. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4930. IF CheckVariable(parameter2) THEN
  4931. IF ~GetProcedureAllowed(parameter2.type) THEN
  4932. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4933. END;
  4934. END;
  4935. END;
  4936. (* ---- ABS ----- *)
  4937. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4938. (* note: ABS on complex numbers is done using overloading *)
  4939. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4940. type := type0;
  4941. IF IsIntegerValue(parameter0,i0) THEN
  4942. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4943. type := Global.GetIntegerType(system,ABS(i0));
  4944. ELSIF IsRealValue(parameter0,r) THEN
  4945. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4946. END;
  4947. ELSE
  4948. type := SyntaxTree.invalidType;
  4949. END;
  4950. (* ---- ASH, ASR ----- *)
  4951. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4952. type := type0;
  4953. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4954. (*
  4955. ConvertOperands(parameter0,parameter1); (* same type *)
  4956. *)
  4957. type := parameter0.type;
  4958. IF IsIntegerValue(parameter0,i0) THEN
  4959. IF IsIntegerValue(parameter1,i1) THEN
  4960. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4961. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4962. result := ResolveExpression(result);
  4963. type := Global.GetIntegerType(system,i0);
  4964. END;
  4965. END;
  4966. IF type.resolved.sizeInBits < 32 THEN
  4967. type := system.longintType;
  4968. END;
  4969. (*!compatibility with release, remove when resolved
  4970. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4971. *)
  4972. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4973. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4974. actualParameters.SetExpression(0,parameter0);
  4975. actualParameters.SetExpression(1,parameter1);
  4976. END;
  4977. (* ---- CAP ----- *)
  4978. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4979. type := system.characterType;
  4980. IF CheckCharacterType (parameter0) THEN
  4981. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4982. actualParameters.SetExpression(0,parameter0);
  4983. IF IsCharacterValue(parameter0,c) THEN
  4984. IF (c <= "z") & (c >= "a") THEN
  4985. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4986. ELSE
  4987. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4988. END;
  4989. END;
  4990. END;
  4991. (* ---- CHR ----- *)
  4992. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4993. IF id = Global.Chr THEN
  4994. type := system.characterType
  4995. ELSE
  4996. type := system.characterType32
  4997. END;
  4998. IF CheckIntegerType(parameter0) THEN
  4999. IF IsIntegerValue(parameter0,i0) THEN
  5000. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5001. result := ResolveExpression(result);
  5002. ELSE
  5003. (*
  5004. result := NewConversion(parameter0.position,parameter0,type);
  5005. *)
  5006. END;
  5007. END
  5008. (* ---- ENTIER ----- *)
  5009. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5010. type := system.longintType;
  5011. IF CheckRealType(parameter0) THEN
  5012. IF IsRealValue(parameter0,r) THEN
  5013. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5014. type := Global.GetIntegerType(system,ENTIER(r));
  5015. END
  5016. END;
  5017. (* ---- ENTIERH ----- *)
  5018. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5019. type := system.hugeintType;
  5020. IF CheckRealType(parameter0) THEN
  5021. IF IsRealValue(parameter0,r) THEN
  5022. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5023. END
  5024. END;
  5025. (* ---- LEN ----- *)
  5026. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5027. type := system.longintType;
  5028. base := type0;
  5029. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5030. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5031. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5032. END;
  5033. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5034. actualParameters.SetExpression(0,parameter0);
  5035. type0 := parameter0.type.resolved;
  5036. base := type0;
  5037. END;
  5038. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5039. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5040. IF i1 < 0 THEN
  5041. Error(position,Diagnostics.Invalid,"invalid dimension");
  5042. base := SyntaxTree.invalidType;
  5043. ELSE
  5044. base := ArrayBase(base,i1);
  5045. IF (base # NIL) & Indexable(base) THEN
  5046. ELSE
  5047. Error(position,Diagnostics.Invalid,"len on no array");
  5048. IF VerboseErrorMessage THEN
  5049. Printout.Info("base",base);
  5050. END;
  5051. base := SyntaxTree.invalidType;
  5052. END;
  5053. END;
  5054. IF numberActualParameters=2 THEN
  5055. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5056. actualParameters.SetExpression(1,parameter1);
  5057. ELSIF base IS SyntaxTree.MathArrayType THEN
  5058. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5059. END;
  5060. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5061. IF base IS SyntaxTree.ArrayType THEN
  5062. arrayType := base(SyntaxTree.ArrayType);
  5063. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5064. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5065. result := Global.NewIntegerValue(system,position,i);
  5066. type := result.type;(* arrayType.length.type;*)
  5067. ASSERT(type # NIL);
  5068. END;
  5069. ELSIF base IS SyntaxTree.MathArrayType THEN
  5070. mathArrayType := base(SyntaxTree.MathArrayType);
  5071. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5072. result := Global.NewIntegerValue(system,position,i);
  5073. type := result.type;
  5074. (*
  5075. type := mathArrayType.length.type;
  5076. *)
  5077. ASSERT(type # NIL);
  5078. END;
  5079. END;
  5080. END;
  5081. ELSE
  5082. type := system.longintType;
  5083. END;
  5084. (* ---- FIRST ---- *)
  5085. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5086. type := system.longintType;
  5087. IF CheckRangeType(parameter0) THEN END;
  5088. result.SetAssignable(parameter0.assignable)
  5089. (* ---- LAST ---- *)
  5090. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5091. type := system.longintType;
  5092. IF CheckRangeType(parameter0) THEN END;
  5093. result.SetAssignable(parameter0.assignable)
  5094. (* ---- STEP ---- *)
  5095. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5096. type := system.longintType;
  5097. IF CheckRangeType(parameter0) THEN END;
  5098. result.SetAssignable(parameter0.assignable)
  5099. (* ---- RE ---- *)
  5100. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5101. IF CheckNumberType(parameter0) THEN
  5102. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5103. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5104. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5105. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5106. type := parameter0.type
  5107. ELSE
  5108. type := system.realType
  5109. END
  5110. END;
  5111. result.SetAssignable(parameter0.assignable)
  5112. (* ---- IM ---- *)
  5113. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5114. IF CheckNumberType(parameter0) THEN
  5115. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5116. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5117. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5118. ELSE
  5119. type := system.realType;
  5120. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5121. END
  5122. END;
  5123. result.SetAssignable(parameter0.assignable)
  5124. (* ---- MAX ----- *)
  5125. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5126. IF numberActualParameters = 1 THEN
  5127. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5128. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5129. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5130. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5131. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5132. *)
  5133. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5134. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5135. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5136. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5137. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5138. END;
  5139. ELSE
  5140. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5141. END
  5142. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5143. ConvertOperands(parameter0,parameter1);
  5144. actualParameters.SetExpression(0,parameter0);
  5145. actualParameters.SetExpression(1,parameter1);
  5146. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5147. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5148. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5149. END;
  5150. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5151. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5152. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5153. END;
  5154. END;
  5155. type := parameter0.type;
  5156. ELSE type := SyntaxTree.invalidType;
  5157. END;
  5158. (* ---- MIN ----- *)
  5159. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5160. IF numberActualParameters = 1 THEN
  5161. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5162. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5163. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5164. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5165. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5166. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5167. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5168. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5169. END;
  5170. ELSE
  5171. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5172. END
  5173. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5174. ConvertOperands(parameter0,parameter1);
  5175. actualParameters.SetExpression(0,parameter0);
  5176. actualParameters.SetExpression(1,parameter1);
  5177. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5178. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5179. ELSE result.SetResolved(parameter1.resolved)
  5180. END;
  5181. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5182. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5183. ELSE result.SetResolved(parameter1.resolved)
  5184. END;
  5185. END;
  5186. type := parameter0.type;
  5187. ELSE type := SyntaxTree.invalidType;
  5188. END;
  5189. (* ---- ODD ----- *)
  5190. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5191. type := system.booleanType;
  5192. IF CheckIntegerType(parameter0) THEN
  5193. IF IsIntegerValue(parameter0,i0) THEN
  5194. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5195. type := system.booleanType;
  5196. END;
  5197. END;
  5198. (* ---- ORD ----- *)
  5199. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5200. IF id = Global.Ord THEN
  5201. type := system.integerType;
  5202. ELSE
  5203. type := system.longintType;
  5204. END;
  5205. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5206. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5207. actualParameters.SetExpression(0,parameter0);
  5208. (* IF CheckCharacterType(parameter0) THEN*)
  5209. IF IsCharacterValue(parameter0,c)THEN
  5210. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5211. type := Global.GetSignedIntegerType(system,ORD(c));
  5212. END;
  5213. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5214. END;
  5215. (* ---- SHORT ----- *)
  5216. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5217. type := type0;
  5218. IF IsSignedIntegerType(type) THEN
  5219. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5220. ELSIF type = system.integerType THEN type := system.shortintType
  5221. ELSIF type = system.longintType THEN type := system.integerType
  5222. ELSIF type = system.hugeintType THEN type:= system.longintType
  5223. ELSE
  5224. CASE type.sizeInBits OF
  5225. 16: type := Global.Integer8
  5226. |32: type := Global.Integer16
  5227. |64: type := Global.Integer32
  5228. END;
  5229. END;
  5230. ELSIF type IS SyntaxTree.FloatType THEN
  5231. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5232. ELSIF type = system.longrealType THEN type := system.realType
  5233. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5234. END;
  5235. ELSIF type IS SyntaxTree.ComplexType THEN
  5236. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5237. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5238. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5239. END;
  5240. ELSE
  5241. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5242. END;
  5243. IF (parameter0.resolved # NIL) THEN
  5244. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5245. IF parameter0 IS SyntaxTree.Value THEN
  5246. result.SetResolved(parameter0(SyntaxTree.Value));
  5247. END;
  5248. END;
  5249. (* ---- LONG ----- *)
  5250. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5251. type := type0;
  5252. IF IsSignedIntegerType(type) THEN
  5253. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5254. ELSIF type = system.longintType THEN type := system.hugeintType
  5255. ELSIF type = system.integerType THEN type := system.longintType
  5256. ELSIF type = system.shortintType THEN type := system.integerType
  5257. ELSE
  5258. CASE type.sizeInBits OF
  5259. 8: type := Global.Integer16
  5260. |16: type := Global.Integer32
  5261. |32: type := Global.Integer64
  5262. END;
  5263. END;
  5264. ELSIF type IS SyntaxTree.FloatType THEN
  5265. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5266. ELSIF type= system.realType THEN type := system.longrealType
  5267. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5268. END;
  5269. ELSIF type IS SyntaxTree.ComplexType THEN
  5270. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5271. ELSIF type = system.complexType THEN type := system.longcomplexType
  5272. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5273. END;
  5274. ELSE
  5275. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5276. END;
  5277. IF (parameter0.resolved # NIL) THEN
  5278. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5279. IF parameter0 IS SyntaxTree.Value THEN
  5280. result.SetResolved(parameter0(SyntaxTree.Value));
  5281. END;
  5282. END;
  5283. (* ---- SIZE OF ----- *)
  5284. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5285. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5286. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5287. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5288. type := system.integerType;
  5289. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5290. ELSE
  5291. (* for variables, system sizeof could represent the physically occupied size
  5292. determined via the type descriptor, implement that ? *)
  5293. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5294. END
  5295. (* ---- SYSTEM.TRACE -----*)
  5296. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5297. FOR i := 0 TO numberActualParameters-1 DO
  5298. parameter0 := actualParameters.GetExpression(i);
  5299. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5300. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5301. END;
  5302. END;
  5303. (* remaining issues can only be tested in backend *)
  5304. (* ---- ADDRESSOF----- *)
  5305. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5306. IF HasAddress(parameter0) THEN
  5307. type := system.addressType;
  5308. ELSE
  5309. type := SyntaxTree.invalidType;
  5310. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5311. END;
  5312. (* ---- BIT ----- *)
  5313. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5314. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5315. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5316. actualParameters.SetExpression(0,parameter0);
  5317. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5318. actualParameters.SetExpression(1,parameter1);
  5319. END;
  5320. type := system.booleanType;
  5321. (* ----- MSK ---- *)
  5322. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5323. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5324. ConvertOperands(parameter0,parameter1);
  5325. actualParameters.SetExpression(0,parameter0);
  5326. actualParameters.SetExpression(1,parameter1);
  5327. END;
  5328. type := parameter0.type;
  5329. (* ---- SYSTEM.GET64 ----- *)
  5330. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5331. IF CheckAddressType(parameter0) THEN
  5332. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5333. actualParameters.SetExpression(0,parameter0);
  5334. END;
  5335. type := system.hugeintType;
  5336. (* ---- SYSTEM.GET32 ----- *)
  5337. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5338. IF CheckAddressType(parameter0) THEN
  5339. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5340. actualParameters.SetExpression(0,parameter0);
  5341. END;
  5342. type := system.longintType;
  5343. (* ---- SYSTEM.GET16 ----- *)
  5344. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5345. IF CheckAddressType(parameter0) THEN
  5346. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5347. actualParameters.SetExpression(0,parameter0);
  5348. END;
  5349. type := system.integerType;
  5350. (* ---- SYSTEM.GET8 ----- *)
  5351. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5352. IF CheckAddressType(parameter0) THEN
  5353. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5354. actualParameters.SetExpression(0,parameter0);
  5355. END;
  5356. type := system.shortintType;
  5357. (* ---- SYSTEM.GetStackPointer ----- *)
  5358. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5359. type := system.addressType;
  5360. (* ---- SYSTEM.GetFramePointer ----- *)
  5361. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5362. type := system.addressType;
  5363. (* ---- SYSTEM.GetActivity ----- *)
  5364. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5365. type := system.objectType;
  5366. (* ---- SYSTEM.SetStackPointer ----- *)
  5367. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5368. IF CheckAddressType(parameter0) THEN
  5369. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5370. actualParameters.SetExpression(0,parameter0);
  5371. END;
  5372. (* ---- SYSTEM.SetFramePointer ----- *)
  5373. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5374. IF CheckAddressType(parameter0) THEN
  5375. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5376. actualParameters.SetExpression(0,parameter0);
  5377. END;
  5378. (* ---- SYSTEM.SetActivity ----- *)
  5379. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5380. IF CheckObjectType(parameter0) THEN
  5381. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5382. actualParameters.SetExpression(0,parameter0);
  5383. END;
  5384. (* ---- LSH, LSL, ROT, ROR ----- *)
  5385. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5386. type := type0;
  5387. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5388. actualParameters.SetExpression(1, parameter1);
  5389. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5390. IF id = Global.Lsh THEN
  5391. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5392. ELSIF id = Global.Rot THEN
  5393. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5394. ELSIF id = Global.Ror THEN
  5395. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5396. END;
  5397. END;
  5398. (* ---- SYSTEM.VAL ----- *)
  5399. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5400. IF CheckTypeDeclarationType(parameter0) THEN
  5401. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5402. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5403. result := SyntaxTree.invalidExpression;
  5404. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5405. ELSE
  5406. IF (parameter1.resolved # NIL) THEN
  5407. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5408. IF parameter0 IS SyntaxTree.Value THEN
  5409. result.SetResolved(parameter0(SyntaxTree.Value));
  5410. END;
  5411. END;
  5412. result.SetAssignable(parameter1.assignable);
  5413. END;
  5414. END;
  5415. (* ---- SYSTEM.GET ----- *)
  5416. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5417. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5418. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5419. actualParameters.SetExpression(0,parameter0);
  5420. END;
  5421. (* ---- SYSTEM.PUT ----- *)
  5422. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5423. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5424. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5425. actualParameters.SetExpression(0,parameter0);
  5426. END;
  5427. (* ---- SYSTEM.PUT64 ----- *)
  5428. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5429. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5430. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5431. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5432. actualParameters.SetExpression(0,parameter0);
  5433. actualParameters.SetExpression(1,parameter1);
  5434. END;
  5435. (* ---- SYSTEM.PUT32 ----- *)
  5436. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5437. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5438. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5439. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5440. actualParameters.SetExpression(0,parameter0);
  5441. actualParameters.SetExpression(1,parameter1);
  5442. END;
  5443. (* ---- SYSTEM.PUT16 ----- *)
  5444. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5445. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5446. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5447. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5448. actualParameters.SetExpression(0,parameter0);
  5449. actualParameters.SetExpression(1,parameter1);
  5450. END;
  5451. (* ---- SYSTEM.PUT8 ----- *)
  5452. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5453. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5454. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5455. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5456. actualParameters.SetExpression(0,parameter0);
  5457. actualParameters.SetExpression(1,parameter1);
  5458. END;
  5459. (* ---- SYSTEM.MOVE ----- *)
  5460. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5461. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5462. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5463. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5464. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5465. actualParameters.SetExpression(0,parameter0);
  5466. actualParameters.SetExpression(1,parameter1);
  5467. actualParameters.SetExpression(2,parameter2);
  5468. END;
  5469. (* ---- SYSTEM.NEW ----- *)
  5470. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5471. IF ~IsPointerType(parameter0.type) THEN
  5472. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5473. ELSIF CheckSizeType(parameter1) THEN
  5474. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5475. actualParameters.SetExpression(1,parameter1);
  5476. END;
  5477. (* ----SYSTEM.REF ---- *)
  5478. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5479. type := system.addressType
  5480. (* ---- INCR ----- *)
  5481. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5482. type := system.sizeType;
  5483. base := type0;
  5484. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5485. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5486. IF i1 < 0 THEN
  5487. Error(position,Diagnostics.Invalid,"invalid dimension");
  5488. base := SyntaxTree.invalidType;
  5489. ELSE
  5490. base := ArrayBase(base,i1);
  5491. IF (base # NIL) & Indexable(base) THEN
  5492. ELSE
  5493. Error(position,Diagnostics.Invalid,"len on no array");
  5494. IF VerboseErrorMessage THEN
  5495. Printout.Info("base",base);
  5496. END;
  5497. base := SyntaxTree.invalidType;
  5498. END;
  5499. END;
  5500. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5501. actualParameters.SetExpression(1,parameter1);
  5502. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5503. mathArrayType := base(SyntaxTree.MathArrayType);
  5504. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5505. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5506. type := system.longintType;
  5507. END;
  5508. END;
  5509. ELSE
  5510. type := system.longintType;
  5511. END;
  5512. (* ---- SUM ----- *)
  5513. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5514. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5515. (* ---- ALL ----- *)
  5516. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5517. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5518. (* ---- DIM ----- *)
  5519. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5520. type := system.sizeType;
  5521. IF type0 IS SyntaxTree.MathArrayType THEN
  5522. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5523. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5524. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5525. END;
  5526. ELSE
  5527. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5528. END;
  5529. (* ---- CAS ----- *)
  5530. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5531. IF type0.IsComposite () THEN
  5532. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5533. result := SyntaxTree.invalidExpression;
  5534. ELSIF ~IsVariable (parameter0) THEN
  5535. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5536. result := SyntaxTree.invalidExpression;
  5537. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5538. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5539. result := SyntaxTree.invalidExpression;
  5540. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5541. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5542. result := SyntaxTree.invalidExpression;
  5543. ELSE
  5544. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5545. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5546. type := type0;
  5547. END;
  5548. (* ---- RESHAPE ----- *)
  5549. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5550. IF type0 IS SyntaxTree.MathArrayType THEN
  5551. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5552. base := ArrayBase(type0,MAX(LONGINT));
  5553. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5554. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5555. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5556. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5557. IF ~CompatibleTo(system,type0,parameterType) THEN
  5558. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5559. result := SyntaxTree.invalidExpression;
  5560. ELSE
  5561. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5562. END;
  5563. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5564. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5565. IF ~CompatibleTo(system,type1,parameterType) THEN
  5566. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5567. result := SyntaxTree.invalidExpression;
  5568. ELSE
  5569. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5570. END;
  5571. ELSE
  5572. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5573. result := SyntaxTree.invalidExpression;
  5574. END;
  5575. (* ---- SYSTEM.TYPECODE ----- *)
  5576. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5577. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5578. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5579. type := type.resolved;
  5580. IF type IS SyntaxTree.PointerType THEN
  5581. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5582. END;
  5583. IF ~(type IS SyntaxTree.RecordType) THEN
  5584. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5585. END;
  5586. ELSE
  5587. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5588. END;
  5589. type := system.addressType;
  5590. (* -------- FLT --------- *)
  5591. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5592. type := system.realType;
  5593. IF IsRealValue(parameter0, r) THEN
  5594. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5595. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5596. i0 := i; i := ABS(i);
  5597. IF i # 0 THEN
  5598. i1 := 23;
  5599. IF i >= 2*800000H THEN
  5600. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5601. ELSIF i < 800000H THEN
  5602. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5603. END;
  5604. i := (i1 + 127)*800000H - 800000H + i;
  5605. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5606. END;
  5607. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5608. END;
  5609. (* ------- CONNECT -------*)
  5610. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5611. IF ~(currentIsCellNet) THEN
  5612. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5613. END;
  5614. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5615. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5616. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5617. END;
  5618. IF numberActualParameters = 3 THEN
  5619. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5620. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5621. END;
  5622. *)
  5623. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5624. actualParameters.SetExpression(2,parameter2);
  5625. END;
  5626. activeCellsStatement := TRUE;
  5627. (* ---------- DELEGATE --------*)
  5628. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5629. IF ~(currentIsCellNet) THEN
  5630. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5631. END;
  5632. IF ~CheckPortType(parameter1, inPort) THEN
  5633. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5634. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5635. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5636. ELSIF (outPort.direction # inPort.direction) THEN
  5637. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5638. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5639. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5640. END;
  5641. activeCellsStatement := TRUE;
  5642. (* --------- RECEIVE ---------*)
  5643. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5644. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5645. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5646. IF inPort.direction # SyntaxTree.InPort THEN
  5647. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5648. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5649. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5650. END;
  5651. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5652. IF ~SameType(parameter2.type, system.integerType) THEN
  5653. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5654. END;
  5655. END;
  5656. END;
  5657. (* --------- SEND ---------*)
  5658. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5659. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5660. IF CheckPortType(parameter0,outPort) THEN
  5661. IF outPort.direction # SyntaxTree.OutPort THEN
  5662. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5663. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5664. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5665. ELSE
  5666. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5667. actualParameters.SetExpression(1,parameter1);
  5668. END;
  5669. END;
  5670. (* ------- custom builtins ----- *)
  5671. ELSIF id = Global.systemSpecial THEN
  5672. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5673. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5674. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5675. type := procedureType.returnType;
  5676. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5677. (* go through all formal parameters *)
  5678. formalParameter := procedureType.firstParameter;
  5679. FOR i := 0 TO actualParameters.Length() - 1 DO
  5680. actualParameter := actualParameters.GetExpression(i);
  5681. IF actualParameter = SyntaxTree.invalidExpression THEN
  5682. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5683. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5684. ELSE
  5685. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5686. END;
  5687. actualParameters.SetExpression(i, actualParameter);
  5688. formalParameter := formalParameter.nextParameter
  5689. END
  5690. END
  5691. ELSE
  5692. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5693. result := SyntaxTree.invalidExpression;
  5694. END;
  5695. END;
  5696. IF result # SyntaxTree.invalidExpression THEN
  5697. type := ResolveType(type);
  5698. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5699. result.SetType(type);
  5700. END;
  5701. RETURN result
  5702. END NewBuiltinCallDesignator;
  5703. (** return type guard designator left(type)
  5704. - check if type can be extended (i.e. is no static record)
  5705. - check if type is a type extension of left.type
  5706. - returns new type guard designator
  5707. returns invalidDesignator = invalidExpression if error
  5708. **)
  5709. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5710. VAR result: SyntaxTree.Designator;
  5711. BEGIN
  5712. result := SyntaxTree.invalidDesignator;
  5713. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5714. Error(position,Diagnostics.Invalid,"no type extension of type");
  5715. IF VerboseErrorMessage THEN
  5716. Printout.Info("left",left);
  5717. Printout.Info("type",type);
  5718. END;
  5719. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5720. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5721. ELSIF IsUnsafePointer(left.type) THEN
  5722. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5723. ELSE
  5724. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5725. result.SetType(type);
  5726. result.SetAssignable(left.assignable);
  5727. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5728. END;
  5729. RETURN result
  5730. END NewTypeGuardDesignator;
  5731. (** check and resolve parameter designator left(expression list)
  5732. - check expression list
  5733. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5734. - elsif left is a procedure type then
  5735. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5736. - else return is a procedure call then return ProcedureCallDesignator
  5737. returns invalidDesignator = invalidExpression if error
  5738. **)
  5739. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5740. VAR
  5741. parameters: SyntaxTree.ExpressionList;
  5742. left: SyntaxTree.Designator;
  5743. result,expression: SyntaxTree.Expression;
  5744. typeDeclaration: SyntaxTree.TypeDeclaration;
  5745. type, expressionType: SyntaxTree.Type;
  5746. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5747. BEGIN
  5748. type := type.resolved;
  5749. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5750. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5751. END;
  5752. RETURN type
  5753. END BaseType;
  5754. BEGIN
  5755. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5756. result := SyntaxTree.invalidDesignator;
  5757. left := ResolveDesignator(designator.left);
  5758. IF left # SyntaxTree.invalidDesignator THEN
  5759. parameters := designator.parameters;
  5760. IF ExpressionList(parameters) THEN
  5761. IF (left.type = NIL) THEN
  5762. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5763. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5764. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5765. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5766. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5767. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5768. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5769. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5770. ELSE
  5771. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5772. END
  5773. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5774. expression := parameters.GetExpression(0);
  5775. type := typeDeclaration.declaredType.resolved;
  5776. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5777. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5778. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5779. OR (expressionType IS SyntaxTree.EnumerationType)
  5780. ) THEN
  5781. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5782. ELSE
  5783. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5784. END;
  5785. ELSE
  5786. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5787. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5788. result := SyntaxTree.invalidDesignator;
  5789. END;
  5790. ELSE
  5791. result := SyntaxTree.invalidDesignator
  5792. END;
  5793. END;
  5794. resolvedExpression := result;
  5795. END VisitParameterDesignator;
  5796. (** check dereference designator left^
  5797. - check if left is pointer type or left is object type
  5798. - return new dereference designator with type = left.baseType.type (if appropriate)
  5799. with error handling
  5800. returns invalidDesignator = invalidExpression if error
  5801. **)
  5802. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5803. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5804. BEGIN
  5805. result := SyntaxTree.invalidDesignator;
  5806. type := left.type;
  5807. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5808. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5809. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5810. result.SetAssignable(TRUE);
  5811. result.SetType(type);
  5812. result.SetHidden(left.isHidden);
  5813. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5814. type := type.resolved;
  5815. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5816. result.SetAssignable(TRUE);
  5817. result.SetType(type);
  5818. result.SetHidden(left.isHidden);
  5819. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5820. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5821. result.SetAssignable(TRUE);
  5822. result.SetType(type);
  5823. result.SetHidden(left.isHidden);
  5824. ELSE
  5825. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5826. IF VerboseErrorMessage THEN
  5827. Printout.Info("pointer", type);
  5828. Printout.Info("scope", currentScope);
  5829. END;
  5830. END;
  5831. RETURN result
  5832. END NewDereferenceDesignator;
  5833. (** check supercall designator left^
  5834. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5835. - return new supercall designator with type = left.type
  5836. with error handling
  5837. **)
  5838. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5839. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5840. objectScope: SyntaxTree.Scope;
  5841. BEGIN
  5842. result := SyntaxTree.invalidDesignator;
  5843. IF left = SyntaxTree.invalidDesignator THEN
  5844. (* error already handled *)
  5845. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5846. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5847. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5848. IF symbol IS SyntaxTree.Procedure THEN
  5849. procedure := symbol(SyntaxTree.Procedure);
  5850. objectScope := currentScope;
  5851. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5852. objectScope := objectScope.outerScope;
  5853. END;
  5854. IF (left.left = NIL) OR ~
  5855. (
  5856. (left.left IS SyntaxTree.SelfDesignator) OR
  5857. (left.left IS SyntaxTree.DereferenceDesignator)
  5858. & (left.left(SyntaxTree.Designator).left # NIL)
  5859. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5860. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5861. IF VerboseErrorMessage THEN
  5862. Printout.Info("left.left",left.left);
  5863. END;
  5864. ELSIF procedure.super # NIL THEN
  5865. result := SyntaxTree.NewSupercallDesignator(position,left);
  5866. result.SetType(left.type.resolved)
  5867. ELSE
  5868. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5869. END;
  5870. ELSE
  5871. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5872. END;
  5873. ELSE
  5874. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5875. END;
  5876. RETURN result
  5877. END NewSupercallDesignator;
  5878. (** check and semantically resolve arrow designator left^
  5879. - if left is procedure type -> result := SupercallDesignator
  5880. - else result := DereferenceDesignator
  5881. returns result via global variable resolvedExpression
  5882. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5883. **)
  5884. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5885. VAR left: SyntaxTree.Designator;
  5886. BEGIN
  5887. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5888. left := ResolveDesignator(arrowDesignator.left);
  5889. IF left # NIL THEN
  5890. IF (left.type = NIL) THEN
  5891. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5892. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5893. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5894. ELSE
  5895. IF IsPointerToObject(left.type) THEN
  5896. Warning(arrowDesignator.position, "forbidden dereference on object");
  5897. END;
  5898. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5899. END
  5900. END
  5901. END VisitArrowDesignator;
  5902. (** check and return expression
  5903. - if expression has no type then resolve expression
  5904. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5905. - return result
  5906. **)
  5907. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5908. VAR result,prev: SyntaxTree.Expression;
  5909. BEGIN
  5910. IF expression = NIL THEN result := NIL
  5911. ELSIF (expression.type = NIL) THEN
  5912. prev := resolvedExpression;
  5913. resolvedExpression := SyntaxTree.invalidExpression;
  5914. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5915. expression.SetType(SyntaxTree.invalidType);
  5916. END;
  5917. expression.Accept(SELF);
  5918. result := resolvedExpression;
  5919. IF currentIsRealtime THEN
  5920. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5921. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5922. END;
  5923. END;
  5924. (* designator modifiers for backends if they support it ...*)
  5925. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5926. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5927. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5928. END;
  5929. resolvedExpression := prev
  5930. ELSE
  5931. result := expression
  5932. END;
  5933. RETURN result
  5934. END ResolveExpression;
  5935. (**
  5936. check expression to be constant expression
  5937. - resolve expression
  5938. - if valid then check that of value type
  5939. report error and return invalidExpression if anything fails
  5940. **)
  5941. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5942. VAR position: LONGINT;
  5943. BEGIN
  5944. position := expression.position;
  5945. expression := ResolveExpression(expression);
  5946. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5947. ELSIF (expression.resolved = NIL) THEN
  5948. Error(position,Diagnostics.Invalid,"expression is not constant");
  5949. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5950. expression := SyntaxTree.invalidExpression;
  5951. END;
  5952. RETURN expression
  5953. END ConstantExpression;
  5954. (** check expression to be constant integer
  5955. - resolve expresssion
  5956. - if valid then check that of integer value type
  5957. report error and return invalidExpression if anything fails
  5958. **)
  5959. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5960. VAR position: LONGINT;
  5961. BEGIN
  5962. position := expression.position;
  5963. expression := ResolveExpression(expression);
  5964. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5965. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5966. expression := SyntaxTree.invalidExpression;
  5967. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5968. END;
  5969. RETURN expression
  5970. END ConstantInteger;
  5971. (** check expression as positive (>=0) constant integer
  5972. - resolve expression
  5973. - if valid then check that integer value
  5974. - if integer value then check that value >= 0
  5975. report error and return invalidExpression if anything fails
  5976. **)
  5977. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5978. VAR position: LONGINT;
  5979. BEGIN
  5980. position := expression.position;
  5981. expression := ConstantExpression(expression);
  5982. IF expression = SyntaxTree.invalidExpression THEN
  5983. (* error already reported *)
  5984. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5985. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5986. expression := SyntaxTree.invalidExpression
  5987. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5988. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5989. END;
  5990. RETURN expression
  5991. END ConstantIntegerGeq0;
  5992. (** check expression as condition
  5993. - resolve expression
  5994. - if valid expression then check that result type is boolean
  5995. report error and return invalidExpression if anything fails
  5996. **)
  5997. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5998. VAR position: LONGINT;
  5999. BEGIN
  6000. position := expression.position;
  6001. expression := ResolveExpression(expression);
  6002. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6003. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6004. expression := SyntaxTree.invalidExpression;
  6005. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6006. END;
  6007. RETURN expression
  6008. END ResolveCondition;
  6009. (*** symbols ***)
  6010. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6011. BEGIN
  6012. x.Accept(SELF);
  6013. END ResolveSymbol;
  6014. (** check a symbol
  6015. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6016. **)
  6017. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6018. VAR scope: SyntaxTree.Scope;
  6019. BEGIN
  6020. (* visibility *)
  6021. scope := symbol.scope;
  6022. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6023. scope := scope.outerScope;
  6024. END;
  6025. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6026. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6027. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6028. IF VerboseErrorMessage THEN
  6029. Printout.Info("symbol",symbol);
  6030. END;
  6031. END;
  6032. END;
  6033. END CheckSymbolVisibility;
  6034. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6035. If node is currently being resolved then emit a cyclic definition error.
  6036. Return TRUE only if node is fully resolved.
  6037. **)
  6038. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6039. VAR result: BOOLEAN;
  6040. BEGIN
  6041. IF SyntaxTree.Resolved IN x.state THEN
  6042. result := FALSE
  6043. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6044. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6045. result := FALSE;
  6046. ELSE
  6047. result := TRUE;
  6048. x.SetState(SyntaxTree.BeingResolved)
  6049. END;
  6050. RETURN result
  6051. END SymbolNeedsResolution;
  6052. (** check and resolve a type declaration symbol = Type
  6053. - set type to declaration type
  6054. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6055. This is so because the type declaration itself does not have a type but it only stands for a type.
  6056. In the implementation of the compiler this made a lot much easier.
  6057. - resolve and set declared type
  6058. - check symbol
  6059. **)
  6060. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6061. VAR prevScope: SyntaxTree.Scope;
  6062. BEGIN
  6063. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6064. IF SymbolNeedsResolution(typeDeclaration) THEN
  6065. prevScope := currentScope;
  6066. currentScope := typeDeclaration.scope;
  6067. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6068. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6069. CheckSymbolVisibility(typeDeclaration);
  6070. typeDeclaration.SetState(SyntaxTree.Resolved);
  6071. currentScope := prevScope;
  6072. END;
  6073. END VisitTypeDeclaration;
  6074. (** check and resolve a constant declaration symbol = (constant) expression
  6075. - check expression
  6076. - set type and value
  6077. - check symbol
  6078. **)
  6079. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6080. VAR
  6081. expression: SyntaxTree.Expression;
  6082. type: SyntaxTree.Type;
  6083. name: Basic.SegmentedName;
  6084. replacement: Replacement;
  6085. BEGIN
  6086. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6087. IF SymbolNeedsResolution(constant) THEN
  6088. expression := constant.value;
  6089. IF replacements # NIL THEN
  6090. Global.GetSymbolSegmentedName(constant, name);
  6091. replacement := replacements;
  6092. WHILE (replacement # NIL) & (replacement.name # name) DO
  6093. replacement := replacement.next;
  6094. END;
  6095. IF replacement # NIL THEN
  6096. InfoSS(constant.position, "replacing constant", constant.name);
  6097. (*
  6098. NEW(stringReader, Strings.Length(replacement.string^));
  6099. stringReader.Set(replacement.string^);
  6100. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6101. NEW(parser, scanner, diagnostics);
  6102. expression := parser.Expression();
  6103. *)
  6104. expression := replacement.expression;
  6105. replacement.used := TRUE;
  6106. END;
  6107. END;
  6108. constant.SetType(SyntaxTree.invalidType);
  6109. expression := ConstantExpression(expression);
  6110. ASSERT(expression.type # NIL);
  6111. type := expression.type.resolved;
  6112. constant.SetType(type);
  6113. constant.SetValue(expression);
  6114. CheckSymbolVisibility(constant);
  6115. constant.SetState(SyntaxTree.Resolved);
  6116. END;
  6117. END VisitConstant;
  6118. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6119. VAR procedureAlignment: LONGINT;
  6120. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6121. (* least common multiple *)
  6122. VAR a,b: LONGINT;
  6123. BEGIN
  6124. a := a0; b := b0;
  6125. WHILE (a # b) DO
  6126. IF a < b THEN a := a+a0
  6127. ELSE b := b + b0
  6128. END;
  6129. END;
  6130. RETURN a
  6131. END LCM;
  6132. BEGIN
  6133. IF alignment > 1 THEN
  6134. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6135. IF (procedureAlignment > 1) THEN
  6136. alignment := LCM(alignment, procedureAlignment);
  6137. END;
  6138. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6139. END;
  6140. END AdaptStackAlignment;
  6141. (** check and resolve a variable / field
  6142. - check and set type
  6143. - negative check on open array type
  6144. - check symbol
  6145. **)
  6146. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6147. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6148. BEGIN
  6149. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6150. IF SymbolNeedsResolution(variable) THEN
  6151. modifiers := variable.modifiers;
  6152. (*
  6153. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6154. variable.AddFlags(flags);
  6155. *)
  6156. variable.SetType(ResolveType(variable.type));
  6157. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6158. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6159. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6160. END;
  6161. END;
  6162. CheckSymbolVisibility(variable);
  6163. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6164. variable.SetUntraced(TRUE);
  6165. IF ~ContainsPointer(variable.type) THEN
  6166. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6167. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6168. END;
  6169. END;
  6170. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6171. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6172. IF ~PowerOf2(value) THEN
  6173. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6174. ELSE
  6175. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6176. END;
  6177. END;
  6178. variable.SetAlignment(FALSE,value);
  6179. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6180. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6181. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6182. END;
  6183. variable.SetAlignment(TRUE, value);
  6184. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6185. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6186. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6187. END;
  6188. variable.SetFictive(value);
  6189. variable.SetOffset(value*system.dataUnit);
  6190. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6191. END;
  6192. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6193. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6194. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6195. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6196. END;
  6197. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6198. IF variable.initializer # NIL THEN
  6199. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6200. END;
  6201. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6202. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6203. pointerType.SetPointerBase(variable.type);
  6204. pointerType.SetHidden(TRUE);
  6205. variable.SetType(ResolveType(pointerType));
  6206. END;
  6207. variable.SetState(SyntaxTree.Resolved);
  6208. END;
  6209. END VisitVariable;
  6210. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6211. BEGIN
  6212. VisitVariable(property)
  6213. END VisitProperty;
  6214. (** check and resolve a (procedure) parameter
  6215. - check and set type
  6216. - check symbol
  6217. - check parameter kind and set read-only flags if appropriate
  6218. **)
  6219. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6220. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6221. BEGIN
  6222. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6223. IF SymbolNeedsResolution(parameter) THEN
  6224. modifiers := parameter.modifiers;
  6225. parameter.SetType(ResolveType(parameter.type));
  6226. ASSERT(parameter.type.resolved # NIL);
  6227. CheckSymbolVisibility(parameter);
  6228. IF parameter.defaultValue # NIL THEN
  6229. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6230. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6231. ELSE
  6232. expression := ConstantExpression(parameter.defaultValue);
  6233. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6234. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6235. parameter.SetDefaultValue(expression);
  6236. END;
  6237. END;
  6238. END;
  6239. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6240. parameter.SetUntraced(TRUE);
  6241. IF ~ContainsPointer(parameter.type) THEN
  6242. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6243. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6244. END;
  6245. END;
  6246. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6247. parameter.SetMoveable(TRUE);
  6248. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6249. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6250. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6251. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6252. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6253. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6254. END;
  6255. END;
  6256. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6257. parameter.SetState(SyntaxTree.Resolved);
  6258. END;
  6259. END VisitParameter;
  6260. (** check and resolve a procedure (with declaration and implementation scope)
  6261. - check the procedure type
  6262. - check if method (i.e. in record scope), if so then
  6263. - check if (unique) constructor
  6264. - check if (unique) finalizer
  6265. - check if super method available, if so then check signature
  6266. - of not in record scope then negative check on constructor flag
  6267. - of not in record scope then negative check on finalizer flag
  6268. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6269. - check procedure symbol
  6270. **)
  6271. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6272. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6273. procedureType: SyntaxTree.ProcedureType;
  6274. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6275. qualifiedType: SyntaxTree.QualifiedType;
  6276. value: LONGINT;
  6277. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6278. position: LONGINT;
  6279. BEGIN
  6280. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6281. IF IsOberonInline(procedure) THEN
  6282. IF SyntaxTree.Public * procedure.access # {} THEN
  6283. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6284. END;
  6285. procedure.SetInline(FALSE);
  6286. procedure.SetOberonInline(TRUE);
  6287. END;
  6288. IF SymbolNeedsResolution(procedure) THEN
  6289. recentIsRealtime := currentIsRealtime;
  6290. recentIsBodyProcedure := currentIsBodyProcedure;
  6291. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6292. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6293. modifiers := procedureType.modifiers;
  6294. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6295. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6296. IF useDarwinCCalls THEN (*fld*)
  6297. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6298. ELSE
  6299. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6300. END
  6301. END;
  6302. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6303. procedureType.SetInterrupt(TRUE);
  6304. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6305. END;
  6306. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6307. procedureType.SetNoReturn(TRUE);
  6308. END;
  6309. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6310. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6311. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6312. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6313. END;
  6314. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6315. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6316. END;
  6317. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6318. IF ~PowerOf2(value) THEN
  6319. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6320. ELSE
  6321. procedureType.SetStackAlignment(value)
  6322. END;
  6323. END;
  6324. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6325. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6326. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6327. END;
  6328. CheckModifiers(modifiers, TRUE);
  6329. modifiers := procedureType.returnTypeModifiers;
  6330. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6331. CheckModifiers(modifiers, TRUE);
  6332. FixProcedureType(procedureType);
  6333. currentIsRealtime := procedureType.isRealtime;
  6334. currentIsBodyProcedure := procedure.isBodyProcedure;
  6335. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6336. THEN
  6337. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6338. END;
  6339. CheckSymbolVisibility(procedure);
  6340. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6341. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6342. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6343. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6344. END;
  6345. END;
  6346. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6347. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6348. procedureType.SetDelegate(TRUE);
  6349. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6350. IF record.pointerType.typeDeclaration = NIL THEN
  6351. selfParameter.SetType(record.pointerType);
  6352. ELSE
  6353. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6354. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6355. qualifiedType.SetResolved(record.pointerType);
  6356. selfParameter.SetType(qualifiedType);
  6357. END;
  6358. selfParameter.SetAccess(SyntaxTree.Hidden);
  6359. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6360. IF procedure.isConstructor THEN
  6361. (*! constructor is always visible, compatibility to paco
  6362. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6363. *)
  6364. procedure.MarkUsed;
  6365. IF procedureType.returnType # NIL THEN
  6366. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6367. END;
  6368. proc := procedure.scope.firstProcedure;
  6369. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6370. proc := proc.nextProcedure;
  6371. END;
  6372. IF proc # NIL THEN
  6373. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6374. ELSE
  6375. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6376. END;
  6377. END;
  6378. IF procedure.isFinalizer THEN
  6379. procedure.MarkUsed;
  6380. IF procedureType.returnType # NIL THEN
  6381. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6382. END;
  6383. IF procedureType.numberParameters # 0 THEN
  6384. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6385. END;
  6386. proc := procedure.scope.firstProcedure;
  6387. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6388. proc := proc.nextProcedure;
  6389. END;
  6390. IF proc # NIL THEN
  6391. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6392. ELSE
  6393. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6394. END;
  6395. END;
  6396. super := FindSuperProcedure(record.recordScope, procedure);
  6397. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6398. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6399. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6400. END;
  6401. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6402. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6403. END;
  6404. IF super.isFinal THEN
  6405. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6406. END;
  6407. procedure.SetSuper(super);
  6408. super.SetOverwritten(TRUE);
  6409. procedure.SetAccess(procedure.access+super.access);
  6410. procedure.MarkUsed;
  6411. END;
  6412. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6413. THEN
  6414. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6415. END;
  6416. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6417. IF cellsAreObjects THEN
  6418. procedureType.SetDelegate(TRUE);
  6419. END;
  6420. IF procedure.isConstructor THEN
  6421. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6422. END;
  6423. ELSIF procedure.isConstructor THEN
  6424. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6425. END;
  6426. Declarations(procedure.procedureScope, FALSE);
  6427. (* body resolution part done as late fix of the procedure type *)
  6428. procedure.SetState(SyntaxTree.Resolved);
  6429. currentIsRealtime := recentIsRealtime;
  6430. currentIsBodyProcedure := recentIsBodyProcedure;
  6431. END;
  6432. END VisitProcedure;
  6433. (**
  6434. a builtin procedure is a global item that may not be modified locally
  6435. instead the resolving of builtin procedure calls are done in the esignator
  6436. **)
  6437. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6438. VAR type: SyntaxTree.Type;
  6439. BEGIN
  6440. type := ResolveType(builtinProcedure.type);
  6441. END VisitBuiltin;
  6442. (* nopov *)
  6443. (** check and resolve operator
  6444. - operators are first checked as procedures
  6445. - then additional operator-specific checks are done
  6446. - note that only module-scope operators are checked here
  6447. (operators in a record scope are only allowed in the context of
  6448. array-structured object types and checked in 'ResolveArrayStructure')
  6449. - also note that inter-operator conformity is not checked here
  6450. **)
  6451. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6452. VAR
  6453. procedureType: SyntaxTree.ProcedureType;
  6454. leftType, rightType: SyntaxTree.Type;
  6455. identifierNumber, position: LONGINT;
  6456. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6457. modifiers: SyntaxTree.Modifier;
  6458. (** whether a type is locally defined in the current module scope
  6459. for arrays, the base type must be locally defined **)
  6460. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6461. BEGIN
  6462. IF type = NIL THEN
  6463. RETURN FALSE
  6464. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6465. RETURN TRUE
  6466. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6467. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6468. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6469. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6470. ELSE
  6471. RETURN FALSE
  6472. END
  6473. END IsLocallyDefined;
  6474. BEGIN
  6475. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6476. procedureType := operator.type(SyntaxTree.ProcedureType);
  6477. modifiers := procedureType.modifiers;
  6478. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6479. CheckModifiers(modifiers, TRUE);
  6480. VisitProcedure(operator);
  6481. IF operator.scope IS SyntaxTree.RecordScope THEN
  6482. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6483. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6484. IF identifierNumber = -1 THEN
  6485. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6486. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6487. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6488. ELSE
  6489. IF procedureType.numberParameters < 1 THEN
  6490. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6491. ELSIF procedureType.numberParameters > 2 THEN
  6492. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6493. ELSE
  6494. (* determine operand types *)
  6495. leftType := procedureType.firstParameter.type;
  6496. IF procedureType.numberParameters > 1 THEN
  6497. rightType := procedureType.firstParameter.nextParameter.type
  6498. ELSE
  6499. rightType := NIL
  6500. END;
  6501. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6502. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6503. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6504. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6505. END
  6506. END;
  6507. (* TODO: refine the checks, think about how restrictive the checks should be
  6508. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6509. They might be used for intersection, union, complement of custom object types *)
  6510. (* defaults *)
  6511. hasReturnType := TRUE;
  6512. mustBeUnary := FALSE;
  6513. mustBeBinary := FALSE;
  6514. mustReturnBoolean := FALSE;
  6515. mustReturnInteger := FALSE;
  6516. mustHaveEquitypedOperands := FALSE;
  6517. (* operator-specific exceptions *)
  6518. CASE identifierNumber OF
  6519. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6520. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6521. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6522. mustBeBinary := TRUE
  6523. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6524. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6525. | Scanner.Times: mustBeBinary := TRUE
  6526. | Scanner.TimesTimes: mustBeBinary := TRUE
  6527. | Scanner.DotTimes: mustBeBinary := TRUE
  6528. | Scanner.PlusTimes: mustBeBinary := TRUE
  6529. | Scanner.Slash: mustBeBinary := TRUE
  6530. | Scanner.Backslash: mustBeBinary := TRUE
  6531. | Scanner.DotSlash: mustBeBinary := TRUE
  6532. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6533. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6534. | Scanner.Not: mustBeUnary := TRUE
  6535. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6536. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6537. | Scanner.Transpose: mustBeUnary := TRUE;
  6538. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6539. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6540. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6541. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6542. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6543. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6544. | Global.Abs: mustBeUnary := TRUE;
  6545. | Global.Ash: (* TODO: arity? *)
  6546. | Global.Cap: (* TODO: arity? *)
  6547. | Global.Chr: mustBeUnary := TRUE;
  6548. | Global.Entier: (* TODO: arity? *)
  6549. | Global.EntierH: (* TODO: arity? *)
  6550. | Global.Len: (* unary and binary *)
  6551. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6552. | Global.Max, Global.Min: (* unary and binary *)
  6553. | Global.Odd: (* TODO: arity? *)
  6554. | Global.Sum: (* TODO: arity? *)
  6555. | Global.All: (* TODO: arity? *)
  6556. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6557. | Scanner.Alias:
  6558. | Scanner.GreaterGreater, Scanner.LessLess:
  6559. mustBeBinary := TRUE; hasReturnType := FALSE;
  6560. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6561. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6562. END;
  6563. (* check parameter count *)
  6564. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6565. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6566. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6567. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6568. END;
  6569. (* check parameter types *)
  6570. (* TODO: is this used at all? *)
  6571. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6572. leftType := procedureType.firstParameter.type;
  6573. rightType := procedureType.firstParameter.nextParameter.type;
  6574. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6575. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6576. END
  6577. END;
  6578. (* check return type *)
  6579. IF hasReturnType THEN
  6580. IF procedureType.returnType = NIL THEN
  6581. Error(operator.position, Diagnostics.Invalid, "return type required")
  6582. ELSIF mustReturnBoolean THEN
  6583. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6584. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6585. END
  6586. ELSIF mustReturnInteger THEN
  6587. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6588. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6589. END
  6590. END
  6591. ELSIF procedureType.returnType # NIL THEN
  6592. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6593. END
  6594. END
  6595. END
  6596. END
  6597. END VisitOperator;
  6598. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6599. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6600. BEGIN
  6601. IF error THEN RETURN FALSE END;
  6602. prevScope := currentScope;
  6603. prevDiagnostics := diagnostics;
  6604. diagnostics := NIL; (* suppress error output *)
  6605. currentScope := module.moduleScope;
  6606. VisitImport(x);
  6607. IF ~error THEN
  6608. module.moduleScope.AddImport(x);
  6609. x.SetScope(module.moduleScope);
  6610. END;
  6611. currentScope := prevScope;
  6612. diagnostics := prevDiagnostics;
  6613. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6614. END AddImport;
  6615. (** check and resolve import
  6616. - check for name = SYSTEM
  6617. - check for forbidden self import
  6618. - search through global import cache: already imported?
  6619. - check if already imported indirectly
  6620. - import if necessary -> set module and enter into import cache
  6621. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6622. - after this import this direct import and all indirect imports are stored in the current module's import list
  6623. **)
  6624. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6625. VAR
  6626. module: SyntaxTree.Module;
  6627. moduleScope: SyntaxTree.ModuleScope;
  6628. import,reimport: SyntaxTree.Import;
  6629. filename: FileName;
  6630. prevScope: SyntaxTree.Scope;
  6631. BEGIN
  6632. IF SymbolNeedsResolution(x) THEN
  6633. prevScope := currentScope;
  6634. x.SetType(SyntaxTree.importType);
  6635. moduleScope := currentScope.ownerModule.moduleScope;
  6636. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6637. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6638. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6639. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6640. ELSE
  6641. (* search through global import list: already imported ? *)
  6642. IF (x.module = NIL) & (importCache # NIL) THEN
  6643. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6644. ELSE import := NIL
  6645. END;
  6646. IF x.module # NIL THEN (* already imported indirectly *)
  6647. module := x.module;
  6648. ELSIF import # NIL THEN (* already in module list *)
  6649. module := import.module;
  6650. ASSERT(module # NIL);
  6651. x.SetModule(module);
  6652. ELSE (* must be imported *)
  6653. Global.ModuleFileName(x.moduleName,x.context,filename);
  6654. IF symbolFileFormat # NIL THEN
  6655. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6656. IF module = NIL THEN
  6657. ErrorSS(x.position,"could not import",filename);
  6658. IF VerboseErrorMessage THEN
  6659. Printout.Info("import",x)
  6660. END
  6661. ELSE
  6662. (*
  6663. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6664. (*! should rather be done by importer *)
  6665. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6666. checker.importCache := importCache;
  6667. checker.arrayBaseImported := arrayBaseImported;
  6668. checker.global := global;
  6669. checker.Module(module); (* semantic check *)
  6670. error := error OR checker.error;
  6671. END;
  6672. *)
  6673. (*
  6674. ASSERT(SyntaxTree.Resolved IN module.state);
  6675. *)
  6676. x.SetModule(module);
  6677. IF importCache # NIL THEN
  6678. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6679. import.SetContext(x.context);
  6680. import.SetModule(module);
  6681. importCache.AddImport(import);
  6682. END;
  6683. END;
  6684. ELSE
  6685. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6686. END;
  6687. END;
  6688. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6689. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6690. END;
  6691. import := module.moduleScope.firstImport;
  6692. WHILE(import # NIL) DO
  6693. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6694. ASSERT(currentScope # NIL);
  6695. ASSERT(currentScope.ownerModule # NIL);
  6696. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6697. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6698. Error(x.position,Diagnostics.Invalid,"recursive import");
  6699. ELSE
  6700. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6701. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6702. IF reimport = NIL THEN (* indirect import *)
  6703. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6704. reimport.SetContext(import.context);
  6705. reimport.SetModule(import.module);
  6706. moduleScope.AddImport(reimport);
  6707. reimport.SetScope(moduleScope);
  6708. ELSE
  6709. ASSERT(import.module # NIL);
  6710. reimport.SetModule(import.module); (* direct or indirect import *)
  6711. END;
  6712. END;
  6713. import := import.nextImport;
  6714. END;
  6715. END;
  6716. END;
  6717. currentScope := prevScope;
  6718. (* ELSE nothing to be done *)
  6719. x.SetState(SyntaxTree.Resolved);
  6720. END;
  6721. END VisitImport;
  6722. (*** statements ***)
  6723. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6724. VAR prev,resolved: SyntaxTree.Statement;
  6725. BEGIN
  6726. prev := resolvedStatement;
  6727. resolvedStatement := x;
  6728. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6729. activeCellsStatement := FALSE;
  6730. x.Accept(SELF);
  6731. (* removed this, implementation restriction should be resolved by backend
  6732. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6733. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6734. END;
  6735. *)
  6736. resolved := resolvedStatement;
  6737. resolvedStatement := prev;
  6738. RETURN resolved
  6739. END ResolveStatement;
  6740. (** check and resolve statement sequence
  6741. - check all statements, replace if necessary
  6742. **)
  6743. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6744. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6745. BEGIN
  6746. IF statementSequence # NIL THEN (* else empty *)
  6747. FOR i := 0 TO statementSequence.Length()-1 DO
  6748. statement := statementSequence.GetStatement(i);
  6749. resolved := ResolveStatement(statement);
  6750. IF (resolved # statement) THEN
  6751. statementSequence.SetStatement(i,resolved);
  6752. END;
  6753. END;
  6754. END;
  6755. END StatementSequence;
  6756. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6757. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6758. - check if procedure is callable
  6759. - check return type = NIL (otherwise must be assignment statement)
  6760. **)
  6761. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6762. VAR call: SyntaxTree.Designator;
  6763. BEGIN
  6764. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6765. call := procedureCall.call;
  6766. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6767. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6768. END;
  6769. call := ResolveDesignator(call);
  6770. IF call = SyntaxTree.invalidDesignator THEN
  6771. (* error already handled *)
  6772. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6773. (* inline call in a statement *)
  6774. ELSIF ~IsCallable(call) THEN
  6775. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6776. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6777. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6778. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6779. END;
  6780. procedureCall.SetCall(call);
  6781. (*
  6782. IF call = SyntaxTree.invalidDesignator THEN
  6783. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6784. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6785. IF IsOberonInline(procedure) THEN
  6786. Warning(procedure.position,"call to inline proc");
  6787. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6788. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6789. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6790. resolvedStatement := block;
  6791. RETURN;
  6792. END;
  6793. END;
  6794. *)
  6795. END VisitProcedureCallStatement;
  6796. (** check and resolve assignment LHS := RHS
  6797. - resolve LHS and RHS
  6798. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6799. - check if assignment is compatible
  6800. - check if LHS is variable (i.e. assignable)
  6801. - convert RHS if necessary
  6802. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6803. - assignment between different ASOTs
  6804. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6805. - assignment to ASOT elements:
  6806. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6807. **)
  6808. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6809. VAR
  6810. left: SyntaxTree.Designator;
  6811. right, expression: SyntaxTree.Expression;
  6812. designator: SyntaxTree.Designator;
  6813. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6814. mathArrayType: SyntaxTree.MathArrayType;
  6815. BEGIN
  6816. right := ResolveExpression(assignment.right);
  6817. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6818. left := ResolveDesignator(assignment.left);
  6819. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6820. (* error already handled *)
  6821. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6822. (* LHS is index write operator call on ASOT *)
  6823. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6824. (* necessary ?
  6825. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6826. type := procedureType.firstParameter.type;
  6827. expression := procedureCallDesignator.parameters.GetExpression(0);
  6828. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6829. *)
  6830. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6831. ELSIF CheckVariable(left) THEN
  6832. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6833. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6834. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6835. (* conversion done by procedure call
  6836. (* try to convert to left argument *)
  6837. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6838. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6839. procedureCallDesignator.parameters.SetExpression(1, right);
  6840. END;
  6841. *)
  6842. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6843. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6844. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6845. ELSIF AssignmentCompatible(left, right) THEN
  6846. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6847. mathArrayType := MathArrayStructureOfType(left.type);
  6848. right := NewConversion(right.position, right, mathArrayType, NIL);
  6849. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6850. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6851. ELSE
  6852. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6853. assignment.SetLeft(left);
  6854. assignment.SetRight(right);
  6855. resolvedStatement := assignment
  6856. END
  6857. END
  6858. END
  6859. END VisitAssignment;
  6860. (** check and resolve assignment LHS := RHS
  6861. - resolve LHS and RHS
  6862. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6863. - check if assignment is compatible
  6864. - check if LHS is variable (i.e. assignable)
  6865. - convert RHS if necessary
  6866. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6867. - assignment between different ASOTs
  6868. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6869. - assignment to ASOT elements:
  6870. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6871. **)
  6872. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6873. VAR
  6874. left: SyntaxTree.Designator;
  6875. right: SyntaxTree.Expression;
  6876. inPort, outPort: SyntaxTree.PortType;
  6877. expression: SyntaxTree.Expression;
  6878. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6879. BEGIN
  6880. right := ResolveExpression(communication.right);
  6881. left := ResolveDesignator(communication.left);
  6882. communication.SetLeft(left);
  6883. communication.SetRight(right);
  6884. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6885. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6886. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6887. (* conversion done by procedure call
  6888. (* try to convert to left argument *)
  6889. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6890. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6891. procedureCallDesignator.parameters.SetExpression(1, right);
  6892. END;
  6893. *)
  6894. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6895. ELSE
  6896. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6897. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6898. (* error already handled *)
  6899. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6900. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6901. IF outPort.direction # SyntaxTree.OutPort THEN
  6902. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6903. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6904. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6905. ELSE
  6906. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6907. communication.SetRight(right)
  6908. END;
  6909. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6910. IF CheckVariable(left) THEN
  6911. IF inPort.direction # SyntaxTree.InPort THEN
  6912. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6913. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6914. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6915. END;
  6916. END;
  6917. ELSE
  6918. Error(communication.position, -1, "unsupported stream operation");
  6919. END;
  6920. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6921. IF outPort.direction # SyntaxTree.OutPort THEN
  6922. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6923. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6924. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6925. ELSE
  6926. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6927. communication.SetRight(right)
  6928. END;
  6929. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6930. IF CheckVariable(right) THEN
  6931. IF inPort.direction # SyntaxTree.InPort THEN
  6932. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6933. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6934. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6935. END;
  6936. END;
  6937. ELSE
  6938. Error(communication.position, -1, "unsupported operation");
  6939. END;
  6940. END;
  6941. END VisitCommunicationStatement;
  6942. (** check and resolve if/eslif part
  6943. - check condition
  6944. - check statement sequence
  6945. **)
  6946. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6947. VAR prevUnreachable, b: BOOLEAN;
  6948. BEGIN
  6949. prevUnreachable := currentIsUnreachable;
  6950. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6951. IF IsBooleanValue(ifPart.condition,b) THEN
  6952. IF b=FALSE THEN
  6953. currentIsUnreachable := TRUE
  6954. ELSIF b=TRUE THEN
  6955. true := TRUE
  6956. END;
  6957. END;
  6958. StatementSequence(ifPart.statements);
  6959. currentIsUnreachable := prevUnreachable;
  6960. END IfPart;
  6961. (** check and resolve if statement
  6962. - check if parts and else part statement sequence
  6963. **)
  6964. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6965. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6966. BEGIN
  6967. prevUnreachable := currentIsUnreachable;
  6968. ifPartTrue := FALSE;
  6969. IfPart(ifStatement.ifPart,ifPartTrue);
  6970. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6971. elsif := ifStatement.GetElsifPart(i);
  6972. IfPart(elsif,ifPartTrue);
  6973. END;
  6974. IF ifStatement.elsePart # NIL THEN
  6975. IF ifPartTrue THEN
  6976. currentIsUnreachable := TRUE
  6977. END;
  6978. StatementSequence(ifStatement.elsePart)
  6979. END;
  6980. currentIsUnreachable := prevUnreachable;
  6981. END VisitIfStatement;
  6982. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6983. VAR variable: SyntaxTree.Designator;
  6984. type,variableType: SyntaxTree.Type;
  6985. withEntry: WithEntry;
  6986. BEGIN
  6987. variable := ResolveDesignator(withPart.variable);
  6988. variableType := variable.type.resolved;
  6989. withPart.SetVariable(variable);
  6990. type := ResolveType(withPart.type);
  6991. withPart.SetType(type);
  6992. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6993. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6994. END;
  6995. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6996. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6997. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6998. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6999. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7000. IF VerboseErrorMessage THEN
  7001. Printout.Info("variable",variable)
  7002. END;
  7003. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7004. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7005. IF VerboseErrorMessage THEN
  7006. Printout.Info("variable",variable);
  7007. Printout.Info("type",type);
  7008. END;
  7009. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7010. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7011. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7012. IF VerboseErrorMessage THEN
  7013. Printout.Info("variable",variable);
  7014. Printout.Info("type",type);
  7015. END;
  7016. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7017. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7018. ELSE
  7019. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7020. NEW(withEntry);
  7021. withEntry.previous := withEntries;
  7022. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7023. withEntry.type := type;
  7024. withEntries := withEntry;
  7025. StatementSequence(withPart.statements);
  7026. withEntries := withEntries.previous;
  7027. END;
  7028. END WithPart;
  7029. (** check and resolve with statement WITH variable: type DO ... END;
  7030. - check type and variable
  7031. - check that variable type is type extension of type
  7032. - check that variable is a variable
  7033. - enter new with scope and enter guardedVariable with same name and reference to variable
  7034. - create if statement:
  7035. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7036. **)
  7037. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7038. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7039. BEGIN
  7040. prevScope := currentScope; symbol := NIL;
  7041. FOR i := 0 TO withStatement.WithParts()-1 DO
  7042. WithPart(withStatement.GetWithPart(i),symbol);
  7043. END;
  7044. IF withStatement.elsePart # NIL THEN
  7045. StatementSequence(withStatement.elsePart)
  7046. END;
  7047. currentScope := prevScope;
  7048. END VisitWithStatement;
  7049. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7050. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7051. - check 'first' < 'last' and no overlaps between different case labels
  7052. - check statement sequence
  7053. **)
  7054. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7055. VAR
  7056. i: LONGINT;
  7057. position: LONGINT;
  7058. expression, left, right: SyntaxTree.Expression;
  7059. expressionType: SyntaxTree.Type;
  7060. l, r: LONGINT;
  7061. cl, cr: CHAR;
  7062. thiscases: SyntaxTree.CaseConstant;
  7063. BEGIN
  7064. thiscases := NIL;
  7065. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7066. expression := casePart.elements.GetExpression(i);
  7067. position := expression.position;
  7068. (* set context of range *)
  7069. IF expression IS SyntaxTree.RangeExpression THEN
  7070. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7071. END;
  7072. expression := ResolveExpression(expression);
  7073. IF expression = SyntaxTree.invalidExpression THEN
  7074. (* error already reported *)
  7075. expressionType := SyntaxTree.invalidType;
  7076. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7077. (* read out 'first' and 'last' *)
  7078. left := expression(SyntaxTree.RangeExpression).first;
  7079. right := expression(SyntaxTree.RangeExpression).last;
  7080. (* guaranteed by VisitRangeExpression: *)
  7081. ASSERT((left # NIL) & (right # NIL));
  7082. ASSERT(left.type.resolved = right.type.resolved);
  7083. left := CompatibleConversion(left.position, left, type);
  7084. right := CompatibleConversion(right.position, right, type);
  7085. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7086. expression(SyntaxTree.RangeExpression).SetLast(right);
  7087. expressionType := RegularType(position,left.type);
  7088. ELSE
  7089. expression := ConstantExpression(expression);
  7090. expression := CompatibleConversion(expression.position, expression, type);
  7091. (*
  7092. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7093. left := Global.NewCharacterValue(system,expression.position,cl);
  7094. expression := casePart.elements.GetExpression(i);
  7095. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7096. expression := left
  7097. END;
  7098. *)
  7099. casePart.elements.SetExpression(i,expression);
  7100. left := expression; right := expression;
  7101. expressionType := RegularType(position,expression.type)
  7102. END;
  7103. IF (expressionType = SyntaxTree.invalidType) THEN
  7104. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7105. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7106. expression := SyntaxTree.invalidExpression;
  7107. ELSE
  7108. l := 0; r := 0;
  7109. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7110. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7111. l := ORD(cl); r := ORD(cr);
  7112. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7113. ELSE
  7114. expression := SyntaxTree.invalidExpression
  7115. END;
  7116. IF expression # SyntaxTree.invalidExpression THEN
  7117. IF l>r THEN
  7118. Error(position,Diagnostics.Invalid,"empty case label")
  7119. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7120. Error(position,Diagnostics.Invalid,"duplicate case label");
  7121. ELSE
  7122. IF l < min THEN min := l END;
  7123. IF r > max THEN max := r END;
  7124. END;
  7125. END;
  7126. END;
  7127. casePart.elements.SetExpression(i,expression);
  7128. END;
  7129. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7130. casePart.SetConstants(thiscases);
  7131. StatementSequence(casePart.statements);
  7132. END CasePart;
  7133. (** check and resolve case statement CASE variable OF ... END;
  7134. - check variable
  7135. - check case parts
  7136. **)
  7137. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7138. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7139. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7140. BEGIN
  7141. expression := ResolveExpression(caseStatement.variable);
  7142. type := RegularType(expression.position,expression.type);
  7143. IF type = SyntaxTree.invalidType THEN
  7144. expression := SyntaxTree.invalidExpression;
  7145. ELSIF IsIntegerType(type) THEN
  7146. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7147. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7148. (*
  7149. expression := Global.NewCharacterValue(system,expression.position,ch);
  7150. *)
  7151. type := expression.type;
  7152. ELSIF IsCharacterType(type) THEN
  7153. ELSIF IsEnumerationType(type) THEN
  7154. ELSE
  7155. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7156. expression := SyntaxTree.invalidExpression;
  7157. END;
  7158. caseStatement.SetVariable(expression);
  7159. caseList := NIL;
  7160. min := MAX(LONGINT); max := MIN(LONGINT);
  7161. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7162. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7163. END;
  7164. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7165. msg := "huge sparse case table ";
  7166. Strings.AppendInt(msg, max-min);
  7167. Strings.Append(msg,"/");
  7168. Strings.AppendInt(msg, caseStatement.CaseParts());
  7169. Warning(caseStatement.position,msg);
  7170. END;
  7171. caseStatement.SetMinMax(min,max);
  7172. StatementSequence(caseStatement.elsePart);
  7173. IF expression.resolved # NIL THEN
  7174. IF IsCharacterValue(expression,ch) THEN
  7175. l := ORD(ch)
  7176. ELSIF IsIntegerValue(expression,l) THEN
  7177. END;
  7178. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7179. END;
  7180. END VisitCaseStatement;
  7181. (** check and resolve while statement
  7182. - check condition
  7183. - check statement sequence
  7184. **)
  7185. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7186. VAR prevIsUnreachable,b: BOOLEAN;
  7187. BEGIN
  7188. prevIsUnreachable := currentIsUnreachable;
  7189. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7190. IF IsBooleanValue(whileStatement.condition,b) THEN
  7191. IF b=FALSE THEN
  7192. currentIsUnreachable := TRUE
  7193. END;
  7194. END;
  7195. StatementSequence(whileStatement.statements);
  7196. currentIsUnreachable := prevIsUnreachable
  7197. END VisitWhileStatement;
  7198. (** check and resolve repeat statement
  7199. - check condition
  7200. - check statement sequence
  7201. **)
  7202. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7203. BEGIN
  7204. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7205. StatementSequence(repeatStatement.statements);
  7206. END VisitRepeatStatement;
  7207. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7208. VAR withEntry: WithEntry;
  7209. BEGIN
  7210. withEntry := withEntries;
  7211. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7212. withEntry := withEntry.previous
  7213. END;
  7214. IF withEntry = NIL THEN RETURN FALSE
  7215. ELSE
  7216. type := withEntry.type;
  7217. RETURN TRUE
  7218. END;
  7219. END GetGuard;
  7220. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7221. - check that variable is an integer variable
  7222. - check that from is integer typed with compatible type
  7223. - check that to has compatible type
  7224. - check that by is constant integer with compatible type
  7225. **)
  7226. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7227. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7228. BEGIN
  7229. designator := ResolveDesignator(forStatement.variable);
  7230. type := SyntaxTree.invalidType;
  7231. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7232. designator := SyntaxTree.invalidDesignator;
  7233. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7234. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7235. designator := SyntaxTree.invalidDesignator;
  7236. ELSIF CheckVariable(designator) THEN
  7237. type := designator.type;
  7238. END;
  7239. forStatement.SetVariable(designator);
  7240. expression := ResolveExpression(forStatement.from);
  7241. IF expression = SyntaxTree.invalidExpression THEN
  7242. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7243. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7244. expression := SyntaxTree.invalidExpression;
  7245. ELSIF type # SyntaxTree.invalidType THEN
  7246. expression := NewConversion(expression.position,expression,type,NIL)
  7247. END;
  7248. forStatement.SetFrom(expression);
  7249. expression := ResolveExpression(forStatement.to);
  7250. IF expression = SyntaxTree.invalidExpression THEN
  7251. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7252. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7253. expression := SyntaxTree.invalidExpression;
  7254. ELSIF type # SyntaxTree.invalidType THEN
  7255. expression := NewConversion(expression.position,expression,type,NIL)
  7256. END;
  7257. forStatement.SetTo(expression);
  7258. IF forStatement.by # NIL THEN
  7259. expression := ConstantInteger(forStatement.by);
  7260. ELSE
  7261. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7262. END;
  7263. IF expression = SyntaxTree.invalidExpression THEN
  7264. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7265. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7266. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7267. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7268. ELSIF type # SyntaxTree.invalidType THEN
  7269. expression := NewConversion(expression.position,expression,type,NIL)
  7270. END;
  7271. forStatement.SetBy(expression);
  7272. StatementSequence(forStatement.statements);
  7273. END VisitForStatement;
  7274. (** check and resolve loop statement LOOP StatementSequence END
  7275. - check statement sequence
  7276. **)
  7277. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7278. BEGIN
  7279. StatementSequence(loopStatement.statements)
  7280. END VisitLoopStatement;
  7281. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7282. BEGIN
  7283. StatementSequence(exitableBlock.statements);
  7284. END VisitExitableBlock;
  7285. (** check and resolve exit statement EXIT
  7286. - check that exit is within LOOP statement block
  7287. **)
  7288. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7289. VAR outer: SyntaxTree.Statement;
  7290. BEGIN
  7291. outer := exitStatement.outer;
  7292. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7293. outer := outer.outer;
  7294. END;
  7295. IF outer = NIL THEN
  7296. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7297. END;
  7298. END VisitExitStatement;
  7299. (** check and resolve return statement RETURN [expression]
  7300. - check expression (if any)
  7301. - check if in procedure scope
  7302. - if in procedure scope then check expression compatibility
  7303. - if not in procecdure scope then check on return without expression
  7304. **)
  7305. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7306. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7307. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7308. BEGIN
  7309. position := returnStatement.position;
  7310. expression := returnStatement.returnValue;
  7311. IF expression # NIL THEN
  7312. expression := ResolveExpression(expression);
  7313. returnStatement.SetReturnValue(expression);
  7314. END;
  7315. outer := returnStatement.outer;
  7316. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7317. outer := outer.outer
  7318. END;
  7319. IF (outer # NIL) THEN
  7320. scope := outer(SyntaxTree.Body).inScope;
  7321. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7322. IF (expression # NIL) THEN
  7323. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7324. END;
  7325. ELSE
  7326. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7327. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7328. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7329. END;
  7330. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7331. IF returnType # NIL THEN
  7332. returnType := returnType.resolved;
  7333. IF expression = NIL THEN
  7334. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7335. ELSIF expression.type = NIL THEN
  7336. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7337. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7338. Error(position,Diagnostics.Invalid,"return type not compatible");
  7339. IF VerboseErrorMessage THEN
  7340. Printout.Info("returnType",returnType);
  7341. Printout.Info("expression",expression);
  7342. END;
  7343. ELSE
  7344. expression := NewConversion(expression.position,expression,returnType,NIL);
  7345. returnStatement.SetReturnValue(expression);
  7346. END;
  7347. ELSIF expression # NIL THEN
  7348. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7349. END;
  7350. END;
  7351. END;
  7352. END VisitReturnStatement;
  7353. (** check and resolve await statement AWAIT(condition: Expression)
  7354. - check await condition
  7355. **)
  7356. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7357. VAR condition: SyntaxTree.Expression;
  7358. BEGIN
  7359. condition := ResolveCondition(awaitStatement.condition);
  7360. IF currentIsRealtime THEN
  7361. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7362. END;
  7363. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7364. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7365. END;
  7366. awaitStatement.SetCondition(condition);
  7367. END VisitAwaitStatement;
  7368. PROCEDURE CheckSystemImport(position: LONGINT);
  7369. VAR import: SyntaxTree.Import;
  7370. BEGIN
  7371. import := currentScope.ownerModule.moduleScope.firstImport;
  7372. WHILE(import # NIL) DO
  7373. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7374. RETURN;
  7375. END;
  7376. import := import.nextImport;
  7377. END;
  7378. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7379. END CheckSystemImport;
  7380. (** check and resolve code statement: do nothing, must be done by assembler
  7381. **)
  7382. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7383. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7384. BEGIN
  7385. CheckSystemImport(code.position);
  7386. FOR i := 0 TO code.inRules.Length()-1 DO
  7387. statement := code.inRules.GetStatement(i);
  7388. IF statement IS SyntaxTree.Assignment THEN
  7389. WITH statement: SyntaxTree.Assignment DO
  7390. statement.SetRight(ResolveExpression(statement.right));
  7391. END;
  7392. ELSE
  7393. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7394. END;
  7395. END;
  7396. FOR i := 0 TO code.outRules.Length()-1 DO
  7397. statement := code.outRules.GetStatement(i);
  7398. IF statement IS SyntaxTree.Assignment THEN
  7399. WITH statement: SyntaxTree.Assignment DO
  7400. statement.SetLeft(ResolveDesignator(statement.left));
  7401. END;
  7402. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7403. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7404. ELSE
  7405. Printout.Info("out statement ", statement);
  7406. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7407. END;
  7408. END;
  7409. END VisitCode;
  7410. (** check and set flags of a statement block
  7411. - check for multiply occurence of a flag
  7412. - check and set priority only in bodies
  7413. - check for valid names
  7414. **)
  7415. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7416. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7417. flag: LONGINT; recordBody: SyntaxTree.Body;
  7418. PROCEDURE SetProtectedRecord;
  7419. VAR scope: SyntaxTree.Scope;
  7420. BEGIN
  7421. scope := currentScope;
  7422. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7423. scope := scope.outerScope
  7424. END;
  7425. IF scope # NIL THEN
  7426. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7427. END;
  7428. END SetProtectedRecord;
  7429. BEGIN
  7430. flags := {};
  7431. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7432. recordBody := block(SyntaxTree.Body)
  7433. ELSE
  7434. recordBody := NIL
  7435. END;
  7436. blockModifier := block.blockModifiers;
  7437. WHILE(blockModifier # NIL) DO
  7438. name := blockModifier.identifier;
  7439. expression := blockModifier.expression;
  7440. position := blockModifier.position;
  7441. flag := -1;
  7442. IF name=Global.NamePriority THEN
  7443. IF expression = NIL THEN
  7444. Error(position,Diagnostics.Invalid,"missing priority expression");
  7445. ELSIF recordBody = NIL THEN
  7446. Error(position,Diagnostics.Invalid,"priority not on record body");
  7447. ELSIF recordBody.priority # NIL THEN
  7448. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7449. ELSE
  7450. recordBody.SetPriority(expression);
  7451. END;
  7452. ELSIF expression # NIL THEN
  7453. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7454. ELSIF name=Global.NameExclusive THEN
  7455. IF block.isExclusive THEN
  7456. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7457. END;
  7458. block.SetExclusive(TRUE); SetProtectedRecord;
  7459. ELSIF name=Global.NameActive THEN
  7460. IF recordBody = NIL THEN
  7461. Error(position,Diagnostics.Invalid,"active not in record body");
  7462. ELSIF recordBody.isActive THEN
  7463. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7464. ELSE
  7465. recordBody.SetActive(TRUE); SetProtectedRecord;
  7466. END;
  7467. ELSIF name=Global.NameSafe THEN
  7468. IF recordBody = NIL THEN
  7469. Error(position,Diagnostics.Invalid,"safe not in record body");
  7470. ELSIF recordBody.isSafe THEN
  7471. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7472. ELSE
  7473. recordBody.SetSafe(TRUE);
  7474. SetProtectedRecord;
  7475. END;
  7476. ELSIF name=Global.NameRealtime THEN
  7477. IF recordBody = NIL THEN
  7478. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7479. ELSIF recordBody.isRealtime THEN
  7480. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7481. ELSE
  7482. recordBody.SetRealtime(TRUE);
  7483. block.SetRealtime(TRUE);
  7484. END;
  7485. ELSIF name=Global.NameUnchecked THEN
  7486. IF block.isUnchecked THEN
  7487. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7488. ELSE
  7489. block.SetUnchecked(TRUE);
  7490. END;
  7491. ELSIF (name=Global.NameUncooperative) THEN
  7492. IF block.isUncooperative THEN
  7493. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7494. ELSE
  7495. block.SetUncooperative(TRUE);
  7496. END;
  7497. ELSE
  7498. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7499. END;
  7500. blockModifier := blockModifier.nextModifier;
  7501. END;
  7502. END BlockFlags;
  7503. (** check and resolve statement block
  7504. - check flags (exclusive)
  7505. - check statement sequence
  7506. **)
  7507. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7508. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7509. BEGIN
  7510. BlockFlags(statementBlock);
  7511. IF statementBlock.isExclusive THEN
  7512. (* check that not in exclusive block *)
  7513. IF currentIsExclusive THEN
  7514. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7515. ELSIF currentIsRealtime THEN
  7516. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7517. END;
  7518. END;
  7519. recentExclusive := currentIsExclusive;
  7520. recentUnreachable := currentIsUnreachable;
  7521. recentRealtime := currentIsRealtime;
  7522. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7523. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7524. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7525. StatementSequence(statementBlock.statements);
  7526. currentIsRealtime := recentRealtime;
  7527. currentIsExclusive := recentExclusive;
  7528. currentIsUnreachable := recentUnreachable;
  7529. END VisitStatementBlock;
  7530. (** check and resolve body
  7531. - check flags (active, priority, safe)
  7532. - check body and finally part
  7533. **)
  7534. PROCEDURE Body(body: SyntaxTree.Body);
  7535. BEGIN
  7536. VisitStatementBlock(body);
  7537. IF body.isActive THEN
  7538. IF ~currentIsBodyProcedure THEN
  7539. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7540. ELSIF body.priority # NIL THEN
  7541. body.SetPriority(ConstantInteger(body.priority));
  7542. END;
  7543. ELSIF body.isSafe THEN
  7544. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7545. ELSIF body.priority # NIL THEN
  7546. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7547. END;
  7548. IF body.code # NIL THEN
  7549. CheckSystemImport(body.position);
  7550. END;
  7551. StatementSequence(body.finally)
  7552. END Body;
  7553. (*** scopes ***)
  7554. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7555. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7556. VAR duplicateSymbol: BOOLEAN;
  7557. BEGIN
  7558. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7559. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7560. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7561. END;
  7562. scope.EnterSymbol(symbol,duplicateSymbol);
  7563. IF ~allowDuplicate & duplicateSymbol THEN
  7564. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7565. IF VerboseErrorMessage THEN
  7566. Printout.Info("multiply defined identifier",symbol);
  7567. Printout.Info("in scope",scope);
  7568. END;
  7569. END;
  7570. END Register;
  7571. (**
  7572. implementation: check and resolve an implementation part
  7573. **)
  7574. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7575. move implementation checker to a separate object ? *)
  7576. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7577. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7578. BEGIN
  7579. prevIsRealtime := currentIsRealtime;
  7580. prevIsBodyProcedure := currentIsBodyProcedure;
  7581. prevIsCellNet := currentIsCellNet;
  7582. prevScope := currentScope;
  7583. currentScope := scope;
  7584. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7585. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7586. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7587. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7588. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7589. (*
  7590. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7591. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7592. END;
  7593. *)
  7594. END;
  7595. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7596. (* module body, record bodies are wrapped into an artifical procedure *)
  7597. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7598. Body(scope(SyntaxTree.ProcedureScope).body)
  7599. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7600. Body(scope(SyntaxTree.ProcedureScope).body)
  7601. END;
  7602. END;
  7603. currentScope := prevScope;
  7604. currentIsRealtime := prevIsRealtime;
  7605. currentIsBodyProcedure := prevIsBodyProcedure;
  7606. currentIsCellNet := prevIsCellNet;
  7607. END Implementation;
  7608. (** implementation phase:
  7609. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7610. **)
  7611. PROCEDURE Implementations(x: SyntaxTree.Module);
  7612. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7613. BEGIN
  7614. prevPhase := phase;
  7615. phase := InlinePhase;
  7616. scope := x.firstScope;
  7617. WHILE(scope # NIL) DO
  7618. Implementation(scope);
  7619. scope := scope.nextScope;
  7620. END;
  7621. phase := ImplementationPhase;
  7622. scope := x.firstScope;
  7623. WHILE(scope # NIL) DO
  7624. Implementation(scope);
  7625. scope := scope.nextScope;
  7626. END;
  7627. phase := prevPhase;
  7628. END Implementations;
  7629. (** declaration phase:
  7630. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7631. - import lists (for module scopes)
  7632. - parameter list (for procedure scopes)
  7633. - constant declarations
  7634. - type declarations
  7635. - variable declarations
  7636. - procedure declarations
  7637. preformed in two stages:
  7638. - first all symbols are entered into the symbol table (with uniqueness check),
  7639. - then all symbols are resolved
  7640. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7641. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7642. **)
  7643. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7644. VAR
  7645. constant: SyntaxTree.Constant;
  7646. typeDeclaration: SyntaxTree.TypeDeclaration;
  7647. variable: SyntaxTree.Variable;
  7648. procedure: SyntaxTree.Procedure;
  7649. prevScope: SyntaxTree.Scope;
  7650. parameter: SyntaxTree.Parameter;
  7651. import: SyntaxTree.Import;
  7652. symbol: SyntaxTree.Symbol;
  7653. prevPhase: LONGINT;
  7654. prevError : BOOLEAN;
  7655. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7656. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7657. BEGIN
  7658. IF type.baseType # NIL THEN
  7659. baseType := type.baseType.resolved;
  7660. IF baseType IS SyntaxTree.PointerType THEN
  7661. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7662. END;
  7663. IF baseType IS SyntaxTree.CellType THEN
  7664. DeclareCell(baseType(SyntaxTree.CellType));
  7665. END;
  7666. END;
  7667. parameter := type.firstParameter;
  7668. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7669. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7670. variable.SetType(parameter.type);
  7671. variable.SetAccess(SyntaxTree.Hidden);
  7672. variable.SetModifiers(parameter.modifiers);
  7673. currentScope.PushVariable(variable);
  7674. (*
  7675. Register(parameter,scope, FALSE);
  7676. *)
  7677. parameter := parameter.nextParameter;
  7678. END;
  7679. property := type.firstProperty;
  7680. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7681. variable := currentScope.FindVariable(property.name);
  7682. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7683. prop := variable(SyntaxTree.Property);
  7684. ELSE (* add, duplicate symbols detection later *)
  7685. prop := SyntaxTree.NewProperty(property.position, property.name);
  7686. currentScope.PushVariable(prop);
  7687. END;
  7688. prop.SetType(property.type);
  7689. prop.SetValue(property.value);
  7690. prop.SetAccess(SyntaxTree.Hidden);
  7691. property := property.nextProperty;
  7692. END;
  7693. END DeclareCell;
  7694. BEGIN
  7695. prevError := error;
  7696. prevPhase := phase;
  7697. phase := DeclarationPhase;
  7698. prevScope := currentScope;
  7699. currentScope := scope;
  7700. error := FALSE;
  7701. (* first enter all symbols in scope *)
  7702. IF scope IS SyntaxTree.ModuleScope THEN
  7703. (* treat imports first for a module scope, , set default context if necessary *)
  7704. import := scope(SyntaxTree.ModuleScope).firstImport;
  7705. WHILE(import # NIL) DO
  7706. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7707. Register(import, currentScope, FALSE);
  7708. import := import.nextImport;
  7709. END;
  7710. import := scope(SyntaxTree.ModuleScope).firstImport;
  7711. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7712. ResolveSymbol(import);
  7713. import := import.nextImport;
  7714. END;
  7715. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7716. (* enter parameters for a procedure scope *)
  7717. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7718. WHILE(parameter # NIL) DO
  7719. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7720. END;
  7721. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7722. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7723. ELSIF scope IS SyntaxTree.CellScope THEN
  7724. IF~skipImplementation THEN
  7725. import := scope(SyntaxTree.CellScope).firstImport;
  7726. WHILE(import # NIL) DO
  7727. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7728. Register(import, currentScope, FALSE);
  7729. import := import.nextImport;
  7730. END;
  7731. import := scope(SyntaxTree.CellScope).firstImport;
  7732. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7733. ResolveSymbol(import);
  7734. import := import.nextImport;
  7735. END;
  7736. END;
  7737. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7738. END;
  7739. IF error THEN RETURN END;
  7740. (* constants *)
  7741. constant := scope.firstConstant;
  7742. WHILE (constant # NIL) DO
  7743. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7744. END;
  7745. (* type declarations *)
  7746. typeDeclaration := scope.firstTypeDeclaration;
  7747. WHILE (typeDeclaration # NIL) DO
  7748. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7749. END;
  7750. (* variables *)
  7751. variable := scope.firstVariable;
  7752. WHILE (variable # NIL) DO
  7753. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7754. END;
  7755. (* procedures *)
  7756. procedure := scope.firstProcedure;
  7757. WHILE (procedure # NIL) DO
  7758. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7759. END;
  7760. IF ~skipImplementation THEN
  7761. (* now process all symbols without any presumption on the order *)
  7762. symbol := scope.firstSymbol;
  7763. WHILE(symbol # NIL) DO
  7764. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7765. ResolveSymbol(symbol);
  7766. END;
  7767. symbol := symbol.nextSymbol;
  7768. END;
  7769. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7770. symbol := scope.firstSymbol;
  7771. WHILE symbol # NIL DO
  7772. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7773. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7774. pointerFixes.Add(symbol, currentScope);
  7775. END;
  7776. IF ~symbol.type.resolved.isRealtime THEN
  7777. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7778. END;
  7779. END;
  7780. symbol := symbol.nextSymbol
  7781. END;
  7782. END;
  7783. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7784. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7785. END;
  7786. IF (scope.ownerModule # NIL) THEN
  7787. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7788. scope.ownerModule.AddScope(scope);
  7789. END;
  7790. END;
  7791. phase := prevPhase;
  7792. currentScope := prevScope;
  7793. error := error OR prevError;
  7794. END Declarations;
  7795. (* nopov *)
  7796. (** check if all operators from one module are compatible to the ones in the other module
  7797. - check if there are not multiple operators with the same signature
  7798. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7799. - check for all operators whose signatures are compatible, whether the return types are compatible
  7800. note that:
  7801. - the return type is not considered to be part of the signature
  7802. - two signatures are considered compatible, if all of the operands are compatible
  7803. **)
  7804. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7805. VAR
  7806. thisOperator, thatOperator: SyntaxTree.Operator;
  7807. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7808. thisParameter, thatParameter: SyntaxTree.Parameter;
  7809. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7810. i: LONGINT;
  7811. BEGIN
  7812. currentScope := thisModuleScope;
  7813. hasError := FALSE;
  7814. (* go through all operators in the other module *)
  7815. thatOperator := thatModuleScope.firstOperator;
  7816. WHILE (thatOperator # NIL) & ~hasError DO
  7817. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7818. (* the other operator is accessible *)
  7819. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7820. (* the other operator is not the conversion operator *)
  7821. (* go through all operators in this module *)
  7822. thisOperator := thisModuleScope.firstOperator;
  7823. WHILE (thisOperator # NIL) & ~hasError DO
  7824. IF thisOperator # thatOperator THEN
  7825. (* the operators are not the same *)
  7826. IF thisOperator.name = thatOperator.name THEN
  7827. (* the operators share the same identifier *)
  7828. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7829. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7830. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7831. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7832. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7833. (* both operators have the same paramter count *)
  7834. thisParameter := thisProcedureType.firstParameter;
  7835. thatParameter := thatProcedureType.firstParameter;
  7836. operandsAreEqual := TRUE;
  7837. operandsAreCompatible := TRUE;
  7838. (* go through all parameters *)
  7839. FOR i := 1 TO thisProcedureType.numberParameters DO
  7840. ASSERT(thatParameter # NIL);
  7841. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7842. operandsAreEqual := FALSE;
  7843. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7844. operandsAreCompatible := FALSE
  7845. END
  7846. END;
  7847. thisParameter := thisParameter.nextParameter;
  7848. thatParameter := thatParameter.nextParameter
  7849. END;
  7850. IF operandsAreEqual THEN
  7851. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7852. hasError := TRUE
  7853. ELSIF operandsAreCompatible THEN
  7854. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7855. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7856. hasError := TRUE
  7857. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7858. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7859. hasError := TRUE
  7860. END
  7861. END
  7862. END
  7863. END
  7864. END;
  7865. thisOperator := thisOperator.nextOperator
  7866. END
  7867. END
  7868. END;
  7869. thatOperator := thatOperator.nextOperator
  7870. END
  7871. END CheckInterOperatorConformity;
  7872. (** check module:
  7873. - check module declaration
  7874. - add context, if necessary
  7875. - remove module from import cache, if necessary
  7876. - check declarations
  7877. - resolve all type fixes
  7878. - check implementation (bodies)
  7879. **)
  7880. PROCEDURE Module*(x: SyntaxTree.Module);
  7881. VAR (* nopov *)
  7882. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7883. BEGIN
  7884. prevScope := currentScope;
  7885. prevIsCellNet := currentIsCellNet;
  7886. module := x;
  7887. ASSERT(x # NIL);
  7888. global := system.globalScope[x.case];
  7889. x.moduleScope.SetGlobalScope(global);
  7890. currentScope := global;
  7891. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7892. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7893. RemoveModuleFromCache(importCache,x);
  7894. Declarations(x.moduleScope, FALSE);
  7895. FixTypes();
  7896. IF module.isCellNet THEN
  7897. currentIsCellNet := TRUE;
  7898. modifier := x.modifiers;
  7899. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7900. CheckModifiers(modifier, FALSE);
  7901. END;
  7902. (* nopov *)
  7903. IF ~error THEN
  7904. (* check if operators conform to each other within this module *)
  7905. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7906. (* go through all imports *)
  7907. import := x.moduleScope.firstImport;
  7908. WHILE import # NIL DO
  7909. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7910. (* check if all operators in this module conform to the ones of the imported module *)
  7911. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7912. END;
  7913. import := import.nextImport
  7914. END;
  7915. END;
  7916. Implementations(x);
  7917. module := NIL;
  7918. currentIsCellNet := prevIsCellNet;
  7919. currentScope := prevScope;
  7920. END Module;
  7921. END Checker;
  7922. Warnings*=OBJECT (SyntaxTree.Visitor)
  7923. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7924. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7925. BEGIN
  7926. SELF.diagnostics := diagnostics
  7927. END InitWarnings;
  7928. (** types *)
  7929. PROCEDURE Type(x: SyntaxTree.Type);
  7930. BEGIN x.Accept(SELF)
  7931. END Type;
  7932. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7933. BEGIN END VisitType;
  7934. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7935. BEGIN END VisitBasicType;
  7936. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7937. BEGIN END VisitCharacterType;
  7938. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7939. BEGIN END VisitIntegerType;
  7940. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7941. BEGIN END VisitFloatType;
  7942. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7943. BEGIN END VisitQualifiedType;
  7944. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7945. BEGIN END VisitStringType;
  7946. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7947. BEGIN END VisitEnumerationType;
  7948. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7949. BEGIN END VisitRangeType;
  7950. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7951. BEGIN
  7952. IF ~(SyntaxTree.Warned IN x.state) THEN
  7953. x.SetState(SyntaxTree.Warned);
  7954. Type(x.arrayBase);
  7955. END;
  7956. END VisitArrayType;
  7957. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7958. BEGIN
  7959. IF ~(SyntaxTree.Warned IN x.state) THEN
  7960. x.SetState(SyntaxTree.Warned);
  7961. Type(x.arrayBase);
  7962. END;
  7963. END VisitMathArrayType;
  7964. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7965. BEGIN
  7966. IF ~(SyntaxTree.Warned IN x.state) THEN
  7967. x.SetState(SyntaxTree.Warned);
  7968. Type(x.pointerBase);
  7969. END;
  7970. END VisitPointerType;
  7971. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7972. BEGIN Scope(x.recordScope) END VisitRecordType;
  7973. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7974. BEGIN Scope(x.cellScope) END VisitCellType;
  7975. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7976. BEGIN END VisitProcedureType;
  7977. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7978. VAR msg: ARRAY 256 OF CHAR;
  7979. BEGIN
  7980. Global.GetSymbolName(x,msg);
  7981. Strings.Append(msg," ");
  7982. Strings.Append(msg,text);
  7983. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7984. END Warning;
  7985. (** symbols *)
  7986. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7987. BEGIN
  7988. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7989. IF ~(x IS SyntaxTree.Parameter) THEN
  7990. Warning(x,"never used");
  7991. END;
  7992. END;
  7993. x.Accept(SELF);
  7994. END Symbol;
  7995. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7996. BEGIN END VisitSymbol;
  7997. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7998. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7999. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8000. BEGIN END VisitConstant;
  8001. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8002. BEGIN END VisitVariable;
  8003. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8004. BEGIN END VisitProperty;
  8005. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8006. BEGIN END VisitParameter;
  8007. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8008. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8009. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8010. BEGIN END VisitOperator;
  8011. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8012. BEGIN END VisitImport;
  8013. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8014. VAR
  8015. symbol: SyntaxTree.Symbol;
  8016. BEGIN
  8017. symbol := scope.firstSymbol;
  8018. WHILE(symbol # NIL) DO
  8019. Symbol(symbol);
  8020. symbol := symbol.nextSymbol;
  8021. END;
  8022. END Scope;
  8023. PROCEDURE Module*(x: SyntaxTree.Module);
  8024. BEGIN
  8025. SELF.module := x;
  8026. Scope(x.moduleScope);
  8027. END Module;
  8028. END Warnings;
  8029. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8030. BEGIN
  8031. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8032. END IsOberonInline;
  8033. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8034. BEGIN
  8035. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8036. END Resolved;
  8037. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8038. VAR i: LONGINT;
  8039. BEGIN
  8040. i := 1;
  8041. WHILE i < x DO
  8042. i := i *2
  8043. END;
  8044. RETURN i=x
  8045. END PowerOf2;
  8046. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8047. BEGIN
  8048. RETURN
  8049. (scope # NIL) &
  8050. (scope IS SyntaxTree.ModuleScope)
  8051. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8052. OR
  8053. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8054. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8055. END IsCellNetScope;
  8056. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8057. BEGIN
  8058. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8059. END IsCellScope;
  8060. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8061. BEGIN
  8062. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8063. RETURN (scope # NIL) & IsCellNetScope(scope)
  8064. END InCellNetScope;
  8065. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8066. BEGIN
  8067. ASSERT(size MOD system.dataUnit = 0);
  8068. RETURN size DIV system.dataUnit
  8069. END ToMemoryUnits;
  8070. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8071. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8072. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8073. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8074. BEGIN
  8075. IF t = NIL THEN
  8076. RETURN TRUE
  8077. ELSE
  8078. t := t.resolved;
  8079. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8080. END;
  8081. END TypeAllowed;
  8082. BEGIN
  8083. type := type.resolved;
  8084. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8085. RETURN FALSE
  8086. ELSE
  8087. procedureType := type(SyntaxTree.ProcedureType);
  8088. numberParameters := procedureType.numberParameters;
  8089. RETURN
  8090. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8091. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8092. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8093. END;
  8094. END GetProcedureAllowed;
  8095. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8096. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8097. VAR import: SyntaxTree.Import;
  8098. BEGIN
  8099. import := importCache.ImportByModuleName(x.name,x.context);
  8100. IF import # NIL THEN
  8101. importCache.RemoveImporters(x.name,x.context);
  8102. END;
  8103. END RemoveModuleFromCache;
  8104. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8105. (* to <- this assignment compatibility *)
  8106. VAR result: BOOLEAN;
  8107. BEGIN
  8108. IF this= NIL THEN result := (to=NIL)
  8109. ELSIF to=NIL THEN result := FALSE
  8110. ELSE
  8111. (*! will be replaced by this:
  8112. ELSE result := this.CompatibleTo(to.resolved);
  8113. *)
  8114. this := this.resolved; to := to.resolved;
  8115. IF to=SyntaxTree.invalidType THEN result := FALSE
  8116. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8117. ELSIF to = this THEN
  8118. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8119. ELSIF to IS SyntaxTree.BasicType THEN
  8120. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8121. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8122. result := this.CompatibleTo(to.resolved)
  8123. ELSE
  8124. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8125. END
  8126. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8127. result := to.sizeInBits = this.sizeInBits;
  8128. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8129. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8130. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8131. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8132. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8133. result := TRUE;
  8134. ELSIF to IS SyntaxTree.AnyType THEN
  8135. 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);
  8136. ELSIF to IS SyntaxTree.ObjectType THEN
  8137. 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 *) ;
  8138. ELSIF to IS SyntaxTree.ByteType THEN
  8139. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8140. ELSIF to IS SyntaxTree.CharacterType THEN
  8141. result := IsCharacterType(this)
  8142. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8143. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8144. 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
  8145. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8146. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8147. result := TRUE;
  8148. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8149. result := TRUE;
  8150. ELSE
  8151. result := FALSE
  8152. END;
  8153. ELSIF to IS SyntaxTree.PointerType THEN
  8154. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8155. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8156. & (~to.isRealtime OR this.isRealtime);
  8157. ELSIF to IS SyntaxTree.ProcedureType THEN
  8158. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8159. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8160. & (~to.isRealtime OR this.isRealtime)
  8161. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8162. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8163. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8164. ELSIF to IS SyntaxTree.RecordType THEN
  8165. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8166. ELSIF to IS SyntaxTree.ArrayType THEN
  8167. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8168. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8169. ELSIF StaticArrayCompatible(to, this) THEN
  8170. result := TRUE
  8171. ELSE
  8172. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8173. END;
  8174. ELSIF to IS SyntaxTree.MathArrayType THEN
  8175. IF this IS SyntaxTree.MathArrayType THEN
  8176. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8177. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8178. result := TRUE;
  8179. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8180. result := TRUE;
  8181. ELSE
  8182. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8183. END;
  8184. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8185. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8186. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8187. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8188. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8189. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8190. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8191. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8192. ELSE
  8193. result := FALSE
  8194. END;
  8195. (* an array-structured object type is compatible to the type of its array structure *)
  8196. ELSIF IsArrayStructuredObjectType(this) THEN
  8197. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8198. ELSE
  8199. result := FALSE;
  8200. END;
  8201. ELSIF to IS SyntaxTree.StringType THEN
  8202. result := FALSE;
  8203. ELSIF to IS SyntaxTree.EnumerationType THEN
  8204. result := IsEnumerationExtension(this,to);
  8205. ELSIF to IS SyntaxTree.PortType THEN
  8206. result := SameType(to, this)
  8207. ELSE
  8208. Printout.Info("CompatibleTo",to);
  8209. HALT(100); (* implement missing type check *)
  8210. END;
  8211. END;
  8212. RETURN result
  8213. END CompatibleTo;
  8214. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8215. VAR actualBase, formalBase: SyntaxTree.Type;
  8216. BEGIN
  8217. IF SameType(formal,actual) THEN
  8218. RETURN TRUE
  8219. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8220. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8221. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8222. RETURN
  8223. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8224. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8225. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8226. & StaticArrayCompatible(formalBase,actualBase)
  8227. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8228. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8229. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8230. RETURN
  8231. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8232. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8233. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8234. & StaticArrayCompatible(formalBase,actualBase)
  8235. ELSE RETURN FALSE
  8236. END;
  8237. END StaticArrayCompatible;
  8238. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8239. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8240. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8241. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8242. BEGIN
  8243. result := SameType(formal,actual);
  8244. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8245. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8246. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8247. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8248. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8249. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8250. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8251. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8252. & TC(formalBase, actualBase);
  8253. END;
  8254. RETURN result
  8255. END TC;
  8256. BEGIN
  8257. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8258. ELSE
  8259. arrayBase := formalType.arrayBase.resolved;
  8260. IF (actualType IS SyntaxTree.StringType) THEN
  8261. result := arrayBase IS SyntaxTree.CharacterType
  8262. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8263. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8264. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8265. result := TC(formalType, actualType);
  8266. ELSE
  8267. result := (arrayBase IS SyntaxTree.ByteType)
  8268. END;
  8269. END;
  8270. RETURN result
  8271. END OpenArrayCompatible;
  8272. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8273. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8274. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8275. BEGIN
  8276. IF actualType IS SyntaxTree.MathArrayType THEN
  8277. actualArray := actualType(SyntaxTree.MathArrayType);
  8278. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8279. (*
  8280. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8281. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8282. *)
  8283. actualBase := ArrayBase(actualType,Infinity);
  8284. formalBase := ArrayBase(formalType,Infinity);
  8285. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8286. ELSE
  8287. (*
  8288. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8289. *)
  8290. formalBase := Resolved(formalType.arrayBase);
  8291. actualBase := Resolved(actualArray.arrayBase);
  8292. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8293. (*
  8294. ARRAY [k] -> ARRAY [n]
  8295. *)
  8296. result := (formalType.staticLength = actualArray.staticLength)
  8297. ELSE
  8298. result := TRUE
  8299. END;
  8300. IF ~result THEN
  8301. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8302. ELSIF actualBase = NIL THEN result := FALSE
  8303. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8304. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8305. ELSE
  8306. result := SameType(formalBase,actualBase)
  8307. END;
  8308. END;
  8309. ELSE
  8310. result := FALSE
  8311. END;
  8312. RETURN result
  8313. END MathArrayCompatible;
  8314. (**
  8315. Math Array Type distance for assignments / parameter passings of the form
  8316. from -> to
  8317. variants:
  8318. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8319. allowed:
  8320. static -> static (& size match)
  8321. static -> open
  8322. static -> tensor
  8323. open -> open
  8324. open -> tensor
  8325. open -> static
  8326. tensor -> tensor
  8327. tensor -> open
  8328. tensor -> static
  8329. **)
  8330. (*! think about the metric here: is form matching more important than element type matching? *)
  8331. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8332. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8333. BEGIN
  8334. fromBase := Resolved(from.arrayBase);
  8335. toBase := Resolved(to.arrayBase);
  8336. i := Infinity;
  8337. IF from = to THEN
  8338. i := 0;
  8339. ELSIF (from.form = to.form) THEN
  8340. (* static -> static, open -> open, tensor -> tensor *)
  8341. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8342. IF fromBase = toBase THEN i := 0
  8343. ELSIF toBase = NIL THEN i := 1
  8344. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8345. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8346. ELSE
  8347. i := TypeDistance(system,fromBase, toBase, varpar);
  8348. END;
  8349. END;
  8350. ELSIF (to.form = SyntaxTree.Static) THEN
  8351. (* forbidden *)
  8352. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8353. (* static -> tensor, open -> tensor, tensor -> open *)
  8354. IF toBase=fromBase THEN i := 0;
  8355. ELSIF toBase = NIL THEN i := 1;
  8356. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8357. toBase := ArrayBase(toBase,Infinity);
  8358. IF (fromBase=toBase) THEN i := 0
  8359. ELSIF (toBase = NIL) THEN i:= 1
  8360. ELSIF (fromBase = NIL) THEN i := Infinity;
  8361. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8362. END;
  8363. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8364. fromBase := ArrayBase(fromBase,Infinity);
  8365. IF (fromBase=toBase) THEN i := 0
  8366. ELSIF (toBase = NIL) THEN i := 1
  8367. ELSIF (fromBase = NIL) THEN i := Infinity;
  8368. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8369. END;
  8370. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8371. END;
  8372. IF i # Infinity THEN INC(i,2) END;
  8373. ELSIF (from.form = SyntaxTree.Static) THEN
  8374. (* static -> open *)
  8375. IF toBase=fromBase THEN i := 0
  8376. ELSIF toBase = NIL THEN i := 1
  8377. ELSIF fromBase = NIL THEN i := Infinity
  8378. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8379. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8380. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8381. END;
  8382. IF i # Infinity THEN INC(i,1) END;
  8383. ELSE HALT(100); (* unknown case *)
  8384. END;
  8385. RETURN i;
  8386. END MathArrayTypeDistance;
  8387. (** compute and return the distance of two array types
  8388. - return the distance of the base types
  8389. **)
  8390. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8391. VAR i: LONGINT;
  8392. BEGIN
  8393. i := Infinity;
  8394. IF from = to THEN
  8395. i := 0
  8396. ELSE
  8397. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8398. (*
  8399. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8400. i := TypeDistance(from.base, to.base);
  8401. IF i >= 0 THEN INC(i) END
  8402. ELSIF (from.mode = open) & (to.mode = open) THEN
  8403. i := TypeDistance(from.base, to.base);
  8404. *)
  8405. END;
  8406. RETURN i
  8407. END ArrayTypeDistance;
  8408. (** compute the signature distance of a procedure and an actual parameter list
  8409. - if any of the parameters are not compatible, the result is infinite
  8410. - add up and return the distance over all parameters
  8411. **)
  8412. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8413. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8414. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8415. BEGIN
  8416. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8417. result := Infinity
  8418. ELSE
  8419. formalParameter := procedureType.firstParameter;
  8420. i := 0;
  8421. result := 0;
  8422. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8423. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8424. actualParameter := actualParameters.GetExpression(i);
  8425. ASSERT(formalParameter.type # NIL);
  8426. IF (actualParameter.type = NIL) THEN distance := Infinity
  8427. ELSE
  8428. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8429. END;
  8430. IF distance = Infinity THEN
  8431. result := Infinity;
  8432. ELSE
  8433. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8434. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8435. (* already handled varpar *)
  8436. (*
  8437. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8438. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8439. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8440. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8441. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8442. END;
  8443. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8444. result := Infinity
  8445. END;
  8446. *)
  8447. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8448. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8449. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8450. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8451. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8452. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8453. END;
  8454. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8455. result := Infinity
  8456. END;
  8457. ELSE
  8458. result := Infinity
  8459. END;
  8460. ELSE
  8461. INC(result,distance);
  8462. END;
  8463. END;
  8464. formalParameter := formalParameter.nextParameter; INC(i);
  8465. END;
  8466. END;
  8467. ASSERT(result >= 0);
  8468. RETURN result
  8469. END Distance;
  8470. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8471. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8472. BEGIN
  8473. IF right.numberParameters # (procedureType.numberParameters) THEN
  8474. result := Infinity
  8475. ELSE
  8476. formalParameter := procedureType.firstParameter;
  8477. rightParameter := right.firstParameter;
  8478. i := 0;
  8479. result := 0;
  8480. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8481. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8482. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8483. IF distance = Infinity THEN
  8484. result := Infinity;
  8485. ELSE
  8486. INC(result,distance);
  8487. END;
  8488. formalParameter := formalParameter.nextParameter;
  8489. rightParameter := rightParameter.nextParameter;
  8490. END;
  8491. END;
  8492. ASSERT(result >= 0);
  8493. RETURN result
  8494. END ProcedureTypeDistance;
  8495. (** compute and return the distance between two types, used for computation of signature distance
  8496. from -> to
  8497. **)
  8498. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8499. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8500. BEGIN
  8501. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8502. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8503. END;
  8504. i := Infinity;
  8505. IF from = to THEN
  8506. i := 0
  8507. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8508. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8509. i := Infinity;
  8510. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8511. i := 1
  8512. ELSIF (from IS SyntaxTree.StringType) THEN
  8513. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8514. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8515. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8516. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8517. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8518. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8519. i := 1
  8520. ELSIF (from IS SyntaxTree.NilType) THEN
  8521. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8522. (*
  8523. ELSIF (from = NoType) THEN
  8524. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8525. *)
  8526. ELSIF (from IS SyntaxTree.BasicType) THEN
  8527. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8528. IF varpar & (i # 0) THEN i := Infinity END;
  8529. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8530. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8531. ELSIF (from IS SyntaxTree.RecordType) THEN
  8532. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8533. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8534. IF to IS SyntaxTree.MathArrayType THEN
  8535. (*
  8536. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8537. i := Infinity;
  8538. ELSE
  8539. *)
  8540. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8541. (*
  8542. END;
  8543. *)
  8544. END
  8545. ELSIF (from IS SyntaxTree.PointerType) THEN
  8546. ptr := from(SyntaxTree.PointerType);
  8547. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8548. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8549. (* ELSE i := TypeDistance(ptr.base, to); *)
  8550. END
  8551. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8552. IF (to IS SyntaxTree.ProcedureType) THEN
  8553. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8554. END;
  8555. ELSIF (from IS SyntaxTree.PortType) THEN
  8556. IF (to IS SyntaxTree.PortType) THEN
  8557. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8558. i := 0;
  8559. END;
  8560. END;
  8561. (*no procedure test, procedure must be the same*)
  8562. END;
  8563. RETURN i
  8564. END TypeDistance;
  8565. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8566. BEGIN
  8567. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8568. END IsIntegerType;
  8569. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8570. BEGIN
  8571. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8572. END IsAddressType;
  8573. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8574. BEGIN
  8575. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8576. END IsSizeType;
  8577. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8578. BEGIN
  8579. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8580. END IsSignedIntegerType;
  8581. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8582. BEGIN
  8583. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8584. END IsUnsignedIntegerType;
  8585. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8586. VAR result: BOOLEAN;
  8587. BEGIN
  8588. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8589. value := x.resolved(SyntaxTree.IntegerValue).value;
  8590. result := TRUE
  8591. ELSE
  8592. result := FALSE
  8593. END;
  8594. RETURN result
  8595. END IsIntegerValue;
  8596. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8597. VAR result: BOOLEAN;
  8598. BEGIN
  8599. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8600. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8601. result := TRUE
  8602. ELSE
  8603. result := FALSE
  8604. END;
  8605. RETURN result
  8606. END IsEnumerationValue;
  8607. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8608. VAR result: BOOLEAN;
  8609. BEGIN
  8610. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8611. value := x.resolved(SyntaxTree.RealValue).value;
  8612. result := TRUE
  8613. ELSE
  8614. result := FALSE
  8615. END;
  8616. RETURN result
  8617. END IsRealValue;
  8618. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8619. VAR result: BOOLEAN;
  8620. BEGIN
  8621. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8622. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8623. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8624. result := TRUE
  8625. ELSE
  8626. result := FALSE
  8627. END;
  8628. RETURN result
  8629. END IsComplexValue;
  8630. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8631. VAR result: BOOLEAN;
  8632. BEGIN
  8633. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8634. value := x.resolved(SyntaxTree.CharacterValue).value;
  8635. result := TRUE
  8636. ELSE
  8637. result := FALSE
  8638. END;
  8639. RETURN result
  8640. END IsCharacterValue;
  8641. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8642. VAR result: BOOLEAN;
  8643. BEGIN
  8644. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8645. value := x.resolved(SyntaxTree.BooleanValue).value;
  8646. result := TRUE
  8647. ELSE
  8648. result := FALSE
  8649. END;
  8650. RETURN result
  8651. END IsBooleanValue;
  8652. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8653. VAR result: BOOLEAN;
  8654. BEGIN
  8655. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8656. value := x.resolved(SyntaxTree.SetValue).value;
  8657. result := TRUE
  8658. ELSE
  8659. result := FALSE
  8660. END;
  8661. RETURN result
  8662. END IsSetValue;
  8663. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8664. VAR result: BOOLEAN;
  8665. BEGIN
  8666. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8667. value := x.resolved(SyntaxTree.StringValue).value;
  8668. result := TRUE
  8669. ELSE
  8670. result := FALSE
  8671. END;
  8672. RETURN result
  8673. END IsStringValue;
  8674. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8675. BEGIN
  8676. x := x.resolved;
  8677. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8678. END Indexable;
  8679. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8680. BEGIN
  8681. RETURN t1.SameType(t2.resolved);
  8682. END SameType;
  8683. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8684. BEGIN
  8685. IF t IS SyntaxTree.MathArrayType THEN
  8686. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8687. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8688. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8689. DEC(max);
  8690. END;
  8691. ELSIF t IS SyntaxTree.ArrayType THEN
  8692. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8693. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8694. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8695. END;
  8696. END;
  8697. RETURN t;
  8698. END ArrayBase;
  8699. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8700. BEGIN
  8701. type := type.resolved;
  8702. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8703. base := type(SyntaxTree.ArrayType).arrayBase;
  8704. RETURN TRUE;
  8705. END;
  8706. RETURN FALSE;
  8707. END IsOpenArray;
  8708. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8709. BEGIN
  8710. type := type.resolved;
  8711. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8712. base := type(SyntaxTree.ArrayType).arrayBase;
  8713. dim := type(SyntaxTree.ArrayType).staticLength;
  8714. RETURN TRUE
  8715. ELSE
  8716. RETURN FALSE
  8717. END;
  8718. END IsStaticArray;
  8719. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8720. BEGIN
  8721. type := type.resolved;
  8722. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8723. base := type(SyntaxTree.ArrayType).arrayBase;
  8724. RETURN TRUE
  8725. ELSE
  8726. RETURN FALSE
  8727. END;
  8728. END IsDynamicArray;
  8729. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8730. VAR i: LONGINT;
  8731. BEGIN
  8732. i := 0;
  8733. t := t.resolved;
  8734. IF t IS SyntaxTree.MathArrayType THEN
  8735. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8736. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8737. END;
  8738. ELSIF t IS SyntaxTree.ArrayType THEN
  8739. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8740. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8741. END;
  8742. END;
  8743. RETURN i
  8744. END Dimension;
  8745. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8746. BEGIN
  8747. RETURN expression.assignable;
  8748. END IsVariable;
  8749. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8750. BEGIN
  8751. IF (symbol IS SyntaxTree.Parameter) THEN
  8752. WITH symbol: SyntaxTree.Parameter DO
  8753. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8754. END;
  8755. ELSE
  8756. RETURN FALSE
  8757. END;
  8758. END IsVariableParameter;
  8759. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8760. VAR result: BOOLEAN;
  8761. BEGIN
  8762. IF type = NIL THEN result := FALSE
  8763. ELSE
  8764. type := type.resolved;
  8765. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8766. END;
  8767. RETURN result
  8768. END IsPointerType;
  8769. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8770. VAR result: BOOLEAN;
  8771. BEGIN
  8772. IF type = NIL THEN result := FALSE
  8773. ELSE
  8774. type := type.resolved;
  8775. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8776. END;
  8777. RETURN result
  8778. END IsUnsafePointer;
  8779. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8780. BEGIN
  8781. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8782. END IsDisposable;
  8783. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8784. VAR result: BOOLEAN;
  8785. BEGIN
  8786. IF type = NIL THEN result := FALSE
  8787. ELSE
  8788. type := type.resolved;
  8789. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8790. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8791. result := result OR (type IS SyntaxTree.ObjectType);
  8792. END;
  8793. RETURN result
  8794. END IsPointerToRecord;
  8795. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8796. VAR result: BOOLEAN;
  8797. BEGIN
  8798. IF type = NIL THEN result := FALSE
  8799. ELSE
  8800. type := type.resolved;
  8801. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8802. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8803. ;
  8804. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8805. result := result OR (type IS SyntaxTree.ObjectType);
  8806. END;
  8807. RETURN result
  8808. END IsPointerToObject;
  8809. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8810. BEGIN
  8811. IF type # NIL THEN
  8812. RETURN type.resolved.hasPointers
  8813. ELSE
  8814. RETURN FALSE
  8815. END;
  8816. END ContainsPointer;
  8817. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8818. BEGIN
  8819. IF type = NIL THEN RETURN FALSE END;
  8820. type := type.resolved;
  8821. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8822. END IsStringType;
  8823. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8824. BEGIN
  8825. IF type = NIL THEN RETURN FALSE END;
  8826. type := type.resolved;
  8827. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8828. END IsCharacterType;
  8829. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8830. BEGIN
  8831. IF type = NIL THEN RETURN FALSE END;
  8832. type := type.resolved;
  8833. RETURN (type IS SyntaxTree.EnumerationType)
  8834. END IsEnumerationType;
  8835. (** cf. section "Type extension (base type)" in the language report **)
  8836. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8837. VAR result: BOOLEAN;
  8838. BEGIN
  8839. ASSERT(base # NIL); ASSERT(extension # NIL);
  8840. base := base.resolved; extension := extension.resolved;
  8841. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8842. result := TRUE;
  8843. ELSE
  8844. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8845. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8846. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8847. END;
  8848. WHILE (extension # NIL) & (extension # base) DO
  8849. IF extension IS SyntaxTree.RecordType THEN
  8850. extension := extension(SyntaxTree.RecordType).baseType;
  8851. IF (extension # NIL) THEN extension := extension.resolved END;
  8852. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8853. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8854. END;
  8855. ELSE extension := NIL;
  8856. END;
  8857. END;
  8858. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8859. END;
  8860. RETURN result
  8861. END IsTypeExtension;
  8862. (** check if base is the base enumeration type of extension **)
  8863. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8864. BEGIN
  8865. base := base.resolved; extension := extension.resolved;
  8866. WHILE (extension # NIL) & (extension # base) DO
  8867. IF extension IS SyntaxTree.EnumerationType THEN
  8868. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8869. IF extension # NIL THEN extension := extension.resolved END;
  8870. ELSE
  8871. extension := NIL
  8872. END;
  8873. END;
  8874. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8875. END IsEnumerationExtension;
  8876. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8877. BEGIN
  8878. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8879. RETURN TRUE
  8880. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8881. RETURN TRUE
  8882. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8883. RETURN TRUE
  8884. ELSE
  8885. RETURN FALSE
  8886. END
  8887. END IsCallable;
  8888. (** compute and return the distance of two record types
  8889. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8890. **)
  8891. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8892. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8893. BEGIN
  8894. i := 0;
  8895. WHILE (from # NIL) & (from # to) DO
  8896. baseType := from.baseType;
  8897. IF (baseType # NIL) THEN
  8898. baseType := baseType.resolved;
  8899. IF baseType IS SyntaxTree.PointerType THEN
  8900. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8901. END;
  8902. IF baseType IS SyntaxTree.RecordType THEN
  8903. from := baseType(SyntaxTree.RecordType);
  8904. ELSE
  8905. from := NIL;
  8906. END;
  8907. ELSE
  8908. from := NIL
  8909. END;
  8910. INC(i)
  8911. END;
  8912. IF from = NIL THEN i := Infinity END;
  8913. RETURN i
  8914. END RecordTypeDistance;
  8915. (** compute and return the distance of two pointer types **)
  8916. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8917. BEGIN
  8918. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8919. RETURN Infinity;
  8920. ELSE
  8921. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8922. END;
  8923. END PointerTypeDistance;
  8924. (** check if expression contains a symbol designator pointing to a type declaration.
  8925. - if so then enter type declaration into typeDeclaration and return true else return false
  8926. **)
  8927. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8928. VAR result: BOOLEAN;
  8929. BEGIN
  8930. result := FALSE;
  8931. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8932. result := TRUE;
  8933. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8934. END;
  8935. RETURN result
  8936. END IsTypeDesignator;
  8937. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8938. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8939. VAR result: BOOLEAN;
  8940. BEGIN
  8941. type := type.resolved;
  8942. IF type IS SyntaxTree.PointerType THEN
  8943. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8944. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8945. result := TRUE
  8946. ELSE
  8947. result := type IS SyntaxTree.RecordType
  8948. END;
  8949. RETURN result
  8950. END IsExtensibleType;
  8951. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8952. BEGIN
  8953. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8954. (d IS SyntaxTree.SymbolDesignator) &
  8955. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8956. OR
  8957. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8958. END IsUnextensibleRecord;
  8959. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8960. BEGIN
  8961. IF IsUnextensibleRecord(d) THEN
  8962. RETURN FALSE
  8963. ELSE RETURN IsExtensibleType(d.type.resolved)
  8964. END;
  8965. END IsExtensibleDesignator;
  8966. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8967. BEGIN
  8968. type := type.resolved;
  8969. IF (type IS SyntaxTree.PointerType) THEN
  8970. RETURN TRUE
  8971. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8972. RETURN TRUE
  8973. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8974. RETURN TRUE
  8975. ELSIF (type IS SyntaxTree.BasicType) THEN
  8976. RETURN TRUE
  8977. END;
  8978. RETURN FALSE
  8979. END IsBasicType;
  8980. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8981. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8982. BEGIN
  8983. baseType := record.baseType;
  8984. IF (baseType # NIL) THEN
  8985. baseType := baseType.resolved;
  8986. IF (baseType IS SyntaxTree.PointerType) THEN
  8987. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8988. END;
  8989. END;
  8990. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8991. recordType := baseType(SyntaxTree.RecordType);
  8992. ELSE
  8993. recordType := NIL;
  8994. END;
  8995. RETURN recordType
  8996. END RecordBase;
  8997. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8998. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8999. BEGIN
  9000. baseRecord := RecordBase(scope.ownerRecord);
  9001. IF baseRecord = NIL THEN RETURN NIL END;
  9002. scope := baseRecord.recordScope;
  9003. procedureType := procedure.type.resolved;
  9004. IF procedure IS SyntaxTree.Operator THEN
  9005. operator := scope.firstOperator;
  9006. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9007. (*
  9008. Printout.Info("not same ",procedureType);
  9009. Printout.Info("with ",operator.type);
  9010. *)
  9011. operator := operator.nextOperator;
  9012. END;
  9013. super := operator;
  9014. ELSE
  9015. super := scope.firstProcedure;
  9016. WHILE (super # NIL) & (super.name # procedure.name) DO
  9017. super := super.nextProcedure;
  9018. END;
  9019. END;
  9020. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9021. RETURN super
  9022. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9023. RETURN super
  9024. ELSE
  9025. RETURN FindSuperProcedure(scope,procedure);
  9026. END;
  9027. END FindSuperProcedure;
  9028. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9029. VAR procedure: SyntaxTree.Procedure;
  9030. BEGIN
  9031. procedure := record.recordScope.constructor;
  9032. IF procedure = NIL THEN
  9033. record := RecordBase(record);
  9034. IF record # NIL THEN
  9035. procedure := GetConstructor(record)
  9036. END;
  9037. END;
  9038. RETURN procedure;
  9039. END GetConstructor;
  9040. (* enter a case into a list of cases in a sorted way and check for collision *)
  9041. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9042. VAR prev,this,new: SyntaxTree.CaseConstant;
  9043. BEGIN
  9044. this := root;
  9045. prev := NIL;
  9046. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9047. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9048. RETURN FALSE
  9049. ELSE
  9050. IF (this # NIL) & (this.min = max+1) THEN
  9051. this.min := min
  9052. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9053. prev.max := min
  9054. ELSE
  9055. NEW(new); new.min := min; new.max := max;
  9056. new.next := this;
  9057. IF prev = NIL THEN
  9058. root := new;
  9059. ELSE
  9060. prev.next := new
  9061. END
  9062. END;
  9063. RETURN TRUE
  9064. END;
  9065. END EnterCase;
  9066. (** generate and return a new checker object, errors are entered into diagnostics **)
  9067. 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;
  9068. VAR checker: Checker;
  9069. BEGIN
  9070. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9071. RETURN checker
  9072. END NewChecker;
  9073. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9074. VAR warnings: Warnings;
  9075. BEGIN
  9076. NEW(warnings, diagnostics); RETURN warnings;
  9077. END NewWarnings;
  9078. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9079. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9080. END IsRangeType;
  9081. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9082. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9083. END IsMathArrayType;
  9084. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9085. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9086. END IsArrayType;
  9087. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9088. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9089. END IsComplexType;
  9090. (** if a type is an array-structured object type *)
  9091. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9092. VAR recordType: SyntaxTree.RecordType;
  9093. BEGIN
  9094. IF type = NIL THEN
  9095. RETURN FALSE
  9096. ELSE
  9097. type := type.resolved;
  9098. IF type IS SyntaxTree.PointerType THEN
  9099. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9100. IF type IS SyntaxTree.RecordType THEN
  9101. recordType := type(SyntaxTree.RecordType);
  9102. RETURN recordType.isObject & recordType.HasArrayStructure()
  9103. ELSE
  9104. RETURN FALSE
  9105. END
  9106. ELSE
  9107. RETURN FALSE
  9108. END
  9109. END
  9110. END IsArrayStructuredObjectType;
  9111. (** the math array structure of a type
  9112. - for math arrays: the array itself
  9113. - for pointers: the math array structure of the pointer base
  9114. - for array-structured object types: the underlying structure
  9115. - for non-math arrays and all other types: NIL
  9116. **)
  9117. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9118. VAR
  9119. result: SyntaxTree.MathArrayType;
  9120. BEGIN
  9121. IF type = NIL THEN
  9122. result := NIL
  9123. ELSE
  9124. type := type.resolved;
  9125. IF type IS SyntaxTree.PointerType THEN
  9126. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9127. END;
  9128. IF type IS SyntaxTree.MathArrayType THEN
  9129. result := type(SyntaxTree.MathArrayType)
  9130. ELSIF type IS SyntaxTree.RecordType THEN
  9131. result := type(SyntaxTree.RecordType).arrayStructure
  9132. ELSE
  9133. result := NIL
  9134. END
  9135. END;
  9136. RETURN result
  9137. END MathArrayStructureOfType;
  9138. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9139. VAR
  9140. result: BOOLEAN;
  9141. rangeExpression: SyntaxTree.RangeExpression;
  9142. BEGIN
  9143. IF x IS SyntaxTree.RangeExpression THEN
  9144. rangeExpression := x(SyntaxTree.RangeExpression);
  9145. result := TRUE;
  9146. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9147. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9148. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9149. ELSE
  9150. result := FALSE
  9151. END;
  9152. RETURN result
  9153. END IsStaticRange;
  9154. (** whether a type is a math array of tensor form **)
  9155. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9156. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9157. END IsTensor;
  9158. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9159. BEGIN
  9160. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9161. length := type(SyntaxTree.MathArrayType).staticLength;
  9162. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9163. RETURN TRUE
  9164. ELSE
  9165. RETURN FALSE
  9166. END;
  9167. END IsStaticMathArray;
  9168. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9169. BEGIN
  9170. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9171. END SymbolHasAddress;
  9172. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9173. BEGIN
  9174. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9175. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9176. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9177. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9178. END HasAddress;
  9179. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9180. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9181. BEGIN
  9182. IF (e IS SyntaxTree.Designator) THEN
  9183. d := e(SyntaxTree.Designator);
  9184. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9185. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9186. e := d.left;
  9187. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9188. END;
  9189. IF d # NIL THEN
  9190. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9191. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9192. END;
  9193. END;
  9194. RETURN FALSE;
  9195. END IsLocalVariable;
  9196. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9197. BEGIN
  9198. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9199. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9200. ELSE
  9201. RETURN TRUE
  9202. END;
  9203. END IsStaticProcedure;
  9204. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9205. CONST OptimizeMethodTable = FALSE;
  9206. BEGIN
  9207. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9208. END InMethodTable;
  9209. END FoxSemanticChecker.
  9210. SystemTools.FreeDownTo FoxSemanticChecker ~