aboutsummaryrefslogtreecommitdiff
path: root/gcc/go/gofrontend/parse.cc
AgeCommit message (Collapse)AuthorFilesLines
2022-07-05compiler: better error message for unknown package nameIan Lance Taylor1-1/+5
Fixes golang/go#51237 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/415994
2022-07-01compiler: use correct init order for multi-value initializationIan Lance Taylor1-3/+15
Use the correct initialization order for var a = c var b, c = x.(bool) The global c is initialized by the preinit of b, but were missing a dependency of c on b, so a would be initialized to the zero value of c rather than the correct value. Simply adding the dependency of c on b didn't work because the preinit of b refers to c, so that appeared circular. So this patch changes the init order to skip dependencies that only appear on the left hand side of assignments in preinit blocks. Doing that didn't work because the write barrier pass can transform "a = b" into code like "gcWriteBarrier(&a, b)" that is not obviously a simple assigment. So this patch moves the collection of dependencies to just after lowering, before the write barriers are inserted. Making those changes permit relaxing the requirement that we don't warn about self-dependency in preinit blocks, so now we correctly warn for var a, b any = b.(bool) The test case is https://go.dev/cl/415238. Fixes golang/go#53619 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/415594
2022-06-29compiler: check repeated const expressions in new scopeIan Lance Taylor1-0/+89
Test case is const8.go in https://go.dev/cl/414795. Fixes golang/go#53585 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/414914
2021-10-26compiler: permit compiler directives in parenthesized groupsIan Lance Taylor1-62/+66
The original compiler directive support was only for //line at the start of a line and for //go: comments before function declarations. When support was added for //go:notinheap for types and //go:embed for variables the code did not adapt to permit spaces before the comment or to permit the comments in var() or type() groups. This change corrects those omissions. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/358699
2021-03-02compiler: allow //go:embed in files underscore-importing embedMichael Matloob1-1/+1
The embed spec allows for //go:embed to be used in files that underscore-import package "embed". This is useful for embeds to []byte and string vars because the embed.FS type may not be referenced if those are the only types of embeds in a file. Because the compiler previously checked whether there were any aliases to the embed package to decide if //go:embed could be used, it would reject files with only underscore imports of embed. Instead, record whether the embed import is encountered at all, similar to what is done with unsafe, to decide whether //go:embed is allowed. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/297553
2021-01-18compiler: read embedcfg files, parse go:embed directivesIan Lance Taylor1-23/+76
This change reads go:embed directives and attaches them to variables. We still don't do anything with the directives. This change also reads the file passed in the -fgo-embedcfg option. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/281533
2020-12-22compiler: initialize saw_send_stmt localsIan Lance Taylor1-2/+2
The C++ compiler wasn't warning because we take their address. Fixes golang/go#43252 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/279752
2020-12-15compiler: discard global sink variables with static initializersIan Lance Taylor1-0/+1
This is specifically for the test fixedbugs/issue23781.go, which builds a large static array. The code does compile and work without this change, but it takes a long time and generates a large object file. Discarding the unnecessary static initializer makes this test much faster. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/278172
2020-12-08compiler: use correct location for iota errorsIan Lance Taylor1-1/+6
Also check for valid array length when reducing len/cap to a constant. For golang/go#8183 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/275654
2020-12-02compiler: reword "declared and not used" error messageIan Lance Taylor1-1/+1
This is a gofrontend copy of https://golang.org/cl/203282. From the CL 203282 description: "declared and not used" is technically correct, but might confuse the user. Switching "and" to "but" will hopefully create the contrast for the users: they did one thing (declaration), but not the other --- actually using the variable. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/273629
2020-12-02compiler: improve mixed named/unnamed parameter error messageIan Lance Taylor1-1/+1
Use the same error as the current gc compiler. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/273628
2020-12-02compiler: don't advance past unexpected semicolonIan Lance Taylor1-1/+0
We've already read the unexpected semicolon, so advancing again causes us to skip the next token, causing future errors to be out of sync. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/274439
2020-11-30compiler: improve error messages for expected curly braceIan Lance Taylor1-3/+3
For golang/go#17328 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/273890
2020-11-30compiler: improve error for import of non-stringIan Lance Taylor1-1/+1
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/273867
2020-11-28compiler: better error for x, x := 1, 2Ian Lance Taylor1-3/+11
Was assign.go:59:28: error: multiple assignments to x Now assign.go:59:28: error: ‘x’ repeated on left side of := Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/273546
2020-01-27compiler: cleanups permitted by GCC requirement of MPFR 3.1.0Ian Lance Taylor1-1/+1
For MPFR functions, change from GMP_RND* to MPFR_RND*. Also change mp_exp_t to mpfr_expt_t. Fixes PR go/92463 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/216417
2020-01-09compiler: don't add composite literal keys to package bindingsIan Lance Taylor1-8/+15
Adding composite literal keys to package bindings gets confusing when it is combined with dot imports. The test case showing the resulting compilation failure is https://golang.org/cl/213899. Fix this by adding a new expression type to hold composite literal keys. We shouldn't see it during lowering if it is a struct field name, because Composite_literal_expression::do_traverse skips struct field names. Or, it should, but that didn't quite work with pointer types so it had to be tweaked. This lets us remove the code that recorded whether an Unknown_expression is a composite literal key. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/214017 From-SVN: r280056
2019-10-04compiler: adjust code to avoid shadowing local variablesIan Lance Taylor1-13/+13
Also add a couple of missing calls to free after mpz_get_str. This should make the code clean with respect to -Wshadow=local. Based on patch by Bernd Edlinger. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/198837 From-SVN: r276579
2019-06-23compiler: edit error messages to avoid -Wformat-diag warningsIan Lance Taylor1-11/+11
GCC recently introduced -Wformat-diag to scrutinize GCC error messages. It reports a number of warnings about gofrontend code, such as: ../../trunk/gcc/go/gofrontend/import.cc: In member function ‘Type* Import::type_for_index(int, const string&, size_t, bool*)’: ../../trunk/gcc/go/gofrontend/import.cc:1129:48: warning: unquoted operator ‘>=’ in format [-Wformat-diag] 1129 | "error in %s at %lu: bad type index %d >= %d", | ^~ ../../trunk/gcc/go/gofrontend/ast-dump.cc: In member function ‘void Ast_dump_context::dump(Gogo*, const char*)’: ../../trunk/gcc/go/gofrontend/ast-dump.cc:203:25: warning: unquoted option name ‘-fgo-dump-ast’ in format [-Wformat-diag] 203 | "cannot open %s:%m, -fgo-dump-ast ignored", dumpname.c_str()); | ^~~~~~~~~~~~~ ../../trunk/gcc/go/gofrontend/expressions.cc: In static member function ‘static Bexpression* Func_expression::get_code_pointer(Gogo*, Named_object*, Location)’: ../../trunk/gcc/go/gofrontend/expressions.cc:1350:29: warning: misspelled term ‘builtin function’ in format; use ‘built-in function’ instead [-Wformat-diag] 1350 | "invalid use of special builtin function %qs; must be called", | ^~~~~~~~~~~~~~~~ ../../trunk/gcc/go/gofrontend/gogo.cc: In member function ‘void Gogo::add_linkname(const string&, bool, const string&, Location)’: ../../trunk/gcc/go/gofrontend/gogo.cc:2527:4: warning: unquoted sequence of 2 consecutive punctuation characters ‘//’ in format [-Wformat-diag] 2527 | ("%s is not a function; " | ~^~~~~~~~~~~~~~~~~~~~~~~~ 2528 | "//go:linkname is only supported for functions"), | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This CL edits error messages to avoid these warnings. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/183497 * go.test/test/blank1.go: Update for diagnostic message changes. From-SVN: r272608
2019-05-08compiler: remove trailing spacesIan Lance Taylor1-2/+2
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/175557 From-SVN: r271014
2018-09-14compiler: don't use address of temporary for deferred deleteIan Lance Taylor1-2/+8
This CL corrects the handling of a deferred delete in a loop, to not use a temporary whose value will, at deferred execution time, wind up being the last value in the loop. The test for this is TestDeferDeleteSlow in the 1.11 runtime package. Reviewed-on: https://go-review.googlesource.com/135358 From-SVN: r264325
2018-09-13compiler: implement //go:nowritebarrierrecIan Lance Taylor1-1/+4
Reviewed-on: https://go-review.googlesource.com/134228 From-SVN: r264283
2018-02-05compiler: update iota handling, fix using iota in array lengthIan Lance Taylor1-33/+7
CL 71750 changed the definition of how iota works. This patch updates gccgo for the new definition. We've been mishandling iota appearing in a type that appears in a const expression, as in `c = len([iota]int{})`. Correct that by copying type expressions when we copy an expression. For simplicity only copy when it can change the size of a type, as that is the only case where iota in a type can affect the value of a constant (I think). This is still a bunch of changes, but almost all boilerplate. Fixes golang/go#22341 Reviewed-on: https://go-review.googlesource.com/91475 From-SVN: r257379
2018-02-05compiler: permit empty statements after fallthroughIan Lance Taylor1-2/+17
The language spec permits empty statements after a fallthrough statement, so implement that. Also give a better error message when a fallthrough statement is in the wrong place. The test case for this is in the master repository, test/fixedbugs/issue14540.go, just not yet in the gccgo repository. Fixes golang/go#14538 Reviewed-on: https://go-review.googlesource.com/91855 From-SVN: r257378
2018-02-05compiler: in range, evaluate array if it has receives or callsIan Lance Taylor1-2/+1
The last change was incomplete, in that it did not evaluate the array argument in some cases where it had to be evaluated. This reuses the existing code for checking whether len/cap is constant. Also clean up the use of _ as the second variable in a for/range, which was previously inconsistent depending on whether the statement used = or :=. Updates golang/go#22313 Reviewed-on: https://go-review.googlesource.com/91715 From-SVN: r257377
2018-02-05compiler: correct parse of parenthesized select caseIan Lance Taylor1-6/+20
We used to mishandle `select { case (<-c): }` and friends. The test case for this is https://golang.org/cl/91657. Fixes golang/go#20923 Reviewed-on: https://go-review.googlesource.com/91695 From-SVN: r257374
2018-01-17compiler: enable escape analysis for runtimeIan Lance Taylor1-15/+0
The runtime package was hard-coded non-escape, and the escape analysis was not run for the runtime package. This CL removes the hard-code, and lets the escape analysis decide. It is not allowed for local variables and closures in the runtime to be heap allocated. This CL adds the check that make sure that they indeed do not escape. The escape analysis is always run when compiling the runtime now. Fixes golang/go#17431 Reviewed-on: https://go-review.googlesource.com/86246 From-SVN: r256820
2017-12-05compiler: no nil check needed for closure var dereferencesIan Lance Taylor1-2/+3
Add the "no nil check needed" annotation to the dereference operations created in Parse::enclosing_var_reference (this is safe since the closure object is under control of the compiler, and pointer fields in it will always be non-nil). Reviewed-on: https://go-review.googlesource.com/81795 From-SVN: r255400
2017-12-01compiler: introduce size threshold for nil checksThan McIntosh1-2/+5
Add a new control variable to the Gogo class that stores the size threshold for nil checks. This value can be used to control the policy for deciding when a given deference operation needs a check and when it does not. A size threshold of -1 means that every potentially faulting dereference needs an explicit check (and branch to error call). A size threshold of K (where K > 0) means that if the size of the object being dereferenced is >= K, then we need a check. Reviewed-on: https://go-review.googlesource.com/80996 * go-c.h (go_create_gogo_args): Add nil_check_size_threshold field. * go-lang.c (go_langhook_init): Set nil_check_size_threshold. From-SVN: r255340
2017-11-22compiler: make comparison operator() methods constIan Lance Taylor1-1/+1
This is required for new versions of libstdc++ in C++17 mode. Fixes GCC PR 83102. Reviewed-on: https://go-review.googlesource.com/79396 From-SVN: r255062
2017-06-23compiler: add go:notinheap magic commentIan Lance Taylor1-14/+38
Implement go:notinheap as the gc compiler does. A type marked as go:notinheap may not live in the heap, and does not require a write barrier. Struct and array types that incorporate notinheap types are themselves notinheap. Allocating a value of a notinheap type on the heap is an error. This is not just an optimization. There is code where a write barrier may not occur that was getting a write barrier with gccgo but not gc, because the types in question were notinheap. The case I found was setting the mcache field in exitsyscallfast. Reviewed-on: https://go-review.googlesource.com/46490 From-SVN: r249594
2017-01-14compiler: implement type aliasesIan Lance Taylor1-6/+15
This is a start of implementing type aliases (`type T1 = T2`) in the Go frontend. This is incomplete, in that the reflection information is not updated for an embedded type alias. It is also not well tested. Finally, the change to the language has not been approved. This should be regarded as preliminary work for experimental use. Update golang/go#18130. Reviewed-on: https://go-review.googlesource.com/35120 From-SVN: r244460
2016-11-10runtime: copy signal code from Go 1.7 runtimeIan Lance Taylor1-1/+1
Add a little shell script to auto-generate runtime.sigtable from the known signal names. Force the main package to always import the runtime package. Otherwise some runtime package global variables may never be initialized. Set the syscallsp and syscallpc fields of g when entering a syscall, so that the runtime package knows when a g is executing a syscall. Fix runtime.funcPC to avoid dead store elimination of the interface value when the function is inlined. Reviewed-on: https://go-review.googlesource.com/33025 From-SVN: r242060
2016-10-13runtime: copy mstats code from Go 1.7 runtimeIan Lance Taylor1-0/+15
This replaces mem.go and the C runtime_ReadMemStats function with the Go 1.7 mstats.go. The GCStats code is commented out for now. The corresponding gccgo code is in runtime/mgc0.c. The variables memstats and worldsema are shared between the Go code and the C code, but are not exported. To make this work, add temporary accessor functions acquireWorldsema, releaseWorldsema, getMstats (the latter known as mstats in the C code). Check the preemptoff field of m when allocating and when considering whether to start a GC. This works with the new stopTheWorld and startTheWorld functions in Go, which are essentially the Go 1.7 versions. Change the compiler to stack allocate closures when compiling the runtime package. Within the runtime packages closures do not escape. This is similar to what the gc compiler does, except that the gc compiler, when compiling the runtime package, gives an error if escape analysis shows that a closure does escape. I added this here because the Go version of ReadMemStats calls systemstack with a closure, and having that allocate memory was causing some tests that measure memory allocations to fail. Reviewed-on: https://go-review.googlesource.com/30972 From-SVN: r241124
2016-09-23compiler: better abstraction layer for diagnostics.Than McIntosh1-243/+256
Introduce an abstraction layer for reporting diagnostics, so as to avoid directly using the native GCC interfaces such as "error_at", "warning_at", "open_quote", "close_quote", etc. The new interfaces have the same look and feel as the GCC equivalents, but make calls into back-end functions to allow the back end to select the proper final reporting routine. Reviewed-on: https://go-review.googlesource.com/29191 * go-gcc-diagnostics.cc: New file. * go-location.h (Location): Remove operator source_location. Add operator==. * go-system.h: #include <sstream>. * Make-lang.in (GO_OBJS): Add go/go-diagnostics.o and go/go-gcc-diagnostics.o. (CFLAGS-go/go-gcc-diagnostics.o): New variable. From-SVN: r240453
2016-09-21compiler, runtime: replace hashmap code with Go 1.7 hashmapIan Lance Taylor1-31/+0
This change removes the gccgo-specific hashmap code and replaces it with the hashmap code from the Go 1.7 runtime. The Go 1.7 hashmap code is more efficient, does a better job on details like when to update a key, and provides some support against denial-of-service attacks. The compiler is changed to call the new hashmap functions instead of the old ones. The compiler now tracks which types are reflexive and which require updating when used as a map key, and records the information in map type descriptors. Map_index_expression is simplified. The special case for a map index on the right hand side of a tuple expression has been unnecessary for some time, and is removed. The support for specially marking a map index as an lvalue is removed, in favor of lowering an assignment to a map index into a function call. The long-obsolete support for a map index of a pointer to a map is removed. The __go_new_map_big function (known to the compiler as Runtime::MAKEMAPBIG) is no longer needed, as the new runtime.makemap function takes an int64 hint argument. The old map descriptor type and supporting expression is removed. The compiler was still supporting the long-obsolete syntax `m[k] = 0, false` to delete a value from a map. That is now removed, requiring a change to one of the gccgo-specific tests. The builtin len function applied to a map or channel p is now compiled as `p == nil ? 0 : *(*int)(p)`. The __go_chan_len function (known to the compiler as Runtime::CHAN_LEN) is removed. Support for a shared zero value for maps to large value types is introduced, along the lines of the gc compiler. The zero value is handled as a common variable. The hash function is changed to take a seed argument, changing the runtime hash functions and the compiler-generated hash functions. Unlike the gc compiler, both the hash and equal functions continue to take the type length. Types that can not be compared now store nil for the hash and equal functions, rather than pointing to functions that throw. Interface hash and comparison functions now check explicitly for nil. This matches the gc compiler and permits a simple implementation for ismapkey. The compiler is changed to permit marking struct and array types as incomparable, meaning that they have no hash or equal function. We use this for thunk types, removing the existing special code to avoid generating hash/equal functions for them. The C runtime code adds memclr, memequal, and memmove functions. The hashmap code uses go:linkname comments to make the functions visible, as otherwise the compiler would discard them. The hashmap code comments out the unused reference to the address of the first parameter in the race code, as otherwise the compiler thinks that the parameter escapes and copies it onto the heap. This is probably not needed when we enable escape analysis. Several runtime map tests that ere previously skipped for gccgo are now run. The Go runtime picks up type kind information and stubs. The type kind information causes the generated runtime header file to define some constants, including `empty`, and the C code is adjusted accordingly. A Go-callable version of runtime.throw, that takes a Go string, is added to be called from the hashmap code. Reviewed-on: https://go-review.googlesource.com/29447 * go.go-torture/execute/map-1.go: Replace old map deletion syntax with call to builtin delete function. From-SVN: r240334
2016-08-09compiler: rewrite compiler directive supportIan Lance Taylor1-22/+76
Rewrite the compiler directive support to recognize all the compiler directives implemented by the current gc compiler. The directives other than go:linkname are now turned into GOPRAGMA flags attached to a function or function declaration. The go:linkname directive is turned into a map attached to the Lex object. No new directives are actually implemented yet, they are just recognized. Reviewed-on: https://go-review.googlesource.com/26610 From-SVN: r239282
2016-07-22compiler: fix check for duplicate declarationIan Lance Taylor1-2/+9
The compiler check that issued a duplicate declaration error for a, a, a := 1, 2, 3 was incorrectly issuing an error for a, a, a = 1, 2, 3 While this is not particularly useful, it is valid Go. Test is https://golang.org/cl/25143. Reviewed-on: https://go-review.googlesource.com/25144 From-SVN: r238618
2016-04-26compiler: Add Enclosed_var_expression.Ian Lance Taylor1-19/+5
Introduces an abstraction for a variable referenced in a closure. This maintains the underlying expression which accesses a field within a closure variable and gives easy access to the underlying Named_object. Reviewed-on: https://go-review.googlesource.com/22374 From-SVN: r235452
2016-01-27compiler: Accept map composite literals with omitted key types.Ian Lance Taylor1-6/+9
compiler: Copy key_path_ when copying a Composite_literal_expression. Fixes golang/go#10263. Reviewed-on: https://go-review.googlesource.com/14299 Reviewed-on: https://go-review.googlesource.com/18988 From-SVN: r232892
2016-01-27compiler: Don't crash on erroneous array return types.Ian Lance Taylor1-0/+2
Another issue with erroneous array types. When an erroneous array type is in a function's signature, particularly the return type, we must guarantee that type is changed into an error type. Otherwise, any operations that work on arrays and slices will crash when applied to the erroneous array return type. Fixes golang/go#12939. Reviewed-on: https://go-review.googlesource.com/16235 From-SVN: r232858
2015-10-06compiler: Record each import as a distinct alias.Ian Lance Taylor1-2/+2
This patch introduces the Package_alias class which is a finer representation of the different between a package and the aliases it is imported under. Each alias keeps track of the location of its import statement and how many times that alias has been used. This allows the gofrontend to report when a specific import has not been used even if a symbol from the package has been used by another import. Fixes golang/go#12326. Reviewed-on: https://go-review.googlesource.com/14259 From-SVN: r228550
2015-08-27compiler: Don't record dependencies of invalid redefinitions.Ian Lance Taylor1-0/+8
The gofrontend would crash when trying to find the initialization order of a variable list where one of the listed variables was an invalid redefinition of another in a call statement. This patch fixes initialization from call statements to consider invalid redefinitions before recording dependency information. Fixes golang/go#11543. Reviewed-on: https://go-review.googlesource.com/13895 From-SVN: r227276
2015-08-25compiler: Type check params in sink function decl.Ian Lance Taylor1-1/+13
When a function is declared and named with the blank identifier, only the syntax is checked. This patch modifies the parser to add a dummy node for each function declaration with a blank identifier name that will be type checked like any function declaration. Fixes golang/go#11535. Reviewed-on: https://go-review.googlesource.com/13792 From-SVN: r227160
2015-08-11compiler: Check for EOF in malformed signatures.Ian Lance Taylor1-1/+2
When parsing a malformed function declaration with invalid parameters, gccgo would infinitely loop looking for the end of the function declaration. Fixes golang/go#11530, golang/go#11531. Reviewed-on: https://go-review.googlesource.com/13065 From-SVN: r226795
2015-08-03compiler: Don't allow multiple function declarations.Ian Lance Taylor1-1/+4
Fixes golang/go#11573. Reviewed-on: https://go-review.googlesource.com/12508 From-SVN: r226529
2015-07-24compiler: Don't parse malformed receiver/parameters.Ian Lance Taylor1-3/+5
Fixes golang/go#11576. Reviewed-on: https://go-review.googlesource.com/12157 From-SVN: r226185
2015-07-23compiler: Make empty interface types for vars during parse time.Ian Lance Taylor1-1/+5
When making the type for a variable with an empty interface type, the parser makes an interface type with a NULL method set and relies on later passes to correct this. For sink variables, which are ignored in later passes, the interface method table is never finalized and a compile time assertion is issued. Instead, the initial type generated by the parser should be the empty interface type. Fixes golang/go#11579. Reviewed-on: https://go-review.googlesource.com/12049 From-SVN: r226123
2015-03-13compiler: Permit label on fallthrough statement.Ian Lance Taylor1-0/+11
Test case: https://go-review.googlesource.com/7523 . From-SVN: r221428
2015-03-06compiler: Do not declare type switch variable outside case statements.Ian Lance Taylor1-47/+57
For expressions containing a TypeSwitchGuard with a short variable declaration e.g. var := x.(type), the spec says that var is declared at the beginning of the implicit block for each in each clause. Previously, var was declared in the block for the switch statement and each implicit block, which led to errors if the type case clause referenced a type with a similar name as the declared variable. Fixes golang/go#10047. From-SVN: r221230