pdecsub.pas 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used in implementation section }
  26. pd_interface, { directive can be used in interface section }
  27. pd_object, { directive can be used with object declaration }
  28. pd_record, { directive can be used with record declaration }
  29. pd_procvar, { directive can be used with procvar declaration }
  30. pd_notobject, { directive can not be used with object declaration }
  31. pd_notrecord, { directive can not be used with record declaration }
  32. pd_notobjintf, { directive can not be used with interface declaration }
  33. pd_notprocvar, { directive can not be used with procvar declaration }
  34. pd_dispinterface,{ directive can be used with dispinterface methods }
  35. pd_cppobject, { directive can be used with cppclass }
  36. pd_objcclass, { directive can be used with objcclass }
  37. pd_objcprot { directive can be used with objcprotocol }
  38. );
  39. tpdflags=set of tpdflag;
  40. function check_proc_directive(isprocvar:boolean):boolean;
  41. procedure insert_funcret_local(pd:tprocdef);
  42. function proc_add_definition(var currpd:tprocdef):boolean;
  43. function proc_get_importname(pd:tprocdef):string;
  44. procedure proc_set_mangledname(pd:tprocdef);
  45. procedure handle_calling_convention(pd:tabstractprocdef);
  46. procedure parse_parameter_dec(pd:tabstractprocdef);
  47. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  48. procedure parse_var_proc_directives(sym:tsym);
  49. procedure parse_object_proc_directives(pd:tabstractprocdef);
  50. procedure parse_record_proc_directives(pd:tabstractprocdef);
  51. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  52. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  53. { helper functions - they insert nested objects hierarcy to the symtablestack
  54. with object hierarchy
  55. }
  56. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  57. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  58. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  59. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  60. implementation
  61. uses
  62. SysUtils,
  63. { common }
  64. cutils,cclasses,
  65. { global }
  66. globtype,globals,verbose,constexp,
  67. systems,
  68. cpuinfo,
  69. { symtable }
  70. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  71. { pass 1 }
  72. fmodule,node,htypechk,
  73. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  74. objcutil,
  75. { parser }
  76. scanner,
  77. pbase,pexpr,ptype,pdecl
  78. ;
  79. const
  80. { Please leave this here, this module should NOT use
  81. these variables.
  82. Declaring it as string here results in an error when compiling (PFV) }
  83. current_procinfo = 'error';
  84. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  85. var
  86. _class,hp : tobjectdef;
  87. begin
  88. if obj.typ=recorddef then
  89. begin
  90. symtablestack.push(obj.symtable);
  91. result:=1;
  92. exit;
  93. end;
  94. result:=0;
  95. { insert class hierarchy in the reverse order }
  96. hp:=nil;
  97. repeat
  98. _class:=tobjectdef(obj);
  99. while _class.childof<>hp do
  100. _class:=_class.childof;
  101. hp:=_class;
  102. symtablestack.push(_class.symtable);
  103. inc(result);
  104. until hp=obj;
  105. end;
  106. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  107. begin
  108. result:=0;
  109. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  110. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  111. inc(result,push_child_hierarcy(obj));
  112. end;
  113. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  114. var
  115. _class : tobjectdef;
  116. begin
  117. if obj.typ=recorddef then
  118. begin
  119. symtablestack.pop(obj.symtable);
  120. result:=1;
  121. exit;
  122. end;
  123. result:=0;
  124. _class:=tobjectdef(obj);
  125. while assigned(_class) do
  126. begin
  127. symtablestack.pop(_class.symtable);
  128. _class:=_class.childof;
  129. inc(result);
  130. end;
  131. end;
  132. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  133. begin
  134. result:=pop_child_hierarchy(obj);
  135. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  136. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  137. end;
  138. procedure insert_funcret_para(pd:tabstractprocdef);
  139. var
  140. storepos : tfileposinfo;
  141. vs : tparavarsym;
  142. paranr : word;
  143. begin
  144. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  145. not is_void(pd.returndef) and
  146. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  147. begin
  148. storepos:=current_tokenpos;
  149. if pd.typ=procdef then
  150. current_tokenpos:=tprocdef(pd).fileinfo;
  151. {$if defined(i386)}
  152. { For left to right add it at the end to be delphi compatible.
  153. In the case of safecalls with safecal-exceptions support the
  154. funcret-para is (from the 'c'-point of view) a normal parameter
  155. which has to be added to the end of the parameter-list }
  156. if (pd.proccalloption in (pushleftright_pocalls)) or
  157. ((tf_safecall_exceptions in target_info.flags) and
  158. (pd.proccalloption=pocall_safecall)) then
  159. paranr:=paranr_result_leftright
  160. else
  161. {$elseif defined(x86) or defined(arm)}
  162. if (target_info.system in systems_all_windows) and
  163. (pd.proccalloption = pocall_safecall) then
  164. paranr:=paranr_result_leftright
  165. else
  166. {$endif}
  167. paranr:=paranr_result;
  168. { Generate result variable accessing function result }
  169. vs:=tparavarsym.create('$result',paranr,vs_var,pd.returndef,[vo_is_funcret,vo_is_hidden_para]);
  170. pd.parast.insert(vs);
  171. { Store the this symbol as funcretsym for procedures }
  172. if pd.typ=procdef then
  173. tprocdef(pd).funcretsym:=vs;
  174. current_tokenpos:=storepos;
  175. end;
  176. end;
  177. procedure insert_parentfp_para(pd:tabstractprocdef);
  178. var
  179. storepos : tfileposinfo;
  180. vs : tparavarsym;
  181. paranr : longint;
  182. begin
  183. if pd.parast.symtablelevel>normal_function_level then
  184. begin
  185. storepos:=current_tokenpos;
  186. if pd.typ=procdef then
  187. current_tokenpos:=tprocdef(pd).fileinfo;
  188. { if no support for nested procvars is activated, use the old
  189. calling convention to pass the parent frame pointer for backwards
  190. compatibility }
  191. if not(m_nested_procvars in current_settings.modeswitches) then
  192. paranr:=paranr_parentfp
  193. { nested procvars require Delphi-style parentfp passing, see
  194. po_delphi_nested_cc declaration for more info }
  195. {$ifdef i386}
  196. else if (pd.proccalloption in pushleftright_pocalls) then
  197. paranr:=paranr_parentfp_delphi_cc_leftright
  198. {$endif i386}
  199. else
  200. paranr:=paranr_parentfp_delphi_cc;
  201. { Generate result variable accessing function result, it
  202. can't be put in a register since it must be accessable
  203. from the framepointer }
  204. vs:=tparavarsym.create('$parentfp',paranr,vs_value
  205. ,voidpointertype,[vo_is_parentfp,vo_is_hidden_para]);
  206. vs.varregable:=vr_none;
  207. pd.parast.insert(vs);
  208. current_tokenpos:=storepos;
  209. end;
  210. end;
  211. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  212. var
  213. storepos : tfileposinfo;
  214. vs : tparavarsym;
  215. hdef : tdef;
  216. vsp : tvarspez;
  217. aliasvs : tabsolutevarsym;
  218. sl : tpropaccesslist;
  219. begin
  220. if (pd.typ=procdef) and
  221. is_objc_class_or_protocol(tprocdef(pd).struct) and
  222. (pd.parast.symtablelevel=normal_function_level) then
  223. begin
  224. { insert Objective-C self and selector parameters }
  225. vs:=tparavarsym.create('$_cmd',paranr_objc_cmd,vs_value,objc_seltype,[vo_is_msgsel,vo_is_hidden_para]);
  226. pd.parast.insert(vs);
  227. { make accessible to code }
  228. sl:=tpropaccesslist.create;
  229. sl.addsym(sl_load,vs);
  230. aliasvs:=tabsolutevarsym.create_ref('_CMD',objc_seltype,sl);
  231. include(aliasvs.varoptions,vo_is_msgsel);
  232. tlocalsymtable(tprocdef(pd).localst).insert(aliasvs);
  233. if (po_classmethod in pd.procoptions) then
  234. { compatible with what gcc does }
  235. hdef:=objc_idtype
  236. else
  237. hdef:=tprocdef(pd).struct;
  238. vs:=tparavarsym.create('$self',paranr_objc_self,vs_value,hdef,[vo_is_self,vo_is_hidden_para]);
  239. pd.parast.insert(vs);
  240. end
  241. else if (pd.typ=procvardef) and
  242. pd.is_methodpointer then
  243. begin
  244. { Generate self variable }
  245. vs:=tparavarsym.create('$self',paranr_self,vs_value,voidpointertype,[vo_is_self,vo_is_hidden_para]);
  246. pd.parast.insert(vs);
  247. end
  248. else
  249. begin
  250. if (pd.typ=procdef) and
  251. assigned(tprocdef(pd).struct) and
  252. (pd.parast.symtablelevel=normal_function_level) then
  253. begin
  254. { static class methods have no hidden self/vmt pointer }
  255. if pd.no_self_node then
  256. exit;
  257. storepos:=current_tokenpos;
  258. current_tokenpos:=tprocdef(pd).fileinfo;
  259. { Generate VMT variable for constructor/destructor }
  260. if (pd.proctypeoption in [potype_constructor,potype_destructor]) and
  261. not(is_cppclass(tprocdef(pd).struct) or is_record(tprocdef(pd).struct)) then
  262. begin
  263. { can't use classrefdef as type because inheriting
  264. will then always file because of a type mismatch }
  265. vs:=tparavarsym.create('$vmt',paranr_vmt,vs_value,voidpointertype,[vo_is_vmt,vo_is_hidden_para]);
  266. pd.parast.insert(vs);
  267. end;
  268. { Generate self variable, for classes we need
  269. to use the generic voidpointer to be compatible with
  270. methodpointers }
  271. vsp:=vs_value;
  272. if (po_staticmethod in pd.procoptions) or
  273. (po_classmethod in pd.procoptions) then
  274. hdef:=tclassrefdef.create(tprocdef(pd).struct)
  275. else
  276. begin
  277. if is_object(tprocdef(pd).struct) or is_record(tprocdef(pd).struct) then
  278. vsp:=vs_var;
  279. hdef:=tprocdef(pd).struct;
  280. end;
  281. vs:=tparavarsym.create('$self',paranr_self,vsp,hdef,[vo_is_self,vo_is_hidden_para]);
  282. pd.parast.insert(vs);
  283. current_tokenpos:=storepos;
  284. end;
  285. end;
  286. end;
  287. procedure insert_funcret_local(pd:tprocdef);
  288. var
  289. storepos : tfileposinfo;
  290. vs : tlocalvarsym;
  291. aliasvs : tabsolutevarsym;
  292. sl : tpropaccesslist;
  293. hs : string;
  294. begin
  295. { The result from constructors and destructors can't be accessed directly }
  296. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  297. not is_void(pd.returndef) then
  298. begin
  299. storepos:=current_tokenpos;
  300. current_tokenpos:=pd.fileinfo;
  301. { We need to insert a varsym for the result in the localst
  302. when it is returning in a register }
  303. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  304. begin
  305. vs:=tlocalvarsym.create('$result',vs_value,pd.returndef,[vo_is_funcret]);
  306. pd.localst.insert(vs);
  307. pd.funcretsym:=vs;
  308. end;
  309. { insert the name of the procedure as alias for the function result,
  310. we can't use realname because that will not work for compilerprocs
  311. as the name is lowercase and unreachable from the code }
  312. if assigned(pd.resultname) then
  313. hs:=pd.resultname^
  314. else
  315. hs:=pd.procsym.name;
  316. sl:=tpropaccesslist.create;
  317. sl.addsym(sl_load,pd.funcretsym);
  318. aliasvs:=tabsolutevarsym.create_ref(hs,pd.returndef,sl);
  319. include(aliasvs.varoptions,vo_is_funcret);
  320. tlocalsymtable(pd.localst).insert(aliasvs);
  321. { insert result also if support is on }
  322. if (m_result in current_settings.modeswitches) then
  323. begin
  324. sl:=tpropaccesslist.create;
  325. sl.addsym(sl_load,pd.funcretsym);
  326. aliasvs:=tabsolutevarsym.create_ref('RESULT',pd.returndef,sl);
  327. include(aliasvs.varoptions,vo_is_funcret);
  328. include(aliasvs.varoptions,vo_is_result);
  329. tlocalsymtable(pd.localst).insert(aliasvs);
  330. end;
  331. current_tokenpos:=storepos;
  332. end;
  333. end;
  334. procedure insert_hidden_para(p:TObject;arg:pointer);
  335. var
  336. hvs : tparavarsym;
  337. pd : tabstractprocdef absolute arg;
  338. begin
  339. if (tsym(p).typ<>paravarsym) then
  340. exit;
  341. with tparavarsym(p) do
  342. begin
  343. { We need a local copy for a value parameter when only the
  344. address is pushed. Open arrays and Array of Const are
  345. an exception because they are allocated at runtime and the
  346. address that is pushed is patched }
  347. if (varspez=vs_value) and
  348. paramanager.push_addr_param(varspez,vardef,pd.proccalloption) and
  349. not(is_open_array(vardef) or
  350. is_array_of_const(vardef)) then
  351. include(varoptions,vo_has_local_copy);
  352. { needs high parameter ? }
  353. if paramanager.push_high_param(varspez,vardef,pd.proccalloption) then
  354. begin
  355. hvs:=tparavarsym.create('$high'+name,paranr+1,vs_const,sinttype,[vo_is_high_para,vo_is_hidden_para]);
  356. hvs.symoptions:=[];
  357. owner.insert(hvs);
  358. end
  359. else
  360. begin
  361. { Give a warning that cdecl routines does not include high()
  362. support }
  363. if (pd.proccalloption in cdecl_pocalls) and
  364. paramanager.push_high_param(varspez,vardef,pocall_default) then
  365. begin
  366. if is_open_string(vardef) then
  367. MessagePos(fileinfo,parser_w_cdecl_no_openstring);
  368. if not(po_external in pd.procoptions) and
  369. (pd.typ<>procvardef) and
  370. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  371. if is_array_of_const(vardef) then
  372. MessagePos(fileinfo,parser_e_varargs_need_cdecl_and_external)
  373. else
  374. MessagePos(fileinfo,parser_w_cdecl_has_no_high);
  375. end;
  376. if (vardef.typ=formaldef) and (Tformaldef(vardef).typed) then
  377. begin
  378. hvs:=tparavarsym.create('$typinfo'+name,paranr+1,vs_const,voidpointertype,
  379. [vo_is_typinfo_para,vo_is_hidden_para]);
  380. owner.insert(hvs);
  381. end;
  382. end;
  383. end;
  384. end;
  385. procedure check_c_para(pd:Tabstractprocdef);
  386. var
  387. i,
  388. lastparaidx : longint;
  389. sym : TSym;
  390. begin
  391. lastparaidx:=pd.parast.SymList.Count-1;
  392. for i:=0 to pd.parast.SymList.Count-1 do
  393. begin
  394. sym:=tsym(pd.parast.SymList[i]);
  395. if (sym.typ=paravarsym) and
  396. (tparavarsym(sym).vardef.typ=arraydef) then
  397. begin
  398. if not is_variant_array(tparavarsym(sym).vardef) and
  399. not is_array_of_const(tparavarsym(sym).vardef) and
  400. (tparavarsym(sym).varspez<>vs_var) then
  401. MessagePos(tparavarsym(sym).fileinfo,parser_h_c_arrays_are_references);
  402. if is_array_of_const(tparavarsym(sym).vardef) and
  403. (i<lastparaidx) and
  404. (tsym(pd.parast.SymList[i+1]).typ=paravarsym) and
  405. not(vo_is_high_para in tparavarsym(pd.parast.SymList[i+1]).varoptions) then
  406. MessagePos(tparavarsym(sym).fileinfo,parser_e_C_array_of_const_must_be_last);
  407. end;
  408. end;
  409. end;
  410. procedure check_msg_para(p:TObject;arg:pointer);
  411. begin
  412. if (tsym(p).typ<>paravarsym) then
  413. exit;
  414. with tparavarsym(p) do
  415. begin
  416. { Count parameters }
  417. if (paranr>=10) then
  418. inc(plongint(arg)^);
  419. { First parameter must be var }
  420. if (paranr=10) and
  421. (varspez<>vs_var) then
  422. MessagePos(fileinfo,parser_e_ill_msg_param);
  423. end;
  424. end;
  425. procedure set_addr_param_regable(p:TObject;arg:pointer);
  426. begin
  427. if (tsym(p).typ<>paravarsym) then
  428. exit;
  429. with tparavarsym(p) do
  430. begin
  431. if not is_managed_type(vardef) and
  432. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  433. varregable:=vr_intreg;
  434. end;
  435. end;
  436. procedure parse_parameter_dec(pd:tabstractprocdef);
  437. {
  438. handle_procvar needs the same changes
  439. }
  440. type
  441. tppv = (pv_none,pv_proc,pv_func);
  442. var
  443. sc : TFPObjectList;
  444. hdef : tdef;
  445. arrayelementdef : tdef;
  446. vs : tparavarsym;
  447. i : longint;
  448. srsym : tsym;
  449. pv : tprocvardef;
  450. varspez : Tvarspez;
  451. defaultvalue : tconstsym;
  452. defaultrequired : boolean;
  453. old_block_type : tblock_type;
  454. currparast : tparasymtable;
  455. parseprocvar : tppv;
  456. locationstr : string;
  457. paranr : integer;
  458. dummytype : ttypesym;
  459. explicit_paraloc,
  460. need_array,
  461. is_univ: boolean;
  462. begin
  463. old_block_type:=block_type;
  464. explicit_paraloc:=false;
  465. consume(_LKLAMMER);
  466. { Delphi/Kylix supports nonsense like }
  467. { procedure p(); }
  468. if try_to_consume(_RKLAMMER) and
  469. not(m_tp7 in current_settings.modeswitches) then
  470. exit;
  471. { parsing a proc or procvar ? }
  472. currparast:=tparasymtable(pd.parast);
  473. { reset }
  474. sc:=TFPObjectList.create(false);
  475. defaultrequired:=false;
  476. paranr:=0;
  477. block_type:=bt_var;
  478. is_univ:=false;
  479. repeat
  480. parseprocvar:=pv_none;
  481. if try_to_consume(_VAR) then
  482. varspez:=vs_var
  483. else
  484. if try_to_consume(_CONST) then
  485. varspez:=vs_const
  486. else
  487. if (m_out in current_settings.modeswitches) and
  488. try_to_consume(_OUT) then
  489. varspez:=vs_out
  490. else
  491. if try_to_consume(_CONSTREF) then
  492. varspez:=vs_constref
  493. else
  494. if (m_mac in current_settings.modeswitches) and
  495. try_to_consume(_POINTPOINTPOINT) then
  496. begin
  497. include(pd.procoptions,po_varargs);
  498. break;
  499. end
  500. else
  501. if (m_nested_procvars in current_settings.modeswitches) and
  502. try_to_consume(_PROCEDURE) then
  503. begin
  504. parseprocvar:=pv_proc;
  505. varspez:=vs_const;
  506. end
  507. else
  508. if (m_nested_procvars in current_settings.modeswitches) and
  509. try_to_consume(_FUNCTION) then
  510. begin
  511. parseprocvar:=pv_func;
  512. varspez:=vs_const;
  513. end
  514. else
  515. varspez:=vs_value;
  516. defaultvalue:=nil;
  517. hdef:=nil;
  518. { read identifiers and insert with error type }
  519. sc.clear;
  520. repeat
  521. inc(paranr);
  522. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  523. currparast.insert(vs);
  524. if assigned(vs.owner) then
  525. sc.add(vs)
  526. else
  527. vs.free;
  528. consume(_ID);
  529. until not try_to_consume(_COMMA);
  530. locationstr:='';
  531. { macpas anonymous procvar }
  532. if parseprocvar<>pv_none then
  533. begin
  534. { inline procvar definitions are always nested procvars }
  535. pv:=tprocvardef.create(normal_function_level+1);
  536. if token=_LKLAMMER then
  537. parse_parameter_dec(pv);
  538. if parseprocvar=pv_func then
  539. begin
  540. block_type:=bt_var_type;
  541. consume(_COLON);
  542. single_type(pv.returndef,[]);
  543. block_type:=bt_var;
  544. end;
  545. hdef:=pv;
  546. { possible proc directives }
  547. if check_proc_directive(true) then
  548. begin
  549. dummytype:=ttypesym.create('unnamed',hdef);
  550. parse_var_proc_directives(tsym(dummytype));
  551. dummytype.typedef:=nil;
  552. hdef.typesym:=nil;
  553. dummytype.free;
  554. end;
  555. { Add implicit hidden parameters and function result }
  556. handle_calling_convention(pv);
  557. end
  558. else
  559. { read type declaration, force reading for value paras }
  560. if (token=_COLON) or (varspez=vs_value) then
  561. begin
  562. consume(_COLON);
  563. { check for an open array }
  564. need_array:=false;
  565. { bitpacked open array are not yet supported }
  566. if (token=_PACKED) and
  567. not(cs_bitpacking in current_settings.localswitches) then
  568. begin
  569. consume(_PACKED);
  570. need_array:=true;
  571. end;
  572. if (token=_ARRAY) or
  573. need_array then
  574. begin
  575. consume(_ARRAY);
  576. consume(_OF);
  577. { define range and type of range }
  578. hdef:=tarraydef.create(0,-1,s32inttype);
  579. { array of const ? }
  580. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  581. begin
  582. consume(_CONST);
  583. srsym:=search_system_type('TVARREC');
  584. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  585. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  586. end
  587. else
  588. begin
  589. { define field type }
  590. single_type(arrayelementdef,[]);
  591. tarraydef(hdef).elementdef:=arrayelementdef;
  592. end;
  593. end
  594. else
  595. begin
  596. if (m_mac in current_settings.modeswitches) then
  597. is_univ:=try_to_consume(_UNIV);
  598. if try_to_consume(_TYPE) then
  599. hdef:=ctypedformaltype
  600. else
  601. begin
  602. block_type:=bt_var_type;
  603. single_type(hdef,[stoAllowSpecialization]);
  604. block_type:=bt_var;
  605. end;
  606. { open string ? }
  607. if is_shortstring(hdef) then
  608. begin
  609. case varspez of
  610. vs_var,vs_out,vs_constref:
  611. begin
  612. { not 100% Delphi-compatible: type xstr=string[255] cannot
  613. become an openstring there, while here it can }
  614. if (cs_openstring in current_settings.moduleswitches) and
  615. (tstringdef(hdef).len=255) then
  616. hdef:=openshortstringtype
  617. end;
  618. vs_value:
  619. begin
  620. { value "openstring" parameters don't make sense (the
  621. original string can never be modified, so there's no
  622. use in passing its original length), so change these
  623. into regular shortstring parameters (seems to be what
  624. Delphi also does) }
  625. if is_open_string(hdef) then
  626. hdef:=cshortstringtype;
  627. end;
  628. end;
  629. end;
  630. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  631. begin
  632. if (idtoken=_LOCATION) then
  633. begin
  634. consume(_LOCATION);
  635. locationstr:=cstringpattern;
  636. consume(_CSTRING);
  637. end
  638. else
  639. begin
  640. if explicit_paraloc then
  641. Message(parser_e_paraloc_all_paras);
  642. locationstr:='';
  643. end;
  644. end
  645. else
  646. locationstr:='';
  647. { default parameter }
  648. if (m_default_para in current_settings.modeswitches) then
  649. begin
  650. if try_to_consume(_EQ) then
  651. begin
  652. vs:=tparavarsym(sc[0]);
  653. if sc.count>1 then
  654. Message(parser_e_default_value_only_one_para);
  655. { prefix 'def' to the parameter name }
  656. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  657. if assigned(defaultvalue) then
  658. begin
  659. include(defaultvalue.symoptions,sp_internal);
  660. pd.parast.insert(defaultvalue);
  661. end;
  662. defaultrequired:=true;
  663. end
  664. else
  665. begin
  666. if defaultrequired then
  667. Message1(parser_e_default_value_expected_for_para,vs.name);
  668. end;
  669. end;
  670. end;
  671. end
  672. else
  673. hdef:=cformaltype;
  674. { File types are only allowed for var and out parameters }
  675. if (hdef.typ=filedef) and
  676. not(varspez in [vs_out,vs_var]) then
  677. CGMessage(cg_e_file_must_call_by_reference);
  678. { Dispinterfaces are restricted to using only automatable types }
  679. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  680. not is_automatable(hdef) then
  681. Message1(type_e_not_automatable,hdef.typename);
  682. { univ cannot be used with types whose size is not known at compile
  683. time }
  684. if is_univ and
  685. not is_valid_univ_para_type(hdef) then
  686. Message1(parser_e_invalid_univ_para,hdef.typename);
  687. for i:=0 to sc.count-1 do
  688. begin
  689. vs:=tparavarsym(sc[i]);
  690. vs.univpara:=is_univ;
  691. { update varsym }
  692. vs.vardef:=hdef;
  693. vs.defaultconstsym:=defaultvalue;
  694. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  695. begin
  696. if locationstr<>'' then
  697. begin
  698. if sc.count>1 then
  699. Message(parser_e_paraloc_only_one_para);
  700. if (paranr>1) and not(explicit_paraloc) then
  701. Message(parser_e_paraloc_all_paras);
  702. explicit_paraloc:=true;
  703. include(vs.varoptions,vo_has_explicit_paraloc);
  704. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  705. message(parser_e_illegal_explicit_paraloc);
  706. end
  707. else
  708. if explicit_paraloc then
  709. Message(parser_e_paraloc_all_paras);
  710. end;
  711. end;
  712. until not try_to_consume(_SEMICOLON);
  713. if explicit_paraloc then
  714. begin
  715. pd.has_paraloc_info:=callerside;
  716. include(pd.procoptions,po_explicitparaloc);
  717. end;
  718. { remove parasymtable from stack }
  719. sc.free;
  720. { reset object options }
  721. block_type:=old_block_type;
  722. consume(_RKLAMMER);
  723. end;
  724. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  725. var
  726. hs : string;
  727. orgsp,sp : TIDString;
  728. srsym : tsym;
  729. checkstack : psymtablestackitem;
  730. procstartfilepos : tfileposinfo;
  731. searchagain : boolean;
  732. st,
  733. genericst: TSymtable;
  734. aprocsym : tprocsym;
  735. popclass : integer;
  736. ImplIntf : TImplementedInterface;
  737. old_parse_generic : boolean;
  738. old_current_structdef: tabstractrecorddef;
  739. old_current_genericdef,
  740. old_current_specializedef: tstoreddef;
  741. lasttoken,lastidtoken: ttoken;
  742. procedure parse_operator_name;
  743. begin
  744. if (lasttoken in [first_overloaded..last_overloaded]) then
  745. begin
  746. optoken:=token;
  747. end
  748. else
  749. begin
  750. case lasttoken of
  751. _CARET:
  752. Message1(parser_e_overload_operator_failed,'**');
  753. _ID:
  754. case lastidtoken of
  755. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  756. _EXPLICIT:optoken:=_OP_EXPLICIT;
  757. _INC:optoken:=_OP_INC;
  758. _DEC:optoken:=_OP_DEC;
  759. else
  760. if (m_delphi in current_settings.modeswitches) then
  761. case lastidtoken of
  762. _IMPLICIT:optoken:=_ASSIGNMENT;
  763. _NEGATIVE:optoken:=_MINUS;
  764. _POSITIVE:optoken:=_PLUS;
  765. _LOGICALNOT:optoken:=_OP_NOT;
  766. _IN:optoken:=_OP_IN;
  767. _EQUAL:optoken:=_EQ;
  768. _NOTEQUAL:optoken:=_NE;
  769. _GREATERTHAN:optoken:=_GT;
  770. _GREATERTHANOREQUAL:optoken:=_GTE;
  771. _LESSTHAN:optoken:=_LT;
  772. _LESSTHANOREQUAL:optoken:=_LTE;
  773. _ADD:optoken:=_PLUS;
  774. _SUBTRACT:optoken:=_MINUS;
  775. _MULTIPLY:optoken:=_STAR;
  776. _DIVIDE:optoken:=_SLASH;
  777. _INTDIVIDE:optoken:=_OP_DIV;
  778. _MODULUS:optoken:=_OP_MOD;
  779. _LEFTSHIFT:optoken:=_OP_SHL;
  780. _RIGHTSHIFT:optoken:=_OP_SHR;
  781. _LOGICALAND:optoken:=_OP_AND;
  782. _LOGICALOR:optoken:=_OP_OR;
  783. _LOGICALXOR:optoken:=_OP_XOR;
  784. _BITWISEAND:optoken:=_OP_AND;
  785. _BITWISEOR:optoken:=_OP_OR;
  786. _BITWISEXOR:optoken:=_OP_XOR;
  787. else
  788. Message1(parser_e_overload_operator_failed,'');
  789. end
  790. else
  791. Message1(parser_e_overload_operator_failed,'');
  792. end
  793. else
  794. Message1(parser_e_overload_operator_failed,'');
  795. end;
  796. end;
  797. sp:=overloaded_names[optoken];
  798. orgsp:=sp;
  799. end;
  800. procedure consume_proc_name;
  801. begin
  802. lasttoken:=token;
  803. lastidtoken:=idtoken;
  804. if potype=potype_operator then
  805. optoken:=NOTOKEN;
  806. if (potype=potype_operator) and (token<>_ID) then
  807. begin
  808. parse_operator_name;
  809. consume(token);
  810. end
  811. else
  812. begin
  813. sp:=pattern;
  814. orgsp:=orgpattern;
  815. consume(_ID);
  816. end;
  817. end;
  818. function search_object_name(sp:TIDString;gen_error:boolean):tsym;
  819. var
  820. storepos:tfileposinfo;
  821. srsymtable:TSymtable;
  822. begin
  823. storepos:=current_tokenpos;
  824. current_tokenpos:=procstartfilepos;
  825. searchsym(sp,result,srsymtable);
  826. if not assigned(result) then
  827. begin
  828. if gen_error then
  829. identifier_not_found(orgsp);
  830. result:=generrorsym;
  831. end;
  832. current_tokenpos:=storepos;
  833. end;
  834. function consume_generic_type_parameter:boolean;
  835. var
  836. i:integer;
  837. ok:boolean;
  838. sym:tsym;
  839. begin
  840. result:=not assigned(astruct)and(m_delphi in current_settings.modeswitches);
  841. if result then
  842. begin
  843. { a generic type parameter? }
  844. srsym:=search_object_name(sp,false);
  845. if (srsym.typ=typesym) and
  846. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  847. begin
  848. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  849. if (df_generic in astruct.defoptions) and try_to_consume(_LT) then
  850. begin
  851. ok:=true;
  852. i:=0;
  853. repeat
  854. if ok and (token=_ID) then
  855. begin
  856. ok:=false;
  857. while i<astruct.symtable.SymList.Count-1 do
  858. begin
  859. sym:=tsym(astruct.symtable.SymList[i]);
  860. if sp_generic_para in sym.symoptions then
  861. begin
  862. ok:=sym.Name=pattern;
  863. inc(i);
  864. break;
  865. end;
  866. inc(i);
  867. end;
  868. if not ok then
  869. Message1(type_e_generic_declaration_does_not_match,astruct.RttiName);
  870. end;
  871. consume(_ID);
  872. until not try_to_consume(_COMMA);
  873. if ok then
  874. while i<astruct.symtable.SymList.Count-1 do
  875. begin
  876. sym:=tsym(astruct.symtable.SymList[i]);
  877. if sp_generic_para in sym.symoptions then
  878. begin
  879. Message1(type_e_generic_declaration_does_not_match,astruct.RttiName);
  880. break;
  881. end;
  882. inc(i);
  883. end;
  884. consume(_GT);
  885. end
  886. else
  887. if (df_generic in astruct.defoptions) and (token=_POINT) then
  888. begin
  889. Message1(type_e_generic_declaration_does_not_match,astruct.RttiName);
  890. end
  891. else
  892. begin
  893. { not a method. routine name just accidentally match some structure name }
  894. astruct:=nil;
  895. if try_to_consume(_LT) then
  896. begin
  897. Message(type_e_type_parameters_are_not_allowed_here);
  898. repeat
  899. consume(_ID);
  900. until not try_to_consume(_COMMA);
  901. consume(_GT);
  902. end;
  903. end;
  904. end;
  905. end;
  906. end;
  907. begin
  908. { Save the position where this procedure really starts }
  909. procstartfilepos:=current_tokenpos;
  910. old_parse_generic:=parse_generic;
  911. result:=false;
  912. pd:=nil;
  913. aprocsym:=nil;
  914. consume_proc_name;
  915. { examine interface map: function/procedure iname.functionname=locfuncname }
  916. if assigned(astruct) and
  917. (astruct.typ=objectdef) and
  918. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  919. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  920. try_to_consume(_POINT) then
  921. begin
  922. srsym:=search_object_name(sp,true);
  923. { qualifier is interface? }
  924. ImplIntf:=nil;
  925. if (srsym.typ=typesym) and
  926. (ttypesym(srsym).typedef.typ=objectdef) then
  927. ImplIntf:=tobjectdef(astruct).find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  928. if ImplIntf=nil then
  929. Message(parser_e_interface_id_expected);
  930. consume(_ID);
  931. { Create unique name <interface>.<method> }
  932. hs:=sp+'.'+pattern;
  933. consume(_EQ);
  934. if assigned(ImplIntf) and
  935. (token=_ID) then
  936. ImplIntf.AddMapping(hs,pattern);
  937. consume(_ID);
  938. result:=true;
  939. exit;
  940. end;
  941. { method ? }
  942. if (consume_generic_type_parameter or not assigned(astruct)) and
  943. (symtablestack.top.symtablelevel=main_program_level) and
  944. try_to_consume(_POINT) then
  945. begin
  946. repeat
  947. searchagain:=false;
  948. if not assigned(astruct) then
  949. srsym:=search_object_name(sp,true);
  950. { consume proc name }
  951. procstartfilepos:=current_tokenpos;
  952. consume_proc_name;
  953. { qualifier is class name ? }
  954. if (srsym.typ=typesym) and
  955. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  956. begin
  957. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  958. if (token<>_POINT) then
  959. if (potype in [potype_class_constructor,potype_class_destructor]) then
  960. sp:=lower(sp)
  961. else
  962. if (potype=potype_operator)and(optoken=NOTOKEN) then
  963. parse_operator_name;
  964. srsym:=tsym(astruct.symtable.Find(sp));
  965. if assigned(srsym) then
  966. begin
  967. if srsym.typ=procsym then
  968. aprocsym:=tprocsym(srsym)
  969. else
  970. if (srsym.typ=typesym) and
  971. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  972. begin
  973. searchagain:=true;
  974. consume(_POINT);
  975. end
  976. else
  977. begin
  978. { we use a different error message for tp7 so it looks more compatible }
  979. if (m_fpc in current_settings.modeswitches) then
  980. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  981. else
  982. Message(parser_e_methode_id_expected);
  983. { rename the name to an unique name to avoid an
  984. error when inserting the symbol in the symtable }
  985. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  986. end;
  987. end
  988. else
  989. begin
  990. Message(parser_e_methode_id_expected);
  991. { recover by making it a normal procedure instead of method }
  992. astruct:=nil;
  993. end;
  994. end
  995. else
  996. Message(parser_e_class_id_expected);
  997. until not searchagain;
  998. end
  999. else
  1000. begin
  1001. { check for constructor/destructor/class operators which are not allowed here }
  1002. if (not parse_only) and
  1003. ((potype in [potype_constructor,potype_destructor,
  1004. potype_class_constructor,potype_class_destructor]) or
  1005. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  1006. Message(parser_e_only_methods_allowed);
  1007. repeat
  1008. searchagain:=false;
  1009. current_tokenpos:=procstartfilepos;
  1010. if (potype=potype_operator)and(optoken=NOTOKEN) then
  1011. parse_operator_name;
  1012. srsym:=tsym(symtablestack.top.Find(sp));
  1013. { Also look in the globalsymtable if we didn't found
  1014. the symbol in the localsymtable }
  1015. if not assigned(srsym) and
  1016. not(parse_only) and
  1017. (symtablestack.top=current_module.localsymtable) and
  1018. assigned(current_module.globalsymtable) then
  1019. srsym:=tsym(current_module.globalsymtable.Find(sp));
  1020. { Check if overloaded is a procsym }
  1021. if assigned(srsym) then
  1022. begin
  1023. if srsym.typ=procsym then
  1024. aprocsym:=tprocsym(srsym)
  1025. else
  1026. begin
  1027. { when the other symbol is a unit symbol then hide the unit
  1028. symbol, this is not supported in tp7 }
  1029. if not(m_tp7 in current_settings.modeswitches) and
  1030. (srsym.typ=unitsym) then
  1031. begin
  1032. HideSym(srsym);
  1033. searchagain:=true;
  1034. end
  1035. else
  1036. begin
  1037. { we use a different error message for tp7 so it looks more compatible }
  1038. if (m_fpc in current_settings.modeswitches) then
  1039. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  1040. else
  1041. Message1(sym_e_duplicate_id,srsym.realname);
  1042. { rename the name to an unique name to avoid an
  1043. error when inserting the symbol in the symtable }
  1044. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  1045. end;
  1046. end;
  1047. end;
  1048. until not searchagain;
  1049. end;
  1050. { test again if assigned, it can be reset to recover }
  1051. if not assigned(aprocsym) then
  1052. begin
  1053. { create a new procsym and set the real filepos }
  1054. current_tokenpos:=procstartfilepos;
  1055. { for operator we have only one procsym for each overloaded
  1056. operation }
  1057. if (potype=potype_operator) then
  1058. begin
  1059. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  1060. if aprocsym=nil then
  1061. aprocsym:=tprocsym.create('$'+sp);
  1062. end
  1063. else
  1064. if (potype in [potype_class_constructor,potype_class_destructor]) then
  1065. aprocsym:=tprocsym.create('$'+lower(sp))
  1066. else
  1067. aprocsym:=tprocsym.create(orgsp);
  1068. symtablestack.top.insert(aprocsym);
  1069. end;
  1070. { to get the correct symtablelevel we must ignore ObjectSymtables }
  1071. st:=nil;
  1072. checkstack:=symtablestack.stack;
  1073. while assigned(checkstack) do
  1074. begin
  1075. st:=checkstack^.symtable;
  1076. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1077. break;
  1078. checkstack:=checkstack^.next;
  1079. end;
  1080. pd:=tprocdef.create(st.symtablelevel+1);
  1081. pd.struct:=astruct;
  1082. pd.procsym:=aprocsym;
  1083. pd.proctypeoption:=potype;
  1084. { methods inherit df_generic or df_specialization from the objectdef }
  1085. if assigned(pd.struct) and
  1086. (pd.parast.symtablelevel=normal_function_level) then
  1087. begin
  1088. if (df_generic in pd.struct.defoptions) then
  1089. begin
  1090. include(pd.defoptions,df_generic);
  1091. parse_generic:=true;
  1092. end;
  1093. if (df_specialization in pd.struct.defoptions) then
  1094. begin
  1095. include(pd.defoptions,df_specialization);
  1096. { Find corresponding genericdef, we need it later to
  1097. replay the tokens to generate the body }
  1098. if not assigned(pd.struct.genericdef) then
  1099. internalerror(200512113);
  1100. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  1101. if not assigned(genericst) then
  1102. internalerror(200512114);
  1103. { We are parsing the same objectdef, the def index numbers
  1104. are the same }
  1105. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  1106. if not assigned(pd.genericdef) or
  1107. (pd.genericdef.typ<>procdef) then
  1108. internalerror(200512115);
  1109. end;
  1110. end;
  1111. { methods need to be exported }
  1112. if assigned(astruct) and
  1113. (
  1114. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  1115. (symtablestack.top.symtablelevel=main_program_level)
  1116. ) then
  1117. include(pd.procoptions,po_global);
  1118. { symbol options that need to be kept per procdef }
  1119. pd.fileinfo:=procstartfilepos;
  1120. pd.visibility:=symtablestack.top.currentvisibility;
  1121. if symtablestack.top.currentlyoptional then
  1122. include(pd.procoptions,po_optional);
  1123. { parse parameters }
  1124. if token=_LKLAMMER then
  1125. begin
  1126. { Add ObjectSymtable to be able to find nested type definitions }
  1127. popclass:=0;
  1128. if assigned(pd.struct) and
  1129. (pd.parast.symtablelevel=normal_function_level) and
  1130. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1131. begin
  1132. popclass:=push_nested_hierarchy(pd.struct);
  1133. old_current_structdef:=current_structdef;
  1134. old_current_genericdef:=current_genericdef;
  1135. old_current_specializedef:=current_specializedef;
  1136. current_structdef:=pd.struct;
  1137. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1138. current_genericdef:=current_structdef;
  1139. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1140. current_specializedef:=current_structdef;
  1141. end;
  1142. { Add parameter symtable }
  1143. if pd.parast.symtabletype<>staticsymtable then
  1144. symtablestack.push(pd.parast);
  1145. parse_parameter_dec(pd);
  1146. if pd.parast.symtabletype<>staticsymtable then
  1147. symtablestack.pop(pd.parast);
  1148. if popclass>0 then
  1149. begin
  1150. current_structdef:=old_current_structdef;
  1151. current_genericdef:=old_current_genericdef;
  1152. current_specializedef:=old_current_specializedef;
  1153. dec(popclass,pop_nested_hierarchy(pd.struct));
  1154. if popclass<>0 then
  1155. internalerror(201011260); // 11 nov 2010 index 0
  1156. end;
  1157. end;
  1158. parse_generic:=old_parse_generic;
  1159. result:=true;
  1160. end;
  1161. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  1162. var
  1163. pd: tprocdef;
  1164. locationstr: string;
  1165. i: integer;
  1166. found: boolean;
  1167. procedure read_returndef(pd: tprocdef);
  1168. var
  1169. popclass: integer;
  1170. old_parse_generic: boolean;
  1171. old_current_structdef: tabstractrecorddef;
  1172. old_current_genericdef,
  1173. old_current_specializedef: tstoreddef;
  1174. begin
  1175. old_parse_generic:=parse_generic;
  1176. { Add ObjectSymtable to be able to find generic type definitions }
  1177. popclass:=0;
  1178. if assigned(pd.struct) and
  1179. (pd.parast.symtablelevel=normal_function_level) and
  1180. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1181. begin
  1182. popclass:=push_nested_hierarchy(pd.struct);
  1183. parse_generic:=(df_generic in pd.struct.defoptions);
  1184. old_current_structdef:=current_structdef;
  1185. old_current_genericdef:=current_genericdef;
  1186. old_current_specializedef:=current_specializedef;
  1187. current_structdef:=pd.struct;
  1188. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1189. current_genericdef:=current_structdef;
  1190. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1191. current_specializedef:=current_structdef;
  1192. end;
  1193. single_type(pd.returndef,[stoAllowSpecialization]);
  1194. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  1195. Message1(type_e_not_automatable,pd.returndef.typename);
  1196. if popclass>0 then
  1197. begin
  1198. current_structdef:=old_current_structdef;
  1199. current_genericdef:=old_current_genericdef;
  1200. current_specializedef:=old_current_specializedef;
  1201. dec(popclass,pop_nested_hierarchy(pd.struct));
  1202. if popclass<>0 then
  1203. internalerror(201012020);
  1204. end;
  1205. parse_generic:=old_parse_generic;
  1206. end;
  1207. begin
  1208. locationstr:='';
  1209. pd:=nil;
  1210. case token of
  1211. _FUNCTION :
  1212. begin
  1213. consume(_FUNCTION);
  1214. if parse_proc_head(astruct,potype_function,pd) then
  1215. begin
  1216. { pd=nil when it is a interface mapping }
  1217. if assigned(pd) then
  1218. begin
  1219. if try_to_consume(_COLON) then
  1220. begin
  1221. read_returndef(pd);
  1222. if (target_info.system in [system_m68k_amiga]) then
  1223. begin
  1224. if (idtoken=_LOCATION) then
  1225. begin
  1226. if po_explicitparaloc in pd.procoptions then
  1227. begin
  1228. consume(_LOCATION);
  1229. locationstr:=cstringpattern;
  1230. consume(_CSTRING);
  1231. end
  1232. else
  1233. { I guess this needs a new message... (KB) }
  1234. Message(parser_e_paraloc_all_paras);
  1235. end
  1236. else
  1237. begin
  1238. if po_explicitparaloc in pd.procoptions then
  1239. { assign default locationstr, if none specified }
  1240. { and we've arguments with explicit paraloc }
  1241. locationstr:='D0';
  1242. end;
  1243. end;
  1244. end
  1245. else
  1246. begin
  1247. if (
  1248. parse_only and
  1249. not(is_interface(pd.struct))
  1250. ) or
  1251. (m_repeat_forward in current_settings.modeswitches) then
  1252. begin
  1253. consume(_COLON);
  1254. consume_all_until(_SEMICOLON);
  1255. end;
  1256. end;
  1257. if isclassmethod then
  1258. include(pd.procoptions,po_classmethod);
  1259. end;
  1260. end
  1261. else
  1262. begin
  1263. { recover }
  1264. consume(_COLON);
  1265. consume_all_until(_SEMICOLON);
  1266. end;
  1267. end;
  1268. _PROCEDURE :
  1269. begin
  1270. consume(_PROCEDURE);
  1271. if parse_proc_head(astruct,potype_procedure,pd) then
  1272. begin
  1273. { pd=nil when it is an interface mapping }
  1274. if assigned(pd) then
  1275. begin
  1276. pd.returndef:=voidtype;
  1277. if isclassmethod then
  1278. include(pd.procoptions,po_classmethod);
  1279. end;
  1280. end;
  1281. end;
  1282. _CONSTRUCTOR :
  1283. begin
  1284. consume(_CONSTRUCTOR);
  1285. if isclassmethod then
  1286. parse_proc_head(astruct,potype_class_constructor,pd)
  1287. else
  1288. parse_proc_head(astruct,potype_constructor,pd);
  1289. if not isclassmethod and
  1290. assigned(pd) and
  1291. assigned(pd.struct) then
  1292. begin
  1293. { Set return type, class constructors return the
  1294. created instance, object constructors return boolean }
  1295. if is_class(pd.struct) or is_record(pd.struct) then
  1296. pd.returndef:=pd.struct
  1297. else
  1298. {$ifdef CPU64bitaddr}
  1299. pd.returndef:=bool64type;
  1300. {$else CPU64bitaddr}
  1301. pd.returndef:=bool32type;
  1302. {$endif CPU64bitaddr}
  1303. end
  1304. else
  1305. pd.returndef:=voidtype;
  1306. end;
  1307. _DESTRUCTOR :
  1308. begin
  1309. consume(_DESTRUCTOR);
  1310. if isclassmethod then
  1311. parse_proc_head(astruct,potype_class_destructor,pd)
  1312. else
  1313. parse_proc_head(astruct,potype_destructor,pd);
  1314. if assigned(pd) then
  1315. pd.returndef:=voidtype;
  1316. end;
  1317. else
  1318. if (token=_OPERATOR) or
  1319. (isclassmethod and (idtoken=_OPERATOR)) then
  1320. begin
  1321. consume(_OPERATOR);
  1322. parse_proc_head(astruct,potype_operator,pd);
  1323. if assigned(pd) then
  1324. begin
  1325. { operators always need to be searched in all units }
  1326. include(pd.procoptions,po_overload);
  1327. if pd.parast.symtablelevel>normal_function_level then
  1328. Message(parser_e_no_local_operator);
  1329. if isclassmethod then
  1330. include(pd.procoptions,po_classmethod);
  1331. if token<>_ID then
  1332. begin
  1333. if not(m_result in current_settings.modeswitches) then
  1334. consume(_ID);
  1335. end
  1336. else
  1337. begin
  1338. pd.resultname:=stringdup(orgpattern);
  1339. consume(_ID);
  1340. end;
  1341. if not try_to_consume(_COLON) then
  1342. begin
  1343. consume(_COLON);
  1344. pd.returndef:=generrordef;
  1345. consume_all_until(_SEMICOLON);
  1346. end
  1347. else
  1348. begin
  1349. read_returndef(pd);
  1350. { check that class operators have either return type of structure or }
  1351. { at least one argument of that type }
  1352. if (po_classmethod in pd.procoptions) and
  1353. (pd.returndef <> pd.struct) then
  1354. begin
  1355. found:=false;
  1356. for i := 0 to pd.parast.SymList.Count - 1 do
  1357. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1358. begin
  1359. found:=true;
  1360. break;
  1361. end;
  1362. if not found then
  1363. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName);
  1364. end;
  1365. if (optoken in [_EQ,_NE,_GT,_LT,_GTE,_LTE,_OP_IN]) and
  1366. ((pd.returndef.typ<>orddef) or
  1367. (torddef(pd.returndef).ordtype<>pasbool)) then
  1368. Message(parser_e_comparative_operator_return_boolean);
  1369. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1370. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1371. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1372. message(parser_e_no_such_assignment)
  1373. else if not isoperatoracceptable(pd,optoken) then
  1374. Message(parser_e_overload_impossible);
  1375. end;
  1376. end
  1377. else
  1378. begin
  1379. { recover }
  1380. try_to_consume(_ID);
  1381. consume(_COLON);
  1382. consume_all_until(_SEMICOLON);
  1383. end;
  1384. end;
  1385. end;
  1386. { file types can't be function results }
  1387. if assigned(pd) and
  1388. (pd.returndef.typ=filedef) then
  1389. message(parser_e_illegal_function_result);
  1390. { support procedure proc stdcall export; }
  1391. if not(check_proc_directive(false)) then
  1392. begin
  1393. if (token=_COLON) then
  1394. begin
  1395. message(parser_e_field_not_allowed_here);
  1396. consume_all_until(_SEMICOLON);
  1397. end;
  1398. consume(_SEMICOLON);
  1399. end;
  1400. result:=pd;
  1401. if locationstr<>'' then
  1402. begin
  1403. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1404. { I guess this needs a new message... (KB) }
  1405. message(parser_e_illegal_explicit_paraloc);
  1406. end;
  1407. end;
  1408. {****************************************************************************
  1409. Procedure directive handlers
  1410. ****************************************************************************}
  1411. procedure pd_far(pd:tabstractprocdef);
  1412. begin
  1413. Message1(parser_w_proc_directive_ignored,'FAR');
  1414. end;
  1415. procedure pd_near(pd:tabstractprocdef);
  1416. begin
  1417. Message1(parser_w_proc_directive_ignored,'NEAR');
  1418. end;
  1419. procedure pd_export(pd:tabstractprocdef);
  1420. begin
  1421. if pd.typ<>procdef then
  1422. internalerror(200304264);
  1423. if assigned(tprocdef(pd).struct) then
  1424. Message(parser_e_methods_dont_be_export);
  1425. if pd.parast.symtablelevel>normal_function_level then
  1426. Message(parser_e_dont_nest_export);
  1427. end;
  1428. procedure pd_forward(pd:tabstractprocdef);
  1429. begin
  1430. if pd.typ<>procdef then
  1431. internalerror(200304265);
  1432. tprocdef(pd).forwarddef:=true;
  1433. end;
  1434. procedure pd_alias(pd:tabstractprocdef);
  1435. begin
  1436. if pd.typ<>procdef then
  1437. internalerror(200304266);
  1438. consume(_COLON);
  1439. tprocdef(pd).aliasnames.insert(get_stringconst);
  1440. include(pd.procoptions,po_has_public_name);
  1441. end;
  1442. procedure pd_public(pd:tabstractprocdef);
  1443. begin
  1444. if pd.typ<>procdef then
  1445. internalerror(200304266);
  1446. if try_to_consume(_NAME) then
  1447. begin
  1448. tprocdef(pd).aliasnames.insert(get_stringconst);
  1449. include(pd.procoptions,po_has_public_name);
  1450. end;
  1451. end;
  1452. procedure pd_asmname(pd:tabstractprocdef);
  1453. begin
  1454. if pd.typ<>procdef then
  1455. internalerror(200304267);
  1456. if token=_CCHAR then
  1457. begin
  1458. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1459. consume(_CCHAR)
  1460. end
  1461. else
  1462. begin
  1463. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1464. consume(_CSTRING);
  1465. end;
  1466. { we don't need anything else }
  1467. tprocdef(pd).forwarddef:=false;
  1468. end;
  1469. procedure pd_internconst(pd:tabstractprocdef);
  1470. var v:Tconstexprint;
  1471. begin
  1472. if pd.typ<>procdef then
  1473. internalerror(200304268);
  1474. consume(_COLON);
  1475. v:=get_intconst;
  1476. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1477. message(parser_e_range_check_error)
  1478. else
  1479. Tprocdef(pd).extnumber:=longint(v.svalue);
  1480. end;
  1481. procedure pd_internproc(pd:tabstractprocdef);
  1482. var v:Tconstexprint;
  1483. begin
  1484. if pd.typ<>procdef then
  1485. internalerror(200304268);
  1486. consume(_COLON);
  1487. v:=get_intconst;
  1488. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1489. message(parser_e_range_check_error)
  1490. else
  1491. Tprocdef(pd).extnumber:=longint(v.svalue);
  1492. { the proc is defined }
  1493. tprocdef(pd).forwarddef:=false;
  1494. end;
  1495. procedure pd_interrupt(pd:tabstractprocdef);
  1496. var v: Tconstexprint;
  1497. begin
  1498. if pd.parast.symtablelevel>normal_function_level then
  1499. Message(parser_e_dont_nest_interrupt);
  1500. if target_info.system in systems_interrupt_table then
  1501. begin
  1502. if token<>_SEMICOLON then
  1503. begin
  1504. pd.proccalloption:=pocall_interrupt;
  1505. v:=get_intconst;
  1506. Tprocdef(pd).interruptvector:=v.uvalue;
  1507. end;
  1508. end;
  1509. end;
  1510. procedure pd_abstract(pd:tabstractprocdef);
  1511. begin
  1512. if pd.typ<>procdef then
  1513. internalerror(200304269);
  1514. if assigned(tprocdef(pd).struct) and
  1515. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1516. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1517. else
  1518. if (po_virtualmethod in pd.procoptions) then
  1519. include(pd.procoptions,po_abstractmethod)
  1520. else
  1521. Message(parser_e_only_virtual_methods_abstract);
  1522. { the method is defined }
  1523. tprocdef(pd).forwarddef:=false;
  1524. end;
  1525. procedure pd_final(pd:tabstractprocdef);
  1526. begin
  1527. if pd.typ<>procdef then
  1528. internalerror(200910170);
  1529. if (po_virtualmethod in pd.procoptions) then
  1530. include(pd.procoptions,po_finalmethod)
  1531. else
  1532. Message(parser_e_only_virtual_methods_final);
  1533. end;
  1534. procedure pd_enumerator(pd:tabstractprocdef);
  1535. begin
  1536. if pd.typ<>procdef then
  1537. internalerror(200910250);
  1538. if (token = _ID) then
  1539. begin
  1540. if pattern='MOVENEXT' then
  1541. begin
  1542. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1543. message(parser_e_only_one_enumerator_movenext);
  1544. pd.calcparas;
  1545. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1546. (pd.minparacount = 0) then
  1547. begin
  1548. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1549. include(pd.procoptions,po_enumerator_movenext);
  1550. end
  1551. else
  1552. Message(parser_e_enumerator_movenext_is_not_valid)
  1553. end
  1554. else
  1555. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1556. consume(token);
  1557. end
  1558. else
  1559. Message(parser_e_enumerator_identifier_required);
  1560. end;
  1561. procedure pd_virtual(pd:tabstractprocdef);
  1562. {$ifdef WITHDMT}
  1563. var
  1564. pt : tnode;
  1565. {$endif WITHDMT}
  1566. begin
  1567. if pd.typ<>procdef then
  1568. internalerror(2003042610);
  1569. if (pd.proctypeoption=potype_constructor) and
  1570. is_object(tprocdef(pd).struct) then
  1571. Message(parser_e_constructor_cannot_be_not_virtual);
  1572. {$ifdef WITHDMT}
  1573. if is_object(tprocdef(pd).struct) and
  1574. (token<>_SEMICOLON) then
  1575. begin
  1576. { any type of parameter is allowed here! }
  1577. pt:=comp_expr(true);
  1578. if is_constintnode(pt) then
  1579. begin
  1580. include(pd.procoptions,po_msgint);
  1581. pd.messageinf.i:=pt.value;
  1582. end
  1583. else
  1584. Message(parser_e_ill_msg_expr);
  1585. disposetree(pt);
  1586. end;
  1587. {$endif WITHDMT}
  1588. end;
  1589. procedure pd_dispid(pd:tabstractprocdef);
  1590. var pt:Tnode;
  1591. begin
  1592. if pd.typ<>procdef then
  1593. internalerror(200604301);
  1594. pt:=comp_expr(true,false);
  1595. if is_constintnode(pt) then
  1596. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1597. message(parser_e_range_check_error)
  1598. else
  1599. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1600. else
  1601. message(parser_e_dispid_must_be_ord_const);
  1602. pt.free;
  1603. end;
  1604. procedure pd_static(pd:tabstractprocdef);
  1605. begin
  1606. if pd.typ=procdef then
  1607. include(tprocdef(pd).procsym.symoptions,sp_static);
  1608. include(pd.procoptions,po_staticmethod);
  1609. end;
  1610. procedure pd_override(pd:tabstractprocdef);
  1611. begin
  1612. if pd.typ<>procdef then
  1613. internalerror(2003042611);
  1614. if not(is_class_or_interface_or_objc(tprocdef(pd).struct)) then
  1615. Message(parser_e_no_object_override)
  1616. else if is_objccategory(tprocdef(pd).struct) then
  1617. Message(parser_e_no_category_override)
  1618. else if not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1619. not is_cppclass(tprocdef(pd).struct) and
  1620. (po_external in pd.procoptions) then
  1621. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1622. end;
  1623. procedure pd_overload(pd:tabstractprocdef);
  1624. begin
  1625. if pd.typ<>procdef then
  1626. internalerror(2003042612);
  1627. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1628. end;
  1629. procedure pd_message(pd:tabstractprocdef);
  1630. var
  1631. pt : tnode;
  1632. paracnt : longint;
  1633. begin
  1634. if pd.typ<>procdef then
  1635. internalerror(2003042613);
  1636. if not is_class(tprocdef(pd).struct) and
  1637. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1638. Message(parser_e_msg_only_for_classes);
  1639. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1640. Message(parser_e_multiple_messages);
  1641. { check parameter type }
  1642. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1643. begin
  1644. if po_external in pd.procoptions then
  1645. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1646. paracnt:=0;
  1647. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1648. if paracnt<>1 then
  1649. Message(parser_e_ill_msg_param);
  1650. end;
  1651. pt:=comp_expr(true,false);
  1652. { message is 1-character long }
  1653. if is_constcharnode(pt) then
  1654. begin
  1655. include(pd.procoptions,po_msgstr);
  1656. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1657. end
  1658. else if pt.nodetype=stringconstn then
  1659. begin
  1660. include(pd.procoptions,po_msgstr);
  1661. if (tstringconstnode(pt).len>255) then
  1662. Message(parser_e_message_string_too_long);
  1663. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1664. end
  1665. else
  1666. if is_constintnode(pt) and
  1667. is_class(tprocdef(pd).struct) then
  1668. begin
  1669. include(pd.procoptions,po_msgint);
  1670. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1671. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1672. message(parser_e_range_check_error)
  1673. else
  1674. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1675. end
  1676. else
  1677. Message(parser_e_ill_msg_expr);
  1678. { check whether the selector name is valid in case of Objective-C }
  1679. if (po_msgstr in pd.procoptions) and
  1680. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1681. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1682. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1683. pt.free;
  1684. end;
  1685. procedure pd_reintroduce(pd:tabstractprocdef);
  1686. begin
  1687. if pd.typ<>procdef then
  1688. internalerror(200401211);
  1689. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1690. not(is_objccategory(tprocdef(pd).struct)) then
  1691. Message(parser_e_no_object_reintroduce);
  1692. end;
  1693. procedure pd_syscall(pd:tabstractprocdef);
  1694. {$if defined(powerpc) or defined(m68k)}
  1695. var
  1696. vs : tparavarsym;
  1697. sym : tsym;
  1698. symtable : TSymtable;
  1699. v: Tconstexprint;
  1700. {$endif defined(powerpc) or defined(m68k)}
  1701. begin
  1702. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1703. internalerror(2003042614);
  1704. tprocdef(pd).forwarddef:=false;
  1705. {$ifdef m68k}
  1706. if target_info.system in [system_m68k_amiga] then
  1707. begin
  1708. include(pd.procoptions,po_syscall_legacy);
  1709. if consume_sym(sym,symtable) then
  1710. begin
  1711. if (sym.typ=staticvarsym) and
  1712. (
  1713. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1714. is_32bitint(tabstractvarsym(sym).vardef)
  1715. ) then
  1716. begin
  1717. tprocdef(pd).libsym:=sym;
  1718. if po_syscall_legacy in tprocdef(pd).procoptions then
  1719. begin
  1720. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1721. paramanager.parseparaloc(vs,'A6');
  1722. pd.parast.insert(vs);
  1723. end
  1724. end
  1725. else
  1726. Message(parser_e_32bitint_or_pointer_variable_expected);
  1727. end;
  1728. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1729. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1730. v:=get_intconst;
  1731. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1732. message(parser_e_range_check_error)
  1733. else
  1734. Tprocdef(pd).extnumber:=v.uvalue;
  1735. end;
  1736. {$endif m68k}
  1737. {$ifdef powerpc}
  1738. if target_info.system = system_powerpc_amiga then
  1739. begin
  1740. include(pd.procoptions,po_syscall_basesysv);
  1741. if consume_sym(sym,symtable) then
  1742. begin
  1743. if (sym.typ=staticvarsym) and
  1744. (
  1745. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1746. is_32bitint(tabstractvarsym(sym).vardef)
  1747. ) then
  1748. begin
  1749. tprocdef(pd).libsym:=sym;
  1750. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1751. pd.parast.insert(vs);
  1752. end
  1753. else
  1754. Message(parser_e_32bitint_or_pointer_variable_expected);
  1755. end;
  1756. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1757. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1758. v:=get_intconst;
  1759. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1760. message(parser_e_range_check_error)
  1761. else
  1762. Tprocdef(pd).extnumber:=v.uvalue;
  1763. end else
  1764. if target_info.system = system_powerpc_morphos then
  1765. begin
  1766. if idtoken=_LEGACY then
  1767. begin
  1768. consume(_LEGACY);
  1769. include(pd.procoptions,po_syscall_legacy);
  1770. end
  1771. else if idtoken=_SYSV then
  1772. begin
  1773. consume(_SYSV);
  1774. include(pd.procoptions,po_syscall_sysv);
  1775. end
  1776. else if idtoken=_BASESYSV then
  1777. begin
  1778. consume(_BASESYSV);
  1779. include(pd.procoptions,po_syscall_basesysv);
  1780. end
  1781. else if idtoken=_SYSVBASE then
  1782. begin
  1783. consume(_SYSVBASE);
  1784. include(pd.procoptions,po_syscall_sysvbase);
  1785. end
  1786. else if idtoken=_R12BASE then
  1787. begin
  1788. consume(_R12BASE);
  1789. include(pd.procoptions,po_syscall_r12base);
  1790. end
  1791. else
  1792. if syscall_convention='LEGACY' then
  1793. include(pd.procoptions,po_syscall_legacy)
  1794. else if syscall_convention='SYSV' then
  1795. include(pd.procoptions,po_syscall_sysv)
  1796. else if syscall_convention='BASESYSV' then
  1797. include(pd.procoptions,po_syscall_basesysv)
  1798. else if syscall_convention='SYSVBASE' then
  1799. include(pd.procoptions,po_syscall_sysvbase)
  1800. else if syscall_convention='R12BASE' then
  1801. include(pd.procoptions,po_syscall_r12base)
  1802. else
  1803. internalerror(2005010404);
  1804. if consume_sym(sym,symtable) then
  1805. begin
  1806. if (sym.typ=staticvarsym) and
  1807. (
  1808. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1809. is_32bitint(tabstractvarsym(sym).vardef)
  1810. ) then
  1811. begin
  1812. tprocdef(pd).libsym:=sym;
  1813. if po_syscall_legacy in tprocdef(pd).procoptions then
  1814. begin
  1815. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1816. paramanager.parseparaloc(vs,'A6');
  1817. pd.parast.insert(vs);
  1818. end
  1819. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1820. begin
  1821. { Nothing to be done for sysv here for now, but this might change }
  1822. end
  1823. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1824. begin
  1825. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1826. pd.parast.insert(vs);
  1827. end
  1828. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1829. begin
  1830. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1831. pd.parast.insert(vs);
  1832. end
  1833. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1834. begin
  1835. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1836. paramanager.parseparaloc(vs,'R12');
  1837. pd.parast.insert(vs);
  1838. end
  1839. else
  1840. internalerror(2005010501);
  1841. end
  1842. else
  1843. Message(parser_e_32bitint_or_pointer_variable_expected);
  1844. end;
  1845. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1846. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1847. v:=get_intconst;
  1848. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1849. message(parser_e_range_check_error)
  1850. else
  1851. Tprocdef(pd).extnumber:=v.uvalue;
  1852. end;
  1853. {$endif powerpc}
  1854. end;
  1855. procedure pd_external(pd:tabstractprocdef);
  1856. {
  1857. If import_dll=nil the procedure is assumed to be in another
  1858. object file. In that object file it should have the name to
  1859. which import_name is pointing to. Otherwise, the procedure is
  1860. assumed to be in the DLL to which import_dll is pointing to. In
  1861. that case either import_nr<>0 or import_name<>nil is true, so
  1862. the procedure is either imported by number or by name. (DM)
  1863. }
  1864. var
  1865. hs : string;
  1866. v:Tconstexprint;
  1867. begin
  1868. if pd.typ<>procdef then
  1869. internalerror(2003042615);
  1870. with tprocdef(pd) do
  1871. begin
  1872. forwarddef:=false;
  1873. { forbid local external procedures }
  1874. if parast.symtablelevel>normal_function_level then
  1875. Message(parser_e_no_local_proc_external);
  1876. { If the procedure should be imported from a DLL, a constant string follows.
  1877. This isn't really correct, an contant string expression follows
  1878. so we check if an semicolon follows, else a string constant have to
  1879. follow (FK) }
  1880. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1881. begin
  1882. { Always add library prefix and suffix to create an uniform name }
  1883. hs:=get_stringconst;
  1884. if ExtractFileExt(hs)='' then
  1885. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1886. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1887. hs:=target_info.sharedlibprefix+hs;
  1888. import_dll:=stringdup(hs);
  1889. include(procoptions,po_has_importdll);
  1890. if (idtoken=_NAME) then
  1891. begin
  1892. consume(_NAME);
  1893. import_name:=stringdup(get_stringconst);
  1894. include(procoptions,po_has_importname);
  1895. if import_name^='' then
  1896. message(parser_e_empty_import_name);
  1897. end;
  1898. if (idtoken=_INDEX) then
  1899. begin
  1900. {After the word index follows the index number in the DLL.}
  1901. consume(_INDEX);
  1902. v:=get_intconst;
  1903. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1904. message(parser_e_range_check_error)
  1905. else
  1906. import_nr:=longint(v.svalue);
  1907. end;
  1908. { default is to used the realname of the procedure }
  1909. if (import_nr=0) and not assigned(import_name) then
  1910. begin
  1911. import_name:=stringdup(procsym.realname);
  1912. include(procoptions,po_has_importname);
  1913. end;
  1914. end
  1915. else
  1916. begin
  1917. if (idtoken=_NAME) then
  1918. begin
  1919. consume(_NAME);
  1920. import_name:=stringdup(get_stringconst);
  1921. include(procoptions,po_has_importname);
  1922. if import_name^='' then
  1923. message(parser_e_empty_import_name);
  1924. end;
  1925. end;
  1926. end;
  1927. end;
  1928. procedure pd_weakexternal(pd:tabstractprocdef);
  1929. begin
  1930. if not(target_info.system in systems_weak_linking) then
  1931. message(parser_e_weak_external_not_supported)
  1932. else
  1933. pd_external(pd);
  1934. end;
  1935. type
  1936. pd_handler=procedure(pd:tabstractprocdef);
  1937. proc_dir_rec=record
  1938. idtok : ttoken;
  1939. pd_flags : tpdflags;
  1940. handler : pd_handler;
  1941. pocall : tproccalloption;
  1942. pooption : tprocoptions;
  1943. mutexclpocall : tproccalloptions;
  1944. mutexclpotype : tproctypeoptions;
  1945. mutexclpo : tprocoptions;
  1946. end;
  1947. const
  1948. {Should contain the number of procedure directives we support.}
  1949. num_proc_directives=42;
  1950. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1951. (
  1952. (
  1953. idtok:_ABSTRACT;
  1954. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  1955. handler : @pd_abstract;
  1956. pocall : pocall_none;
  1957. pooption : [po_abstractmethod];
  1958. mutexclpocall : [pocall_internproc];
  1959. mutexclpotype : [];
  1960. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1961. ),(
  1962. idtok:_ALIAS;
  1963. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1964. handler : @pd_alias;
  1965. pocall : pocall_none;
  1966. pooption : [];
  1967. mutexclpocall : [];
  1968. mutexclpotype : [];
  1969. mutexclpo : [po_external,po_inline]
  1970. ),(
  1971. idtok:_ASMNAME;
  1972. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1973. handler : @pd_asmname;
  1974. pocall : pocall_cdecl;
  1975. pooption : [po_external];
  1976. mutexclpocall : [pocall_internproc];
  1977. mutexclpotype : [];
  1978. mutexclpo : [po_external,po_inline]
  1979. ),(
  1980. idtok:_ASSEMBLER;
  1981. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1982. handler : nil;
  1983. pocall : pocall_none;
  1984. pooption : [po_assembler];
  1985. mutexclpocall : [];
  1986. mutexclpotype : [];
  1987. mutexclpo : [po_external]
  1988. ),(
  1989. idtok:_C; {same as cdecl for mode mac}
  1990. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1991. handler : nil;
  1992. pocall : pocall_cdecl;
  1993. pooption : [];
  1994. mutexclpocall : [];
  1995. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  1996. mutexclpo : [po_assembler,po_external]
  1997. ),(
  1998. idtok:_CDECL;
  1999. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2000. handler : nil;
  2001. pocall : pocall_cdecl;
  2002. pooption : [];
  2003. mutexclpocall : [];
  2004. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2005. mutexclpo : [po_assembler,po_external]
  2006. ),(
  2007. idtok:_DISPID;
  2008. pd_flags : [pd_dispinterface];
  2009. handler : @pd_dispid;
  2010. pocall : pocall_none;
  2011. pooption : [po_dispid];
  2012. mutexclpocall : [pocall_internproc];
  2013. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2014. mutexclpo : [po_interrupt,po_external,po_inline]
  2015. ),(
  2016. idtok:_DYNAMIC;
  2017. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2018. handler : @pd_virtual;
  2019. pocall : pocall_none;
  2020. pooption : [po_virtualmethod];
  2021. mutexclpocall : [pocall_internproc];
  2022. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2023. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2024. ),(
  2025. idtok:_EXPORT;
  2026. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord];
  2027. handler : @pd_export;
  2028. pocall : pocall_none;
  2029. pooption : [po_exports,po_global];
  2030. mutexclpocall : [pocall_internproc];
  2031. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2032. mutexclpo : [po_external,po_interrupt,po_inline]
  2033. ),(
  2034. idtok:_EXTERNAL;
  2035. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord];
  2036. handler : @pd_external;
  2037. pocall : pocall_none;
  2038. pooption : [po_external];
  2039. mutexclpocall : [pocall_internproc,pocall_syscall];
  2040. { allowed for external cpp classes }
  2041. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2042. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2043. ),(
  2044. idtok:_FAR;
  2045. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord];
  2046. handler : @pd_far;
  2047. pocall : pocall_none;
  2048. pooption : [];
  2049. mutexclpocall : [pocall_internproc];
  2050. mutexclpotype : [];
  2051. mutexclpo : [po_inline]
  2052. ),(
  2053. idtok:_FAR16;
  2054. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord];
  2055. handler : nil;
  2056. pocall : pocall_far16;
  2057. pooption : [];
  2058. mutexclpocall : [];
  2059. mutexclpotype : [];
  2060. mutexclpo : [po_external]
  2061. ),(
  2062. idtok:_FINAL;
  2063. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2064. handler : @pd_final;
  2065. pocall : pocall_none;
  2066. pooption : [po_finalmethod];
  2067. mutexclpocall : [pocall_internproc];
  2068. mutexclpotype : [];
  2069. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2070. ),(
  2071. idtok:_FORWARD;
  2072. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord];
  2073. handler : @pd_forward;
  2074. pocall : pocall_none;
  2075. pooption : [];
  2076. mutexclpocall : [pocall_internproc];
  2077. mutexclpotype : [];
  2078. mutexclpo : [po_external,po_inline]
  2079. ),(
  2080. idtok:_OLDFPCCALL;
  2081. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2082. handler : nil;
  2083. pocall : pocall_oldfpccall;
  2084. pooption : [];
  2085. mutexclpocall : [];
  2086. mutexclpotype : [];
  2087. mutexclpo : []
  2088. ),(
  2089. idtok:_INLINE;
  2090. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2091. handler : nil;
  2092. pocall : pocall_none;
  2093. pooption : [po_inline];
  2094. mutexclpocall : [];
  2095. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2096. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  2097. ),(
  2098. idtok:_INTERNCONST;
  2099. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord];
  2100. handler : @pd_internconst;
  2101. pocall : pocall_none;
  2102. pooption : [po_internconst];
  2103. mutexclpocall : [];
  2104. mutexclpotype : [potype_operator];
  2105. mutexclpo : []
  2106. ),(
  2107. idtok:_INTERNPROC;
  2108. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord];
  2109. handler : @pd_internproc;
  2110. pocall : pocall_internproc;
  2111. pooption : [];
  2112. mutexclpocall : [];
  2113. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2114. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2115. ),(
  2116. idtok:_INTERRUPT;
  2117. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord];
  2118. handler : @pd_interrupt;
  2119. pocall : pocall_oldfpccall;
  2120. pooption : [po_interrupt];
  2121. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  2122. pocall_pascal,pocall_far16,pocall_oldfpccall];
  2123. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2124. mutexclpo : [po_external,po_inline]
  2125. ),(
  2126. idtok:_IOCHECK;
  2127. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2128. handler : nil;
  2129. pocall : pocall_none;
  2130. pooption : [po_iocheck];
  2131. mutexclpocall : [pocall_internproc];
  2132. mutexclpotype : [];
  2133. mutexclpo : [po_external]
  2134. ),(
  2135. idtok:_LOCAL;
  2136. pd_flags : [pd_implemen,pd_body];
  2137. handler : nil;
  2138. pocall : pocall_none;
  2139. pooption : [po_kylixlocal];
  2140. mutexclpocall : [pocall_internproc,pocall_far16];
  2141. mutexclpotype : [];
  2142. mutexclpo : [po_external,po_exports]
  2143. ),(
  2144. idtok:_MESSAGE;
  2145. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2146. handler : @pd_message;
  2147. pocall : pocall_none;
  2148. pooption : []; { can be po_msgstr or po_msgint }
  2149. mutexclpocall : [pocall_internproc];
  2150. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2151. mutexclpo : [po_interrupt,po_inline]
  2152. ),(
  2153. idtok:_MWPASCAL;
  2154. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2155. handler : nil;
  2156. pocall : pocall_mwpascal;
  2157. pooption : [];
  2158. mutexclpocall : [];
  2159. mutexclpotype : [];
  2160. mutexclpo : []
  2161. ),(
  2162. idtok:_NEAR;
  2163. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord];
  2164. handler : @pd_near;
  2165. pocall : pocall_none;
  2166. pooption : [];
  2167. mutexclpocall : [pocall_internproc];
  2168. mutexclpotype : [];
  2169. mutexclpo : []
  2170. ),(
  2171. idtok:_NOSTACKFRAME;
  2172. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2173. handler : nil;
  2174. pocall : pocall_none;
  2175. pooption : [po_nostackframe];
  2176. mutexclpocall : [pocall_internproc];
  2177. mutexclpotype : [];
  2178. mutexclpo : []
  2179. ),(
  2180. idtok:_OVERLOAD;
  2181. pd_flags : [pd_implemen,pd_interface,pd_body];
  2182. handler : @pd_overload;
  2183. pocall : pocall_none;
  2184. pooption : [po_overload];
  2185. mutexclpocall : [pocall_internproc];
  2186. mutexclpotype : [];
  2187. mutexclpo : []
  2188. ),(
  2189. idtok:_OVERRIDE;
  2190. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord];
  2191. handler : @pd_override;
  2192. pocall : pocall_none;
  2193. pooption : [po_overridingmethod,po_virtualmethod];
  2194. mutexclpocall : [pocall_internproc];
  2195. mutexclpotype : [];
  2196. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2197. ),(
  2198. idtok:_PASCAL;
  2199. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2200. handler : nil;
  2201. pocall : pocall_pascal;
  2202. pooption : [];
  2203. mutexclpocall : [];
  2204. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2205. mutexclpo : [po_external]
  2206. ),(
  2207. idtok:_PUBLIC;
  2208. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord];
  2209. handler : @pd_public;
  2210. pocall : pocall_none;
  2211. pooption : [po_public,po_global];
  2212. mutexclpocall : [pocall_internproc];
  2213. mutexclpotype : [];
  2214. mutexclpo : [po_external,po_inline]
  2215. ),(
  2216. idtok:_REGISTER;
  2217. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2218. handler : nil;
  2219. pocall : pocall_register;
  2220. pooption : [];
  2221. mutexclpocall : [];
  2222. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2223. mutexclpo : [po_external]
  2224. ),(
  2225. idtok:_REINTRODUCE;
  2226. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord];
  2227. handler : @pd_reintroduce;
  2228. pocall : pocall_none;
  2229. pooption : [po_reintroduce];
  2230. mutexclpocall : [pocall_internproc];
  2231. mutexclpotype : [];
  2232. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2233. ),(
  2234. idtok:_SAFECALL;
  2235. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2236. handler : nil;
  2237. pocall : pocall_safecall;
  2238. pooption : [];
  2239. mutexclpocall : [];
  2240. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2241. mutexclpo : [po_external]
  2242. ),(
  2243. idtok:_SOFTFLOAT;
  2244. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2245. handler : nil;
  2246. pocall : pocall_softfloat;
  2247. pooption : [];
  2248. mutexclpocall : [];
  2249. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2250. { it's available with po_external because the libgcc floating point routines on the arm
  2251. uses this calling convention }
  2252. mutexclpo : []
  2253. ),(
  2254. idtok:_STATIC;
  2255. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_notobjintf];
  2256. handler : @pd_static;
  2257. pocall : pocall_none;
  2258. pooption : [po_staticmethod];
  2259. mutexclpocall : [pocall_internproc];
  2260. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2261. mutexclpo : [po_external,po_interrupt,po_exports]
  2262. ),(
  2263. idtok:_STDCALL;
  2264. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2265. handler : nil;
  2266. pocall : pocall_stdcall;
  2267. pooption : [];
  2268. mutexclpocall : [];
  2269. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2270. mutexclpo : [po_external]
  2271. ),(
  2272. idtok:_SYSCALL;
  2273. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2274. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2275. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2276. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2277. handler : @pd_syscall;
  2278. pocall : pocall_syscall;
  2279. pooption : [];
  2280. mutexclpocall : [];
  2281. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2282. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2283. ),(
  2284. idtok:_VIRTUAL;
  2285. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2286. handler : @pd_virtual;
  2287. pocall : pocall_none;
  2288. pooption : [po_virtualmethod];
  2289. mutexclpocall : [pocall_internproc];
  2290. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2291. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2292. ),(
  2293. idtok:_CPPDECL;
  2294. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2295. handler : nil;
  2296. pocall : pocall_cppdecl;
  2297. pooption : [];
  2298. mutexclpocall : [];
  2299. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2300. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2301. ),(
  2302. idtok:_VARARGS;
  2303. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2304. handler : nil;
  2305. pocall : pocall_none;
  2306. pooption : [po_varargs];
  2307. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2308. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2309. mutexclpotype : [];
  2310. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2311. ),(
  2312. idtok:_COMPILERPROC;
  2313. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2314. handler : nil;
  2315. pocall : pocall_none;
  2316. pooption : [po_compilerproc];
  2317. mutexclpocall : [];
  2318. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2319. mutexclpo : [po_interrupt]
  2320. ),(
  2321. idtok:_WEAKEXTERNAL;
  2322. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord];
  2323. handler : @pd_weakexternal;
  2324. pocall : pocall_none;
  2325. { mark it both external and weak external, so we don't have to
  2326. adapt all code for external symbols to also check for weak external
  2327. }
  2328. pooption : [po_external,po_weakexternal];
  2329. mutexclpocall : [pocall_internproc,pocall_syscall];
  2330. { allowed for external cpp classes }
  2331. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2332. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2333. ),(
  2334. idtok:_ENUMERATOR;
  2335. pd_flags : [pd_interface,pd_object,pd_record];
  2336. handler : @pd_enumerator;
  2337. pocall : pocall_none;
  2338. pooption : [];
  2339. mutexclpocall : [pocall_internproc];
  2340. mutexclpotype : [];
  2341. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2342. )
  2343. );
  2344. function check_proc_directive(isprocvar:boolean):boolean;
  2345. var
  2346. i : longint;
  2347. begin
  2348. result:=false;
  2349. for i:=1 to num_proc_directives do
  2350. if proc_direcdata[i].idtok=idtoken then
  2351. begin
  2352. if ((not isprocvar) or
  2353. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2354. { don't eat a public directive in classes }
  2355. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2356. result:=true;
  2357. exit;
  2358. end;
  2359. end;
  2360. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2361. {
  2362. Parse the procedure directive, returns true if a correct directive is found
  2363. }
  2364. var
  2365. p : longint;
  2366. found : boolean;
  2367. name : TIDString;
  2368. begin
  2369. parse_proc_direc:=false;
  2370. name:=tokeninfo^[idtoken].str;
  2371. found:=false;
  2372. { Hint directive? Then exit immediatly }
  2373. if (m_hintdirective in current_settings.modeswitches) then
  2374. begin
  2375. case idtoken of
  2376. _LIBRARY,
  2377. _PLATFORM,
  2378. _UNIMPLEMENTED,
  2379. _EXPERIMENTAL,
  2380. _DEPRECATED :
  2381. exit;
  2382. end;
  2383. end;
  2384. { C directive is MacPas only, because it breaks too much existing code
  2385. on other platforms (PFV) }
  2386. if (idtoken=_C) and
  2387. not(m_mac in current_settings.modeswitches) then
  2388. exit;
  2389. { retrieve data for directive if found }
  2390. for p:=1 to num_proc_directives do
  2391. if proc_direcdata[p].idtok=idtoken then
  2392. begin
  2393. found:=true;
  2394. break;
  2395. end;
  2396. { Check if the procedure directive is known }
  2397. if not found then
  2398. begin
  2399. { parsing a procvar type the name can be any
  2400. next variable !! }
  2401. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2402. not(idtoken=_PROPERTY) then
  2403. Message1(parser_w_unknown_proc_directive_ignored,name);
  2404. exit;
  2405. end;
  2406. { check if method and directive not for object, like public.
  2407. This needs to be checked also for procvars }
  2408. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2409. (symtablestack.top.symtabletype=ObjectSymtable) and
  2410. { directive allowed for cpp classes? }
  2411. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  2412. exit;
  2413. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2414. (symtablestack.top.symtabletype=recordsymtable) then
  2415. exit;
  2416. { Conflicts between directives ? }
  2417. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2418. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2419. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2420. begin
  2421. Message1(parser_e_proc_dir_conflict,name);
  2422. exit;
  2423. end;
  2424. { set calling convention }
  2425. if proc_direcdata[p].pocall<>pocall_none then
  2426. begin
  2427. if (po_hascallingconvention in pd.procoptions) then
  2428. begin
  2429. Message2(parser_w_proc_overriding_calling,
  2430. proccalloptionStr[pd.proccalloption],
  2431. proccalloptionStr[proc_direcdata[p].pocall]);
  2432. end;
  2433. { check if the target processor supports this calling convention }
  2434. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2435. begin
  2436. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2437. { recover }
  2438. proc_direcdata[p].pocall:=pocall_stdcall;
  2439. end;
  2440. pd.proccalloption:=proc_direcdata[p].pocall;
  2441. include(pd.procoptions,po_hascallingconvention);
  2442. end;
  2443. if pd.typ=procdef then
  2444. begin
  2445. { Check if the directive is only for objects }
  2446. if (pd_object in proc_direcdata[p].pd_flags) and
  2447. not assigned(tprocdef(pd).struct) then
  2448. exit;
  2449. { Check if the directive is only for records }
  2450. if (pd_record in proc_direcdata[p].pd_flags) and
  2451. not assigned(tprocdef(pd).struct) then
  2452. exit;
  2453. { check if method and directive not for interface }
  2454. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2455. is_interface(tprocdef(pd).struct) then
  2456. exit;
  2457. { check if method and directive not for interface }
  2458. if is_dispinterface(tprocdef(pd).struct) and
  2459. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2460. exit;
  2461. { check if method and directive not for objcclass }
  2462. if is_objcclass(tprocdef(pd).struct) and
  2463. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2464. exit;
  2465. { check if method and directive not for objcprotocol }
  2466. if is_objcprotocol(tprocdef(pd).struct) and
  2467. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2468. exit;
  2469. end;
  2470. { consume directive, and turn flag on }
  2471. consume(token);
  2472. parse_proc_direc:=true;
  2473. { Check the pd_flags if the directive should be allowed }
  2474. if (pd_interface in pdflags) and
  2475. not(pd_interface in proc_direcdata[p].pd_flags) then
  2476. begin
  2477. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2478. exit;
  2479. end;
  2480. if (pd_implemen in pdflags) and
  2481. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2482. begin
  2483. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2484. exit;
  2485. end;
  2486. if (pd_procvar in pdflags) and
  2487. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2488. begin
  2489. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2490. exit;
  2491. end;
  2492. { Return the new pd_flags }
  2493. if not(pd_body in proc_direcdata[p].pd_flags) then
  2494. exclude(pdflags,pd_body);
  2495. { Add the correct flag }
  2496. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2497. { Call the handler }
  2498. if pointer(proc_direcdata[p].handler)<>nil then
  2499. proc_direcdata[p].handler(pd);
  2500. end;
  2501. function proc_get_importname(pd:tprocdef):string;
  2502. function maybe_cprefix(const s:string):string;
  2503. begin
  2504. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  2505. result:=s
  2506. else
  2507. result:=target_info.Cprefix+s;
  2508. end;
  2509. begin
  2510. result:='';
  2511. if not(po_external in pd.procoptions) then
  2512. internalerror(200412151);
  2513. { external name or number is specified }
  2514. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2515. begin
  2516. if assigned(pd.import_dll) then
  2517. begin
  2518. { If we are not using direct dll linking under win32 then imports
  2519. need to use the normal name since two functions can refer to the
  2520. same DLL function. This is also needed for compatability
  2521. with Delphi and TP7 }
  2522. (*
  2523. case target_info.system of
  2524. system_i386_emx,
  2525. system_i386_os2 :
  2526. begin
  2527. { keep normal mangledname }
  2528. if not (Assigned (PD.Import_Name)) then
  2529. Result := PD.MangledName;
  2530. end;
  2531. else
  2532. *)
  2533. if assigned(pd.import_name) then
  2534. begin
  2535. if target_info.system in (systems_all_windows + systems_nativent +
  2536. [system_i386_emx, system_i386_os2]) then
  2537. { cprefix is not used in DLL imports under Windows or OS/2 }
  2538. result:=pd.import_name^
  2539. else
  2540. result:=maybe_cprefix(pd.import_name^);
  2541. end
  2542. else
  2543. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2544. end
  2545. else
  2546. result:=maybe_cprefix(pd.import_name^);
  2547. end
  2548. else
  2549. begin
  2550. { Default names when importing variables }
  2551. case pd.proccalloption of
  2552. pocall_cdecl :
  2553. begin
  2554. if assigned(pd.struct) then
  2555. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2556. else
  2557. result:=target_info.Cprefix+pd.procsym.realname;
  2558. end;
  2559. pocall_cppdecl :
  2560. begin
  2561. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2562. end;
  2563. else
  2564. begin
  2565. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2566. { but according to MacPas mode description
  2567. Cprefix should still be used PM }
  2568. if (m_mac in current_settings.modeswitches) then
  2569. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2570. end;
  2571. end;
  2572. end;
  2573. end;
  2574. procedure proc_set_mangledname(pd:tprocdef);
  2575. var
  2576. s : string;
  2577. begin
  2578. { When the mangledname is already set we aren't allowed to change
  2579. it because it can already be used somewhere (PFV) }
  2580. if not(po_has_mangledname in pd.procoptions) then
  2581. begin
  2582. if (po_external in pd.procoptions) then
  2583. begin
  2584. { External Procedures are only allowed to change the mangledname
  2585. in their first declaration }
  2586. if (pd.forwarddef or (not pd.hasforward)) then
  2587. begin
  2588. s:=proc_get_importname(pd);
  2589. if s<>'' then
  2590. begin
  2591. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  2592. { This allows to import VC++ mangled names from DLLs. }
  2593. { Do not perform replacement, if external symbol is not imported from DLL. }
  2594. if (target_info.system in systems_all_windows) and (pd.import_dll<>nil) then
  2595. begin
  2596. Replace(s,'?','__q$$');
  2597. {$ifdef arm}
  2598. { @ symbol is not allowed in ARM assembler only }
  2599. Replace(s,'@','__a$$');
  2600. {$endif arm}
  2601. end;
  2602. pd.setmangledname(s);
  2603. end;
  2604. end;
  2605. end
  2606. else
  2607. { Normal procedures }
  2608. begin
  2609. if (po_compilerproc in pd.procoptions) then
  2610. begin
  2611. pd.setmangledname(lower(pd.procsym.name));
  2612. end;
  2613. end;
  2614. end;
  2615. { Public/exported alias names }
  2616. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2617. not(po_has_public_name in pd.procoptions) then
  2618. begin
  2619. case pd.proccalloption of
  2620. pocall_cdecl :
  2621. begin
  2622. if assigned(pd.struct) then
  2623. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2624. else
  2625. begin
  2626. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2627. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2628. (po_exports in pd.procoptions) then
  2629. pd.aliasnames.insert(pd.procsym.realname)
  2630. else
  2631. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2632. end;
  2633. end;
  2634. pocall_cppdecl :
  2635. begin
  2636. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2637. end;
  2638. end;
  2639. { prevent adding the alias a second time }
  2640. include(pd.procoptions,po_has_public_name);
  2641. end;
  2642. end;
  2643. procedure handle_calling_convention(pd:tabstractprocdef);
  2644. begin
  2645. { set the default calling convention if none provided }
  2646. if (pd.typ=procdef) and
  2647. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2648. is_cppclass(tprocdef(pd).struct)) then
  2649. begin
  2650. { none of the explicit calling conventions should be allowed }
  2651. if (po_hascallingconvention in pd.procoptions) then
  2652. internalerror(2009032501);
  2653. if is_cppclass(tprocdef(pd).struct) then
  2654. pd.proccalloption:=pocall_cppdecl
  2655. else
  2656. pd.proccalloption:=pocall_cdecl;
  2657. end
  2658. else if not(po_hascallingconvention in pd.procoptions) then
  2659. pd.proccalloption:=current_settings.defproccall
  2660. else
  2661. begin
  2662. if pd.proccalloption=pocall_none then
  2663. internalerror(200309081);
  2664. end;
  2665. { handle proccall specific settings }
  2666. case pd.proccalloption of
  2667. pocall_cdecl,
  2668. pocall_cppdecl :
  2669. begin
  2670. { check C cdecl para types }
  2671. check_c_para(pd);
  2672. end;
  2673. pocall_far16 :
  2674. begin
  2675. { Temporary stub, must be rewritten to support OS/2 far16 }
  2676. Message1(parser_w_proc_directive_ignored,'FAR16');
  2677. end;
  2678. end;
  2679. { Inlining is enabled and supported? }
  2680. if (po_inline in pd.procoptions) and
  2681. not(cs_do_inline in current_settings.localswitches) then
  2682. begin
  2683. { Give an error if inline is not supported by the compiler mode,
  2684. otherwise only give a warning that this procedure will not be inlined }
  2685. if not(m_default_inline in current_settings.modeswitches) then
  2686. Message(parser_e_proc_inline_not_supported)
  2687. else
  2688. Message(parser_w_inlining_disabled);
  2689. exclude(pd.procoptions,po_inline);
  2690. end;
  2691. { For varargs directive also cdecl and external must be defined }
  2692. if (po_varargs in pd.procoptions) then
  2693. begin
  2694. { check first for external in the interface, if available there
  2695. then the cdecl must also be there since there is no implementation
  2696. available to contain it }
  2697. if parse_only then
  2698. begin
  2699. { if external is available, then cdecl must also be available,
  2700. procvars don't need external }
  2701. if not((po_external in pd.procoptions) or
  2702. (pd.typ=procvardef) or
  2703. { for objcclasses this is checked later, because the entire
  2704. class may be external. }
  2705. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2706. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2707. Message(parser_e_varargs_need_cdecl_and_external);
  2708. end
  2709. else
  2710. begin
  2711. { both must be defined now }
  2712. if not((po_external in pd.procoptions) or
  2713. (pd.typ=procvardef)) or
  2714. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2715. Message(parser_e_varargs_need_cdecl_and_external);
  2716. end;
  2717. end;
  2718. { insert hidden high parameters }
  2719. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2720. { insert hidden self parameter }
  2721. insert_self_and_vmt_para(pd);
  2722. { insert funcret parameter if required }
  2723. insert_funcret_para(pd);
  2724. { Make var parameters regable, this must be done after the calling
  2725. convention is set. }
  2726. { this must be done before parentfp is insert, because getting all cases
  2727. where parentfp must be in a memory location isn't catched properly so
  2728. we put parentfp never in a register }
  2729. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2730. { insert parentfp parameter if required }
  2731. insert_parentfp_para(pd);
  2732. { Calculate parameter tlist }
  2733. pd.calcparas;
  2734. end;
  2735. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2736. {
  2737. Parse the procedure directives. It does not matter if procedure directives
  2738. are written using ;procdir; or ['procdir'] syntax.
  2739. }
  2740. var
  2741. res : boolean;
  2742. begin
  2743. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2744. begin
  2745. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2746. include(pd.procoptions,po_public);
  2747. include(pd.procoptions,po_has_public_name);
  2748. include(pd.procoptions,po_global);
  2749. end;
  2750. { methods from external class definitions are all external themselves }
  2751. if (pd.typ=procdef) and
  2752. assigned(tprocdef(pd).struct) and
  2753. (tprocdef(pd).struct.typ=objectdef) and
  2754. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  2755. tprocdef(pd).make_external;
  2756. { Class constructors and destructor are static class methods in real. }
  2757. { There are many places in the compiler where either class or static }
  2758. { method flag changes the behavior. It is simplier to add them to }
  2759. { the class constructors/destructors options than to fix all the }
  2760. { occurencies. (Paul) }
  2761. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2762. begin
  2763. include(pd.procoptions,po_classmethod);
  2764. include(pd.procoptions,po_staticmethod);
  2765. end;
  2766. while token in [_ID,_LECKKLAMMER] do
  2767. begin
  2768. if try_to_consume(_LECKKLAMMER) then
  2769. begin
  2770. repeat
  2771. parse_proc_direc(pd,pdflags);
  2772. until not try_to_consume(_COMMA);
  2773. consume(_RECKKLAMMER);
  2774. { we always expect at least '[];' }
  2775. res:=true;
  2776. end
  2777. else
  2778. begin
  2779. res:=parse_proc_direc(pd,pdflags);
  2780. end;
  2781. { A procedure directive normally followed by a semicolon, but in
  2782. a const section or reading a type we should stop when _EQ is found,
  2783. because a constant/default value follows }
  2784. if res then
  2785. begin
  2786. if (block_type=bt_const_type) and
  2787. (token=_EQ) then
  2788. break;
  2789. { support procedure proc;stdcall export; }
  2790. if not(check_proc_directive((pd.typ=procvardef))) then
  2791. begin
  2792. { support "record p : procedure stdcall end;" and
  2793. "var p : procedure stdcall = nil;" }
  2794. if (pd_procvar in pdflags) and
  2795. (token in [_END,_RKLAMMER,_EQ]) then
  2796. break
  2797. else
  2798. begin
  2799. if (token=_COLON) then
  2800. begin
  2801. Message(parser_e_field_not_allowed_here);
  2802. consume_all_until(_SEMICOLON);
  2803. end;
  2804. consume(_SEMICOLON)
  2805. end;
  2806. end;
  2807. end
  2808. else
  2809. break;
  2810. end;
  2811. end;
  2812. procedure parse_var_proc_directives(sym:tsym);
  2813. var
  2814. pdflags : tpdflags;
  2815. pd : tabstractprocdef;
  2816. begin
  2817. pdflags:=[pd_procvar];
  2818. pd:=nil;
  2819. case sym.typ of
  2820. fieldvarsym,
  2821. staticvarsym,
  2822. localvarsym,
  2823. paravarsym :
  2824. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2825. typesym :
  2826. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2827. else
  2828. internalerror(2003042617);
  2829. end;
  2830. if pd.typ<>procvardef then
  2831. internalerror(2003042618);
  2832. { names should never be used anyway }
  2833. parse_proc_directives(pd,pdflags);
  2834. end;
  2835. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2836. var
  2837. pdflags : tpdflags;
  2838. begin
  2839. pdflags:=[pd_object];
  2840. parse_proc_directives(pd,pdflags);
  2841. end;
  2842. procedure parse_record_proc_directives(pd:tabstractprocdef);
  2843. var
  2844. pdflags : tpdflags;
  2845. begin
  2846. pdflags:=[pd_record];
  2847. parse_proc_directives(pd,pdflags);
  2848. end;
  2849. function proc_add_definition(var currpd:tprocdef):boolean;
  2850. {
  2851. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2852. forwarddef is found and reused it returns true
  2853. }
  2854. var
  2855. fwpd : tprocdef;
  2856. currparasym,
  2857. fwparasym : tsym;
  2858. currparacnt,
  2859. fwparacnt,
  2860. curridx,
  2861. fwidx,
  2862. i : longint;
  2863. po_comp : tprocoptions;
  2864. paracompopt: tcompare_paras_options;
  2865. forwardfound : boolean;
  2866. symentry: TSymEntry;
  2867. begin
  2868. forwardfound:=false;
  2869. { check overloaded functions if the same function already exists }
  2870. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2871. begin
  2872. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2873. { Skip overloaded definitions that are declared in other units }
  2874. if fwpd.procsym<>currpd.procsym then
  2875. continue;
  2876. { check the parameters, for delphi/tp it is possible to
  2877. leave the parameters away in the implementation (forwarddef=false).
  2878. But for an overload declared function this is not allowed }
  2879. if { check if empty implementation arguments match is allowed }
  2880. (
  2881. not(m_repeat_forward in current_settings.modeswitches) and
  2882. not(currpd.forwarddef) and
  2883. is_bareprocdef(currpd) and
  2884. not(po_overload in fwpd.procoptions)
  2885. ) or
  2886. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2887. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2888. (
  2889. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2890. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  2891. ) then
  2892. begin
  2893. { Check if we've found the forwarddef, if found then
  2894. we need to update the forward def with the current
  2895. implementation settings }
  2896. if fwpd.forwarddef then
  2897. begin
  2898. forwardfound:=true;
  2899. if not(m_repeat_forward in current_settings.modeswitches) and
  2900. (fwpd.proccalloption<>currpd.proccalloption) then
  2901. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2902. else
  2903. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2904. { Check calling convention }
  2905. if (fwpd.proccalloption<>currpd.proccalloption) then
  2906. begin
  2907. { In delphi it is possible to specify the calling
  2908. convention in the interface or implementation if
  2909. there was no convention specified in the other
  2910. part }
  2911. if (m_delphi in current_settings.modeswitches) then
  2912. begin
  2913. if not(po_hascallingconvention in currpd.procoptions) then
  2914. currpd.proccalloption:=fwpd.proccalloption
  2915. else
  2916. if not(po_hascallingconvention in fwpd.procoptions) then
  2917. fwpd.proccalloption:=currpd.proccalloption
  2918. else
  2919. begin
  2920. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2921. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2922. { restore interface settings }
  2923. currpd.proccalloption:=fwpd.proccalloption;
  2924. end;
  2925. end
  2926. else
  2927. begin
  2928. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2929. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2930. { restore interface settings }
  2931. currpd.proccalloption:=fwpd.proccalloption;
  2932. end;
  2933. end;
  2934. { Check static }
  2935. if (po_staticmethod in fwpd.procoptions) then
  2936. begin
  2937. if not (po_staticmethod in currpd.procoptions) then
  2938. begin
  2939. include(currpd.procoptions, po_staticmethod);
  2940. if (po_classmethod in currpd.procoptions) then
  2941. begin
  2942. { remove self from the hidden paras }
  2943. symentry:=currpd.parast.Find('self');
  2944. if symentry<>nil then
  2945. begin
  2946. currpd.parast.Delete(symentry);
  2947. currpd.calcparas;
  2948. end;
  2949. end;
  2950. end;
  2951. end;
  2952. { Check if the procedure type and return type are correct,
  2953. also the parameters must match also with the type }
  2954. if ((m_repeat_forward in current_settings.modeswitches) or
  2955. not is_bareprocdef(currpd)) and
  2956. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2957. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)) then
  2958. begin
  2959. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2960. fwpd.fullprocname(false));
  2961. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2962. break;
  2963. end;
  2964. { Check if both are declared forward }
  2965. if fwpd.forwarddef and currpd.forwarddef then
  2966. begin
  2967. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2968. currpd.fullprocname(false));
  2969. end;
  2970. { internconst or internproc only need to be defined once }
  2971. if (fwpd.proccalloption=pocall_internproc) then
  2972. currpd.proccalloption:=fwpd.proccalloption
  2973. else
  2974. if (currpd.proccalloption=pocall_internproc) then
  2975. fwpd.proccalloption:=currpd.proccalloption;
  2976. { Check procedure options, Delphi requires that class is
  2977. repeated in the implementation for class methods }
  2978. if (m_fpc in current_settings.modeswitches) then
  2979. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2980. else
  2981. po_comp:=[po_classmethod,po_methodpointer];
  2982. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2983. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2984. begin
  2985. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2986. fwpd.fullprocname(false));
  2987. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2988. { This error is non-fatal, we can recover }
  2989. end;
  2990. { Forward declaration is external? }
  2991. if (po_external in fwpd.procoptions) then
  2992. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2993. { Check parameters }
  2994. if (m_repeat_forward in current_settings.modeswitches) or
  2995. (currpd.minparacount>0) then
  2996. begin
  2997. { If mangled names are equal then they have the same amount of arguments }
  2998. { We can check the names of the arguments }
  2999. { both symtables are in the same order from left to right }
  3000. curridx:=0;
  3001. fwidx:=0;
  3002. currparacnt:=currpd.parast.SymList.Count;
  3003. fwparacnt:=fwpd.parast.SymList.Count;
  3004. repeat
  3005. { skip default parameter constsyms }
  3006. while (curridx<currparacnt) and
  3007. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  3008. inc(curridx);
  3009. while (fwidx<fwparacnt) and
  3010. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  3011. inc(fwidx);
  3012. { stop when one of the two lists is at the end }
  3013. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  3014. break;
  3015. { compare names of parameters, ignore implictly
  3016. renamed parameters }
  3017. currparasym:=tsym(currpd.parast.SymList[curridx]);
  3018. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  3019. if not(sp_implicitrename in currparasym.symoptions) and
  3020. not(sp_implicitrename in fwparasym.symoptions) then
  3021. begin
  3022. if (currparasym.name<>fwparasym.name) then
  3023. begin
  3024. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  3025. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  3026. break;
  3027. end;
  3028. end;
  3029. { next parameter }
  3030. inc(curridx);
  3031. inc(fwidx);
  3032. until false;
  3033. end;
  3034. { Everything is checked, now we can update the forward declaration
  3035. with the new data from the implementation }
  3036. fwpd.forwarddef:=currpd.forwarddef;
  3037. fwpd.hasforward:=true;
  3038. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  3039. { marked as local but exported from unit? }
  3040. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  3041. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  3042. if fwpd.extnumber=$ffff then
  3043. fwpd.extnumber:=currpd.extnumber;
  3044. while not currpd.aliasnames.empty do
  3045. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  3046. { update fileinfo so position references the implementation,
  3047. also update funcretsym if it is already generated }
  3048. fwpd.fileinfo:=currpd.fileinfo;
  3049. if assigned(fwpd.funcretsym) then
  3050. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  3051. if assigned(currpd.deprecatedmsg) then
  3052. begin
  3053. stringdispose(fwpd.deprecatedmsg);
  3054. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  3055. end;
  3056. { import names }
  3057. if assigned(currpd.import_dll) then
  3058. begin
  3059. stringdispose(fwpd.import_dll);
  3060. fwpd.import_dll:=stringdup(currpd.import_dll^);
  3061. end;
  3062. if assigned(currpd.import_name) then
  3063. begin
  3064. stringdispose(fwpd.import_name);
  3065. fwpd.import_name:=stringdup(currpd.import_name^);
  3066. end;
  3067. fwpd.import_nr:=currpd.import_nr;
  3068. { for compilerproc defines we need to rename and update the
  3069. symbolname to lowercase }
  3070. if (po_compilerproc in fwpd.procoptions) then
  3071. begin
  3072. { rename to lowercase so users can't access it }
  3073. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  3074. { the mangeled name is already changed by the pd_compilerproc }
  3075. { handler. It must be done immediately because if we have a }
  3076. { call to a compilerproc before it's implementation is }
  3077. { encountered, it must already use the new mangled name (JM) }
  3078. end;
  3079. { Release current procdef }
  3080. currpd.owner.deletedef(currpd);
  3081. currpd:=fwpd;
  3082. end
  3083. else
  3084. begin
  3085. { abstract methods aren't forward defined, but this }
  3086. { needs another error message }
  3087. if (po_abstractmethod in fwpd.procoptions) then
  3088. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  3089. else
  3090. begin
  3091. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  3092. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3093. end;
  3094. end;
  3095. { we found one proc with the same arguments, there are no others
  3096. so we can stop }
  3097. break;
  3098. end;
  3099. { check for allowing overload directive }
  3100. if not(m_fpc in current_settings.modeswitches) then
  3101. begin
  3102. { overload directive turns on overloading }
  3103. if ((po_overload in currpd.procoptions) or
  3104. (po_overload in fwpd.procoptions)) then
  3105. begin
  3106. { check if all procs have overloading, but not if the proc is a method or
  3107. already declared forward, then the check is already done }
  3108. if not(fwpd.hasforward or
  3109. assigned(currpd.struct) or
  3110. (currpd.forwarddef<>fwpd.forwarddef) or
  3111. ((po_overload in currpd.procoptions) and
  3112. (po_overload in fwpd.procoptions))) then
  3113. begin
  3114. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3115. break;
  3116. end;
  3117. end
  3118. else
  3119. begin
  3120. if not(fwpd.forwarddef) then
  3121. begin
  3122. if (m_tp7 in current_settings.modeswitches) then
  3123. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  3124. else
  3125. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3126. break;
  3127. end;
  3128. end;
  3129. end; { equal arguments }
  3130. end;
  3131. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  3132. list }
  3133. if not forwardfound then
  3134. begin
  3135. { can happen in Delphi mode }
  3136. if (currpd.proctypeoption = potype_function) and
  3137. is_void(currpd.returndef) then
  3138. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3139. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3140. end;
  3141. proc_add_definition:=forwardfound;
  3142. end;
  3143. end.