pdecsub.pas 121 KB

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